This file contains 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
let john = { name: "John", age: 18 } | |
john.name = "Patricio" |
This file contains 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
module Core where | |
type Columns = Int | |
type Rows = Int | |
type Point = (Rows, Columns) | |
type Apple = Point | |
type Snake = [Point] | |
type Moves = [Move] | |
data Move = North | South | West | East deriving (Show) |
This file contains 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() |
This file contains 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 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 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 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 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 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 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))) | |
} |
NewerOlder