Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Dice Roll

843789Oct 7 2009 — edited Oct 7 2009
Hey all!

For a game I'm working on I need to determine the order of who goes first by rolling a pair of dice. I am stuck on the situation when two or more players roll the same number and would have to re-roll the dice. This is the code I am using that just implemts a dice roll for each player. An array of size numberOfPlayers is created to store the dice roll for each player. Index 1 refers to the first player to roll the dice, and index n would refer to the nth player to to roll the dice. How should I go about handleing the situation I stated above? Thanks for all your time!!!
private void diceRoll(int numPlayers) 
	{
		int [] playerRoll = new int [numPlayers+1];
		int die1;   // Number showing on the first die.
                int die2;   // Number showing on the second die.

		for(int player = 1; player < numPlayers; player++)
		{
			JOptionPane.showMessageDialog(null, "Player " + player +
					                     " roll the dice to determine order.");

			die1 = (int)(Math.random()*6) + 1;   // Range 1-6
			die2 = (int)(Math.random()*6) + 1; 
			int total = die1 + die2;
			playerRoll[player] = total;
		}		
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 4 2009
Added on Oct 7 2009
12 comments
455 views