-
Notifications
You must be signed in to change notification settings - Fork 9
/
Game.java
executable file
·105 lines (88 loc) · 2.28 KB
/
Game.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
public class Game
{
private Table t;
private Deck d;
public Game()
{
t = new Table();
d = new Deck();
// There are always 81 cards in a new Deck, so this can't fail
for(int i = 0; i < 12; i++)
t.add(d.getNext());
}
public Game(String filename)
{
t = new Table();
d = new Deck(filename);
// There might not be 12 cards
while(d.hasNext() && t.numCards() < 12)
t.add(d.getNext());
}
public int numSets()
{
return t.numSets();
}
public int numCards()
{
return t.numCards();
}
public boolean isGameOver()
{
if(d.hasNext())
return false;
// if we get here, there are no more cards in the deck
// the game continues until there are no sets on the table
return t.numSets() == 0;
}
public void playRound()
{
// If the game is over, they shouldn't have called this...
if(isGameOver())
return;
// If there are no sets, we have to add cards
if(t.numSets() == 0)
{
// There must be at least one card
t.add(d.getNext());
// If we have a custom deck, it might not have a multiple
// of three cards.
if(d.hasNext())
t.add(d.getNext());
if(d.hasNext())
t.add(d.getNext());
return;
}
// There is a set on the table...
for(int i = 0; i < t.numCards(); i++)
{
for(int j = i + 1; j < t.numCards(); j++)
{
for(int k = j + 1; k < t.numCards(); k++)
{
Card c1 = t.getCard(i);
Card c2 = t.getCard(j);
Card c3 = t.getCard(k);
if(c1.isSet(c2, c3))
{
t.removeSet(c1, c2, c3);
// If there are 12 or more cards on the table,
// we do not add cards this round.
if(t.numCards() < 12)
{
// there may or may not be cards left in the deck
if(d.hasNext())
t.add(d.getNext());
if(d.hasNext())
t.add(d.getNext());
if(d.hasNext())
t.add(d.getNext());
}
// regardless of whether we add cards, we are done
// when we find a set.
return;
}
}
}
}
}
}