go left | go up | go right | go down | spacebar | pause/play |
go up/left | go up/right |
go down/left | go down/right |
Snake is an open source project. Contribute to the github repository by submitting issues and pull requests. Submit cheat algorithms into the /play/js/cheat directory.
This implementation of Snake! was specifically built to explore the interesting strategy of the game. It is designed to be easily pluggable with configurable behavior. Cheat plugins can be added and interchanged dynamically at runtime using the sideloader module. The game provides an API available to the developers to access important information about the game such as the position of the food or the direction the snake is travelling. What are you waiting for? Go write a snake bot!
Cheat
object in JavaScript. See more on the API here.
top-left | top-right |
bottom-left | bottom-right |
top-left | top-right |
bottom-left | bottom-right |
This API outlines the documentation of the Snake and Cheat classes as well as the Position and Direction types. Questions or suggestions can be posted on the Github repository.
Snake
Snake is a singleton class used to expose all interesting information about the game. Snake is the outward facing interface that should be used to design cheat behaviors. If there is a piece of information that your cheat needs that you'd like to be added to the API, submit an issue or pull request on the Github repository.
No public fields
int Snake.getHeight()
getHeight() returns the height of the game board in tiles. When the game is loaded, the
board is resized dynamically to fit the screen.
params - none
return - integer height of board
int Snake.getWidth()
getWidth() returns the width of the game board in tiles. When the game is loaded, the
board is resized dynamically to fit the screen.
params - none
return - integer width of board
int[][] Snake.getPositions()
getPositions() returns a copy of the two dimensional array of size
Snake.getWidth() x Snake.getHeight() that represents all the positions on the board.
Each entry is either a 0, 1, or 2. 0 represents an empty space, 1 represents a space
filled by the snake, and 2 represents a space filled by the food.
params - none
return - copy of the 2D positions array
int Snake.checkPosition(int x, int y)
checkPosition() returns 0, 1, or 2 based on the x and y coordinates passed in. If the position is filled
by the snake or represents a position outside the game board (i.e. a wall), it returns 1. If the position
is filled by the food, it returns 2. Otherwise it returns 0.
params - int x: horizontal position, int y: vertical position
return - integer state of the position
Position Snake.getPosition()
getPosition() returns a copy of the Position that represents the the head of the snake.
params - none
return - Position of the head
Position Snake.getTailPosition()
getTailPosition() returns a copy of the Position that represents the the tail of the snake.
params - none
return - Position of the tail
Position Snake.getFoodPosition()
getFoodPosition() returns a copy of the Position that represents the the food.
params - none
return - Position of the food
int Snake.getLength()
getLength() returns the length of the snake. Score can be derived from this because
the length of the snake at the start of the game is 5.
params - none
return - Integer length of the snake
Direction Snake.getDirection()
getDirection() returns the current Direction in which the snake is heading.
params - none
return - current Direction of the snake
Cheat
Cheat is a singleton class used to define the cheat behavior the game implements. At the start of the game it is undefined, and the Sideloader module loads custom JavaScript to override it. The game expects the Cheat object to have a method Cheat.cheat() that returns a Direction. Every update, Cheat.cheat() is called before the snake is moved. If Cheat.cheat() returns null, then the snake continues in the same direction. When writing custom behavior, the Cheat.cheat() method is the access point for your behavior.
No public fields
Direction Cheat.cheat()
cheat() returns a Direction to turn the snake.
params - none
return - Direction to turn the snake
Position
The Position class is used to define a unique poisition in the 2D array that defines the game board
int x
the x position in the 2D array game board.
int y
the y position in the 2D array game board.
No public methods
Direction
The Direction class is used to define the four possible directions the snake can be travelling: left, up, right, and down. Note that if |x| > 0, then y must be 0, and likewise if |y| > 0, then x must be 0. Diagonal directions are illegal values for Direction.
int x
the x direction, -1, 0, or 1.
int y
the y direction, -1, 0, or 1.
No public methods