ints, random, control statements Idea sketch
The oldest dice seem to have been used, 5000 years ago, to play backgammon in Iran. Those dice were made from animal's hooves; now, dice tend to made of plastic or code.
Below is a simple script that uses Java's built in
gen
.gen
to give its next integer in the range [0, 6). Then, we add 1 to what the nextInt
function returns, because we want our dice to give back 1, 2, 3, 4, 5, or 6.That makes a normal dice from code.
Let's make a tricky dice instead. It's up to you how to do this, but there's a few rules (even for tricky die):
class Dice{
public static void rollTrickyDice(){
/* Your code here */
}
public static void rollFairDice(){
Random gen = new Random();
int roll = gen.nextInt(6) + 1;
System.out.println("Fair dice rolls a " + roll);
}
public static void main(String[] args){
for(int i = 0; i < 5; i++){
rollFairDice();
rollTrickyDice();
System.out.println("-----")
}
}
}
Fair dice rolls a 5
Tricky dice rolls a 2
-----
Fair dice rolls a 3
Tricky dice rolls a 2
-----
Fair dice rolls a 1
Tricky dice rolls a 3
-----
Fair dice rolls a 4
Tricky dice rolls a 2
-----
Fair dice rolls a 2
Tricky dice rolls a 2
-----