-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathDeck.java
84 lines (71 loc) · 2.41 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Collections;
public class Deck {
// Instance variables must be cards and nextCardIndex for the
// provided constructor to work
ArrayList<Card> cards;
int nextCardIndex = 0;
public Deck(String filename) {
cards = new ArrayList<Card>(81);
try {
String line;
BufferedReader infile = new BufferedReader(new FileReader(filename));
int position = 0;
while((line = infile.readLine()) != null) {
// Blank lines might contain white space, so trim it off
line = line.trim();
// ignore blank lines
if(line.length() == 0)
continue;
// ignore comments
if(line.startsWith("#"))
continue;
// a valid line contains 4 integers
StringTokenizer tokenizer = new StringTokenizer(line);
int quantity = Integer.parseInt(tokenizer.nextToken());
int color = Integer.parseInt(tokenizer.nextToken());
int shading = Integer.parseInt(tokenizer.nextToken());
int shape = Integer.parseInt(tokenizer.nextToken());
cards.add(new Card(quantity, color, shading, shape));
nextCardIndex = 0;
}
}
catch(Exception e) {
throw new RuntimeException("Error while reading file: " + e.toString());
}
}
public Deck() {
// Creates a deck with all standard 81 set cards, shuffled.
// Usage (in test mode, and in the later code):
// Deck d = new Deck()
cards = new ArrayList<Card>(81);
for (int quantity = 1; quantity < 4; quantity++) {
for (int color = 1; color < 4; color++) {
for (int shading = 1; shading < 4; shading++) {
for (int shape = 1; shape < 4; shape++) {
cards.add(new Card(quantity, color, shading, shape));
}
}
}
}
Collections.shuffle(cards);
}
public boolean hasNext() {
if (nextCardIndex <= cards.size()-1)
return true;
return false;
}
public Card getNext() {
// Returns the next card in the deck if there is one,
// and null, otherwise
// You can call hasNext within this method to see if you
// should return a card or return null
if (hasNext() == false)
return null;
nextCardIndex++;
return cards.get(nextCardIndex-1);
}
}