1
- public class Table {
1
+ public class Table {
2
2
private TableNode head ;
3
- private int length ;
4
3
5
- public Table (){
4
+ public Table () {
6
5
head = null ;
7
- length = 0 ;
8
6
}
9
7
10
- public void add (Card c1 ) {
11
- TableNode tempNode = new TableNode (c1 );
12
- tempNode .setNext (head );
13
- head = tempNode ;
8
+ public void add (Card card ) {
9
+ TableNode Node1 = new TableNode (card );
10
+ Node1 .setNext (head );
11
+ head = Node1 ;
14
12
}
15
13
16
- public void removeSet (Card c1 , Card c2 , Card c3 ){
17
- TableNode curr = head ;
18
- if (c1 .isSet (c2 , c3 ) == false ){
14
+ public void removeSet (Card c1 , Card c2 , Card c3 ) {
15
+ if (c1 .isSet (c2 , c3 ) == false ) {
19
16
return ;
20
17
}
21
- else if (c1 .isSet (c2 , c3 )){
22
-
23
- c1 = null ;
24
- c2 = null ;
25
- c2 = null ;
18
+
19
+ else {
20
+ Card remove1 = null ;
21
+ Card remove2 = null ;
22
+ Card remove3 = null ;
23
+
24
+ TableNode temp = head ;
25
+
26
+ while (temp != null ) {
27
+ if (c1 .equals (temp .getCard ()))
28
+ remove1 = c1 ;
29
+
30
+ if (c2 .equals (temp .getCard ()))
31
+ remove2 = c2 ;
32
+
33
+ if (c3 .equals (temp .getCard ()))
34
+ remove3 = c3 ;
35
+
36
+ temp = temp .getNext ();
37
+ }
38
+
39
+ if (remove1 != null && remove2 != null && remove3 != null ) {
40
+ TableNode curr = head ;
41
+ TableNode prev = null ;
42
+
43
+ while (curr != null ) {
44
+
45
+ if (c1 .equals (curr .getCard ()) || c2 .equals (curr .getCard ()) || c3 .equals (curr .getCard ())) {
46
+ if (curr == head ) {
47
+ curr = curr .getNext ();
48
+ head = head .getNext ();
49
+ }
50
+ else {
51
+ prev .setNext (curr .getNext ());
52
+ curr = curr .getNext ();
53
+ }
54
+ }
55
+ else {
56
+ prev = curr ;
57
+ curr = curr .getNext ();
58
+ }
59
+ }
60
+ }
26
61
}
27
- else {
28
- return ;
62
+ }
63
+
64
+ public int numCards () {
65
+ TableNode currNode = head ;
66
+ int count = 0 ;
67
+ while (currNode != null ) {
68
+ count += 1 ;
69
+ currNode = currNode .getNext ();
29
70
}
71
+ return count ;
30
72
}
31
73
32
- public int numCards (){
33
- TableNode curr = head ;
34
- int cardCount = 0 ;
74
+ public Card getCard (int index ) {
75
+ if (index > numCards () - 1 ) {
76
+ return null ;
77
+ }
35
78
36
- if (curr == null ){
37
- return 0 ;
79
+ TableNode temp = head ;
80
+ if (temp == null ) {
81
+ return null ;
38
82
}
39
- else {
40
83
41
- while (curr != null ){
42
- curr = curr .getNext ();
43
- cardCount += 1 ;
84
+ else {
85
+ for (int i = 0 ; i < index ; i ++) {
86
+ temp = temp .getNext ();
87
+ }
88
+ return temp .getCard ();
44
89
}
45
- return cardCount ;
46
90
}
47
- }
48
91
49
- public Card getCard (int index ){
50
- TableNode curr = head ;
51
-
52
- if (curr == null ){
53
- return null ;
54
- }
55
- else {
56
- for (int i = 0 ; i < index ; i ++){
57
- curr = curr .getNext ();
92
+ public int numSets () {
93
+ int count = 0 ;
94
+ if (numCards () < 3 ) {
95
+ return 0 ;
58
96
}
59
- }
97
+ else {
98
+ TableNode n1 = head ;
99
+ while (n1 != null && n1 .getNext ().getNext () != null ) {
100
+ TableNode n2 = n1 .getNext ();
101
+
102
+ while (n2 != null && n2 .getNext () != null ) {
103
+ TableNode n3 = n2 .getNext ();
104
+
105
+ while (n3 != null ) {
106
+ if (n3 .getCard ().isSet (n2 .getCard (), n1 .getCard ()) == true ) {
107
+ count += 1 ;
108
+ }
109
+ n3 = n3 .getNext ();
110
+ }
111
+ n2 = n2 .getNext ();
112
+ }
113
+ n1 = n1 .getNext ();
60
114
61
- return curr .getCard ();
115
+ }
116
+ return count ;
117
+ }
62
118
}
63
-
64
-
65
- public int numSets (){
66
- TableNode curr = head ;
67
- TableNode curr2 = curr .getNext ();
68
- TableNode curr3 = curr2 .getNext ();
69
- int setCount = 0 ;
70
- if (curr .getCard ().isSet (curr2 .getCard (), curr3 .getCard ()) == true ){
71
- setCount +=1 ;
72
- }
73
- return setCount ;
74
- }
75
119
}
76
120
0 commit comments