-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPile.java
More file actions
246 lines (199 loc) · 4.23 KB
/
Copy pathPile.java
File metadata and controls
246 lines (199 loc) · 4.23 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Alyza Diaz Rodriguez
* Chelsea Dillon
*
*/
public class Pile {
private Card topCard;
private Card bottomCard;
private Node first;
private Node last;
private int nCards;
public Pile() {
topCard = null;
bottomCard = null;
first = null;
last = null;
nCards = 0;
}
public void add(Card c) {
Node newNode = new Node(c);
if(first==null) {
first = newNode;
last = first;
topCard = newNode.getCard();
bottomCard = newNode.getCard();
}else {
first.setPrevious(newNode);
newNode.setNext(first);
first = newNode;
topCard = newNode.getCard();
}
nCards++;
}
public void addToBottom(Card c) {
Node newNode = new Node(c);
Node temp = last;
last.setNext(newNode);
last = newNode;
last.setPrevious(temp);
bottomCard = c;
nCards++;
}
public void remove(Card c) {
if(isEmpty()) {
throw new NullPointerException();
}else if(searching(c)==-1) {
throw new IndexOutOfBoundsException();
}
int place = searching(c);
if(place==0) {
if(nCards==1) {
first = null;
last = null;
topCard = null;
bottomCard = null;
nCards = 0;
}else {
first = first.getNext();
first.setPrevious(null);
topCard = first.getCard();
nCards--;
}
}else if(place==(nCards-1)) {
last = last.getPrevious();
last.setNext(null);
bottomCard = last.getCard();
nCards--;
}else {
int i=0;
Node nextNode = first;
for(Node currNode=first;nextNode!=null;currNode=nextNode) {
nextNode = currNode.getNext();
if(i==place) {
currNode.getPrevious().setNext(nextNode);
currNode.getNext().setPrevious(currNode.getPrevious());
nCards--;
break;
}
i++;
}
}
}
public int searching(Card c) {
Card[] cards = toArray();
int index = -1;
for(int i=0;i<cards.length;i++) {
if(cards[i].equals(c)) {
index = i;
}
}
return index;
}
public void shuffle() {
Card[] cards = toArray();
for(int i=0;i<cards.length;i++) {
int rng = (int)(Math.random()*cards.length);
Card temp = cards[i];
cards[i] = cards[rng];
cards[rng] = temp;
}
clear();
for(int i=0;i<cards.length;i++) {
add(cards[i]);
}
}
public void sort() {
/*
* sort by rank and suit
* lowest suit: clubs
* highest suit: spades
*
* lowest rank: one
* highest rank: ace
*
*/
int lowest = 1, count = 0;
Suit[] suits = {Suit.CLUBS, Suit.HEARTS, Suit.DIAMONDS, Suit.SPADES};
Card[] cards = toArray(), array = new Card[nCards];
for(int a=0;a<suits.length;a++) {
for(int b=lowest;b<=14;b++) {
for(int c=0;c<cards.length;c++) {
if(cards[c].getSuit().equals(suits[a]) && cards[c].rankOrder()==b) {
array[count] = cards[c];
count++;
}
}
}
}
clear();
for(int i=array.length-1;i>=0;i--) {
add(array[i]);
}
}
public Card getTop() {
return topCard;
}
public Card getBottom() {
return bottomCard;
}
public boolean isEmpty() {
return nCards==0;
}
public void clear() {
if(isEmpty()) {
throw new NullPointerException();
}
while(!isEmpty()) {
remove(topCard);
}
}
public int getNumCards() {
return nCards;
}
public Card[] toArray() {
Card[] cards = new Card[getNumCards()];
int i=0;
Node n = first;
while(n!=null && i<getNumCards()) {
Card c = n.getCard();
n = n.getNext();
cards[i] = c;
i++;
}
return cards;
}
private class Node{
private Card c;
private Node next;
private Node previous;
@SuppressWarnings("unused")
public Node() {
c = null;
next = null;
previous = null;
}
public Node(Card c) {
this.c = c;
}
@SuppressWarnings("unused")
public void setCard(Card c) {
this.c = c;
}
public void setNext(Node next) {
this.next = next;
}
public void setPrevious(Node previous) {
this.previous = previous;
}
public Card getCard() {
return c;
}
public Node getNext() {
return next;
}
public Node getPrevious() {
return previous;
}
}
}