This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const point = (x, y) => { | |
| return { | |
| x: x, | |
| y: y, | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const r = require("ramda") | |
| const { intercalate, update } = require("./helper") | |
| const createWorld = (rows, columns, state) => { | |
| const repeatDot = r.repeat(".") | |
| const map = r.map(r.thunkify(repeatDot)(rows), repeatDot(columns)) | |
| return r.pipe(addSnake(state), addApple(state))(map) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const displayWorld = matrix => { | |
| console.clear() | |
| console.log(intercalate("\r\n", r.map(intercalate(" "), matrix))) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const COLUMNS = 15 | |
| const ROWS = 15 | |
| const SPEED = 125 | |
| let uglyMutableState = initialState | |
| const displayState = display(COLUMNS, ROWS) | |
| const runGameLoop = () => { | |
| setInterval(() => { | |
| displayState(uglyMutableState) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // index.js | |
| const setupInput = () => { | |
| readline.emitKeypressEvents(process.stdin) | |
| process.stdin.setRawMode(true) | |
| process.stdin.on("keypress", (str, key) => { | |
| if (key.ctrl && key.name === "c") process.exit() | |
| const options = { | |
| UP: addMove(direction.NORTH), | |
| LEFT: addMove(direction.WEST), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const nextSnake = r.curry((cols, rows, state) => { | |
| return willCrash(cols, rows, state) | |
| ? initialState | |
| : { | |
| ...state, | |
| snake: willEat(nextHead(cols, rows, state), state.apple) | |
| ? [nextHead(cols, rows, state), ...state.snake] | |
| : [nextHead(cols, rows, state), ...r.dropLast(1, state.snake)], | |
| } | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const willEat = r.equals | |
| const willCrash = (cols, rows, state) => | |
| r.find(r.equals(nextHead(cols, rows, state)))(state.snake) | |
| const nextHead = (cols, rows, { move, snake }) => | |
| point( | |
| modulo(cols)(r.head(snake).x + move.x), | |
| modulo(rows)(r.head(snake).y + move.y) | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const nextApple = r.curry((cols, rows, state) => | |
| willEat(r.head(state.snake), state.apple) | |
| ? { ...state, apple: point(randomPos(cols), randomPos(rows)) } | |
| : state | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const step = r.curry((cols, rows, state) => | |
| r.pipe(nextSnake(cols, rows), nextApple(cols, rows))(state) | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const COLUMNS = 15 | |
| const ROWS = 15 | |
| const SPEED = 125 | |
| let uglyMutableState = initialState | |
| const setupInput = () => { | |
| readline.emitKeypressEvents(process.stdin) | |
| process.stdin.setRawMode(true) | |
| process.stdin.on("keypress", (str, key) => { | |
| if (key.ctrl && key.name === "c") process.exit() |
OlderNewer