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
}
return false;// not 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
}
}
}
Bookmarks