-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardMatch.java
More file actions
195 lines (164 loc) · 6.12 KB
/
CardMatch.java
File metadata and controls
195 lines (164 loc) · 6.12 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
/*
* CardMatch.java
*
* The main class for a program that plays a card game called CardMatch.
* It also serves as a blueprint class for a CardMatch object, which maintains
* the state of the game.
*
*/
import java.util.*;
public class CardMatch {
/* 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 = 10;
/* the penalty for ending up with MAX_CARDS cards */
public static final int MAX_CARDS_PENALTY = 20;
/* fields of a CardMatch 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 CardMatch(Scanner console, int randomSeed, String playerName) {
this.console = console;
// Create and shuffle the deck.
this.deck = new Deck(randomSeed);
this.deck.shuffle();
// Create the players.
this.players = new Player[NUM_PLAYERS];
this.players[0] = new Player(playerName);
this.players[1] = new ComputerPlayer("the computer");
// Deal the cards.
this.dealHands();
this.topDiscard = this.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++) {
this.players[j].addCard(this.deck.drawCard());
}
}
}
/*
* play - plays an entire game of CardMatch
*/
public void play() {
this.printGameState();
while (true) {
// Each player makes one play.
for (int i = 0; i < this.players.length; i++) {
executeOnePlay(this.players[i]);
}
this.printGameState();
if (this.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 < this.players.length; i++) {
this.players[i].displayHand();
}
System.out.println("discard pile: ");
System.out.println(" " + this.topDiscard);
}
/*
* executeOnePlay - carries out a single play by the specified player
*/
public void executeOnePlay(Player player) {
// Keep looping until a valid play is obtained.
// We break out of the loop using a return statement.
while (true) {
int cardNum = player.getPlay(this.console, this.topDiscard);
if (cardNum == -1) {
System.out.println(player + " draws a card.");
player.addCard(this.deck.drawCard());
return;
} else {
Card cardToPlay = player.getCard(cardNum);
if (cardToPlay.matches(this.topDiscard)) {
System.out.println(player + " discards " + cardToPlay + ".");
player.removeCard(cardNum);
// The card played is now at the top of the discard pile.
this.topDiscard = cardToPlay;
return;
} 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 < this.players.length; i++) {
if (this.players[i].getNumCards() == 0
|| this.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 = this.players[0].getHandValue();
totalValue += winnerValue;
boolean isTie = false;
for (int i = 1; i < this.players.length; i++) {
int playerValue = this.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 " + this.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 Card Match!");
System.out.print("What's your first name? ");
String name = console.next();
CardMatch game = new CardMatch(console, seed, name);
game.play();
game.reportResults();
console.close();
}
}