-
Notifications
You must be signed in to change notification settings - Fork 0
/
quarto.js
244 lines (211 loc) · 6.91 KB
/
quarto.js
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
// INITIALIZE GAME VARIABLES
let gamePhase = 'choose';
let activePlayer = 'A';
// what if I create a grid of coordinates
let gameCoords = [
['A1', null], ['A2', null], ['A3', null], ['A4', null],
['B1', null], ['B2', null], ['B3', null], ['B4', null],
['C1', null], ['C2', null], ['C3', null], ['C4', null],
['D1', null], ['D2', null], ['D3', null], ['D4', null],
];
// create a list of winning sets???
let winningRows = [
['A1', 'A2', 'A3', 'A4'],
['B1', 'B2', 'B3', 'B4'],
['C1', 'C2', 'C3', 'C4'],
['D1', 'D2', 'D3', 'D4'],
];
let winningColumns = [
['A1', 'B1', 'C1', 'D1'],
['A2', 'B2', 'C2', 'D2'],
['A3', 'B3', 'C3', 'D3'],
['A4', 'B4', 'C4', 'D4'],
];
let winningDiagonals = [
['A1', 'B2', 'C3', 'D4'],
['A4', 'B3', 'C2', 'D1'],
];
let winningSquares = [
['A1', 'B1', 'A2', 'B2'],
['A2', 'B2', 'A3', 'B3'],
['A3', 'B3', 'A4', 'B4'],
['B1', 'C1', 'B2', 'C2'],
['B2', 'C2', 'B3', 'C3'],
['B3', 'C3', 'B4', 'C4'],
['C1', 'D1', 'C2', 'D2'],
['C2', 'D2', 'C3', 'D3'],
['C3', 'D3', 'C4', 'D4'],
['A1', 'C1', 'A3', 'C3'],
['A2', 'C2', 'A4', 'C4'],
['B1', 'D1', 'B3', 'D3'],
['B2', 'D2', 'B4', 'D4'],
['A1', 'A4', 'D1', 'D4'],
];
let winningSets = winningRows.concat(winningColumns, winningDiagonals);
// DECLARE VARIABLES FROM DOM HERE //
let startingPieces = document.querySelectorAll('.inactive .piece');
let activeSpot = document.getElementById('activespot');
let inactiveSpot = document.getElementsByClassName('inactive')[0];
let banner = document.getElementsByClassName('banner')[0];
let boardSpots = document.getElementsByClassName('boardspot')
// helper functions convert ID to "tuple" arrays and vice-versa
function idToTuple(id) { // id is text e.g. p0101
let tuple = [];
let value = null;
for (i = 1; i < id.length; i++) { // ignore the starting 'p'
value = id.slice(i, i + 1);
tuple.push(parseInt(value));
}
return tuple;
}
function tupleToId(tuple) {
let id = 'p';
for (i = 0; i < tuple.length; i++) {
id = id.concat(tuple[i]);
}
return id;
}
function updateBannerText() {
// update banner text based on turn phase
let newText = '';
if (gamePhase === 'place') {
newText = 'Player ' + activePlayer + ', place the active piece!';
} else if (gamePhase === 'choose') {
newText = 'Player ' + activePlayer + ', choose a piece!';
} else if (gamePhase === 'win') {
newText = 'Player ' + activePlayer + ' wins! Click here to reset.';
} else if (gamePhase === 'draw') {
newText = 'The match ended in a draw! Click here to reset.';
}
banner.innerHTML = newText;
}
function updateBannerColor() {
// update the banner color based on player turn
if (activePlayer === 'A') {
banner.style.backgroundColor = 'lightgreen';
} else if (activePlayer === 'B') {
banner.style.backgroundColor = 'pink';
} else if (activePlayer === null) {
banner.style.backgroundColor = 'lightgrey';
}
}
function changePlayer() {
// switch players and update the banner color accordingly
if (activePlayer === 'A') {
activePlayer = 'B';
} else if (activePlayer === 'B') {
activePlayer = 'A';
}
updateBannerColor();
gamePhase = 'place'; // New player has to place the active piece
updateBannerText();
}
function findBoardspot(id) { // id is text
for (f = 0; f < gameCoords.length; f++) {
if (gameCoords[f][0] === id) {
return f;
}
}
}
function checkWinConditions(newSpot) { // newSpot format e.g. 'A2'
// for every winning combination, filter for relevant/eligible ones
let eligibleSets = [];
for (i = 0; i < winningSets.length; i++) {
if (winningSets[i].includes(newSpot)) {
eligibleSets.push(winningSets[i]);
}
}
for (i = 0; i < eligibleSets.length; i++) {
// for every eligible combination, check for 4-in-a-row
let set = eligibleSets[i];
let compare = [];
for (j = 0; j < set.length; j++) {
let attributes = gameCoords[findBoardspot(set[j])][1];
if (attributes !== null) {
compare.push(attributes);
}
}
if (compare.length === 4) {
for (j = 0; j < 4; j++) {
let val = compare[0][j];
if (val === compare[1][j] && val === compare[2][j] && val === compare[3][j]) {
for (k = 0; k < set.length; k++) {
// highlight winning squares in yellow. This part works to loop.
let square = document.getElementById(set[k]);
square.style.backgroundColor = 'yellow';
}
return true;
}
}
}
}
return false;
}
// testing lines
// BELOW THIS LINE - GAME OPERATIONS //
// Event listeners for choosing pieces
for (i = 0; i < startingPieces.length; i++) {
let elem = startingPieces[i];
elem.used = false;
elem.addEventListener('click', function () {
if (gamePhase === 'choose' && elem.used === false) {
// move the node to the active spot
activeSpot.appendChild(elem);
elem.used = true;
// change phase and player
changePlayer();
}
});
}
// Event listeners for placing pieces
for (i = 0; i < boardSpots.length; i++) {
let elem = boardSpots[i];
elem.addEventListener('click', function () {
// locate which board square this is
let index = findBoardspot(elem.id);
// Check to make sure the space is empty
if (gameCoords[index][1] === null) {
// locate the active piece
let activePiece = document.querySelectorAll('#activespot .piece')[0];
// convert id to tuple and save in the gameCoords grid
gameCoords[index][1] = idToTuple(activePiece.id);
// move the active piece to the board square
elem.appendChild(activePiece);
// check for winning conditions
if (checkWinConditions(elem.id)) {
gamePhase = 'win';
} else if (inactiveSpot.children.length === 0) { // check to see if all pieces are used
gamePhase = 'draw';
activePlayer = null;
updateBannerColor();
} else { // if no win, change the turn phase to 'choose'
gamePhase = 'choose';
}
updateBannerText();
}
});
}
// Event listener for game reset
banner.addEventListener('click', function () {
// check for either a win or all pieces placed
if (gamePhase === 'win' || gamePhase === 'draw') {
// replace the pieces to the inactive area
for (let piece of startingPieces) {
piece.used = false;
inactiveSpot.appendChild(piece);
}
// reset game variables and banner display
activePlayer = 'A';
gamePhase = 'choose';
updateBannerText();
updateBannerColor();
// clear out all the data in the coordinate grid
for (let cell of gameCoords) {
cell[1] = null;
}
// color all the spots white again
for (let spot of boardSpots) {
spot.style.backgroundColor = 'initial';
}
}
});