-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGrid.pde
More file actions
248 lines (219 loc) · 7.97 KB
/
Grid.pde
File metadata and controls
248 lines (219 loc) · 7.97 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
247
248
public class Grid {
// In the physical SET game, cards are placed on the table.
// The table contains the grid of cards and is typically called the board.
//
// Note that the minimum number of cards that guarantees a set is 21,
// so we create an array with enough columns to accommodate that.
// (MAX_COLS == 7)
Card[][] board = new Card[MAX_COLS][ROWS];
ArrayList<Location> selectedLocs = new ArrayList<Location>(); // Locations selected by the player
ArrayList<Card> selectedCards = new ArrayList<Card>(); // Cards selected by the player
// (corresponds to the locations)
int cardsInPlay; // Number of cards visible on the board
public Grid() {
cardsInPlay = 0;
}
// GRID MUTATION PROCEDURES
// 1. Highlight (or remove highlight) selected card
// 2. Add (or remove) the location of the card in selectedLocs
// 3. Add the card to (or remove from) the list of selectedCards
public void updateSelected(int col, int row) {
Card card = board[col][row];
if (selectedCards.contains(card)) {
int index = selectedCards.indexOf(card);
selectedLocs.remove(index);
selectedCards.remove(card);
//score--;
} else {
selectedLocs.add(new Location(col, row));
selectedCards.add(card);
}
//System.out.println("Cards = " + selectedCards + ", Locations = " + selectedLocs);
}
// Precondition: A Set has been successfully found
// Postconditions:
// * The number of columns is adjusted as needed to reflect removal of the set
// * The number of cards in play is adjusted as needed
// * The board is mutated to reflect removal of the set
public void removeSet() {
// Because it seems to make for a better UX, cards should not change locations unless
// the number of columns has decreased. If that happens, cards from the rightmost
// column should be moved to locations where cards that formed the selected set
// Put the locations of the selected cells in order. Cards from the rightmost column
// that are part of the set should be removed instead of being migrated.
selectedLocs.sort(null); // Don't delete this line as it orders the selected locations
// You may wish to look up how the Location class decides
// how to compare two different locations. Also look up the
// documentation on ArrayList to see how sort(null) works
if (cardsInPlay > 12 || deck.size() == 0){
//filter selectedLocs arrayList to get the non-last column ones
ArrayList<Location> selList = new ArrayList<Location>();
for (Location lok : selectedLocs){
if (lok.getIndex() >= cardsInPlay - 3){
board[lok.getCol()][lok.getRow()] = null;
}
else{
selList.add(lok);
}
}
//filter column list arrayList to get the non selectedLocs ones
ArrayList<Location> colList = new ArrayList<Location>();
for (int i = 0 ; i < 3 ; i++){
if (board[currentCols - 1][i] != null){
colList.add(new Location(currentCols - 1, i));
}
}
//switch last column positions with selected positions
for (int x = 0; x < selList.size() ; x++){
board[selList.get(x).getCol()][selList.get(x).getRow()] = board[currentCols - 1][colList.get(x).getRow()];
board[currentCols - 1][colList.get(x).getRow()] = null;
}
currentCols-- ;
cardsInPlay -= 3;
}
else if (cardsInPlay == 12){
for ( Location lok : selectedLocs){
board[lok.getCol()][lok.getRow()] = deck.deal();
}
}
}
// Precondition: Three cards have been selected by the player
// Postcondition: Game state, score, game message mutated, selected cards list cleared
public void processTriple() {
if (isSet(selectedCards.get(0), selectedCards.get(1), selectedCards.get(2))) {
score += 10;
removeSet();
if (isGameOver()) {
state = state.GAME_OVER;
runningTimerEnd = millis();
timerScore();
message = 7;
} else {
state = State.PLAYING;
message = 1;
}
} else {
score -= 5;
state = State.PLAYING;
message = 2;
}
clearSelected();
}
// DISPLAY CODE
public void display() {
int cols = cardsInPlay / 3;
for (int col = 0; col < cols; col++) {
for (int row = 0; row < ROWS; row++) {
board[col][row].display(col, row);
}
}
}
public void highlightSelectedCards() {
color highlight;
if (state == State.FIND_SET) {
highlight = FOUND_HIGHLIGHT;
selectedLocs = findSet();
if (selectedLocs.size() == 0) {
message = 6;
return;
}
} else if (selectedLocs.size() < 3) {
highlight = SELECTED_HIGHLIGHT;
} else {
highlight = isSet(selectedCards.get(0), selectedCards.get(1), selectedCards.get(2)) ?
CORRECT_HIGHLIGHT :
INCORRECT_HIGHLIGHT;
}
for (Location loc : selectedLocs) {
drawHighlight(loc, highlight);
}
}
public void drawHighlight(Location loc, color highlightColor) {
stroke(highlightColor);
strokeWeight(5);
noFill();
int col = loc.getCol();
int row = loc.getRow();
rect(GRID_LEFT_OFFSET+col*(CARD_WIDTH+GRID_X_SPACER),
GRID_TOP_OFFSET+row*(CARD_HEIGHT+GRID_Y_SPACER),
CARD_WIDTH,
CARD_HEIGHT);
stroke(#000000);
strokeWeight(1);
}
// DEALING CARDS
// Preconditions: cardsInPlay contains the current number of cards on the board
// the array board contains the cards that are on the board
// Postconditions: board has been updated to include the card
// the number of cardsInPlay has been increased by one
public void addCardToBoard(Card card) {
board[col(cardsInPlay)][row(cardsInPlay)] = card;
cardsInPlay++ ;
}
//adds column to board if needed
public void addColumn() {
//if there are no cards in play say that
if (cardsInPlay == 0){
message = 5;
}
//if there is no set, add 3 new cards
else if (findSet().size() == 0){
score += 5;
for (int i = 1; i <= 3 ; i++){
addCardToBoard(deck.deal());
}
currentCols++ ;
message = 3;
}
// else there was a set
else{
score -= 5;
message = 4;
}
}
// GAME PROCEDURES
//returns true is game is over
public boolean isGameOver() {
// deck is empty, and there are no cards in play, or there are no more sets possible
return (deck.size() == 0 && (cardsInPlay == 0 || findSet().size() == 0));
}
public boolean tripleSelected() {
return (selectedLocs.size() == 3);
}
// Preconditions: --
// Postconditions: The selected locations and cards ArrayLists are empty
public void clearSelected() {
selectedLocs.clear();
selectedCards.clear();
}
// findSet(): If there is a set on the board, existsSet() returns an ArrayList containing
// the locations of three cards that form a set, an empty ArrayList (not null) otherwise
// Preconditions: --
// Postconditions: No change to any state variables
public ArrayList<Location> findSet() {
ArrayList<Location> locs = new ArrayList<Location>();
for (int i = 0; i < currentCols*3 - 2; i++) {
for (int j = i+1; j < currentCols*3 - 1; j++) {
for (int k = j+1; k < currentCols*3; k++) {
if (isSet(board[col(i)][row(i)], board[col(j)][row(j)], board[col(k)][row(k)])) {
locs.add(new Location(col(i), row(i)));
locs.add(new Location(col(j), row(j)));
locs.add(new Location(col(k), row(k)));
return locs;
}
}
}
}
return new ArrayList<Location>();
}
// UTILITY FUNCTIONS FOR GRID CLASS
public int col(int n) {
return n/3;
}
public int row(int n) {
return n % 3;
}
public int rightOffset() {
return GRID_LEFT_OFFSET + currentCols * (CARD_WIDTH + GRID_X_SPACER);
}
}