-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPile.java
More file actions
265 lines (238 loc) · 4.76 KB
/
Copy pathPile.java
File metadata and controls
265 lines (238 loc) · 4.76 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
/**
* This class will maintain a pile of cards. Should be used for Pile of Cards in the middle of table, but can be used to
* maintain piles of cards owner by each player.
*
* @author Bryce J. Fisher, Carlos Arredondo
*
*/
public class Pile {
private final ArrayList<Card> cards = new ArrayList<Card>();
private Player pileOwner = null;
int x;
int i2;
/**
* create an empty pile with no owner
*/
public Pile() {
}
/**
* Create an un-owned pile with no owner
*
* @param cards
*/
public Pile(final Card[] cards) {
this.cards.addAll(Arrays.asList(cards));
}
public Pile(final ArrayList<Card> cards) {
this.cards.addAll(cards);
}
/**
* Create a pile with a specific owner
*
* @param cards
* @param owner
*/
public Pile(final Card[] cards, final Player owner) {
this.cards.addAll(Arrays.asList(cards));
setPileOwner(owner);
}
/**
* Get a copy of the cards in this pile. Does not empty the pile
*
* @return
*/
public Card[] getCards() {
return cards.toArray(new Card[cards.size()]);
}
public int size() {
return cards.size();
}
/**
* View the card on the bottom of the pile without removing it.
*
* @return
*/
public Card viewBottomCard() {
if (cards.isEmpty()) {
return null;
}
return cards.get(0);
}
/**
* View the card on the top of the pile without removing it.
*
* @return
*/
public Card viewTopCard() {
if (cards.isEmpty()) {
return null;
}
return cards.get(cards.size() - 1);
}
/**
* View the card n times before the top card in the pile without removing it.
*
* @return
*/
public Card viewCardAtNFromTop(final int n) {
if (cards.isEmpty()) {
return null;
}
return cards.get(cards.size() - n);
}
/**
* This decrements the pile one card.
*
* @return card from the bottom of the pile
*/
public Card getBottomCard() {
if (cards.isEmpty()) {
return null;
}
return cards.remove(0);
}
/**
* This decrements the pile one card.
*
* @return card from the top of the pile
*/
public Card getTopCard() {
if (cards.isEmpty()) {
return null;
}
return cards.remove(cards.size() - 1);
}
/**
*
* @return true if there are cards in the pile.
*/
public boolean hasCards() {
return !cards.isEmpty();
}
/**
*
* @return true if there are no cards in the pile.
*/
public boolean isEmpty() {
return cards.isEmpty();
}
/**
* Add a card to the top of the pile
*
* @param card
*/
public void addCardToPile(final Card card) {
cards.add(card);
card.setOwner(getPileOwner());
}
/**
* Give all the cards in this pile to a player
*
* @param player
*/
public void givePileToPlayer(final Player player) {
player.giveCardPile(this);
}
/**
* Deal the cards from this pile to players.
*
* @param players
*/
public void dealPileToPlayers(final Player... players) {
shuffleCards();
while (hasCards()) {
for (final Player p : players) {
if (hasCards()) {
p.giveCard(getTopCard());
}
}
}
}
/**
* Shuffle the cards in this pile.
*/
public void shuffleCards() {
Collections.shuffle(cards);
}
/**
* @return the pileOwner
*/
public Player getPileOwner() {
return pileOwner;
}
/**
* @param pileOwner
* the pileOwner to set
*/
public void setPileOwner(final Player pileOwner) {
this.pileOwner = pileOwner;
}
/**
* Check the double rule. Top 2 cards are the same number.
*
* @return
*/
public boolean doble() {
if (cards.size() < 2) {
return false;
}
if (viewTopCard().getValue() == viewCardAtNFromTop(2).getValue()) {
return true;
}
return false;
}
/**
* Check the sandwich rule. Top and bottom cards of the top 3 cards must be the same number.
*
* @return
*/
public boolean sandwich() {
if (cards.size() < 3) {
return false;
}
if (viewTopCard().getValue() == viewCardAtNFromTop(3).getValue()) {
return true;
}
return false;
}
/**
* Check four rule. Top four cards must be in consecutive ascending order.
*
* @return
*/
public boolean four() {
if (cards.size() < 4) {
return false;
}
for (int i = 4; i > 0; i--) {
for (int j = 3; j > 0; j--) {
if (viewCardAtNFromTop(i).getValue() - 1 == viewCardAtNFromTop(
j).getValue()) {
return true;
}
}
}
return false;
}
/**
* Check Marriage rule. Top card must be King
*
* @return
*/
public boolean marriage() {
if (cards.size() < 2) {
return false;
}
if (viewTopCard().getValue() == Card.VALUE_KING
&& viewCardAtNFromTop(2).getValue() == Card.VALUE_QUEEN) {
return true;
} else if (viewTopCard().getValue() == Card.VALUE_QUEEN
&& viewCardAtNFromTop(2).getValue() == Card.VALUE_KING) {
return true;
}
return false;
}
}