J Doe
J Doe

Reputation: 11

Rock, Paper, Scissors Game not keeping score correctly

I'm having an issue with my rock, paper, scissors game. When I play the game it records a win or a loss as both win and a loss. I'm trying to make it so that it records a win and a loss and does not add a point to win and loss. Any suggestions?

import java.io.*;

public class RPS {
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static int x;

    public static void main(String[] args) throws IOException {

        int choice;
        do {
            System.out.println("Game Menu");
            System.out.println("---------------------------------------------");
            System.out.println("1. Start Game");
            System.out.println("2. Quit Program");

            choice = Integer.valueOf(in.readLine()).intValue();

            if(choice == 1) {
                System.out.println("Welcome to Rock, Paper, Scissors");
                System.out.println("---------------------------------------------");
                System.out.println("Please enter Rock, Paper or Scissors to play");

            int wins = 0, loss = 0, ties = 0;
            String user = in.readLine();
            String comp="";
            int compc=(int)(3*Math.random())+1;

            if (compc==1) {
                comp="Rock";
            }
            else if (compc==2) {
                comp="Paper";
            }
            else if (compc==3) {
                comp="Scissors";
            }

            if (user.equals("Rock") || user.equals("rock")) {
                System.out.println("You chose : Rock");
                user = "Rock";
            }
            else if (user.equals("Paper") || user.equals("paper")) {
                System.out.println("You chose : Paper");
                user = "Paper";
            }
            else if (user.equals("Scissors")|| user.equals("scissors")) {
                System.out.println("You chose : Scissors");
                user = "Scissors";
            }
            else {
                System.out.println("Error: Please type Rock, Paper or Scissors");
            }
            System.out.println("Computer: I chose " + comp);
            if(user.equals(comp)) {
                System.out.println("It's a tie!");
                ties++;
            }
            else if (user.equals("Rock")) {

                if (comp.equals("Scissors"))
                    System.out.println("Rock beats scissors, congrats you win!");
                wins++;

                 if (comp.equals("Paper"))
                     System.out.println("Paper beats rock, sorry you lost!");
                loss++;


            }
            else if (user.equals("Paper")) {

                if (comp.equals("Rock"))
                    System.out.println("Paper beats rock, congrats you win!");
                    wins++;

                 if (comp.equals("Scissors"))
                    System.out.println("Scissors beats paper, sorry you lost!");
                    loss++;         
            }
            else if (user.equals("Scissors")) {

                if (comp.equals("Paper"))
                    System.out.println("Scissors beats paper, congrats you win!");
                    wins++;
                 if (comp.equals("Rock"))
                    System.out.println("Rock beats scissors, sorry you lost!");
                    loss++;     
            }
            System.out.println("Total wins : " +wins);
            System.out.println("Total losses : " +loss);
            System.out.println("Total ties : " +ties);
            }
            if (choice == 2) {
                System.out.println("Good-bye!");
                in.close();
            }
        } while (choice !=2); 
    }
}

Upvotes: 0

Views: 1783

Answers (3)

John OConnor
John OConnor

Reputation: 21

Look at the win++ and loss++ - they get called automatically regardless of outcome... you need to put the code inside the if statements... if statements in java will automatically contain the next line but not after the first line so you will want to do:

if(condition to be met){
System.out.println("BLAH");
win++
}

I make it a habit to always include the curly brackets unless I have space issues. I find it makes it easier to read.

Rookie mistake - as a recent comp sci graduate - you will make a lot of them trust me. Just keep plugging along. Good Luck

Upvotes: 0

J Earls
J Earls

Reputation: 1812

if (comp.equals("Scissors"))
    System.out.println("Rock beats scissors, congrats you win!");
wins++;

if (comp.equals("Paper"))
    System.out.println("Paper beats rock, sorry you lost!");
loss++;

The wins++ and loss++ are not part of the if statements, and so get executed each time.

Upvotes: 0

Andy Turner
Andy Turner

Reputation: 140318

You need braces: {} around your indented conditional code. Unlike in languages like Python, indentation in Java has no semantic meaning.

For example.

if (comp.equals("Rock"))
    System.out.println("Paper beats rock, congrats you win!");
    wins++;

is the same as:

if (comp.equals("Rock")) {
    System.out.println("Paper beats rock, congrats you win!");
}
wins++;

i.e. wins is incremented unconditionally.

Try:

if (comp.equals("Rock")) {
    System.out.println("Paper beats rock, congrats you win!");
    wins++;
}

Upvotes: 2

Related Questions