Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predictability of validMove function #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions 001-snake/snake.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ const WEST = { x:-1, y: 0 }

// Point operations
const pointEq = p1 => p2 => p1.x == p2.x && p1.y == p2.y
const pointOp = p1 => p2 => p1.x == -p2.x && p1.y == -p2.y

// Booleans
const willEat = state => pointEq(nextHead(state))(state.apple)
const willCrash = state => state.snake.find(pointEq(nextHead(state)))
const validMove = move => state =>
state.moves[0].x + move.x != 0 || state.moves[0].y + move.y != 0
const validMove = move => state => !pointEq(move)(lastMove(state)) && !pointOp(move)(lastMove(state))

// Next values based on state
const lastMove = state => state.moves[state.moves.length - 1]
const nextMoves = state => state.moves.length > 1 ? dropFirst(state.moves) : state.moves
const nextApple = state => willEat(state) ? rndPos(state) : state.apple
const nextHead = state => state.snake.length == 0
Expand Down