-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ch3Ex10.java
67 lines (63 loc) · 1.56 KB
/
Ch3Ex10.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
//Pig Game adapted from my python code
import java.util.Random;
import java.util.Scanner;
public class Ch3Ex10
{
public static void main(String[] args)
{;
int scoreHuman = 0;
int scoreComputer = 0;
int rolltotal = 0;
Scanner input = new Scanner(System.in);
Random rand = new Random();
while (scoreComputer< 100 && scoreHuman < 100)
{
//Player turn
System.out.println("You: "+scoreHuman+" | Computer: "+scoreComputer);
System.out.println(" - - - - - - - - Human Playing - - - - - - - - - ");
rolltotal = 0;
int roll = rand.nextInt(6)+1;
String sel = "0";
System.out.println("You rolled a "+ roll);
while (roll>1 && !sel.equals("h"))
{
System.out.println("Roll again or Hold (r/h) : ");
sel = input.nextLine();
if (sel.equals("r"))
{
rolltotal += roll;
roll = rand.nextInt(6)+1;
System.out.println("You rolled a "+ roll);
}
else if (sel.equals("h"))
{
rolltotal+=roll;
scoreHuman +=rolltotal;
}
else
System.out.println("Try Again");
}
System.out.println(" - - - - - - - - Computer Playing - - - - - - - - - ");
rolltotal = 0;
roll = rand.nextInt(6)+1;
while (roll!= 1 && rolltotal <20)
{
rolltotal += roll;
roll = rand.nextInt(6)+1;
}
if (rolltotal > 20)
{
scoreComputer += rolltotal;
}
}
System.out.println("Good Game");
if (scoreHuman>scoreComputer)
{
System.out.println("You Won!");
}
else
System.out.println("You Lost!");
System.out.println("You: "+scoreHuman+ " | Computer: "+scoreComputer);
input.close();
}
}