-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommands.js
62 lines (47 loc) · 1.98 KB
/
Commands.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
const readline = require('readline');
const Snake = require('./snake')
const base = require('./base')
Object.getOwnPropertyNames(base).map(p => global[p] = base[p])
let State = Snake.initialState()
const Matrix = {
make: table => rep(rep('.')(table.cols))(table.rows),
set: val => pos => adjust(pos.y)(adjust(pos.x)(k(val))),
addSnake: state => pipe(...map(Matrix.set('X'))(state.snake)),
addApple: state => Matrix.set('o')(state.apple),
addPoisonApple: state => Matrix.set('O')(state.poisonapple),
addCrash: state => state.snake.length == 0 ? map(map(k('#'))) : id,
toString: xsxs => xsxs.map(xs => xs.join(' ')).join('\r\n'),
fromState: state => pipe(
Matrix.make,
Matrix.addSnake(state),
Matrix.addApple(state),
Matrix.addCrash(state),
Matrix.addPoisonApple(state),
)(state)
}
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') process.exit()
switch (key.name.toUpperCase()) {
case 'W': case 'K': case 'UP': State = Snake.enqueue(State, Snake.NORTH); break
case 'A': case 'H': case 'LEFT': State = Snake.enqueue(State, Snake.WEST); break
case 'S': case 'J': case 'DOWN': State = Snake.enqueue(State, Snake.SOUTH); break
case 'D': case 'L': case 'RIGHT': State = Snake.enqueue(State, Snake.EAST); break
}
});
const show = () => console.log('\x1Bc' + Matrix.toString(Matrix.fromState(State)) + '\n Sua pontuação: '+Snake.next(State).score)
const step = () => State = Snake.next(State)
const vel = (v=3) => {
switch (v) {
case 0: return 300; break;
case 1: return 250; break;
case 2: return 200; break;
case 3: return 150; break;
case 4: return 100; break;
case 5: return 50; break;
default: return 25; break;
}
}
setInterval(() => { step(); show(); }, 0 + vel(0))
setInterval(() => { step(); show(); }, 0 + vel(Snake.next(State).increase))