swingBoris
swingBoris

Reputation: 35

Making my first project in java

So after like 2 hours of Java guides I started making my first project, a simple rock paper scissors game, but then I found a problem. I want to add 1 point to the winner of the game each time you pick rock paper or scissors and you win and its the only thing I need to do before its completed (I am new to Java).

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame {

    private static final long serialVersionUID = 1L;

    private static String Default = "Rock,Paper,Scissors";
    private static JLabel update;
    private static JLabel userScore;
    private static JLabel pcScore;
    private static Container panel;
    private static int userCounter;

    public static int getUserCounter() {
        return userCounter;
    }

    public static void setUserCounter(int userCounter) {
        Main.userCounter = userCounter + userCounter;
    }

    public static int getPcCounter() {
        return pcCounter;
    }

    public static void setPcCounter(int pcCounter) {
        Main.pcCounter = pcCounter + pcCounter;
    }

    private static int pcCounter;
    private static final int bwidth = 90;
    private static final int bheight = 25;
    private static Random random;
    private static final String ROCK = "Rock";
    private static final String PAPER = "Paper";
    private static final String SCISSORS = "Scissors";
    private static JButton rock;
    private static JButton paper;
    private static JButton scissors;

    public Main() {
        super("Simple Game :D");
        panel = new Container();
        getContentPane().add(panel);

        userScore = new JLabel();
        userScore.setText("Your Score " + getUserCounter());
        userScore.setBounds(300, 50, 100, 40);

        pcScore = new JLabel();
        pcScore.setText("Pc Score " + getPcCounter());
        pcScore.setBounds(430, 50, 100, 40);

        update = new JLabel(Default);
        update.setBounds(325, 0, 400, 40);
        update.enableInputMethods(false);
        update.setBackground(Color.BLUE.brighter());

        panel.add(update);
        panel.add(Rock());
        panel.add(Paper());
        panel.add(Scissors());

        panel.add(userScore);
        panel.add(pcScore);

    }

    public Component Rock() {
        rock = new JButton("Rock");
        rock.setBounds(350, 100, bwidth, bheight);
        rock.setBackground(Color.gray.brighter());
        rock.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Game(ROCK);
            }
        });
        return rock;
    }

    public Component Paper() {
        paper = new JButton("Paper");
        paper.setBounds(350, 130, bwidth, bheight);
        paper.setBackground(Color.WHITE);
        paper.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Game(PAPER);
            }
        });
        return paper;
    }

    public Component Scissors() {
        scissors = new JButton("scissors");
        scissors.setBounds(350, 160, bwidth, bheight);
        scissors.setBackground(new Color(174, 166, 166));
        scissors.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Game(SCISSORS);
            }
        });
        return scissors;
    }

    public String Pc() {
        String pc = "";
        random = new Random();
        int choice = 1 + random.nextInt(3);
        if (choice == 1) {
            pc = ROCK;
        } else if (choice == 2) {
            pc = PAPER;
        } else if (choice == 3) {
            pc = SCISSORS;
        }
        return pc;
    }

    public void Game(String user) {
        String pc = Pc();
        int winner = 0;
        if (user == ROCK && pc == ROCK) {
            update.setText(user + " vs " + pc + " Its a Draw!!");
        } else if (user == ROCK && pc == PAPER) {
            setPcCounter(1);
            update.setText(user + " vs " + pc + " Pc Wins!! ");

        } else if (user == ROCK && pc == SCISSORS) {
            setUserCounter(1);
            update.setText(user + " vs " + pc + " You Win!!");

            // PAPER
        } else if (user == PAPER && pc == PAPER) {
            update.setText(user + " vs " + pc + " Its a Draw!!");
        } else if (user == PAPER && pc == ROCK) {
            update.setText(user + " vs " + pc + " You Win!!");
            userCounter += 1;
        } else if (user == PAPER && pc == SCISSORS) {
            update.setText(user + " vs " + pc + " Pc Wins!!");
            pcCounter += 1;
            // SCISSORS
        } else if (user == SCISSORS && pc == SCISSORS) {
            update.setText(user + " vs " + pc + " Its a Draw!!");
        } else if (user == SCISSORS && pc == ROCK) {
            update.setText(user + " vs " + pc + " Pc Wins!!");
            pcCounter += 1;
        } else if (user == SCISSORS && pc == PAPER) {
            update.setText(user + " vs " + pc + " You Win!!");
            userCounter += 1;
        } else if (user == null || pc == null) {
            System.exit(0);
        }

    }
}

Upvotes: 2

Views: 169

Answers (1)

Ajk_P
Ajk_P

Reputation: 1874

You have an error in

public static void setUserCounter(int userCounter) {
    Main.userCounter = userCounter + userCounter;
}

Should be

Main.userCounter += userCounter;

or

Main.userCounter = Main.userCounter + userCounter;

Same thing for:

public static void setPcCounter(int pcCounter) {
    Main.pcCounter = pcCounter + pcCounter;
}

Also in some places at the bottom, you use directly the userCounter variable without using setUserCounter();

} else if (user == SCISSORS && pc == PAPER) {
update.setText(user + " vs " + pc + " You Win!!");
userCounter += 1;

Keep your style consistent and use setUserCounter();

Note that in objects, when there is myVar Object field and myVar argument, the Object field can be accessed by typing "this.myVar" to differentiate it from the argument which is "myVar"

Upvotes: 1

Related Questions