forked from stjkr02/gitWorkflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.java
executable file
·92 lines (74 loc) · 2.08 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Collections;
public class Deck
{
private ArrayList<Card> theCards;
private int nextCardIndex;
public Deck()
{
theCards = 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++)
{
theCards.add(new Card(quantity, color, shading, shape));
}
}
}
}
Collections.shuffle(theCards);
nextCardIndex = 0;
}
public Deck(String filename)
{
theCards = 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());
theCards.add(new Card(quantity, color, shading, shape));
nextCardIndex = 0;
}
}
catch(Exception e)
{
throw new RuntimeException("Error while reading file: " + e.toString());
}
}
public boolean hasNext()
{
return nextCardIndex < theCards.size();
}
public Card getNext()
{
if(nextCardIndex > 80)
return null;
Card ret = theCards.get(nextCardIndex);
nextCardIndex++;
return ret;
}
}