-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtic-tac-toe-2players.js
45 lines (39 loc) · 987 Bytes
/
tic-tac-toe-2players.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
const Board = require("./board");
const GameUI = require("./gameUI");
const board = new Board();
const gameUI = new GameUI(board);
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});
const getPlayerInput = (question) => {
return new Promise((resolve, reject) => {
readline.question(question, (answer) => {
resolve(answer);
});
});
};
let player = "X";
const gamePlay = async (player) => {
while (!board.gameOver()) {
let position = await getPlayerInput(`What's your move ${player}?`);
position = parseInt(position);
try {
board.write(position, player);
if (player === "X") {
player = "0";
} else {
player = "X";
}
} catch (error) {
gameUI.displayErrorMessage(error);
}
gameUI.renderBoard();
if (board.gameOver()) {
gameUI.displayWinner();
return readline.close();
}
}
};
gameUI.initialize();
gamePlay(player);