-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWar.java
More file actions
256 lines (227 loc) · 7.63 KB
/
War.java
File metadata and controls
256 lines (227 loc) · 7.63 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* Author: Sam Solheim
* Date: 3/21/2021
* Sources: None in this class, source used for Deck class is listed in "Deck.java"
*/
package cardGame;
import java.io.IOException;
public class War {
// Creates private variables for player/computer scores and cards
private int p_score;
private int c_score;
private Card p_card;
private Card c_card;
// Cards to be used in case of a draw
private Card p_draw1; private Card p_draw2;
private Card c_draw1; private Card c_draw2;
public War()
{
Deck d = new Deck();
// initializes player/computer scores and cards, shuffles deck
p_score = 0; c_score = 0;
p_card = new Card(2, 'C'); c_card = new Card(2, 'C');
p_draw1 = new Card(2, 'C'); p_draw2 = new Card(2, 'C');
c_draw1 = new Card(2, 'C'); c_draw2 = new Card(2, 'C');
d.shuffle();
System.out.println("The deck has been shuffled...");
System.out.println();
// Variable to keep track of which round it is
int round = 1;
// Prompts user to start the game
System.out.println("Please press ENTER to start the game:");
System.out.println("_____________________________________________________");
promptEnterKey();
System.out.println("Round 1");
System.out.println();
// Checks if the deck is empty
while(d.isEmpty() == false)
{
// Outputs the current round after the game is initialized in first round, prompts user to press ENTER
if(round > 1)
{
System.out.println("Please press ENTER to start the round:");
System.out.println();
System.out.println("_____________________________________________________");
promptEnterKey();
System.out.println("Round " + round);
System.out.println();
}
// Player and computer both draw cards
p_card = d.draw();
c_card = d.draw();
// Checks for case where Player/Computer's card is assigned a value of null
if(p_card == null || c_card == null)
{
end_dialogue();
}
// Main program for majority of time: when both opponents have values stored in their cards
else if(p_card != null && c_card != null)
{
// Displays the cards drawn by the Player and the Computer
System.out.println("Player: " + p_card.getRank() + " " + p_card.getSuit());
System.out.println("Computer: " + c_card.getRank() + " " + c_card.getSuit());
// Player wins the round
if(p_card.getRank() > c_card.getRank())
{
System.out.println("The player wins this round.");
p_score += 1;
round += 1;
}
// Computer wins round
else if(p_card.getRank() < c_card.getRank())
{
System.out.println("The computer wins this round.");
c_score += 1;
round += 1;
}
// Case for a War
else if(p_card.getRank() == c_card.getRank())
{
// Each player draws two more cards
p_draw1 = d.draw();
p_draw2 = d.draw();
c_draw1 = d.draw();
c_draw2 = d.draw();
if(p_draw1 == null || c_draw1 == null)
{
end_dialogue();
}
else if(p_draw2 == null || c_draw2 == null)
{
special_case_war();
end_dialogue();
}
else
{
war_dialogue();
round += 1;
}
}
// Following statement checks if the Deck is empty, and will also check if p_card and c_card have been assigned the value of null.
// Note: The last two "or's" are used as a safety net to avoid game-breaking issues encountered earlier in coding process
else if (d.isEmpty() == true || p_card == null || c_card == null)
{
end_dialogue();
}
}
}
}
public static void main(String args[])
{
// Calls the War function to start the game
War w = new War();
}
// The following method cleans up the main "War" function
private void war_dialogue()
{
System.out.println("It's a War!");
System.out.println("Press ENTER to play your first card:");
promptEnterKey();
// Player and Computer place their first cards in the war
System.out.println("Player: " + p_draw1.getRank() + " " + p_draw1.getSuit());
System.out.println("Computer: " + c_draw1.getRank() + " " + c_draw1.getSuit());
// Player wins war with first card
if(p_draw1.getRank() > c_draw1.getRank())
{
System.out.println("The player won the war and the round!");
p_score += 3;
}
// Computer wins war with first card
else if(p_draw1.getRank() < c_draw1.getRank())
{
System.out.println("The computer won the war and the round!");
c_score += 3;
}
// Neither wins with first card
else if(p_draw1.getRank() == c_draw1.getRank())
{
System.out.println("It's another tie! The war continues:");
System.out.println("Press ENTER to play your second card:");
promptEnterKey();
System.out.println("Player: " + p_draw2.getRank() + " " + p_draw2.getSuit());
System.out.println("Computer: " + c_draw2.getRank() + " " + c_draw2.getSuit());
// Player wins with second card
if(p_draw2.getRank() > c_draw2.getRank())
{
System.out.println("The player won the war and the round!");
p_score += 3;
}
// Computer wins with second card
else if(p_draw2.getRank() < c_draw2.getRank())
{
System.out.println("The computer won the war and the round!");
c_score += 3;
}
// Neither wins, ends in stalemate
else if(p_draw2.getRank() == c_draw2.getRank())
{
System.out.println("The war ended in a stalemate!");
}
}
}
// The "special case" refers to a war happening and having enough cards for the first draw of the war, but not the second part of the war
private void special_case_war()
{
System.out.println("It's a War!");
System.out.println("Press ENTER to play your first card:");
promptEnterKey();
// Player and Computer place their first cards in the war
System.out.println("Player: " + p_draw1.getRank() + " " + p_draw1.getSuit());
System.out.println("Computer: " + c_draw1.getRank() + " " + c_draw1.getSuit());
// Player wins war with first card
if(p_draw1.getRank() > c_draw1.getRank())
{
System.out.println("The player won the war and the round!");
System.out.println();
p_score += 3;
}
// Computer wins war with first card
else if(p_draw1.getRank() < c_draw1.getRank())
{
System.out.println("The computer won the war and the round!");
System.out.println();
c_score += 3;
}
// Neither wins with first card
else if(p_draw1.getRank() == c_draw1.getRank())
{
// Nothing needs to be done here, other than printing a blank line for formatting purposes
System.out.println();
}
}
// Starts ending dialogue of game and prints out who won, and the player/computer scores
private void end_dialogue()
{
System.out.println("There are no more cards in the deck. Good game!");
System.out.println();
System.out.println("_____________________________________________________");
System.out.println();
// Case if player wins
if(p_score > c_score)
{
System.out.println("The player wins with a score of " + p_score + "!");
System.out.println("Computer Score: " + c_score);
}
// Case if computer wins
else if(p_score < c_score)
{
System.out.println("The computer wins with a score of " + c_score + "!");
System.out.println("Player Score: " + p_score);
}
// Case if the game ends in a Draw
else
{
System.out.println("It's a tie!");
System.out.println("Tie Score: " + c_score);
}
}
// Code given to prompt the user to click the enter key
public static void promptEnterKey()
{
try {
System.in.read(new byte[2]);
}
catch (IOException e) {
e.printStackTrace();
}
}
}