Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program that simulates a game of dice. In this game, 3 players will
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. After each turn
(when both roll), the code checks the sum of each player and the first player
to reach a total of 15 or more will win the first place. The code then checks
for the second and third place winners. The code will print out to the user
the winners in order.

What I have tried:

Java
static int player1Total = 0;
	static int player2Total = 0;
	static int winner = 0;	//0 = no winner yet;  1 = player 1 wins; 2 = player 2 wins
	static int round = 1; //record the play round;
	public static void main(String [] args)
	{		
		while (winner == 0)
		{
			System.out.println("\n------------This is round "+round+"-------------");
			round++;			
			rollDice1();			
			rollDice2();
		}
	}		
	public static void rollDice1()
	{
		if (winner == 0)
		{
			int num1=0;
			int num2=0;	
			do
			{
				num1 = (int) (Math.random() * 6)+1;
				num2 = (int) (Math.random() * 6)+1;
				System.out.println("Player 1 rolls a "+num1+" and a "+num2);	
				player1Total = player1Total+num1+num2;
				System.out.println("Player 1 now has "+player1Total);
				if (num1==num2&&player1Total<75)
				{
					System.out.println("Player 1 gets to roll again");
				}				
			}
			while(num1==num2&&player1Total<75);
			if (player1Total>=75)
			{
				winner = 1;
				System.out.println("Player 1 wins with a total of "+player1Total);
				System.out.println("\nFinal score is \nplayer 1: "+player1Total+"\nplayer 2: "+player2Total);
			}
		}		
	}
	

	public static void rollDice2()
	{
		if (winner == 0)
		{			
			int num1 = (int) (Math.random() * 6)+1;			
			int num2 = (int) (Math.random() * 6)+1;
			System.out.println("Player 2 rolls a "+num1+" and a "+num2);			
			player2Total = player2Total+num1+num2;
			System.out.println("Player 2 now has "+player2Total);
			if (player2Total <75)
			{
				if(num1==num2)
				{
					System.out.println("Player 2 gets to roll again");
					rollDice2();//recall		
				}
			}
			else
			{
				winner = 2;
				System.out.println("Player 2 wins with a total of "+player2Total);
				System.out.println("\nFinal score is \nplayer 1: "+player1Total+"\nplayer 2: "+player2Total);
			}
		}
	}
}
Posted
Updated 3-Nov-20 20:10pm
v2

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did. That code doesn't answer the question in any significant detail: where is player three? Where did "75" come from? Why is the code for "rollDice1" and "rollDice2" pretty much identical? To be honest, that code looks like someone glanced at the question and just leapt into code without thinking at all hard about what the task involved, and that's why its a complete mess.
Start by reading the question carefully, and thinking about what is required. Then think about how you would do it on paper before jumping

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
CPallini 4-Nov-20 2:06am    
5.
You requirements are pretty clear. So try to follow them verbatim. At first, you may skip the doublet corner case (and then add it in the final release).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900