-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
92 lines (80 loc) · 2.69 KB
/
Copy pathgame.js
File metadata and controls
92 lines (80 loc) · 2.69 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
import { init, makeMove, gameStatus } from './engine.js';
import { positionFromIndex } from './helpers.js';
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const updateStatus = (state) => {
state.status = gameStatus(state.board);
if (state.board.lastPid) {
const lp = state.board.pieces[state.board.lastPid];
state.turn = lp.type[0] === 'w' ? 'b' : 'w';
} else {
state.turn = 'w';
}
const statusElement = document.querySelector('.status');
if (state.status === 'wcm') {
statusElement.innerHTML = 'White is checkmate!';
} else if (state.status === 'wc') {
statusElement.innerHTML = 'White is check!';
} else if (state.status === 'bcm') {
statusElement.innerHTML = 'Black is checkmate!';
} else if (state.status === 'bc') {
statusElement.innerHTML = 'Black is check!';
} else if (state.status === 'sm') {
statusElement.innerHTML = 'Stalemate!';
} else if (state.status === 'd') {
statusElement.innerHTML = 'Draw!';
} else {
statusElement.innerHTML = `${state.turn === 'w' ? 'White\'s' : 'Black\'s'} turn`
}
}
const styleForPiece = (piece) => ({
...positionFromIndex(piece.index),
display: piece.dead ? 'none' : 'block',
backgroundImage: `url(img/${piece.type}.png)`
});
const renderBoard = (state) => {
for (let i = 0; i < state.board.pieces.length; i++) {
const piece = state.board.pieces[i];
const el = state.ui.pieces[i];
Object.assign(el.style, styleForPiece(piece));
}
};
const generateBoard = (state) => {
return state.board.pieces.map((piece) => {
const el = document.createElement('div');
el.className = `piece piece-${piece.type[0]}`;
state.ui.board.appendChild(el);
Object.assign(el.style, styleForPiece(piece));
return el;
})
}
export default async (white, black, wait = 200) => {
document.documentElement.style.setProperty('--animation-speed', `${Math.max(Math.min(wait, 200), 40) / 1000}s`);
const gameState = {
ui: {
board: document.querySelector('.board'),
pieces: [],
},
turn: 'w',
status: null,
board: init()
};
gameState.ui.pieces = generateBoard(gameState);
await timeout(100);
let ended = false
let turns = 0;
while (!ended) {
turns++;
const player = gameState.turn === 'w' ? white : black;
const move = await player.makeMove(gameState);
gameState.board = await makeMove(gameState.board, move.pid, move.toIndex, (board) => {
const gs = { ...gameState, board };
renderBoard(gs);
return player.promote(move.pid, gs);
})
renderBoard(gameState);
updateStatus(gameState);
ended = ['wcm', 'bcm', 'sm', 'd'].includes(gameState.status);
await timeout(wait);
}
console.log('Game ended');
}