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 #3

Open
mendeljacks opened this issue Aug 1, 2018 · 1 comment
Open

Predictability of validMove function #3

mendeljacks opened this issue Aug 1, 2018 · 1 comment

Comments

@mendeljacks
Copy link

Problems:

  1. When moving right, and press down and left rapidly (within the same draw), the left will be rejected because it fails the check against the first item in the movement array. Expected behavior should be that rapid key press will perform both moves.

  2. Valid move does not check if the move is the same as the current direction, so rapid presses of key fills up the array with junk. Expected behavior is that pressing the key in the direction of travel is completely ignored.

Proposed valid move function:
const validMove = move => state =>{
let indexLast = state.moves.length - 1
let OppositeDirection = state.moves[indexLast].x == -move.x || state.moves[indexLast].y == -move.y
let SameDirection = state.moves[indexLast].x == move.x && state.moves[indexLast].y == move.y
return (!SameDirection && !OppositeDirection)
}

The above should be placed into snake.js replacing the existing const validMove. It will make the game feel much more responsive.

PS indexLast can be turned into a method and placed in base.js for modularity

@chrokh
Copy link
Owner

chrokh commented Aug 7, 2018

Aha! Excellent catch! Great explanation and great suggestion! If you would like to create a pull request that would be prime! :) Otherwise just let me know and I'll do it.

I would suggest also breaking out oppositeDirection and sameDirection into their own functions, which would render sameDirection the same function as pointEq, which would suggest that oppositeDirection could be named pointOp (as in "opposite") so that:

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

And if we extract lastMove like this:

const lastMove = state => state.moves[state.moves.length - 1]

We can transform the validMove function you suggested into this:

const validMove = move => state =>
  !pointEq(move)(lastMove(state)) && !pointOp(move)(lastMove(state))

AntonStrand added a commit to AntonStrand/fp-games that referenced this issue Oct 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants