-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigTwo.java
More file actions
320 lines (282 loc) · 9.07 KB
/
Copy pathBigTwo.java
File metadata and controls
320 lines (282 loc) · 9.07 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
import java.util.ArrayList;
/**
* The BigTwo class implements the CardGame interface and is used to model
* the BigTwo Card Game with private variable storing the basic information
* of the BigTwo game and method that runs the game.
*
* @author Chan Yuk Yin (UID: 3035786574)
*/
public class BigTwo implements CardGame{
private BigTwoClient client; //client of the game
private int numOfPlayers; // number of player
private Deck deck; // deck of card
private ArrayList<CardGamePlayer> playerList; // list of player
private ArrayList<Hand> handsOnTable; // list of hand played on the table
private int currentPlayerIdx; // current player index
private BigTwoGUI gui; // BigTwogui that provide the user interface
private int pass = 0; // counting the number passed in this round
// Constructor of BigTwo
public BigTwo(){
playerList = new ArrayList<CardGamePlayer>();
handsOnTable = new ArrayList<Hand>();
this.numOfPlayers = 4;
for (int i = 0; i < numOfPlayers; i++){
CardGamePlayer player = new CardGamePlayer();
playerList.add(player);
}
gui = new BigTwoGUI(this);
}
/**
* Getter of numOfPlayers
* @return int
*/
public int getNumOfPlayers(){
return this.numOfPlayers;
}
/**
* Getter of deck
* @return Deck
*/
public Deck getDeck(){
return this.deck;
}
/**
* Getter of playerlist
* @return ArrayList<CardGamePlayer>
*/
public ArrayList<CardGamePlayer> getPlayerList(){
return this.playerList;
}
/**
* Getter of handsOnTable
* @return ArrayList<Hand>
*/
public ArrayList<Hand> getHandsOnTable(){
return this.handsOnTable;
}
/**
* Getter of currentPlayerIdx
* @return int
*/
public int getCurrentPlayerIdx(){
return this.currentPlayerIdx;
}
/**
* Public Boolen method for the status of end game or not
* @return boolean
*/
public boolean endOfGame(){
for (int i = 0; i < playerList.size(); i++){
if (playerList.get(i).getCardsInHand().isEmpty()){
return true;
}
}
return false;
}
/**
* Public method that start the BigTwo game
* @param deck
*/
public void start(Deck deck){
// remove all cards from the players and table
for (int i = 0; i < playerList.size(); i++){
playerList.get(i).removeAllCards();
}
this.handsOnTable = new ArrayList<Hand>();
// distribute cards to player
for (int i = 0; i < deck.size(); i++){
playerList.get(i%4).addCard(deck.getCard(i));
}
// make player who hold Three of Diamonds as the starting player
for (int i = 0; i < playerList.size(); i++){
playerList.get(i).sortCardsInHand();
if (playerList.get(i).getCardsInHand().contains(new Card(0,2))){
this.currentPlayerIdx = i;
this.gui.setActivePlayer(i);
}
}
this.gui.promptActivePlayer();
this.gui.repaint();
/**
while (!endOfGame()){
this.gui.repaint();
this.gui.promptActivePlayer();
}*/
}
/**
* priavate method for check valid move
* 1: valid, 2: invalid input 3: pass
* @param playerIdx
* @param cardIdx
* @return int
*/
private int validMove(int playerIdx, int[] cardIdx){
// check pass condition
if (cardIdx == null){
if (this.pass < 3 & !(handsOnTable.isEmpty())){
return 3;
}
return 2;
}
// check whether the input of function is within range
if (cardIdx.length > 5){
return 2;
}
if (playerIdx < 0 || playerIdx > 3){
return 2;
}
// check whether the player input is exist in player hand
CardGamePlayer player = playerList.get(playerIdx);
for (int i = 0; i < cardIdx.length; i++){
if (cardIdx[i] >= player.getNumOfCards() || cardIdx[i] < 0){
return 2;
}
}
CardList PlayerHand = new CardList();
PlayerHand = player.play(cardIdx);
Hand hand = composeHand(player, PlayerHand);
// return 2 if no valid hand
if (hand == null){
return 2;
}
// Check for first hand condition
if (handsOnTable.isEmpty()){
if (hand.isValid() & hand.contains(new Card(0,2))){
return 1;
}
return 2;
}
// Check for hand beats or not
else if(pass < 3 & hand.beats(handsOnTable.get(handsOnTable.size()-1))){
return 1;
}
// If all other players pass
else if (this.pass == 3 & hand.isValid()){
return 1;
}
return 2;
}
/**
* Public method for making a valid move with index specifiied by the player
* @param playerIdx
* @param cardIdx
*/
public void makeMove(int playerIdx, int[] cardIdx){
getClient().sendMessage(new CardGameMessage(CardGameMessage.MOVE, -1, cardIdx));
}
/**
* Print message when the player does not make a valid move or pass
* @param playerIdx
* @param cardIdx
*/
public void checkMove(int playerIdx, int[] cardIdx){
int check = validMove(playerIdx, cardIdx);
// ask again for a valid move if it is not
if (check == 2){
gui.printMsg("Not a legal move!!!");
this.gui.promptActivePlayer();
gui.repaint();
}
// pass if the player pass
else if (check == 3){
gui.printMsg("{Pass}");
gui.printMsg("");
this.pass += 1;
this.currentPlayerIdx = (playerIdx + 1) % 4;
this.gui.setActivePlayer(this.currentPlayerIdx);
this.gui.promptActivePlayer();
gui.repaint();
}
// make the valid move if the player input correctly
else if (check == 1){
this.pass = 0;
CardGamePlayer player = playerList.get(playerIdx);
CardList PlayerHand = new CardList();
PlayerHand = player.play(cardIdx);
Hand hand = composeHand(player, PlayerHand);
for (int i = 0; i < PlayerHand.size(); i++){
playerList.get(playerIdx).getCardsInHand().removeCard(PlayerHand.getCard(i));
}
this.handsOnTable.add(hand);
gui.printMsg("{"+hand.getType()+"} "+ hand);
gui.printMsg("");
this.currentPlayerIdx = (playerIdx + 1) % 4;
this.gui.setActivePlayer(currentPlayerIdx);
gui.promptActivePlayer();
gui.repaint();
}
// Print message for ending the game
if (endOfGame()){
this.gui.printMsg("Game ends");
for (int i = 0; i < 4; i++){
if (playerList.get(i).getNumOfCards() != 0){
this.gui.printMsg("Player "+i+" has "+playerList.get(i).getNumOfCards()+" cards in hand.");
}
else{
this.gui.printMsg("Player "+i+" wins the game.");
}
}
this.gui.disable();
}
}
/**
* For checking the hand type is valid or not
* Returning the hand type or null if it is not valid
* @param player
* @param cards
* @return Hand
*/
Hand composeHand(CardGamePlayer player, CardList cards){
Single single = new Single(player, cards);
Pair pair = new Pair(player, cards);
Triple triple = new Triple(player, cards);
Straight straight = new Straight(player, cards);
Flush flush = new Flush(player, cards);
FullHouse fullhouse = new FullHouse(player, cards);
Quad quad = new Quad(player, cards);
StraightFlush straightFlush = new StraightFlush(player, cards);
if (straightFlush.isValid()){
return straightFlush;
}
else if (single.isValid()){
return single;
}
else if (pair.isValid()){
return pair;
}
else if (triple.isValid()){
return triple;
}
else if (straight.isValid()){
return straight;
}
else if (flush.isValid()){
return flush;
}
else if (fullhouse.isValid()){
return fullhouse;
}
else if (quad.isValid()){
return quad;
}
return null;
}
// Make connection with game server
public void connect(){
this.client = new BigTwoClient(this, this.gui);
}
//getter of client
public BigTwoClient getClient(){
return this.client;
}
/**
* main function for running the game
* @param args
*/
public static void main(String[] args){
BigTwo game = new BigTwo();
BigTwoDeck deck = new BigTwoDeck();
deck.shuffle();
game.start(deck);
}
}