-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
41 lines (35 loc) · 1.16 KB
/
example.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
const {Minesweeper, Difficulty, SquareState} = require('.');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
async function playGame() {
console.log('Welcome to Minesweeper!');
let difficulty = await askInput('Type difficulty level [beginner, intermediate, expert]:');
let game = new Minesweeper(Difficulty.valueOf(difficulty));
while (!game.isEnded()) {
printBoard(game);
let coordinate = await askCoordinateToReveal();
game.revealSquareAt(coordinate);
}
printBoard(game);
if (game.isCleared())
console.log('Congratulations, you have cleared the minefield!!');
else
console.log('Unfortunately you were blown up!');
process.exit(0);
}
async function askCoordinateToReveal() {
let xy = await askInput('Type coordinate to reveal [x,y]?:');
let [x, y] = xy.split(',');
return {x: parseInt(x), y: parseInt(y)};
}
function askInput(question) {
return new Promise((resolve, reject) => {
readline.question(question, (input) => resolve(input));
})
}
function printBoard(game) {
console.log(game.toString());
}
playGame();