-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
357 lines (307 loc) · 9.44 KB
/
game.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* A class representing the structure of a Connect Four game
* implements game logic and visualisation
*/
class Game {
constructor(cols, rows, size) {
this.cols = cols;
this.rows = rows;
this.size = size;
this.board;
this.line = [];
// player "enums"
this.playerA = Object.freeze({
name: "Red",
val: 1,
code: 'r',
color: [255, 0, 0]
});;
this.playerB = Object.freeze({
name: "Yellow",
val: -1,
code: 'y',
color: [255, 255, 0]
});
this.current;
this.previous;
this.over = false;
this.paused = false;
}
getEmptyBoard() {
let arr = new Array(this.cols);
for (let i = 0; i < this.cols; i++) {
arr[i] = new Array(this.rows);
for (let j = 0; j < this.rows; j++) {
arr[i][j] = 0;
}
}
return arr;
}
newGame(currentPlayer) {
this.over = false;
this.paused = false;
this.board = this.getEmptyBoard();
this.line = [];
if (currentPlayer) {
this.current = currentPlayer;
} else { // random player starts game
this.current = random([this.playerA, this.playerB]);
}
this.previous = this.current === this.playerA ? this.playerB : this.playerA;
}
/**
* inserts a coin into the board with the given position
* fails if the position is invalid
*/
makeMove(x, y) {
let pos = this.getCoinPosition(x, y);
if (!pos)
return;
this.insertCoin(pos.column, pos.row, this.current.val);
}
/**
* inserts a coin with a given value into the board at a valid position
* starts evaluation process and switches players turns
*/
insertCoin(column, row, val) {
// place coin and eval if game is over
this.board[column][row] = val;
this.over = this.isGameOver(column, row);
// switch turns
let temp = this.current;
this.current = this.previous;
this.previous = temp;
// small input delay to make placing of coins "more natural"
this.paused = true;
setTimeout(() => {
this.paused = false;
}, 200);
}
/**
* returns the column and row of the a given (x/y) position (e.g. mouse position)
* returns null if position is outside of board or column is full
*/
getCoinPosition(x, y) {
if (x >= width || x < 0 || y >= height || y < 0)
return null; // position not on board
let column = floor(x / this.size);
for (let i = 0; i < this.rows; i++) {
if (this.board[column][i] === 0) {
return {
column,
row: i
};
}
}
return null; // no empty spot in column
}
/*
* evaluates if the game is over, e.g. game is won by a player or is draw
* returns true if game is over, false otherwise
* also starts a new game if game is over
*/
isGameOver(column, row) {
if (this.isWonByMove(column, row)) {
let name = this.current.name;
setTimeout(() => {
alert(`Congratulations ${name}! You are the winner!`);
this.newGame(this.current);
}, 100);
return true;
} else if (this.isDraw()) {
setTimeout(() => {
alert("Oh no! It is a draw...");
this.newGame(this.current);
}, 100);
return true;
}
return false;
}
/**
* checks wether the previous player won by his move leading to the current board
* returns true if game is won, false otherwise
*/
isWonByMove(column, row) {
return this.checkLine(column, row, 1, 0) ||
this.checkLine(column, row, 0, 1) ||
this.checkLine(column, row, 1, 1) ||
this.checkLine(column, row, 1, -1);
}
/**
* checks wether a line of four is filled by a player
* returns true if line is filled, i.e. game is won, false otherwise
*/
checkLine(column, row, dc, dr) {
let val = this.board[column][row];
let sum = 1;
this.line.push({
column,
row
});
// move right
for (let i = 1; i < 4; i++) {
let c = column + dc * i;
let r = row + dr * i;
if (c >= this.cols || r >= this.rows || this.board[c][r] != val || sum >= 4) {
break;
} else {
sum++;
this.line.push({
column: c,
row: r
});
}
}
// move left
for (let i = 1; i < 4; i++) {
let c = column - dc * i;
let r = row - dr * i;
if (c < 0 || r < 0 || this.board[c][r] != val || sum >= 4) {
break;
} else {
sum++;
this.line.push({
column: c,
row: r
});
}
}
if (sum < 4) {
this.line = [];
return false;
}
return true;
}
/**
* checks whether the game is a draw by checking if for empty spots
* does not check if any side won a game
* returns true if the game is a draw, false otherwise
*/
isDraw() {
// check if any empty spot in top row
for (let i = 0; i < this.cols; i++) {
if (this.board[i][this.rows - 1] === 0) {
return false;
}
}
return true;
}
showBoard() {
background(0);
for (let i = 0; i < this.cols; i++) {
for (let j = 0; j < this.rows; j++) {
let val = this.board[i][j];
this.showCoin(i, j, val, 255, false);
}
}
}
showCoin(column, row, val, alpha, highlight) {
if (val === this.playerA.val) {
fill(...this.playerA.color, alpha);
} else if (val === this.playerB.val) {
fill(...this.playerB.color, alpha);
} else { // empty spot
fill(200);
}
if (highlight) {
strokeWeight(this.size * 0.05);
stroke(0, 0, 255, alpha);
} else {
noStroke();
}
let cx = this.size * column + this.size * 0.5;
let cy = height - this.size * row - this.size * 0.5;
circle(cx, cy, this.size * 0.7);
}
hoverCoin(x, y) {
let pos = this.getCoinPosition(x, y);
if (pos) {
let val = this.current.val;
this.showCoin(pos.column, pos.row, val, 100, false);
}
}
showLine() {
for (let coin of this.line) {
let column = coin.column;
let row = coin.row;
let val = this.board[column][row];
this.showCoin(column, row, val, 255, true);
}
}
/*
* builds a game from a given character sequence
* (similar to Forsyth-Edwards-Notation in chess)
* e.g.: "7/7/7/2y4/2ry3/1ryry2 r"
*/
gameFromFEN(fen) {
// convert fen into single rows and indication of active player
let fenBoard, currentPlayer;
[fenBoard, currentPlayer] = fen.split(' ');
let fenRows = fenBoard.split('/');
// build empty board
let board = this.getEmptyBoard();
// fill empty board with values
for (let i = 0; i < fenRows.length; i++) {
let fenRow = fenRows[i];
let index = 0;
for (let c in fenRow) {
let char = fenRow[c];
let number = parseInt(char);
if (number) {
index += number;
} else {
let val = this.getPlayer(char).val;
board[index][this.rows - i - 1] = val;
index++;
}
}
}
this.board = board;
// set active player
this.current = this.getPlayer(currentPlayer);
}
/*
* converts the current game state into a string representation
* (similar to Forsyth-Edwards-Notation in chess)
*/
gameToFEN() {
let fen = "";
// convert board
for (let i = this.rows - 1; i >= 0; i--) {
let counter = 0;
for (let j = 0; j < this.cols; j++) {
let val = this.board[j][i];
if (val === 0) {
counter++;
} else {
if (counter > 0) {
fen += counter;
counter = 0;
}
if (val === this.playerA.val) {
fen += this.playerA.code;
} else if (val === this.playerB.val) {
fen += this.playerB.code;
}
}
}
if (counter > 0) {
fen += counter;
}
if (i != 0) {
fen += '/';
}
}
// convert active player
fen += ' ' + this.current.code;
return fen;
}
getPlayer(code) {
if (code === this.playerA.code) {
return this.playerA;
} else if (code === this.playerB.code) {
return this.playerB;
}
return null;
}
}