#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Program with classes trouble


    I am making a two player game. Each player gets to roll the dice three times each, where they can keep numbers that they roll each roll. If they keep one number from the first roll, they can continue to roll two more times until they choose to keep the number. The two dice are added together and the player with the highest sum wins.

    I have a player class (in charge of keeping the dice values), the pairOfdice class where the dice are rolled and the sum is determined, and the main game. When I run the program, if you decide to hold only dice dice one each turn, or only dice 2 each turn, It will set whichever dice that was not chosen to zero. if this only dice ones were chosen each time, it would add the last chosen value of dice one to the last rolled value of dice 2. Why isn't it working.


    diceGame
    Code:
    import java.io.*;
    
    public class diceGame
    {
      public static void main(String[] args) throws IOException
      {
        BufferedReader myInput = new BufferedReader (new InputStreamReader(System.in));
        
        pairOfDice dice; // Refer to pairOfDice class
        
        dice = new pairOfDice();
        
        playerGame player;// Refer to playerGame class
        
        player = new playerGame();
        
        int totalA = 0, totalB = 0, holdA = 0;// intialize int variables for totals of each dice
        
        int playerNum [] = new int [2];//initialize array for number of players
        playerNum[0] = 1;
        playerNum[1] = 2;
        
        for (int i = 0; i <= 1; i++){//for loop runs twice. Once for each player
          dice.setTurns();//reset the turns, so player to recieves three attempts at rolling dice
          while (dice.getTurns() < 3){//runs three times to simulate three dice rolls
         
            dice.roll();    // Roll the dice form the pairOfDice class.
            System.out.println("PLAYER " + playerNum[i]);//array prints player number
            System.out.println("---------------\n");
            
            
            System.out.println("Dice 1: " + dice.getDiceA() + "\n" + "Dice 2: " + dice.getDiceB() + "\n" + "The total is: " + dice.getTotal());//get value of both dice and total from the pairOfDice class, from respective methods
            
            System.out.println("Do you want to hold the value of the dice? (Press 0 - hold none, 1 - hold die 1, 2 - hold die 2, 3 - hold both");//asks user which dice they want to hold
            
            String hold = myInput.readLine();// reads users response
            
             holdA = Integer.parseInt(hold);//converts user response into an integer
            
            //if statements using user response
            if (holdA == 0){// user doesn't want to hold any dice
            }//end if
            
            if (holdA == 1){//if user wants to hold first die
              player.setHoldA(dice.getDiceA());//sets holdA in setHoldA method (playerGame class) to the value of diceA
              System.out.println("Value of dice A is held" + "\n");//print statement 
            }//end if
            
            if (holdA == 2){//if user wants to hold second die
              player.setHoldB(dice.getDiceB());//sets holdB in setHoldB method (playerGame class) to the value of diceB
              System.out.println("Value of dice B is held" + "\n");//print statement
            }//end if
            
            if (holdA == 3){//if user wants to hold both dice
              player.setHoldA(dice.getDiceA());//sets holdA in setHoldA method (playerGame class) to the value of diceA
              System.out.println("Value of dice A is held" + "\n");//print statement
              player.setHoldB(dice.getDiceB());//sets holdB in setHoldB method (playerGame class) to the value of diceB
              System.out.println("Value of dice B is held" + "\n");//print statement
            }//end if
          }// end while
          
          if (i == 0){//if statement runs to calculate total score for player one
            totalA = ((player.getHoldA() + player.getHoldB()));// sum of values of holdA and holdB are assigned to totalA
            System.out.println("FINAL TOTAL: " + totalA + "\n");//final total is printed to screen
          }//end if
          
          if (i == 1){//if statement runs to calculate total score for player two 
            totalB = ((player.getHoldA() + player.getHoldB()));// sum of values of holdA and holdB are assigned to totalB
            System.out.println("FINAL TOTAL: " + totalB + "\n");//final total is printed to screen  
          }//end if 
        }//end for
        
        if (totalA > totalB){//if totalA is greater than totalB player one is the winner
          System.out.println("CONGRATULATIONS! PLAYER 1 IS THE WINNER WITH A TOTAL OF: " + totalA);//print statement stating winner and total points
        }//end if
        
        if(totalA < totalB){//if totalA is less than totalB player one is the winner
          System.out.println("CONGRATULATIONS! PLAYER 2 IS THE WINNER WITH A TOTAL OF: " + totalB);//print statement stating winner and total points
        }//end if
        
        
      }//end main
    }//end diceGame
    playerGame
    Code:
    import java.io.*;
    
    public class playerGame
    {
      private int holdA = 0, holdB = 0;//private int variables holdA and holdB (holds desired dice numbers) are initialized
      
      
      public playerGame(){//constructor
      }
      
      public void setHoldA (int valA){//setter with parameters of int valA. 
        holdA = valA;//holdA is set to valA or whatever value is sent in to the method
      }//end setHoldA
      public void setHoldB (int valB){//setter with parameters of int valB.
        holdB = valB;//holdB is set to valB or whatever value is sent in to the method
      }//end setHoldB
      
      public int getHoldA () {//getter for holdA, returns value of holdA
        return holdA;
      }//end getHoldA
      public int getHoldB () {//getter for holdB, returns value of holdB
        return holdB;
      }//end getHoldB    
    }//end playerGame
    pairOfDice
    Code:
    import java.io.*;
    
    public class pairOfDice
    {
      
      private int diceA = 0, diceB = 0, turns = 0;// initialize private int variable(dice A & B and turns)
      
      public pairOfDice() {//constructor containing roll method
        roll();  
      }// end pairOfDice
     
      public void roll(){//roll method finds the random numbers for the dice and contains a 'turn' counter to resemble three dice rolls
        diceA = (int)(Math.random()*6) + 1;
        diceB = (int)(Math.random()*6) + 1;
        turns = turns +1;
      }//end roll
      
      public void setDice(int newDiceA, int diceA) {//setter for diceA, sets it to a new value
        diceA = newDiceA;
      }//end setDice
     
      public void setDiceB(int newDiceB, int diceB) {//setter for diceB, sets it to a new value
        diceB = newDiceB;
      }//end setDiceB
     
      public void setTurns() {//setter for number of turns, reset turns to zero when called
        turns = 0;
      }//end setTurns
      
      public int getDiceA() {//getter for diceA, returns new value of diceA
        return diceA;
      }//end getDiceA
     
      public int getDiceB() {//getter for diceB, returns new value of diceB
        return diceB;
      }//end getDiceB
     
      public int getTotal() {//getter for total of dice A & B. returns sum of both dice
        return (diceA + diceB);
      }//end getTotal
     
      public int getTurns() {//getter for turns.returns number of turns made
        return turns;
      }//end getTurns
    }//end pairOfDice class
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    Why isn't it working.
    Can you show the program's output and explain what is wrong with it and add some comments to it showing what the output should be?

    Also posted at: http://www.javaprogrammingforums.com...s-trouble.html
    Last edited by NormR; October 3rd, 2013 at 03:17 PM.

IMN logo majestic logo threadwatch logo seochat tools logo