forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic-Tac-Toe-YZ.js
362 lines (335 loc) · 7.34 KB
/
Tic-Tac-Toe-YZ.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
358
359
360
361
362
/*
@title: Tic-Tac-Toe
@author: bochk0
@tags: ['retro']
@addedOn: 2024-07-17
*/
const PLAYER_Y = "Y";
const PLAYER_Z = "Z";
const POINTER = "P";
const POWERUP = "U";
const DOUBLE_MOVE = "D";
const EMPTY_TILE = "E";
const END_TILE = "T";
setLegend(
[POINTER, bitmap`
................
................
..777777777777..
..7..........7..
..7..........7..
..7..........7..
..7..........7..
..7..........7..
..7..........7..
..7..........7..
..7..........7..
..7..........7..
..7..........7..
..777777777777..
................
................`],
[PLAYER_Y, bitmap`
................
................
................
................
....9......9....
....99....99....
.....99..99.....
......9999......
.......99.......
......99........
.....99.........
....99..........
....9...........
................
................
................`],
[PLAYER_Z, bitmap`
................
................
................
.....444444.....
....44444444....
..........44....
.........44.....
........44......
.......44.......
......44........
.....44.........
....44444444....
.....444444.....
................
................
................`],
[POWERUP, bitmap`
................
................
.......66.......
......6666......
......6666......
.....666666.....
..666666666666..
..666666666666..
...6666666666...
....66666666....
.....666666.....
....666..666....
...666....666...
..666......666..
................
................`],
[DOUBLE_MOVE, bitmap`
................
................
...HHHHH........
...H...HH.......
...H.H..HH......
...H.HH..HH.....
...H.H.H..HH....
...H.H..H..HH...
...H.H..H..HH...
...H.H.H..HH....
...H.HH..HH.....
...H.H..HH......
...H...HH.......
...HHHHH........
................
................`],
[EMPTY_TILE, bitmap`
3333333333333333
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3FFFFFFFFFFFFFF3
3333333333333333`],
[END_TILE, bitmap`
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC`]
);
setMap(map`
EEE
EEE
EEE`);
addSprite(1, 1, POINTER);
let currentPlayer = true;
let gameBoard = [
[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]
];
let gameFinished = false;
let aiLoseTurn = false;
let doubleMove = false;
let extraMove = false;
let aiSkipMoves = 0;
function hasWinner(board) {
const symbol = currentPlayer ? PLAYER_Y : PLAYER_Z;
const lines = [
[board[0][0], board[0][1], board[0][2]],
[board[1][0], board[1][1], board[1][2]],
[board[2][0], board[2][1], board[2][2]],
[board[0][0], board[1][0], board[2][0]],
[board[0][1], board[1][1], board[2][1]],
[board[0][2], board[1][2], board[2][2]],
[board[0][0], board[1][1], board[2][2]],
[board[0][2], board[1][1], board[2][0]]
];
for (let line of lines) {
if (line[0] === symbol && line[1] === symbol && line[2] === symbol) {
return true;
}
}
return false;
}
function isDraw(board) {
for (let row of board) {
for (let cell of row) {
if (cell === " ") return false;
}
}
return true;
}
function endGame(message) {
gameFinished = true;
setMap(map`
TTT
TTT
TTT`);
addText(message, { y: 7, color: color`2` });
}
function resetGame() {
gameFinished = false;
currentPlayer = true;
aiLoseTurn = false;
doubleMove = false;
extraMove = false;
gameBoard = [
[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]
];
clearText();
setMap(map`
EEE
EEE
EEE`);
addSprite(1, 1, POINTER);
}
function spawnPowerUp() {
if (Math.random() < 0.1) { // 10% chance to spawn a power-up after each move
const emptyCells = [];
for (let y = 0; y < 3; y++) {
for (let x = 0; x < 3; x++) {
if (gameBoard[y][x] === " ") {
emptyCells.push([y, x]);
}
}
}
if (emptyCells.length > 0) {
const [y, x] = emptyCells[Math.floor(Math.random() * emptyCells.length)];
const powerUpType = Math.random() < 0.5 ? POWERUP : DOUBLE_MOVE;
addSprite(x, y, powerUpType);
}
}
}
function minimax(board, depth, isMaximizing) {
if (hasWinner(board)) {
return isMaximizing ? -1 : 1;
}
if (isDraw(board)) {
return 0;
}
let bestScore = isMaximizing ? -Infinity : Infinity;
const symbol = isMaximizing ? PLAYER_Z : PLAYER_Y;
for (let y = 0; y < 3; y++) {
for (let x = 0; x < 3; x++) {
if (board[y][x] === " ") {
board[y][x] = symbol;
const score = minimax(board, depth + 1, !isMaximizing);
board[y][x] = " ";
bestScore = isMaximizing ? Math.max(score, bestScore) : Math.min(score, bestScore);
}
}
}
return bestScore;
}
function aiMove() {
if (aiLoseTurn || aiSkipMoves > 0) {
if (aiLoseTurn) {
aiLoseTurn = false;
} else {
aiSkipMoves--;
}
addText("AI loses a turn!", { y: 1, color: color`2` });
setTimeout(() => clearText(), 2000);
currentPlayer = !currentPlayer;
return;
}
let bestScore = -Infinity;
let move;
for (let y = 0; y < 3; y++) {
for (let x = 0; x < 3; x++) {
if (gameBoard[y][x] === " ") {
gameBoard[y][x] = PLAYER_Z;
const score = minimax(gameBoard, 0, false);
gameBoard[y][x] = " ";
if (score > bestScore) {
bestScore = score;
move = { y, x };
}
}
}
}
if (move) {
gameBoard[move.y][move.x] = PLAYER_Z;
addSprite(move.x, move.y, PLAYER_Z);
checkGameEnd();
}
}
function checkGameEnd() {
if (hasWinner(gameBoard)) {
endGame(`${currentPlayer ? PLAYER_Y : PLAYER_Z} wins!`);
} else if (isDraw(gameBoard)) {
endGame("It's a draw!");
} else {
currentPlayer = !currentPlayer;
if (!currentPlayer && !extraMove) {
aiMove();
}
}
}
onInput("w", () => {
if (!gameFinished) getFirst(POINTER).y -= 1;
});
onInput("a", () => {
if (!gameFinished) getFirst(POINTER).x -= 1;
});
onInput("s", () => {
if (!gameFinished) getFirst(POINTER).y += 1;
});
onInput("d", () => {
if (!gameFinished) getFirst(POINTER).x += 1;
});
onInput("i", () => {
if (gameFinished || gameBoard[getFirst(POINTER).y][getFirst(POINTER).x] !== " ") return;
const pointer = getFirst(POINTER);
const tiles = getTile(pointer.x, pointer.y);
if (tiles.some(tile => tile.type === POWERUP)) {
const powerUp = tiles.find(tile => tile.type === POWERUP);
if (powerUp) {
powerUp.remove();
}
aiLoseTurn = true;
addText("Power-up collected!", { y: 1, color: color`2` });
setTimeout(() => clearText(), 2000);
} else if (tiles.some(tile => tile.type === DOUBLE_MOVE)) {
const powerUp = tiles.find(tile => tile.type === DOUBLE_MOVE);
if (powerUp) {
powerUp.remove();
}
aiSkipMoves = 2; // AI skips 2 moves
addText("Double Move collected!", { y: 1, color: color`2` });
setTimeout(() => clearText(), 2000);
} else {
const symbol = currentPlayer ? PLAYER_Y : PLAYER_Z;
addSprite(pointer.x, pointer.y, symbol);
gameBoard[pointer.y][pointer.x] = symbol;
checkGameEnd();
if (!gameFinished) {
spawnPowerUp();
if (extraMove) {
extraMove = false;
currentPlayer = true;
}
}
}
});
onInput("k", () => {
resetGame();
});