Files
functional-programming-snake/js/src/model.js
T

40 lines
1.5 KiB
JavaScript

import { todo } from './todo.js'
/** @typedef {{x: number, y: number}} Point */
/** @typedef {'Up'|'Down'|'Left'|'Right'} Direction */
/** @typedef {'Playing'|'Paused'|'GameOver'|'Won'} Status */
/** @typedef {{width: number, height: number, snake: Point[], dir: Direction, nextDir: Direction,
* apple: Point|null, seed: number, score: number, status: Status, frame: number}} GameState */
/** A grid cell `{x, y}`. */
export const point = (x, y) => todo('m02')
/** Structural equality: same x and same y. */
export const pointEquals = (a, b) => todo('m02')
/** The unit step for a direction: Up {0,-1}, Down {0,1}, Left {-1,0}, Right {1,0} (y grows downward). */
export const delta = (dir) => todo('m02')
/** The direction pointing the other way. */
export const opposite = (dir) => todo('m02')
/**
* Everything that can happen to the game, as data:
* `Tick`, `Turn(dir)`, `TogglePause`, `Restart`, `Frame`, each `{ tag, ... }`.
* `Turn` is a function because it carries a direction; the rest are constants
* (the getters below only exist so the stub compiles; replace them with plain values).
*/
export const Action = {
get Tick() { return todo('m02') },
Turn: (dir) => todo('m02'),
get TogglePause() { return todo('m02') },
get Restart() { return todo('m02') },
get Frame() { return todo('m02') },
}
/**
* A fresh game: 20x20, snake [(10,10),(9,10),(8,10)] heading Right (dir and nextDir),
* apple null, the given seed, score 0, status 'Playing', frame 0.
*/
export const initialState = (seed) => todo('m02')