-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPig.java
More file actions
124 lines (105 loc) · 4.54 KB
/
Pig.java
File metadata and controls
124 lines (105 loc) · 4.54 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//Max Brodheim
//Lab 1
//September 10th, 2016
//CPSC 124
//the AI in this game is mediocre at best:
//When it is in the lead, it plays defensively, and when it's losing agressively
//however, when its losing badly, it plays terribly.
import java.util.Scanner;
public class Pig {
public static void main (String[] args) {
Scanner input = new Scanner (System.in);
String outputRound = " This is your current score: ";
String outputGame = " These are the current scores in the game: ";
double playerScore = 0;
//player total score
double playerRound = 0;
//score this round
double computerScore = 0;
//computer's total score
double computerRound = 0;
//computer score this round
int roll = 42;
// value of the rolled die
double whosTurn = 1;
//denotes who's turn it is, -1 for computer, 1 for player
System.out.println(" Welcome to Pig! Please enter a name for your opponent" );
String name = input.nextLine();
//gives your opponent a name
System.out.println("(To quit, enter 'quit')");
while ( playerScore < 100 && computerScore < 100 ) {
if (whosTurn == 1) {
System.out.println(" Would you like to roll? (yes/no)");
String choice = input.nextLine();
if ( choice.equals("yes") ) {
roll = (int) (Math.random() * 6 + 1 );
System.out.println ( " Your roll is " + roll);
if ( roll == 1 ) {
playerRound = 0;
System.out.println( " Bad luck!! yours score for this round is now 0");
System.out.println();
System.out.println( outputGame );
System.out.println( " " + name + "'s score is " + computerScore);
System.out.println( " Your score is " + playerScore);
System.out.println();
whosTurn = -whosTurn;
} else {
playerRound = playerRound + roll;
System.out.println( outputRound + playerRound);
}
} else if (choice.equals("no") ) {
playerScore = playerRound + playerScore;
playerRound = 0;
whosTurn = -whosTurn;
System.out.println();
System.out.println( outputGame );
System.out.println( " " + name + "'s score is " + computerScore);
System.out.println( " Your score is " + playerScore);
System.out.println();
} else if (choice.equals("quit") ) {
playerScore = 1000;
} else {
System.out.println("It's not that hard to type one of two words. Yes or No");
}
} else if (whosTurn == -1) {
System.out.println( " " + name + ", it's your turn now. Do you want to roll?");
if (computerRound >= (playerScore - computerScore) ) {
System.out.println( name + ": Im not losing. I'll stick with this.");
computerScore = computerScore + computerRound;
computerRound= 0;
System.out.println( outputGame );
System.out.println();
System.out.println( " " + name + "'s score is " + computerScore);
System.out.println( " Your score is " + playerScore);
System.out.println();
whosTurn = -whosTurn;
} else if (computerRound < (playerScore - computerScore) || computerRound >= 30) {
System.out.println(name + ": I'm losing! Better be aggresive");
roll = (int) (Math.random() * 6 + 1 );
if (roll == 1) {
computerRound = 0;
System.out.println(" Your roll is: " + roll);
System.out.println(name + ": Aw shucks...");
System.out.println( outputGame );
System.out.println();
System.out.println( " " + name + "'s score is " + computerScore);
System.out.println( " Your score is " + playerScore);
System.out.println();
whosTurn = - whosTurn;
} else {
System.out.println("Your roll is: " +roll);
System.out.println( name + ": YAY!");
computerRound = computerRound + roll;
}
}
}
}
if (playerScore == 1000) {
System.out.println("Sorry to see you go!");
} else if (playerScore > computerScore) {
System.out.println("The winner is: The Player!");
}else {
System.out.println("You lost to a computer. Maybe try kickboxing?");
}
}
}