-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.java
295 lines (250 loc) · 8.85 KB
/
Deck.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
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
/*A deck's main feature is the array of Card objects. Each card belongs
to a deck of 52 cards, which is represented using that array. It also
contains methods to manipulate that array*/
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Deck<T extends Comparable<? super T>>
implements Serializable, Iterable<T>
{
//used for version control when serializing
static final long serialVersionUID = 100239761 - 10;
public long id;
//declaring arrays representing the deck
public Card[] deckArray;
/*-----------------CONSTRUCTOR METHOD-------------*/
//default constructor creates a deck containing all possible cards
public Deck()
{
//keeping track of position in deckArray
int cardNum = 0;
deckArray = new Card[52];
//populating deckArray
//iterating through all possible cards and adding to current position in deckArray
for(Card.Suit suit : Card.Suit.values())
{
for(Card.Rank rank : Card.Rank.values())
{
deckArray[cardNum] = new Card(rank.name(), suit.name());
cardNum++;
}
}
//generates a random id for this deck, used when serializing
id = 1L + (long) (Math.random() * (100L - 1L));
}
/*---------------CLASS METHODS-------------*/
//returns number of cards in deck
public int size()
{
return deckArray.length;
}
//shuffles the deck into random order
public Card[] shuffle()
{
//converts deckArray to ArrayList so Collections.shuffle can be called on it
List<Card> temp = Arrays.asList(this.deckArray);
Collections.shuffle(temp);
//setting deckArray to shuffled List and returning it
Card[] newDeckArray = temp.toArray(new Card[0]);
return newDeckArray;
}
//deals a card from the deck and updates all attributes affected
public Card deal()
{
//retrieving the card at the top of the deck and storing it in dealtCard
int topCardIndex = getTopCard();
Card dealtCard = deckArray[topCardIndex];
//removing the dealt card from deckArray
deckArray[topCardIndex] = null;
return dealtCard;
}
//Demonstrates all functionality of Deck class
//Called from the projects main method
public void main()
{
//initialising Deck object and iterator objects
Deck deck = new Deck();
Iterator<Card> it = deck.iterator();
Iterator<Card> it2 = deck.oddEvenIterator();
//prints deck in order
deck.printDeck();
//shuffles the deck
deck.shuffle();
//the default iterator is a DeckIterator
//iterates through the deck in order they'll be dealt and prints to console
System.out.println("DeckIterator now iterating through deck:");
while(it.hasNext())
{
Card c = it.next();
System.out.println(c.toString());
}
//an OddEvenIterator iterates through the deck and prints to console
System.out.println("OddEvenIterator now iterating through deck:");
while(it2.hasNext())
{
Card c = it2.next();
System.out.println(c.toString());
}
//using getTopCard method to get number of cards left in deck
//incrementing getTopCard by 1 to get true size because it returns an array index
System.out.println("Number of cards in deck: " + deck.getTopCard());
//dealing 2 cards and printing them to console
System.out.println("dealt: " + deck.deal().toString());
System.out.println("dealt: " + deck.deal().toString());
System.out.println("Number of cards in deck: " + deck.getTopCard());
//serializing then deserializing the deck object - should print size of deck to console (getTopCard() + 1)
//in this example, loadFromByteCode should print 50 to the console
String file = deck.saveThisToByteCode();
Deck newDeck = new Deck();
newDeck = deck.loadFromByteCode(file);
}
//prints every card remaining in the deck to the console
public void printDeck()
{
for(int i = 0; i < getTopCard(); i++)
{
System.out.println(deckArray[i].toString());
}
}
//returns a newly-initialised Deck object that can be used to reset the deck
public final Deck newDeck()
{
return new Deck();
}
/*-------------ATTRIBUTE ACCESSOR METHODS---------*/
//returns the deck array
public Card[] getDeck()
{
return deckArray;
}
//returns the index of the top card from the deck
public int getTopCard()
{
for(int i = 0; i < deckArray.length; i++)
{
if(deckArray[i] == null)
{
//the top card is the one just before a null pointer
return i - 1;
}
}
//if none are null, the deck is full and 51 is the top card
return 51;
}
/*-------------ITERATOR METHODS----------*/
//default iterator method returns the standard DeckIterator
public Iterator<T> iterator()
{
return new DeckIterator();
}
//for special cases, oddEvenIterator method returns the OddEvenIterator class
public Iterator<T> oddEvenIterator()
{
return new OddEvenIterator();
}
/*---------------SERIALIZATION METHODS--------------*/
//saves the Deck object the method is called from to byte code using serialization and returns its unique filename
private String saveThisToByteCode()
{
String filename = id + "deck.ser";
try{
FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(this);
out.close();
fos.close();
System.out.println(filename + " has been serialized");
}
catch(Exception ex)
{
ex.printStackTrace();
}
return filename;
}
//loads in the Deck object given by the method argument from byte code into a Card object and returns it
private Deck loadFromByteCode(String filename)
{
try{
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fis);
Deck deck = (Deck)in.readObject();
in.close();
fis.close();
System.out.println(filename + " has been deserialized");
//evidence that the correct deck has been loaded in
System.out.println("Size of deserialized deck: " + deck.getTopCard());
return deck;
}
catch(Exception ex)
{
ex.printStackTrace();
}
//if the deck is not found it is reinitialised as a new deck
System.out.println("Error - deck not found. New deck created");
return new Deck();
}
/*-----------ITERATOR INNER CLASSES--------------*/
/*-----------DeckIterator inner class------------*/
private class DeckIterator<T> implements Iterator<T>
{
//initialising attribute to keep track of position in array
int pos = 0;
//overriding hasNext to check if reached top card in deck
@Override
public boolean hasNext()
{
if(pos < getTopCard())
{
return true;
}
//reset pos variable so can be used more than once
pos = 0;
return false;
}
@Override
public T next()
{
//return card at current position in array and increments position
return (T)deckArray[pos++];
}
}
/*-------------OddEvenIterator inner class-------------*/
private class OddEvenIterator<T> implements Iterator<T>
{
//keeps track of position in array
int pos = 1;
//determines whether currently checking odd or even positions
boolean even = false;
//overriding hasNext to check if reached end of deck
@Override
public boolean hasNext()
{
//when not reached end of deck
if(pos < 52)
{
return true;
}
//when at end of deck and still checking odd, move onto checking even
if(!even)
{
//starting pos at 0 will check even numbers
pos = 0;
even = true;
return true;
}
//when at end of deck and just checked even
pos = 0;
return false;
}
@Override
public T next()
{
//increase pos for next call
pos = pos + 2;
//return card at previous pos
return (T)deckArray[pos - 2];
}
}
}