A tic-tac-toe board is one of nine positions, each position occupied by either player 1, player 2 or neither and with invariants specific to the rules of tic-tac-toe.
For example, the number of positions occupied by player 1 is equal to, or one more, than the positions occupied by player 2.
- data EmptyBoard
- data Board
- data FinishedBoard
- empty :: EmptyBoard
- getResult :: FinishedBoard -> GameResult
- class Move from to | from -> to where
- (-?->) :: Position -> MoveResult -> MoveResult
- data MoveResult
- foldMoveResult :: a -> (Board -> a) -> (FinishedBoard -> a) -> MoveResult -> a
- keepPlayingOr :: a -> (Board -> a) -> MoveResult -> a
- keepPlaying :: MoveResult -> Maybe Board
- class TakeBack to from | to -> from where
- takeBack :: to -> from
- data TakenBack
- foldTakenBack :: a -> (Board -> a) -> TakenBack -> a
- takenBackBoard :: TakenBack -> Maybe Board
- class BoardLike b where
- whoseTurn :: b -> Player
- whoseNotTurn :: b -> Player
- isEmpty :: b -> Bool
- occupiedPositions :: b -> Set Position
- moves :: b -> Int
- isSubboardOf :: b -> b -> Bool
- isProperSubboardOf :: b -> b -> Bool
- playerAt :: b -> Position -> Maybe Player
- playerAtOr :: b -> Position -> Player -> Player
- isOccupied :: b -> Position -> Bool
- isNotOccupied :: b -> Position -> Bool
- printBoard :: b -> IO ()
- printEachPosition :: (Position -> String) -> IO ()
Board data types
A tic-tac-toe board.
data FinishedBoard Source
A finished board is a completed tic-tac-toe game and does not accept any more moves.
Start new game
Start an empty tic-tac-toe board.
Game completed
getResult :: FinishedBoard -> GameResultSource
Return the result of a completed tic-tac-toe game.
Make a move on a board
(-?->) :: Position -> MoveResult -> MoveResultSource
data MoveResult Source
The result of making a move on a tic-tac-toe board.
:: a | The move was to a position that is already occupied by a player. |
-> (Board -> a) | The move was valid and the board is in a new state. |
-> (FinishedBoard -> a) | The move was valid and the game is complete. |
-> MoveResult | |
-> a |
Deconstruct a move result.
:: a | The value to return if there is no board to keep playing with. |
-> (Board -> a) | A function to apply to the board to keep playing with. |
-> MoveResult | |
-> a |
Return the value after function application to the board to keep playing.
keepPlaying :: MoveResult -> Maybe BoardSource
Return the possible board from a move result. A board is returned if the result is to continue play.
Taking a move back from a board
foldTakenBack :: a -> (Board -> a) -> TakenBack -> aSource
Operations common to boards in-play and completed
Functions that work on boards that are in play or have completed.
This class specifically does not specify moving on a board, since this is illegal on a completed board.
whoseTurn :: b -> PlayerSource
Returns whose turn it is on a tic-tac-toe board.
whoseNotTurn :: b -> PlayerSource
Returns whose turn it is not on a tic-tac-toe board.
Returns whether or not the board is empty.
occupiedPositions :: b -> Set PositionSource
Returns positions that are occupied.
Returns the number of moves that have been played.
isSubboardOf :: b -> b -> BoolSource
Returns whether or not the first given board can transition to the second given board.
isProperSubboardOf :: b -> b -> BoolSource
Returns whether or not the first given board can transition to the second given board and they are inequal.
playerAt :: b -> Position -> Maybe PlayerSource
Returns the player at the given position.
playerAtOr :: b -> Position -> Player -> PlayerSource
Returns the player at the given position or the given default.
isOccupied :: b -> Position -> BoolSource
Returns whether or not the given position is occupied on the board. true
if occupied.
isNotOccupied :: b -> Position -> BoolSource
Returns whether or not the given position is occupied on the board. false
if occupied.
printBoard :: b -> IO ()Source
Prints the board to standard output using an ASCII grid representation.