-
November 14th, 2016, 04:48 PM
#1
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
-
November 14th, 2016, 06:45 PM
#2
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
-
November 14th, 2016, 11:04 PM
#3
Re: Dice Game Assignment
Originally Posted by Norm
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
-
November 15th, 2016, 02:44 AM
#4
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)
-
November 15th, 2016, 10:13 AM
#5
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
-
November 15th, 2016, 11:12 AM
#6
Re: Dice Game Assignment
Originally Posted by Norm
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.
-
November 15th, 2016, 11:31 AM
#7
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
-
November 15th, 2016, 01:38 PM
#8
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)
-
November 16th, 2016, 09:15 PM
#9
Re: Dice Game Assignment
Originally Posted by 2kaud
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
-
November 20th, 2016, 12:11 AM
#10
Re: Dice Game Assignment
Originally Posted by Norm
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 ?
-
November 20th, 2016, 06:34 AM
#11
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
-
November 21st, 2016, 01:27 AM
#12
Re: Dice Game Assignment
Originally Posted by Norm
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 ?
-
November 21st, 2016, 06:36 AM
#13
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
-
November 21st, 2016, 08:56 PM
#14
Re: Dice Game Assignment
Originally Posted by Norm
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.
-
November 21st, 2016, 09:09 PM
#15
Re: Dice Game Assignment
Please post the new code (wrapped in code tags)
and the output that shows what you are talking about.
Norm
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|