TicTacToe-0.0.1: A sub-project (exercise) for a functional programming course

Data.TicTacToe.Board

Contents

Description

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.

Synopsis

Board data types

data FinishedBoard Source

A finished board is a completed tic-tac-toe game and does not accept any more moves.

Start new game

empty :: EmptyBoardSource

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

class Move from to | from -> to whereSource

Methods

(-->) :: Position -> from -> toSource

data MoveResult Source

The result of making a move on a tic-tac-toe board.

foldMoveResultSource

Arguments

:: 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.

keepPlayingOrSource

Arguments

:: 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

class TakeBack to from | to -> from whereSource

Methods

takeBack :: to -> fromSource

foldTakenBack :: a -> (Board -> a) -> TakenBack -> aSource

Operations common to boards in-play and completed

class BoardLike b whereSource

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.

Methods

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.

isEmpty :: b -> BoolSource

Returns whether or not the board is empty.

occupiedPositions :: b -> Set PositionSource

Returns positions that are occupied.

moves :: b -> IntSource

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.

Debugging

printEachPositionSource

Arguments

:: (Position -> String)

The function returning the string to substitute each position.

-> IO () 

Prints out a board using ASCII notation and substituting the returned string for each position.