/
console.mjs
37 lines (33 loc) · 874 Bytes
/
console.mjs
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
import jsChessEngine from '../dist/js-chess-engine.js'
import readline from 'readline'
const game = new jsChessEngine.Game()
play()
function play () {
game.printToConsole()
let rl = getInput()
rl.question('From? ', from => {
rl.close()
const moves = game.moves(from)
console.log('Your options: ', moves)
rl = getInput()
rl.question('To? ', to => {
rl.close()
try {
game.move(from, to)
} catch (error) {
console.log(`Skipping: ${error}`)
}
if (game.exportJson().isFinished) {
console.log('Game over')
} else {
play()
}
})
})
}
function getInput () {
return readline.createInterface({
input: process.stdin,
output: process.stdout,
})
}