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).

Please note that this is an assignment for school and I do not want to publicly post my code online..but I don't mind PMing/emailing a user(s). I get everything except for the part of how to alternate it between the two different players. For example, I have two dice randomly generating:

  dice1 = (int)(Math.random() * 6) + 1;
  dice2 = (int)(Math.random() * 6) + 1;

Then I have what is displayed below. However I don't know how to make it so it alternates for the two players. I also have booleans for "player one's turn, when that player loses a turn and when player two loses a turn". I also know the conditions for which the program should run (while loop) and in an if, else-if I have "doublets" and "snake eyes" defiend respectively.

  total1 = dice1 + dice2;
  total2 = dice1 + dice2;
  sum1 = total1 + total2;
  sum2 = total1+total2;

Recommended Answers

All 7 Replies

just have a loop that handles a turn for each play in order.
Something like :

while(playing)
    player1.doTurn()
    player2.doTurn()
    if(end)
        playing = false

im assuming .doTurn() is a method? We haven't learned methods/creating objects or whatever that is..yet..so not sure how to do that. Thanks for your response though, any other way to do it?

yeah it was just a way of saying do player one's turn. So you would just replace that by the code that does the turn for each player .

im so confused lol I saw a sample code online where it says to make one player = 1 and the other player = 2 and then sort of tie them into if/else statements.

It depends on what you are trying to do. The example i gave is to have turn in which player 1 plays first and they player 2 plays. If this order has to be different you indeed have to use if's within your loop. However without you posting any code i might have misunderstood your question.

I could PM you if you'd like? It is supposed to be player 1 before player 2.

if player one is always before player two, then a complete turn would consist of a turn for player one followed by a turn for player two ?

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.