-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
53 lines (42 loc) · 1.16 KB
/
game.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
const { Puzzle, Solver } = require('./index.js')
const keypress = require('keypress');
const printBoard = () => {
console.clear()
// console.log(puzzle.board.join(', '))
console.log('\n\n')
console.log(puzzle.toString())
console.log('\n\n')
}
let puzzle = new Puzzle(process.argv[2] || 4);
puzzle.shuffle()
printBoard()
console.log("Press 's' key to solve or play with arrow keys")
// make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);
// listen for the "keypress" event
process.stdin.on('keypress', async function (ch, key) {
switch(key.name) {
case 'left':
case 'right':
case 'down':
case 'up':
puzzle.move(key.name)
printBoard()
// console.log(key.name)
return
case 's':
let solver = new Solver(puzzle)
const path = solver.getPath()
for (const dir of path) {
await new Promise(resolve => setTimeout(resolve, 100)); // sleep(100)
puzzle.moveEmpty(dir)
printBoard()
console.log(path.toString())
}
}
if (key && key.ctrl && key.name == 'c') {
process.stdin.pause();
}
});
process.stdin.setRawMode(true);
process.stdin.resume();