Functional Noughts and Crosses
- Functional Reactive Programming - Bacon
- Immutability - Mori
- Basic Lenses - Enhancing Mori
- View is a function of state - React
Two players - X and O.
Game board - 3 by 3 grid of buttons.
Reset button - Reset the board and start a new game.
Forfeit button - Ends the game, the non-active player wins.
Scores - Each player has a score, initially set to 0. Winning a game increases your score by 1.
A coordinate system is used to reference the grid locations.
A scalar value is either A, B, or C.
A pair of scalar values make up a location.
Locations identify grid locations, e.g:
(A, A)is left-top(B, A)is middle-top
Scalar = A | B | C
Loc = Scalar * ScalarA player is either X or O.
Player = X | OA board is a mapping from locations to players.
Board = Map Loc PlayerThe game can be in one of three modes: Active, Draw, or Won.
Play = Active | Draw | WonScore is defined as an association between players and natural numbers.
Score = Map Player NatA game is the combination of:
- a current
Player - a state of
Play - a
Boardstate - and a
Scoretable
Game = Player * Play * Board * ScoreAn operation that changes a game is called an Action.
Action = Game -> GameActions include:
- Resetting the game
- A player taking their go
- A player forfeiting the current game
Possible instances for Player, Scalar, and Play are modelled as
strings:
var X = "X"
var O = "O"
var A = "A"
var B = "B"
// etc.A location is a Mori hash_map with two keys:
var Loc = function (col, row) {
return M.hash_map('col', col, 'row', row) }A Board is a Mori hash_map from locations to players.
var initBoard = M.hash_map()Score is a Mori hash_map from Players to Naturals.
The initial Score has both players associated with 0.
var initScore = M.hash_map(X, 0, O, 0)A Game has a current Player, Play, Board, and Score.
var initGame = M.hash_map(
'player', X, // Player X to go first
'play', Active, // Ready for play
'board', initBoard, // The empty board
'score', initScore // Initial scores
)