- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPile.java
More file actions
Latest commit
227 lines (210 loc) · 6.94 KB
/
Copy pathPile.java
File metadata and controls
227 lines (210 loc) · 6.94 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
/*
* Chapter 13 Section 9-10
*
* Basically just working with ArrayList's and using the Card & Deck classes
* for their functions to make a deck of cards that can be removed and added
* at will.
*/
importjava.util.ArrayList;
publicclassPile
{
//Our implementation of Deck uses a Card array, and array
//length can't be changed.???
//The Pile class represents a pile of cards, and it uses an
//ArrayList to store Card objects
privateArrayList<Card> cards;
publicPile()
{
//the constructor initializes this.cards with an empty ArrayList
this.cards = newArrayList<Card>();
}
//These 3 methods are "wrapper methods" because they just invoke
//methods on the instance variables
publicCarddraw()
{
//??What is this here???????
returnthis.cards.remove(0);
}
publicvoidaddCard(Cardcard)
{
//ArrayList's method add; adds cards to the bottom of the collection
this.cards.add(card);
}
publicbooleanisEmpty()
{
returnthis.cards.isEmpty();
}
publicvoidaddDeck(Deckdeck)
{
//(02)Except for this deck cause it's a param, WHAT'S this??????
//??Also why can't just deck work here or maybe a different
//method in the deck class????? Deck's instance variable is a
//creation of an array of Card object's so since a deck is our
//parameter then any argument we get is already a Card[]??????
//???Also what was the purpose of the getCards() in deck??????
//BOOK NOTES:Notice that it does not remove the cards from the
//Deck, so the Deck and the Pile share cards. But that won't be
//a problem because cards are immutable.
//?I think that that might have answered some stuff. I think
//getCards() in the deck class doesn't set the Deck classes
//object = to anything and it only returns cards but again
//what's the point???Maybe it acts as an alias situation, so it
//allows something? to = the cards and returns it, like makes
//its return value = the reference to those cards, so that it
//doesn't interrupt the organization of the deck???????????????
for(Cardcard: deck.getCards())
this.cards.add(card);
//??I assume this is the object ArrayList<Card> so why can we
//invoke add from the variable(cards) if add is for ArrayList
//objects??? Shouldn't that be invoked from the object???? So
//either this.add or
}
//(01)??^Till this point and up we never had anything to do with the
//Deck class so why is he talking about us getting from the deck?
//Card has no correlation to the Deck class so I'm confused?????
//Here we use Deck and Pile to play War!
publicstaticvoidmain(String[] args)
{
Deckdeck = newDeck();
//?Why doesn't this.shuffle work or this.cards.shuffle???????
deck.shuffle();
Pilep1 = newPile();
//??What exactly does this mean and do????????? I feel like
//doing all these calls is unnecessary so is there a reason
//we are forced to do this or purpose???????????????????????
//???Also isn't it bad to have literals??READ top of subdeck!
p1.addDeck(deck.subdeck(0, 25));
Pilep2 = newPile();
p2.addDeck(deck.subdeck(26, 51));
longx = 0;
//???????????????????????????????????????????????????????????
// Sometimes this code runs endlessly
//???????????????????????????????????????????????????????????
while(!p1.isEmpty() && !p2.isEmpty())
{
x++;
System.out.println(x);
if(p1.cards.size()+p2.cards.size() != 52)
{
System.out.println("Missing some cards!");
System.out.println("x = " + x);
}
Cardc1 = p1.draw();
Cardc2 = p2.draw();
intdiff = c1.getRank() - c2.getRank();
if(diff > 0)
{
p1.addCard(c1);
p1.addCard(c2);
}
elseif(diff < 0)
{
p2.addCard(c1);
p2.addCard(c2);
}
else
{
Piletie = newPile();
tie.addCard(c1);
tie.addCard(c2);
Pilep1Tie = newPile();
Pilep2Tie = newPile();
//Why can't I make a Card object???? yet I can make a Deck??
//Card p1Best = new Card(0);
//Deck p1Best = new Deck(0); This might've worked better!!??
Cardp1Best = newCard(0, 0);
Cardp2Best = newCard(0, 0);
intdrawSize = 0;
//while(p1Best == p2Best) WHY? compares same reference!?
while(p1Best.getRank() == p2Best.getRank()) {
for(inti = 0; i < 4; i++)
{
if(!p1.isEmpty())
{
//this does remove the top card after this call!!!
c1 = p1.draw();
p1Tie.addCard(c1);
}
if(!p2.isEmpty())
{
c2 = p2.draw();
p2Tie.addCard(c2);
}
}
if(p1Tie.cards.size() <= p2Tie.cards.size())
drawSize = p2Tie.cards.size();
else
drawSize = p1Tie.cards.size();
//p1Best = p1Tie.get(0); is WRONG!!!
//p1Best = p1Tie.cards.get(0);
//p2Best = p2Tie.cards.get(0);
for(inti = 1; i < drawSize; i++)
{
//size is constant and i is counting up so this won't work
if((p1Tie.cards.size() > i))
{
//if(p1Best < p1Tie.get(i)) is WRONG!!!
if(p1Best.getRank() < p1Tie.cards.get(i).getRank())
p1Best = p1Tie.cards.get(i);
}
if((p2Tie.cards.size() > i))
{
if(p2Best.getRank() < p2Tie.cards.get(i).getRank())
p2Best = p2Tie.cards.get(i);
}
}
if(p1Best.getRank() == 13 && p2Best.getRank() == 13)
break;
}
//This needed to be moved here before putting our 2 tie cards in
//their respected collections in case a players last card is tie
//!!!!!!!!!!!JK Pretty sure this never mattered!!!!!!!!!!!!!!!!!!!
//int tieBreaker = p1Best.getRank() - p2Best.getRank();
//In a tie situation, Pile tie will only ever have 2 cards!
p1Tie.addCard(tie.cards.get(0));
p2Tie.addCard(tie.cards.get(1));
inttieBreaker = 0;
if(p1Best.getRank() == 13 && p2Best.getRank() == 13)
{
if(p1Best.compareTo(p2Best) == 1)
tieBreaker = 1;
else
tieBreaker = -1;
}
else
tieBreaker = p1Best.getRank() - p2Best.getRank();
drawSize++;
if(tieBreaker > 0)
{
//if anything find a way to shuffle through all these
//piles and add them individually can addDeck won't
for(inti = 0; i < drawSize; i++)
{
if((p1Tie.cards.size() > i))
p1.addCard(p1Tie.cards.get(i));
if((p2Tie.cards.size() > i))
p1.addCard(p2Tie.cards.get(i));
}
}
else
{
for(inti = 0; i < drawSize; i++)
{
//************SMART ERROR********************
//I mistakingly made both if-else add the card to p1 so
//that when I debug I run into errors much faster!!!!!!!
//p1.addCard(tie.cards.get(i)); What I used!
if((p1Tie.cards.size() > i))
p2.addCard(p1Tie.cards.get(i));
if((p2Tie.cards.size() > i))
p2.addCard(p2Tie.cards.get(i));
}
}
}
}
if(p2.isEmpty())
System.out.println("Player 1 wins!");
else
System.out.println("Player 2 wins!");
}
}