-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
360 lines (321 loc) · 9.93 KB
/
Card.java
File metadata and controls
360 lines (321 loc) · 9.93 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import java.util.*;
// Abstract Card class
abstract class Card {
boolean isFaceUp;
byte position;
String id;
// Constructor
Card(byte position) {
this.position = position;
this.isFaceUp = false;
this.id = id;
}
Card(){
this.position = position;
this.isFaceUp = false;
this.id = id;
}
// Abstract method for flipping the card
public abstract void initializeGame();
public abstract void flip(byte position);
public boolean IsFacedUp(){
return isFaceUp;
}
public byte getPosition() {
return position;
}
public String getId() {
return toString();
}
}
// Subclass: AnimalCard
class AnimalCard extends Card {
public enum AnimalType {
MOUSE, PIG, CHICK, COW, LION, SHEEP, CAT, DOG, DUCK, FROG
}
private AnimalType type;
//This is the list of cards that are in the game when we inicialize, they are stored here.
private List<AnimalCard> animalCards;
// Constructor
AnimalCard(byte position) {
super(position);
this.id = id;
this.animalCards = animalCards;
}
// Constructor 2
//This second constructor is created because to call the methods in other classes i needed to instanciate a card object, where it wasnt in any position
AnimalCard(){
super();
this.id = id;
this.animalCards = animalCards;
}
@Override
public String toString() {
return type.name();
}
public AnimalType getType() {
return type;
}
public void setAnimalCards(List<AnimalCard> animalCards){
this.animalCards = animalCards;
}
public List<AnimalCard> getAnimalCards(){
return animalCards;
}
// Method overloading
public AnimalCard findCardByPosition(byte position) {
for (AnimalCard card : animalCards) {
if (card.getPosition() == position) {
return card;
}
}
return null; // If no matching card is found
}
// this creats the list of cards for the game, it is shuffled to make sure its all in random positions, here we use the first constructor with the positions and in the end set the list of cards to the class variable
@Override
public void initializeGame() {
List<AnimalType> availableTypes = Arrays.asList(AnimalType.values());
Collections.shuffle(availableTypes);
List<Byte> positions = new ArrayList<>();
for (byte i = 0; i < 20; i++) {
positions.add(i);
}
Collections.shuffle(positions);
List<AnimalCard> animalCards = new ArrayList<>();
for (int i = 0; i < 10; i++) {
byte position = positions.get(i);
AnimalCard card1 = new AnimalCard(position);
card1.type = availableTypes.get(i);
animalCards.add(card1);
// Create the matching card
AnimalCard card2 = new AnimalCard(positions.get(i + 10));
card2.type = availableTypes.get(i);
animalCards.add(card2);
}
setAnimalCards(animalCards);
}
@Override
public void flip(byte position) {
if(isFaceUp == false) {
isFaceUp = !isFaceUp;
// animalflipview();//calls the view function for the specific motion for flipping an animal card // *** EXPLAINED IN ReadMe ***
} else{
isFaceUp = false;
}
}
}
// Subclass: ShapeCard
class ShapeCard extends Card {
public enum ShapeType {
CIRCLE, SQUARE, RECTANGLE, DIAMOND, RHOMBUS, TRIANGLE, PENTAGON, OVAL, STAR, CROSS
}
private ShapeType type;
private List<ShapeCard> shapeCards;
// Constructor
ShapeCard(byte position) {
super(position);
this.id = id;
this.shapeCards = shapeCards;
}
// Constructor 2
ShapeCard(){
super();
this.id = id;
this.shapeCards = shapeCards;
}
@Override
public String toString() {
return type.name();
}
public ShapeType getType() {
return type;
}
public void setShapeCards(List<ShapeCard> shapeCards){
this.shapeCards = shapeCards;
}
public List<ShapeCard> getShapeCards(){
return shapeCards;
}
// Method overloading
public ShapeCard findCardByPosition(byte position) {
for (ShapeCard card : shapeCards) {
if (card.getPosition() == position) {
return card;
}
}
return null; // If no matching card is found
}
@Override
public void initializeGame() {
List<ShapeType> availableTypes = Arrays.asList(ShapeType.values());
Collections.shuffle(availableTypes);
List<Byte> positions = new ArrayList<>();
for (byte i = 0; i < 20; i++) {
positions.add(i);
}
Collections.shuffle(positions);
List<ShapeCard> shapeCards = new ArrayList<>();
for (int i = 0; i < 10; i++) {
byte position = positions.get(i);
ShapeCard card1 = new ShapeCard(position);
card1.type = availableTypes.get(i);
shapeCards.add(card1);
// Create the matching card
ShapeCard card2 = new ShapeCard(positions.get(i + 10));
card2.type = availableTypes.get(i);
shapeCards.add(card2);
}
setShapeCards(shapeCards);
}
@Override
public void flip(byte position) {
if(isFaceUp == false){
isFaceUp = !isFaceUp;
//shapeflipview();//calls the view function for the specific motion for flipping an animal card
} else{
isFaceUp = false;
}
}
}
class ColorCard extends Card {
public enum ColorType {
RED, YELLOW, GREEN, BLUE, ORANGE, PURPLE, PINK, GREY, DARKBLUE, DARKGREEN
}
private ColorType type;
private List<ColorCard> colorCards;
// Constructor
ColorCard(byte position) {
super(position);
this.id = id;
this.colorCards = colorCards;
}
// Constructor 2
ColorCard(){
super();
this.id = id;
this.colorCards = colorCards;
}
@Override
public String toString() {
return type.name();
}
public ColorType getType() {
return type;
}
public void setColorCards(List<ColorCard> colorCards){
this.colorCards = colorCards;
}
public List<ColorCard> getColorCards(){
return colorCards;
}
// Method overloading
public ColorCard findCardByPosition(byte position) {
for (ColorCard card : colorCards) {
if (card.getPosition() == position) {
return card;
}
}
return null; // If no matching card is found
}
@Override
public void initializeGame() {
List<ColorType> availableTypes = Arrays.asList(ColorType.values());
Collections.shuffle(availableTypes);
List<Byte> positions = new ArrayList<>();
for (byte i = 0; i < 20; i++) {
positions.add(i);
}
Collections.shuffle(positions);
List<ColorCard> colorCards = new ArrayList<>();
for (int i = 0; i < 10; i++) {
byte position = positions.get(i);
ColorCard card1 = new ColorCard(position);
card1.type = availableTypes.get(i);
colorCards.add(card1);
// Create the matching card
ColorCard card2 = new ColorCard(positions.get(i + 10));
card2.type = availableTypes.get(i);
colorCards.add(card2);
}
setColorCards(colorCards);;
}
@Override
public void flip(byte position) {
if(isFaceUp == false){
isFaceUp = !isFaceUp;
//colorflipview();//calls the view function for the specific motion for flipping an animal card
} else{
isFaceUp = false;
}
}
}
class NumberCard extends Card {
public enum NumberType {
ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE
}
private NumberType type;
private List<NumberCard> numberCards;
// Constructor
NumberCard(byte position) {
super(position);
this.id = id;
}
// Constructor 2
NumberCard(){
super();
this.id = id;
this.numberCards = numberCards;
}
@Override
public String toString() {
return type.name();
}
public NumberType getType() {
return type;
}
public void setNumberCards(List<NumberCard> numberCards){
this.numberCards = numberCards;
}
public List<NumberCard> getNumberCards(){
return numberCards;
}
// Method overloading
public NumberCard findCardByPosition(byte position) {
for (NumberCard card : numberCards) {
if (card.getPosition() == position) {
return card;
}
}
return null; // If no matching card is found
}
public void initializeGame() {
List<NumberType> availableTypes = Arrays.asList(NumberType.values());
Collections.shuffle(availableTypes);
List<Byte> positions = new ArrayList<>();
for (byte i = 0; i < 20; i++) {
positions.add(i);
}
Collections.shuffle(positions);
List<NumberCard> numberCards = new ArrayList<>();
for (int i = 0; i < 10; i++) {
byte position = positions.get(i);
NumberCard card1 = new NumberCard(position);
card1.type = availableTypes.get(i);
numberCards.add(card1);
// Create the matching card
NumberCard card2 = new NumberCard(positions.get(i + 10));
card2.type = availableTypes.get(i);
numberCards.add(card2);
}
setNumberCards(numberCards);
}
@Override
public void flip(byte position) {
if(isFaceUp == false){
isFaceUp = !isFaceUp;
//numberflipview();//calls the view function for the specific motion for flipping an animal card
} else{
isFaceUp = false;
}
}
}