-
Notifications
You must be signed in to change notification settings - Fork 0
/
P2_Khot_Tanvi_Board.java
201 lines (181 loc) · 6.59 KB
/
P2_Khot_Tanvi_Board.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
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
import java.util.*;
public class P2_Khot_Tanvi_Board
{
/* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */
// Attributes
ArrayList<P2_Khot_Tanvi_Deck> stacks;
P2_Khot_Tanvi_Deck drawPile;
ArrayList<P2_Khot_Tanvi_Deck> completedStacks;
/**
* Sets up the Board and fills the stacks and draw pile from a Deck
* consisting of numDecks Decks. The number of Cards in a Deck
* depends on the number of suits. Here are examples:
*
* # suits # numDecks #cards in overall Deck
* 1 1 13 (all same suit)
* 1 2 26 (all same suit)
* 2 1 26 (one of each suit)
* 2 2 52 (two of each suit)
* 4 2 104 (two of each suit)
*
* Once the overall Deck is built, it is shuffled and half the cards
* are placed as evenly as possible into the stacks. The other half
* of the cards remain in the draw pile. If you'd like to specify
* more than one suit, feel free to add to the parameter list.
*/
public P2_Khot_Tanvi_Board(int numStacks, int numDecks) {
/* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */
// Build the deck
P2_Khot_Tanvi_Deck deck = new P2_Khot_Tanvi_Deck();
for (int i = 0; i < numDecks; i++) {
deck.fillDeck();
}
deck.shuffle();
// Draw cards from deck and move to stacks
stacks = new ArrayList<>(numStacks);
for (int i=0; i < numStacks; i++) {
stacks.add(new P2_Khot_Tanvi_Deck());
}
int cardsForDrawPile = deck.size()/2/numStacks*numStacks;
int cardsForStacks = deck.size() - cardsForDrawPile;
for (int i = 0; i < cardsForStacks; i++) {
int currentStack = i % numStacks;
P2_Khot_Tanvi_Card card = deck.draw();
if (card != null) {
stacks.get(currentStack).add(card);
}
}
for (P2_Khot_Tanvi_Deck stack : stacks) {
stack.getTopCard().setFaceUp(true);
}
// remaining cards go to the drawPile
drawPile = deck;
}
public P2_Khot_Tanvi_Board(String saveState) {
this.setState(saveState);
}
/**
* Moves a run of cards from src to dest (if possible) and flips the
* next card if one is available. Change the parameter list to match
* your implementation of Card if you need to.
*/
public void makeMove(String symbol, int src, int dest) {
/* *** TO BE IMPLEMENTED IN ACTIVITY 5 *** */
if (src >= stacks.size() || dest >= stacks.size()) {
System.out.println("****Invalid move****\n\n");
return;
}
P2_Khot_Tanvi_Deck stackSrc = stacks.get(src);
P2_Khot_Tanvi_Deck stackDest = stacks.get(dest);
int indexRequestedCard = stackSrc.getRunStartForSymbol(symbol);
if (indexRequestedCard == -1) {
System.out.println("****Invalid move****\n\n");
return;
}
int lastCardValue = stackSrc.getCard(indexRequestedCard).getValue();
if (stackDest.size() > 0 && stackDest.getTopCard().getValue() != lastCardValue + 1) {
System.out.println("****Invalid move****\n\n");
return;
}
ArrayList<P2_Khot_Tanvi_Card> run = stackSrc.getRun(indexRequestedCard);
stackDest.addAll(run);
if (stackSrc.size() > 0) {
stackSrc.getTopCard().setFaceUp(true);
}
}
/**
* Moves one card onto each stack, or as many as are available
*/
public void drawCards() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 5 *** */
for (int i = 0; i < stacks.size(); i++) {
P2_Khot_Tanvi_Card card = drawPile.draw();
if (card != null) {
card.setFaceUp(true);
stacks.get(i).add(card);
}
}
}
/**
* Returns true if all stacks and the draw pile are all empty
*/
public boolean isEmpty() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 5 *** */
for (P2_Khot_Tanvi_Deck stack : stacks) {
if (stack.size() > 0) {
return false;
}
}
if (drawPile.size() > 0) {
return false;
}
return true;
}
/**
* If there is a run of A through K starting at the end of sourceStack
* then the run is removed from the game or placed into a completed
* stacks area.
*
* If there is not a run of A through K starting at the end of sourceStack
* then an invalid move message is displayed and the Board is not changed.
*/
public void clear(int sourceStack) {
/* *** TO BE IMPLEMENTED IN ACTIVITY 5 *** */
if (sourceStack >= stacks.size()) {
System.out.println("\n\n****Invalid move****\n\n");
return;
}
if (stacks.get(sourceStack).hasRun()) {
stacks.get(sourceStack).removeRun();
} else {
System.out.println("****Invalid move****\n\n");
}
if (stacks.get(sourceStack).size() > 0) {
stacks.get(sourceStack).getTopCard().setFaceUp(true);
}
}
/**
* Prints the board to the terminal window by displaying the stacks, draw
* pile, and done stacks (if you chose to have them)
*/
public void printBoard() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */
int i=1;
for (P2_Khot_Tanvi_Deck stack : stacks) {
System.out.println(i + ": " + stack.toString());
i++;
}
System.out.println();
System.out.println("Draw Pile:");
System.out.println(drawPile);
}
@Override
public String toString() {
String str = "";
for (P2_Khot_Tanvi_Deck stack : stacks) {
str += stack.toString();
str += "\n";
}
str += "DrawPile: ";
str += drawPile.toString();
return str;
}
public String getSaveState() {
String str = "";
for (P2_Khot_Tanvi_Deck deck : stacks) {
str += deck.getSaveState();
str += "\n";
}
str += this.drawPile.getSaveState();
return str;
}
public void setState(String saveState) {
String[] stateParts = saveState.split("\n");
this.stacks = new ArrayList<P2_Khot_Tanvi_Deck>();
for (String deckState : stateParts) {
P2_Khot_Tanvi_Deck deck = new P2_Khot_Tanvi_Deck(deckState);
this.stacks.add(deck);
}
this.drawPile = this.stacks.remove(this.stacks.size() - 1);
}
}