-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
254 lines (227 loc) · 4.09 KB
/
Copy pathPlayer.java
File metadata and controls
254 lines (227 loc) · 4.09 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
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JLabel;
/**
* This class will maintain a Player and his Cards.
*
* @author Bryce J. Fisher
*
*/
public class Player extends JLabel {
private static final long serialVersionUID = 1L;
ArrayList<Card> cards = new ArrayList<Card>();
private String name;
private int money;
private int points;
private final boolean isComputer;
private final int ID;
public boolean isComputer() {
return isComputer;
}
/**
* Create a new computer player
*
* @param name
*/
public Player(final String name, final int id) {
super();
ID = id;
this.name = name;
isComputer = true;
updateText();
}
/**
* Create a new player
*
* @param name
*/
public Player(final String name, final int id, final boolean isComputer) {
super();
ID = id;
this.name = name;
this.isComputer = isComputer;
updateText();
}
public void setName(final String n) {
name = n;
}
/**
* Get's player's name
*
* @return
*/
public String getName() {
return name;
}
public int getID() {
return ID;
}
/**
* Get String about Player
*/
public String toString() {
return getName() + ":($" + getMoney() + ", " + getPoints() + " points)";
}
/**
* Give a card to this player
*
* @param card
*/
public void giveCard(final Card card) {
cards.add(0, card);
card.setOwner(this);
updateText();
}
/**
* Give this player a pile of cards
*
* @param pile
*/
public void giveCardPile(final Pile pile) {
while (pile.hasCards()) {
giveCard(pile.getBottomCard());
}
updateText();
}
/**
* Gets this players cards without removing any
*
* @return
*/
public Card[] viewCards() {
return cards.toArray(new Card[cards.size()]);
}
/**
* View the card on the bottom of this player's cards without removing it.
*
* @return
*/
public Card viewBottomCard() {
if (cards.isEmpty()) {
return null;
}
return cards.get(0);
}
/**
* View the card on the top of this player's cards without removing it.
*
* @return
*/
public Card viewTopCard() {
if (cards.isEmpty()) {
return null;
}
return cards.get(cards.size() - 1);
}
/**
* This decrements this player's cards one card.
*
* @return card from the bottom of this player's cards
*/
public Card getBottomCard() {
if (cards.isEmpty()) {
return null;
}
updateText(cards.size() - 1);
return cards.remove(0);
}
/**
* This decrements this player's cards one card.
*
* @return card from the top of this player's cards
*/
public Card getTopCard() {
if (cards.isEmpty()) {
return null;
}
updateText(cards.size() - 1);
return cards.remove(cards.size() - 1);
}
/**
*
* @return true if player has cards
*/
public boolean hasCards() {
return !cards.isEmpty();
}
/**
*
* @return number of cards
*/
public int getCardCount() {
return cards.size();
}
/**
* Get's user's money
*
* @return
*/
public int getMoney() {
return money;
}
/**
* Add money to user's money
*
* @param addition
*/
public void addMoney(final int addition) {
money += addition;
}
/**
* Subtract money, returns a negative nonzero number if the user is in debt.
*
* @param subtraction
* @return
*/
public int lostMoney(final int subtraction) {
money -= subtraction;
if (money < 0) {
return money;
}
return 0;
}
/**
* Get users points
*
* @return
*/
public int getPoints() {
return points;
}
/**
* Set points indiscriminately
*
* @param points
*/
public void setPoints(final int points) {
this.points = points;
}
/**
* Add points to player's points
*
* @param points
*/
public void addPoints(final int pointsToAdd) {
points += pointsToAdd;
}
/**
* Shuffles this player's cards
*/
public void shuffleCards() {
Collections.shuffle(cards);
}
/**
* Update this Player's JLabel text
*/
public void updateText() {
updateText(cards.size());
}
/**
* Update this Player's JLabel text with the specified number of cards
*
* @param size
*/
public void updateText(final int size) {
setText(size + " Cards");
}
}