/
index.js
69 lines (55 loc) · 2.33 KB
/
index.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
const Ayoayo = require('@chidiwilliams/ayoayo');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
readline.write(` █████ ██ ██ ██████ █████ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ████ ██ ██ ███████ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██████ ██ ██ ██ ██████
Let's play!
To begin, enter the column number for the cell you wish to pick from (1-6).`);
const ayoayo = new Ayoayo();
requestUserInput();
function requestUserInput() {
readline.write(boardText(ayoayo.board, ayoayo.captured));
if (ayoayo.isGameOver) {
readline.write(`GAME OVER. Winner: Player ${ayoayo.winner + 1}`);
readline.close();
return;
}
const question = `\nPlayer ${ayoayo.nextPlayer + 1}'s turn. Pick a cell: `;
readline.question(question, (cell) => {
const cellNum = Number(cell);
if (
Number.isNaN(cellNum) ||
!ayoayo.permissibleMoves.includes(cellNum - 1)
) {
readline.write('Please enter a valid and allowed cell index.');
requestUserInput();
return;
}
ayoayo.play(cellNum - 1);
requestUserInput();
});
}
function boardText(board, captured) {
let s = `\n\n 1 2 3 4 5 6 Captured\n ------------------------------------------- ------------\n`;
board.forEach((row, rowIndex) => {
s += `Player ${rowIndex + 1} |`;
row.forEach((cell) => {
s += ` ${padDigit(cell)} |`;
});
const capturedCount = padDigit(captured[rowIndex]);
s += ` | ${capturedCount} |\n ------------------------------------------- ------------\n`;
});
return s;
}
function padDigit(val) {
if (val < 10) {
return ' ' + val;
}
return '' + val;
}