CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Join Date
    Nov 2016
    Posts
    12

    Dice Game Assignment

    Whenever I have 1 and 1 or 6 and 6 player 1 loses a turn, and player 2 ( or viceversa) should roll two times instead is rolling once. can someone help me please ?

    Code:
    import java.util.Random;
    
    public class dim1 {
    
        public static void main (String[] args) {
    
             	Random dice = new Random();
    
     
    			int die1 = 0; 
    			int die2 = 0;
    			int sum1 = 0;
          		int player = 1;
    			int playerTotal1 = 0;
    			int playerTotal2 = 0;
    			 
    			while (playerTotal1 < 75 && playerTotal2 < 75){ 
    			
                  while (player == 1){
                  	die1 = 1 + dice.nextInt(6);
                  	die2 = 1 + dice.nextInt(6);
                    System.out.println("Player 1 rolls " + die1 + " " + die2);
    				
    			  
    				
                    if(((die1 == 1) && (die2 == 1)) || ((die1 == 6) && (die2 == 6))){
                      
    				sum1 = die1 + die2;
                    playerTotal1 = playerTotal1 + sum1;
    				  
                      player = 2;
    				  
    				  
                    }else if(die1 == die2){
                      sum1 = die1 + die2;
                      playerTotal1 = playerTotal1 + sum1;
                      player = 1;
    				  
                    }else{
                      sum1 = die1 + die2;
                      playerTotal1 = playerTotal1 + sum1;
                      player = 2;
                    }
                    System.out.println("Player 1 " + "now has " + sum1);
                  }
                  
                   while (player == 2){
                  	die1 = 1 + dice.nextInt(6);
                  	die2 = 1 + dice.nextInt(6);
                    System.out.println("Player 2 rolls " + die1 + " " + die2);
    				
                    	if(((die1 == 1) && (die2 == 1)) || ((die1 == 6) && (die2 == 6))){
                      		sum1 = die1 + die2;
                      		playerTotal2 = playerTotal2 + sum1;
    						
                          	player = 1;
    						
    						
                    	}else if(die1 == die2){
                      		sum1 = die1 + die2;
                      		playerTotal2 = playerTotal2 + sum1;
                      		player = 2;
    						
                    	}else{
                      		sum1 = die1 + die2;
                      		playerTotal2 = playerTotal2 + sum1;
                      		player = 1;
                    	}
                     System.out.println("Player 2 " + "now has " + sum1);
                  	}   
    			}
    Last edited by 2kaud; November 15th, 2016 at 02:43 AM. Reason: Added code tags

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Dice Game Assignment

    Please edit your post and wrap the code in code tags to preserve formatting and make the code more readable.

    player 2 ( or viceversa) should roll two times instead is rolling once
    Sounds like a logic problem. How does the code know when it should roll twice?

    Is there a comment in the code saying where and when there should be two rolls?
    Last edited by Norm; November 14th, 2016 at 06:47 PM.
    Norm

  3. #3
    Join Date
    Nov 2016
    Posts
    12

    Re: Dice Game Assignment

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap the code in code tags to preserve formatting and make the code more readable.


    Sounds like a logic problem. How does the code know when it should roll twice?

    Is there a comment in the code saying where and when there should be two rolls?
    Sorry I don't know wheres is the code tag, after system checks for die1 = 1 && die = 1 || die1 == 6 && die2 = 6

    lets say player 1 gets a 6 & 6
    this player loses its turn and player 2 follows rolls twice

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Dice Game Assignment

    Sorry I don't know wheres is the code tag,
    Go advanced, select the formatted code and click '#'.

    [Code tags added]
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Dice Game Assignment

    player 1 gets a 6 & 6
    this player loses its turn and player 2 follows rolls twice
    How does the program keep track that a player has rolled 2 6s so it knows the next player gets to roll twice?
    Norm

  6. #6
    Join Date
    Nov 2016
    Posts
    12

    Re: Dice Game Assignment

    Quote Originally Posted by Norm View Post
    How does the program keep track that a player has rolled 2 6s so it knows the next player gets to roll twice?

    It doesn't. I don't know how to

    Write a program that simluates a game of dice. In this game, players take alternate turns
    rolling two dice. On each turn, they record the sum of the two dice and add this to their
    total. If a player rolls a doublet (both dice have the same value), then the player gets to
    roll again. However, if the doublet is “snake eyes” (both dice have a value of 1) or “box
    cars” (both dice have a value of 6), then the player loses their next turn instead. The first
    player to reach a total of 75 will win.
    For games to 20, the output should be as follow (note: no user input is required):
    Example 1:
    Player 1 rolls a 3 and a 3
    Player 1 now has 6
    Player 1 gets to roll again
    Player 1 rolls a 5 and a 1
    Player 1 now has 12
    Player 2 rolls a 5 and a 1
    Player 2 now has 6
    Player 1 rolls a 5 and a 6
    Player 1 now has 23
    Player 1 wins with a total of 23
    Example 2:
    Player 1 rolls a 4 and a 6
    Player 1 now has 10
    Player 2 rolls a 4 and a 1
    Player 2 now has 5
    Player 1 rolls a 2 and a 5
    Player 1 now has 17
    Player 2 rolls a 6 and a 3
    Player 2 now has 14
    Player 1 rolls a 1 and a 1
    Player 1 now has 19
    Player 1 loses a turn
    Player 2 rolls a 1 and a 2
    Player 2 now has 17
    Player 2 rolls a 3 and a 5
    Player 2 now has 25
    Player 2 wins with a total of 25
    Note 1: No rolls should occur after a player reaches the winning score (even if they
    rolled a doublet).
    Note 2: Do not worry about the exact formatting of program output.

  7. #7
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Dice Game Assignment

    One way is to use a boolean variable that is set true when a player rolls 2 6s and left false otherwise.
    Then use that variable in the other player's turn to take two rolls when it is set true.

    Note: The code in the three blocks of the if/else if/else statements for a player is identical except for where it sets the player variable.
    Under some conditions the player gets to roll again. For other conditions the turn swaps to the other player.
    The code could be simplified to have a common part and a part that decides to swap players.
    Norm

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Dice Game Assignment

    You seem to have all the code in main(). As Java is an OOP language, depending upon how you have advanced with the language, have you considered using classes? Eg a class for a person with methods such as roll dice, get score, get/set status etc? Then main() interacts with the classes to control the game.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Nov 2016
    Posts
    12

    Re: Dice Game Assignment

    Quote Originally Posted by 2kaud View Post
    You seem to have all the code in main(). As Java is an OOP language, depending upon how you have advanced with the language, have you considered using classes? Eg a class for a person with methods such as roll dice, get score, get/set status etc? Then main() interacts with the classes to control the game.

    I'm really new to Java, my previous code was messy. So my friend fixed it for me. I understand how it works but don't know how to fix it

  10. #10
    Join Date
    Nov 2016
    Posts
    12

    Re: Dice Game Assignment

    Quote Originally Posted by Norm View Post
    One way is to use a boolean variable that is set true when a player rolls 2 6s and left false otherwise.
    Then use that variable in the other player's turn to take two rolls when it is set true.

    Note: The code in the three blocks of the if/else if/else statements for a player is identical except for where it sets the player variable.
    Under some conditions the player gets to roll again. For other conditions the turn swaps to the other player.
    The code could be simplified to have a common part and a part that decides to swap players.

    How would you do it ?

  11. #11
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Dice Game Assignment

    Define a boolean: boolean playerCanRollTwice
    set playerCanRollTwice true when a player rolls 2 6s
    when the other player rolls, if playerCanRollTwice is true, the player rolls a second time
    Norm

  12. #12
    Join Date
    Nov 2016
    Posts
    12

    Re: Dice Game Assignment

    Quote Originally Posted by Norm View Post
    Define a boolean: boolean playerCanRollTwice
    set playerCanRollTwice true when a player rolls 2 6s
    when the other player rolls, if playerCanRollTwice is true, the player rolls a second time
    Sorry, where do I place it in my loop ?

  13. #13
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Dice Game Assignment

    define it outside of the loop
    set it true when double 6's are rolled
    test its value when deciding if a player can roll a second time

    The first thing you should do is simplify the code:
    For example
    loop for a player's turn
    roll for player
    show roll and player's total
    check if player gets another roll:
    if pair and pair not 1-1 or 6-6 player gets another roll. if 6-6 other player gets second roll
    if not a pair, and not gets second roll, end this player's turn and swap to other player
    end loop
    Last edited by Norm; November 21st, 2016 at 09:11 PM.
    Norm

  14. #14
    Join Date
    Nov 2016
    Posts
    12

    Re: Dice Game Assignment

    Quote Originally Posted by Norm View Post
    define it outside of the loop
    set it true when double 6's are rolled
    test its value when deciding if a player can roll a second time

    The first thing you should do is simplify the code:
    For example
    loop for a player's turn
    roll for player
    show roll and player's total
    check if player gets another roll:
    if pair and pair not 1-1 or 6-6 player gets another roll. if 6-6 other player gets second roll
    if not a pair, or gets second roll, end this player's turn and swap to other player
    end loop

    I tried what you said but it's not working. Maybe I'm doing it wrong.

  15. #15
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Dice Game Assignment

    Please post the new code (wrapped in code tags)
    and the output that shows what you are talking about.
    Norm

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured