Java Reference
In-Depth Information
rollDice(convert(keyboard.nextLine()));
//reads the dice
//to reroll and rerolls them
System.out . println(diceToString()) ;
//prints the dice
if
(isYahtzee())
{
System. out . println (
"You got Yahtzee!"
);
}
else
{
System. out . println (
"Sorry, better luck next time!"
);
}
}
{
public static
String diceToString()
String result =
"Your dice are: "
;
for
(
int
el : dice)
{
result += el +
""
;
return
result ;
}
public static boolean
isYahtzee()
{
for
(
int
el : dice)
{
if
(el != dice[0])
{
return false
;
}
return true
;
}
public static void
rollDice(
int
[ ] diceToChange)
{
for
(
int
i : diceToChange)
{
dice [ i
−
1] = getRandomDieValue () ;
}
}
public static void
rollDice()
{
for
(
int
i=0;i
<
NUMBER OF DICE ;
i ++)
{
dice [ i ] = getRandomDieValue () ;
}
}
public static int
getRandomDieValue ()
{
return
(
int
)
(Math . random ( )
6+1);
∗
}
public static int
[] convert(String s)
{
StringTokenizer st =
new
StringTokenizer(s) ;
int
[] a =
new i n t
[ st . countTokens () ] ;
int
i=0;
while
( st . hasMoreTokens () )
{
a[ i++] = Integer . parseInt(st .nextToken()) ;
return
a;
}
}