-
Notifications
You must be signed in to change notification settings - Fork 0
/
PigDice.java
71 lines (61 loc) · 2.09 KB
/
PigDice.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.Scanner;
public class PigDice {
public static void main( String[] args ) {
Scanner keyboard = new Scanner(System.in);
int roll, ptot, ctot, turnTotal;
String choice = "";
ptot = 0;
ctot = 0;
do {
turnTotal = 0;
System.out.println( "You have " + ptot + " points." );
do {
roll = 1 + (int)(Math.random()*6);
System.out.println( "\tYou rolled a " + roll + "." );
if ( roll == 1 ) {
System.out.println( "\tThat ends your turn." );
turnTotal = 0;
}
else {
turnTotal += roll;
System.out.print( "\tYou have " + turnTotal + " points" );
System.out.print( " so far this round.\n" );
System.out.print( "\tWould you like to \"roll\" again" );
System.out.print( " or \"hold\"? " );
choice = keyboard.next();
}
} while (roll != 1 && choice.equals("roll") );
ptot += turnTotal;
System.out.println("\tYou end the round with " + ptot + " points." );
if (ptot < 100 ) {
turnTotal = 0;
System.out.println(" \tComputer has " + ctot + " points." );
do {
roll = 1+ (int)(Math.random()*6);
System.out.println( "\tComputer rolled a " + roll + "." );
if ( roll == 1 ) {
System.out.println( "\tThat ends its turn." );
turnTotal = 0;
}
else {
turnTotal += roll;
System.out.print( "\tComputer has " + turnTotal );
System.out.print( " points so far this round.\n" );
if ( turnTotal < 20 ) {
System.out.println( "\tComputer will roll again." );
}
}
} while (roll != 1 && turnTotal < 20 && (ctot + turnTotal < 100) );
ctot += turnTotal;
System.out.print( "\tComputer ends the round with " );
System.out.print( ctot + " points.\n" );
}
} while ( ptot < 100 && ctot < 100 );
if ( ptot > ctot ) {
System.out.println( "Humanity wins!" );
}
else {
System.out.println(" The computer wins." );
}
}
}