Skip to content

sambou/snake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake

Reusable logic for the classic game Snake, implemented with the reducer pattern. This library exposes a reducer function with the signature (state, action) -> state, actions for advancing the game one tick (actions.advance), controlling the snake (actions.goUp, actions.goDown, actions.goLeft, actions.goRight) and restarting (actions.restart) and an initialState Object. All actions are synchronous (ie. return an object like {type: "ADVANCE"}) and none take any arguments.

Screenshot

The type of the game state looks like this:

type State = {
  // Grid dimensions
  width: number;
  height: number;
  // Snake's head position
  x: number;
  y: number;
  direction: 'left' | 'right' | 'up' | 'down';
  // Game status
  status: 'running' | 'over';
  score: number;
  tail: Array<{ x: number; y: number }>;
  items: Array<{ x: number; y: number }>;
};

Check out the examples for detailed usage paterns.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published