-
Notifications
You must be signed in to change notification settings - Fork 9
/
Table.java
140 lines (105 loc) · 2.26 KB
/
Table.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
public class Table
{
private TableNode head;
public Table()
{
head = null;
}
public void add(Card card)
{
TableNode newNode = new TableNode(card);
newNode.setNext(head);
head = newNode;
}
public void removeSet(Card c1, Card c2, Card c3)
{
if(!contains(c1) || !contains(c2) || !contains(c3))
return;
if(!c1.isSet(c2, c3))
return;
TableNode prev = findPrev(c1);
remove(prev);
prev = findPrev(c2);
remove(prev);
prev = findPrev(c3);
remove(prev);
}
private boolean contains(Card c)
{
TableNode curr = head;
while(curr != null)
{
if(curr.getCard().equals(c))
return true;
curr = curr.getNext();
}
return false;
}
private TableNode findPrev(Card c)
{
TableNode curr = head;
TableNode prev = null;
while(!(curr.getCard().equals(c)))
{
prev = curr;
curr = curr.getNext();
}
return prev;
}
private void remove(TableNode prev)
{
// check for a head remove
if(prev == null)
{
head = head.getNext();
return;
}
TableNode toRemove = prev.getNext();
prev.setNext(toRemove.getNext());
}
public int numCards()
{
TableNode temp = head;
int count = 0;
while(temp != null)
{
count++;
temp = temp.getNext();
}
return count;
}
public Card getCard(int index)
{
if(index >= numCards())
return null;
TableNode temp = head;
for(int i = 0; i < index; i++)
temp = temp.getNext();
return temp.getCard();
}
public int numSets()
{
TableNode n1 = head;
int count = 0;
while(n1 != null && n1.getNext() != null)
{
TableNode n2 = n1.getNext();
while(n2 != null && n2.getNext() != null)
{
TableNode n3 = n2.getNext();
while(n3 != null)
{
Card c1 = n1.getCard();
Card c2 = n2.getCard();
Card c3 = n3.getCard();
if(c1.isSet(c2, c3))
count++;
n3 = n3.getNext();
}
n2 = n2.getNext();
}
n1 = n1.getNext();
}
return count;
}
}