3 Replies - 849 Views - Last Post: 06 June 2012 - 06:16 PM Rate Topic: -----

#1 commonSense  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-June 12

Dice game task

Posted 05 June 2012 - 06:02 PM

//Hi, I need to create a dice game for my computer science class and I ran into a problem. Basically, you roll five dice and I am trying to figure out if you scored doubles. I know I need to do an if statement. I tried something along the lines of

if (dice1=dice2)
{
    System.out.println("You rolled doubles!");
}


But, for this to work, I would have to repeat this step 25 times. I am wondering if there is an efficient way to do this. Your help would be greatly appreciated.

Jonah

Is This A Good Question/Topic? 0
  • +

Replies To: Dice game task

#2 Sheph  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Dice game task

Posted 05 June 2012 - 06:17 PM

You have the right idea. For each dice you rolled, you have to check each other dice. But you're wrong about the 25 comparisons. The first dice only needs to be checked against 4 others, the next one against 3 others, then 2 then 1, then you know the last one isn't any of the other 4. So, in actuality, there are only 10 comparisons. With that little bit of knowledge, we have shorted our code dramatically.

We can shorten it further if we use an array, and have the computer do the looping instead of hand writing it. Have you learned about arrays? How about for loops?
Was This Post Helpful? 1
  • +
  • -

#3 commonSense  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-June 12

Re: Dice game task

Posted 06 June 2012 - 03:55 PM

Thanks for replying! I can't believe I didn't see that earlier. And yes, that significantly shortens the code. I am comfortable with loops but I only need to do one dice roll. Would using a loop be beneficial? Thanks again for the help.

Jonah
Was This Post Helpful? 0
  • +
  • -

#4 Sheph  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Dice game task

Posted 06 June 2012 - 06:16 PM

Yes a loop would save you from rewriting all of the code for each of the dice. (Notice the "for each" in that sentence. Usually sentences like that qualify for a loop) Imagine you have an array of 5 dice, and that you initialize them with 5 random numbers from 1-6.

Random rand = new Random();
final int numberOfDice = 5;
int[] dice = new int[numberOfDice];

for(int i = 0; i < dice.length; i++) {
	dice[i] = rand.nextInt(6) + 1; // 1-6
}


Based on the very little code you showed, I imagine this is already an improvement. You have variables called dice1, dice2 etc. That usually qualifies for an array. With an array we are able to shove repetitive code into a loop because they can be accessed simply by an index.

Now imagine you want to use 6 dice instead. All you have to do is change the numberOfDice variable, and you have more dice and you didn't have to rewrite any code. If you write the code correctly for verifying doubles, you won't have to change that either. Give it a try!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1