I need help with incrementing my score. I tried ++wins..., wins++..., wins += 1..., wins = wins + 1... and so far nothing works. I just keep getting 0.

My main class:

import java.util.Scanner;

public class Test
{
    public static void main (String [] args)
     {
         Rock game = new Rock();

          int totalGame = game.getTotalGames();
          int wins = game.getWins();
          int losses = game.getLosses();
          int draws = game.getDraws();

          System.out.println("Enter move 1)rock 2)paper 3)scissors");
         Scanner user = new Scanner (System.in);
          int userMove = user.nextInt();

          game.play(userMove);

          String compMove;
          compMove = game.getComputerMoveAsString();
          System.out.print("Computer's move: " + compMove);
         System.out.println();

          String gameOutcome;
         gameOutcome = game.getOutcomeAsString();
          System.out.println(gameOutcome);

          System.out.println("total " + totalGame);
          System.out.println("wins " + wins);
          System.out.println("losses " + losses);
          System.out.println("draws " + draws);

     }
}

My rock class:

import java.util.Scanner;

public class Test
{
    public static void main (String [] args)
     {
         Rock game = new Rock();

          int totalGame = game.getTotalGames();
          int wins = game.getWins();
          int losses = game.getLosses();
          int draws = game.getDraws();

          System.out.println("Enter move 1)rock 2)paper 3)scissors");
         Scanner user = new Scanner (System.in);
          int userMove = user.nextInt();

          game.play(userMove);

          String compMove;
          compMove = game.getComputerMoveAsString();
          System.out.print("Computer's move: " + compMove);
         System.out.println();

          String gameOutcome;
         gameOutcome = game.getOutcomeAsString();
          System.out.println(gameOutcome);

          System.out.println("total " + totalGame);
          System.out.println("wins " + wins);
          System.out.println("losses " + losses);
          System.out.println("draws " + draws);

     }
}


class Rock
{
    public static final int ROCK = 1;
     public static final int PAPER = 2;
     public static final int SCISSORS = 3;
     public static final int WIN = 1;
     public static final int LOSE = 2;
     public static final int DRAW = 3;
     public static final int NO_MOVE = 0;
     private int playerMove;
     private int computerMove;
     private int outcome;
     private int wins;
     private int losses;
     private int draws;


    public RockPaperScissors12()
    {
        playerMove = 0;
          computerMove = 0;
          wins = 0;
          losses = 0;
          draws = 0;
     }

    public boolean play(int playerMove)
    {
         double random;
          double computerRandom;
          int computerMove;  
          random = Math.random();
          computerRandom = random * 3;
          this.computerMove = (int)computerRandom + 1;  

         if (playerMove == 1)
          {
              this.playerMove = ROCK;
                switch (this.computerMove)
                {
                    case 1:
                         outcome = DRAW;
                          draws++;
                          break; 
                     case 2:
                         outcome = LOSE; 
                          losses++;
                          break;
                     case 3:
                         outcome = WIN; 
                          wins++;
                          break;
                } 
                return true;
        }
          else if (playerMove == 2)
          {
              this.playerMove = PAPER;
                switch (this.computerMove)
                {
                    case 1:
                         outcome = WIN;
                          ++wins;
                          break;
                     case 2:
                         outcome = DRAW;
                          ++draws;
                          break;
                     case 3:
                         outcome = LOSE;
                          ++losses;
                          break;
                }
                return true;
          }
          else if (playerMove == 3)
          {
              this.playerMove = SCISSORS;
                switch (this.computerMove)
                {
                    case 1:
                         outcome = LOSE;
                          ++losses;
                          break;
                     case 2:
                         outcome = WIN;
                          ++wins;
                          break;
                     case 3:
                         outcome = DRAW;
                          ++draws;
                          break;
                }
                return true;
          }
          else
          {
              return false;
          }
     }

    public int getComputerMove()
    {
         if (playerMove == 1 || playerMove == 2 || playerMove == 3)
        {
              return this.computerMove;
          }
          else
          {
           return NO_MOVE;
          }         
     }

     public String getComputerMoveAsString()
     {
         if (this.computerMove == 1)
          {
              return("rock");
          }
          else if (this.computerMove == 2)
          {
              return("paper");
          }
          else 
          {
              return("scissors");
          }
     }

     public int getOutcome()
     {
         if (playerMove == 1 || playerMove == 2 || playerMove == 3)
        {
              return outcome;
          }
          else
          {
           return NO_MOVE;
          }            
     }

     public String getOutcomeAsString()
     {
         if (outcome == 1)
          {
              return("You Won!");
          }
          else if (outcome == 2)
          {
              return("The computer wins.");
          }
          else 
          {
              return("The game was a tie.");
          }
     }

     public int getWins()
     {
         return wins;
     }

     public int getLosses()
     {
         return losses;
     }

     public int getDraws()
     {
         return draws;
     }

     public int getTotalGames()
     {
         return wins + losses + draws;
     }
}

Recommended Answers

All 2 Replies

Overrated,
The reason that you are getting 0 is because of this:

System.out.println("total " + totalGame);
System.out.println("wins " + wins);
System.out.println("losses " + losses);
System.out.println("draws " + draws);

Not to say that your 0 result is not due to declaring your variables in the Rock class as privates. This is good practice but you should remeber that you should use an accessor to look at privates.
You also have a instance of Rock class called game in your main...this should be used in conjunction with the accessor methods to return the data like:

System.out.println("total " + game.getTotalGames());
System.out.println("wins " + game.getWins());
System.out.println("losses " + game.getLosses());
System.out.println("draws " + game.getDraws());

Cheers,
Corliss

You are correctly getting a wins value from your game instance 10 int wins = game.getWins(); BUT you do that before starting the game, and you never update it, so it remains zero.
If you just move that line to somewhere after the game is finished that should give you the final value. However, it's better to get rid of the wins variable in class Test completely, and use game.getWins() whenever you want the value. That way it's always up-to-date. Corliss's code shows you the right way to do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.