Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Program with objects and classes trouble

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Program with objects and 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
    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

    pairOfDice
    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

    playerGame
    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


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Program with objects and classes trouble

    public void setDice(int newDiceA, int diceA) {//setter for diceA, sets it to a new value
        diceA = newDiceA;
      }//end setDice

    Why are you passing it diceA again? What you are doing, I believe, is you are setting the diceA in the parameter to newDiceA. (At least I think that's how the java scoping would work. At the very least, you don't need that second param.)


    This might help a bit

    public void setDice(int newDiceA)
    {
    diceA = newDiceA;
    }

    I believe you are also making the same mistake with setDiceB.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Program with objects and classes trouble

    Also posted at: Program with classes trouble - Dev Shed
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Objects and Classes
    By Worst Programmer Ever in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 4th, 2013, 03:13 PM
  2. Objects and Classes
    By Kristenw17 in forum Object Oriented Programming
    Replies: 1
    Last Post: April 23rd, 2013, 12:43 AM
  3. Replies: 17
    Last Post: July 27th, 2012, 12:52 AM
  4. Trouble using aggregate classes
    By pf1matt in forum Object Oriented Programming
    Replies: 3
    Last Post: June 22nd, 2012, 11:04 AM
  5. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM