-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicWhist.java
More file actions
440 lines (357 loc) · 14.6 KB
/
Copy pathBasicWhist.java
File metadata and controls
440 lines (357 loc) · 14.6 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Module: CMP-5015Y Programming 2
// Assignment: Coursework 1
//
// Author: Matthew Taylor
//
// Description: Runs a basic simulation, with 4 BasicPlayers
package whist;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import whist.Card.*;
public class BasicWhist {
// game parameters
static final int NO_TRICKS = 13;
static final int WINNING_POINTS = 7;
// stores the players
Player[] players;
// game state variables
int roundNumber;
int handNumber;
int currentPlayer;
Suit trumpSuit;
Trick currentTrick;
Deck deck;
int team1points = 0;
int team2points = 0;
// constructor
public BasicWhist(Player[] p) {
// set up players
players = p;
// game starts on round 1/hand 1
roundNumber = 1;
handNumber = 1;
currentTrick = new Trick(trumpSuit);
// create new deck (constructed shuffled)
deck = new Deck();
}
// gets the next player
public int getCurrentPlayer() {
return currentPlayer;
}
// sets the next player
public void setNextPlayer() {
currentPlayer++;
if(currentPlayer > 4)
currentPlayer = 1;
}
// deals cards to all the players
public void dealHands() {
for(int i = 0; i < 52; i++) {
players[i%4].dealCard(deck.deal());
}
}
// gets card from each player for given trick, starting with first player
public Trick playTrick() {
// stores trick to be returned
currentTrick = new Trick(trumpSuit);
// stores card from player
Card c;
// calculated player number from ordering
int pNum;
// go through players in order
for(int i = 0; i < 4; i++) {
// always returns players in order, starting with current player
pNum = (i + currentPlayer) % 4;
// get card from next player
c = players[pNum].playCard(currentTrick);
// add found card to trick with player's number
currentTrick.setCard(c, pNum);
}
return currentTrick;
}
// runs a game (13 tricks)
public void playGame() {
// initialise game
deck = new Deck(); // decks are created shuffled
dealHands(); // deal cards to all players
handNumber = 1; // reset handNumber for each round
// randomly determine starting player
Random rand = new Random();
currentPlayer = rand.nextInt(4) + 1;
// determine trump suit for hand
trumpSuit = Suit.getRandom();
// for each player
for (Player player : players) {
player.resetState();
player.setTrumps(trumpSuit); // set trump suit for each player
}
// loop for playing each hand
for(int i = 0; i < NO_TRICKS; i++) {
Trick t = playTrick();
// send completed trick to each player
for (Player player : players) {
player.viewTrick(t);
}
// print output of current game state after each trick
printGameState();
// update game state
handNumber++;
setNextPlayer();
}
// determines winner of game and updates points
int team1 = players[0].getTricksWon() + players[2].getTricksWon();
int team2 = players[1].getTricksWon() + players[3].getTricksWon();
int winningTeam;
if(team1 > team2) {
team1points += team1 - 6;
winningTeam = 1;
}
else {
team2points += team2 - 6;
winningTeam = 2;
}
// reset player's points for round
for (Player player : players) {
player.resetTricksWon();
}
printRoundSummary(winningTeam);
// update game state
roundNumber++;
}
// runs a match (until either team has 7+ points)
// returns an int of the winning team (1 or 2)
public int playMatch() {
// ensure team's point are set to 0
team1points = 0;
team2points = 0;
while(team1points < WINNING_POINTS && team2points < WINNING_POINTS) {
playGame();
}
if(team1points>=WINNING_POINTS) {
System.out.println("Winning team is Team 1 with " + team1points);
System.out.println("-------------------------------------------------");
return 1;
}
else {
System.out.println("Winning team is Team 2 with " + team2points);
System.out.println("-------------------------------------------------");
return 2;
}
}
// displays the current game state
public void printGameState() {
// flags to hold ends of trick/round
boolean trickComplete = false;
if(currentTrick.getNumOfCardsInTrick() == 4)
trickComplete = true;
System.out.println("-------------------------------------------------");
System.out.println("Round: " + roundNumber + " | Hand: " + handNumber +
" | Trump suit is: " + trumpSuit);
System.out.println("-------------------------------------------------");
System.out.println("Tricks won this round:");
System.out.println("[Team 1] Player 1: " + players[0].getTricksWon());
System.out.println("[Team 2] Player 2: " + players[1].getTricksWon());
System.out.println("[Team 1] Player 3: " + players[2].getTricksWon());
System.out.println("[Team 2] Player 4: " + players[3].getTricksWon());
System.out.println("-------------------------------------------------");
System.out.println("Current trick:");
printCurrentTrick();
// if trick is complete, print out the winner
if(trickComplete) {
int winningID = currentTrick.findWinner();
System.out.println("-------------------------------------------------");
System.out.println("Player " + (winningID + 1) + " wins the trick!");
System.out.println("-------------------------------------------------");
}
System.out.println("\n");
}
// displays round summary
public void printRoundSummary(int winningTeam) {
System.out.println("-------------------------------------------------");
System.out.println("ROUND " + roundNumber + " SUMMARY");
System.out.println("-------------------------------------------------");
System.out.println("Team " + winningTeam + " wins!");
System.out.println("-------------------------------------------------");
System.out.println("Team scores are now");
System.out.println("> Team 1: " + team1points);
System.out.println("> Team 2: " + team2points);
System.out.println("-------------------------------------------------");
}
// logic to print the current trick
public void printCurrentTrick() {
if(currentTrick.getNumOfCardsInTrick() == 0) {
System.out.println("No cards currently played.");
}
else {
// get trick information from currentTrick
ArrayList<Card> cards = currentTrick.getCards();
ArrayList<Integer> players = currentTrick.getPlayers();
ArrayList<Boolean> lead = currentTrick.getLeadCard();
// loop through cards in trick
for(int i = 0; i < currentTrick.getNumOfCardsInTrick(); i++) {
// print player and card
System.out.print("P" + (players.get(i) + 1) + ": " +
cards.get(i));
// print if lead card or not
if(lead.get(i))
System.out.print(" (lead)");
// always print new line
System.out.print("\n");
}
}
}
public static void basicGame() {
boolean playGame = true;
// loop that runs as long as play another option set to y
while(playGame) {
Player[] playerList = { new BasicPlayer(1),
new BasicPlayer(2),
new BasicPlayer(3),
new BasicPlayer(4),
};
BasicWhist whist = new BasicWhist(playerList);
whist.playMatch();
System.out.println("\nPlay another? (y/n)");
// get and process user input
char c = 'a';
while(c != 'y' && c!= 'n') {
Scanner reader = new Scanner(System.in);
c = reader.next().charAt(0);
if(c == 'n')
playGame = false;
}
}
System.exit(0); // quit game
}
public static void humanGame() {
boolean playGame = true;
// loop that runs as long as play another option set to y
while(playGame) {
Player[] playerList = { new BasicPlayer(1),
new BasicPlayer(2),
new BasicPlayer(3),
new BasicPlayer(4),
};
playerList[0].setStrategy(new HumanStrategy(1));
BasicWhist whist = new BasicWhist(playerList);
whist.playMatch();
System.out.println("\nPlay another? (y/n)");
// get and process user input
char c = 'a';
while(c != 'y' && c!= 'n') {
Scanner reader = new Scanner(System.in);
c = reader.next().charAt(0);
if(c == 'n')
playGame = false;
}
}
System.exit(0); // quit game
}
public static void advancedGame() {
boolean playGame = true;
// loop that runs as long as play another option set to y
while(playGame) {
int team1wins = 0;
int team2wins = 0;
// run game multiple times, output results
int TIMES_TO_RUN = 10;
for(int i = 0; i < TIMES_TO_RUN; i++) {
Player[] playerList = { new BasicPlayer(1),
new BasicPlayer(2),
new BasicPlayer(3),
new BasicPlayer(4),
};
// set players 1 and 3 to use advanced strategy
playerList[1].setStrategy(new AdvancedStrategy(2));
playerList[3].setStrategy(new AdvancedStrategy(4));
BasicWhist whist = new BasicWhist(playerList);
int result = whist.playMatch();
if(result == 1)
team1wins++;
if(result == 2)
team2wins++;
}
System.out.println("Team 1 wins: " + team1wins);
System.out.println("Team 2 wins: " + team2wins);
System.out.println("\nPlay another? (y/n)");
// get and process user input
char c = 'a';
while(c != 'y' && c!= 'n') {
Scanner reader = new Scanner(System.in);
c = reader.next().charAt(0);
if(c == 'n')
playGame = false;
}
}
System.exit(0); // quit game
}
// test harness
public static void main(String args[]) {
// // test construcutor / game initialisation
// Player[] playerList = { new BasicPlayer(1),
// new BasicPlayer(2),
// new BasicPlayer(3),
// new BasicPlayer(4),
// };
//
// BasicWhist whist = new BasicWhist(playerList);
//whist.trumpSuit = Suit.DIAMONDS;
//System.out.println("Random starting player is " + whist.getCurrentPlayer());
// // test game state printing
// whist.printGameState();
// System.out.println("Currently winning player is " + whist.currentTrick.findWinner());
//
// // test adding cards to tricks and printing game state
// Card card = new Card(Rank.SEVEN, Suit.HEARTS);
// whist.currentTrick.setCard(card, 1);
// whist.printGameState();
// System.out.println("Currently winning player is " + whist.currentTrick.findWinner());
//
// Card card2 = new Card(Rank.NINE, Suit.DIAMONDS);
// whist.currentTrick.setCard(card2, 2);
// whist.printGameState();
// System.out.println("Currently winning player is " + whist.currentTrick.findWinner());
//
// Card card3 = new Card(Rank.TEN, Suit.DIAMONDS);
// whist.currentTrick.setCard(card3, 3);
// whist.printGameState();
// System.out.println("Currently winning player is " + whist.currentTrick.findWinner());
//
// Card card4 = new Card(Rank.EIGHT, Suit.HEARTS);
// whist.currentTrick.setCard(card4, 4);
// whist.printGameState();
//
// System.out.println("Winning player is " + whist.currentTrick.findWinner());
//
// // test dealHands() method
// whist.dealHands();
//
// // output result of dealHands() test
// System.out.println(whist.players[0]);
// System.out.println(whist.players[1]);
// System.out.println(whist.players[2]);
// System.out.println(whist.players[3]);
//
// // test playTrick() method
// whist.printGameState();
// Trick testTrick = whist.playTrick();
// System.out.println(testTrick);
// whist.printGameState();
//
// // test playGame() implementation
// whist.playGame();
//
// // test playMatch() implementation
// whist.playMatch();
//
// // test basicGame()
// basicGame();
//
// // test humanGame()
// humanGame();
//
// test advancedGame() multiple times, output results for comparison
advancedGame();
}
}