Dice problem. Help needed.

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Dice problem. Help needed.

1. Registered User
Join Date
Nov 2005
Posts
3

## Dice problem. Help needed.

Greetings to everyone.

Assume that we have a dice program a very simple one, just prints line of information.
Imagine that we don't want to roll two sixes in any six consecutive roll, how can we do that?

code:
lastRoll = diceone.roll();
for(int i = 0; i<600; i++){
int newRoll = diceone.roll();
if (newRoll == lastRoll){
System.out.println("new and last roll are identical");
break;
}
lastRoll = newRoll;
}

d.

2. Registered User
Join Date
Sep 2005
Location
istanbul / Turkey
Posts
133
rollsIndex = rollsIndex + 1 % 6;
this dont let more than 6 element in array .
includes function search in arrray.

Code:
```import java.util.Random;

public class DiceTest {
// roll the dice
public static int diceone_roll(){
int i =  ( r.nextInt(6) + 1 );//a random between 1-6
System.out.println(i);
return i;
}
static Random r = new Random(System.currentTimeMillis());

// if newRoll's value repeated rolls
public static boolean includes(int newRoll,int[] rolls ){
for ( int i = 0 ; i < rolls.length ; i++ ){
if ( newRoll == rolls[i] ) return true;//found
}
}

public static void main(String[] args) {
int[] rolls = new int[6];
int rollsIndex = 0;
for (int i = 0; i < 600; i++) {
int newRoll = diceone_roll();
if ( includes(newRoll,rolls) ) {
// rollsIndex = 0; //
System.out.println("new and last roll are identical");
break;
}
rollsIndex = rollsIndex + 1 % 6;
rolls[rollsIndex] = newRoll; // dont let more than 6 consecutive
}

}
}```
Last edited by mr1yh1; 11-11-2005 at 05:18 AM.

3. Registered User
Join Date
Nov 2005
Posts
3
thanks for that. really helpful stuff. can anyone tell me how can you create a class that will only roll 50 sixes and 110 of the other values in 600 rolls? and how can yu make a class so that two separate new derived dice objects can communicate and not roll consecutive sixes? i think something with the static int will be an ideal way but how?

d.

4. Registered User
Join Date
Nov 2005
Posts
3
after getting my head down to the problem I found it. thanks for your help.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise