-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardList.java
188 lines (173 loc) · 4.7 KB
/
CardList.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
import java.io.Serializable;
import java.util.ArrayList;
/**
* This class is used to represent a list of cards.
*
* @author Kenneth Wong
*/
public class CardList implements Serializable {
private static final long serialVersionUID = -3711761437629470849L;
private ArrayList<Card> cards = new ArrayList<Card>();
/**
* Appends the specified card to the end of this list.
*
* @param card the card to be appended to this list
*/
public void addCard(Card card) {
if (card != null) {
cards.add(card);
}
}
/**
* Returns the card at the specified position in this list.
*
* @param i the index of the card to returned
* @return the card at the specified position in this list, or null if the index
* is invalid
*/
public Card getCard(int i) {
if (i >= 0 && i < cards.size()) {
return cards.get(i);
} else {
return null;
}
}
/**
* Removes the card at the specified position in this list. Shifts any
* subsequent cards to the left (subtracts one from their indices).
*
* @param i the index of the card to be removed
* @return the card that is removed from the list, or null if the index is
* invalid
*/
public Card removeCard(int i) {
if (i >= 0 && i < cards.size()) {
return cards.remove(i);
} else {
return null;
}
}
/**
* Removes the first occurrence of the specified card from this list, if it is
* present. If the list does not contain the card, it remains unchanged. Returns
* true if this list contained the specified card (or equivalently, if this list
* changed as a result of the call).
*
* @param card the card to be removed from this list, if presents
* @return true if this list contained the specified card; otherwise false
*/
public boolean removeCard(Card card) {
return cards.remove(card);
}
/**
* Removes all cards from this list.
*/
public void removeAllCards() {
cards = new ArrayList<Card>();
}
/**
* Replaces the card at the specified position in this list with the specified
* card.
*
* @param i the index of the card to be replaced
* @param card the card to be stored at the specified position
* @return the card previously stored at the specified position, or null if the
* index is invalid
*/
public Card setCard(int i, Card card) {
if (i >= 0 && i < cards.size()) {
return cards.set(i, card);
} else {
return null;
}
}
/**
* Returns true if this list contains the specified card.
*
* @param card the card whose presence in this list is to be tested
* @return true if this list contains the specified card; otherwise false
*/
public boolean contains(Card card) {
return cards.contains(card);
}
/**
* Returns true if this list contains no cards.
*
* @return true if this list contains no cards; otherwise false
*/
public boolean isEmpty() {
return cards.isEmpty();
}
/**
* Sorts this list according to the order of the cards.
*/
public void sort() {
cards.sort(null);
}
/**
* Returns the number of cards in this list.
*
* @return the number of cards in this list
*/
public int size() {
return cards.size();
}
/**
* Prints the cards in this list to the UI. Equivalent to calling
* print(true, false);
*/
public void print() {
print(true, false);
}
/**
* Prints the cards in this list to the UI.
*
* @param printFront a boolean value specifying whether to print the face (true)
* or the black (false) of the cards
* @param printIndex a boolean value specifying whether to print the index in
* front of each card
*/
public void print(boolean printFront, boolean printIndex) {
if (cards.size() > 0) {
for (int i = 0; i < cards.size(); i++) {
String string = "";
if (printIndex) {
string = i + " ";
}
if (printFront) {
string = string + "[" + cards.get(i) + "]";
} else {
string = string + "[ ]";
}
if (i % 13 != 0) {
string = " " + string;
}
System.out.print(string);
if (i % 13 == 12 || i == cards.size() - 1) {
System.out.println("");
}
}
} else {
System.out.println("[Empty]");
}
}
/**
* Returns a string representation of the cards in the list
*
* @return a string representation of the cards in the list
*/
public String toString() {
String string = "";
if (cards.size() > 0) {
for (int i = 0; i < cards.size(); i++) {
string = string + "[" + cards.get(i) + "]";
if (i != cards.size() - 1) {
string = string + " ";
}
}
} else {
string = "[Empty]";
}
return string;
}
}