Skip to content

Predictability of validMove function #3

@mendeljacks

Description

@mendeljacks

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions