-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuno.java
242 lines (207 loc) · 8.25 KB
/
Huno.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
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
/*
* Huno.java
*
* Computer Science S-111, Harvard University
*
* The main class for a program that plays a card game called Huno.
* It also serves as a blueprint class for a Huno object, which maintains
* the state of the game.
*
* Constituent files:
* Huno.java - maintains entire game and game flow
* Deck.java - stores random selection of cards
* Card.java - represents a single card with color and value
* Player.java - represents a human player
* ComputerPlayer.java - represents a cpu (extended from Player.java)
*
* modified by: Vineet Parikh (vince.parikh@gmail.com)
*
* Mod Docs:
* - Added special cards: draw 2, skip, and wild card
* - Modified game flow and computer AI to account for new cards: computer now prioritizes
* other cards over wild cards, and uses wild cards as a last resort.
*
* Ways to extend this:
* - Create tutorial section
* - Improve AI to read player strategy and adapt by abruptly changing colors or using wilds.
* (this can probably be done with color "priorities" which are constantly updated)
* - Create a setup to choose btw. player vs. player, or player v.s cpu
* - Update this to work with more than 2 players (cpu or human)
* - Add more special cards ("Reverse","Draw 4")
* - Add taunts by computer (i.e. "Is that your BEST?")
* - Possibly use a linked list over an array to make # of cards limitless (however, this means that there's now
* a slight efficiency problem involved: do this at your own risk).
* etc. etc. etc.
*/
import java.util.*;
public class Huno {
/* the number of players in the game */
public static final int NUM_PLAYERS = 2;
/* cards per player at start of game */
public static final int NUM_INIT_CARDS = 5;
/* the game ends if a player ends up with this many cards */
public static final int MAX_CARDS = 20;
/* the penalty for ending up with MAX_CARDS cards */
public static final int MAX_CARDS_PENALTY = 25;
/* fields of a Huno object */
private Scanner console; // used to read from the console
private Deck deck; // the deck of cards used for the game
private Player[] players; // the players of the game
private Card topDiscard; // card at the top of the discard pile
/*
* constructor - takes the Scanner to be used to read from the
* console, a randomSeed for the Deck's random-number generator,
* and the name of the player.
*/
public Huno(Scanner console, int randomSeed, String playerName) {
this.console = console;
// Create and shuffle the deck.
deck = new Deck(randomSeed);
deck.shuffle();
// Create the players.
players = new Player[NUM_PLAYERS];
players[0] = new Player(playerName);
players[1] = new ComputerPlayer("the computer");
// Deal the cards.
dealHands();
topDiscard = deck.drawCard();
}
/*
* dealHands - deals the initial hands of the players.
* Each player gets NUM_INIT_CARDS cards.
*/
public void dealHands() {
for (int i = 0; i < NUM_INIT_CARDS; i++) {
for (int j = 0; j < NUM_PLAYERS; j++) {
players[j].addCardToHand(deck.drawCard());
}
}
}
/*
* play - plays an entire game of Huno
*/
public void play() {
printGameState();
while (true) {
// Each player makes one play.
for (int i = 0; i < players.length; i++) {
executeOnePlay(players[i],players[players.length-i-1]); // there's only 2 players here, so this
// is pretty solid.
}
printGameState();
if (gameOver()) {
return;
}
}
}
/*
* printGameState - prints the players' hands and the card at the
* top of the discard pile
*/
public void printGameState() {
System.out.println();
for (int i = 0; i < players.length; i++) {
players[i].printHand();
}
System.out.println("discard pile: ");
System.out.println(" " + topDiscard);
//System.out.println();
}
/*
* executeOnePlay - carries out a single play by the specified player
*/
public void executeOnePlay(Player player, Player other) {
// Keep looping until a valid play is obtained.
// We break out of the loop using a return statement.
while (true) {
int cardNum = player.getPlay(console, topDiscard);
if (cardNum == -1) {
System.out.println(player + " draws a card.");
player.addCardToHand(deck.drawCard());
return;
} else {
Card cardToPlay = player.getCardFromHand(cardNum);
if (cardToPlay.getValue()==12){ // this is a wildcard
player.removeCardFromHand(cardNum);
String newColor = player.selectColor(console);
cardToPlay.setColor(newColor);
System.out.println(player + " discards " + cardToPlay + ".");
topDiscard = cardToPlay;
return;
}else if (cardToPlay.matches(topDiscard)) {
System.out.println(player + " discards " + cardToPlay + ".");
player.removeCardFromHand(cardNum);
if (cardToPlay.getValue()==10){ // this is a draw 2
other.addCardToHand(deck.drawCard());
other.addCardToHand(deck.drawCard());
}
// The card played is now at the top of the discard pile.
topDiscard = cardToPlay;
if (cardToPlay.getValue()!=11)
return; // a skip wasn't used, so stop the turn
else
printGameState(); // a skip was used, so show the new hand here
} else {
System.out.println("invalid play -- " + cardToPlay +
" doesn't match top of discard pile");
}
}
}
}
/*
* gameOver - returns true if the game is over -- either because
* a player has no cards or because a player has the maximum
* number of cards -- and false otherwise.
*/
public boolean gameOver() {
for (int i = 0; i < players.length; i++) {
if (players[i].getNumCards() == 0
|| players[i].getNumCards() == MAX_CARDS) {
return true;
}
}
return false;
}
/*
* reportResults - determines and prints the results of the game.
*/
public void reportResults() {
int totalValue = 0;
int winnerIndex = 0;
int winnerValue = players[0].getHandValue();
totalValue += winnerValue;
boolean isTie = false;
for (int i = 1; i < players.length; i++) {
int playerValue = players[i].getHandValue();
totalValue += playerValue;
if (playerValue < winnerValue) {
winnerValue = playerValue;
winnerIndex = i;
isTie = false;
} else if (playerValue == winnerValue) {
isTie = true;
}
}
if (isTie) {
System.out.println("The game is a tie; no one earns any points.");
} else {
System.out.print("The winner is " + players[winnerIndex]);
System.out.print(", who earns " + (totalValue - winnerValue));
System.out.println(" points.");
}
}
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int seed = -1;
if (args.length > 0) {
seed = Integer.parseInt(args[0]);
}
System.out.println("Welcome to the game of Huno!");
//System.out.println();
System.out.print("What's your first name? ");
String name = console.next();
Huno game = new Huno(console, seed, name);
game.play();
game.reportResults();
}
}