Skip to content

Instantly share code, notes, and snippets.

@clojj
Created January 1, 2019 11:20
Show Gist options
  • Save clojj/22183e52cd0fb1fbd07a11c37b742079 to your computer and use it in GitHub Desktop.
Save clojj/22183e52cd0fb1fbd07a11c37b742079 to your computer and use it in GitHub Desktop.
Tictactoe.hs
data Coord = One | Two | Three deriving (Enum, Eq)
data Symb = O | X | Nada deriving (Show, Eq)
type TicTacToe = Coord -> Coord -> Symb
showBoard :: TicTacToe -> String
showBoard b = unlines $ map showLine [One .. Three]
where
showLine c = unwords $ map (showHouse $ b c) [One .. Three]
showHouse b' c = show $ b' c
composeBoard :: TicTacToe -> TicTacToe -> TicTacToe
composeBoard b1 b2 = b
where b c1 c2 | b1 c1 c2 == Nada = b2 c1 c2
| b2 c1 c2 == Nada = b1 c1 c2
| otherwise = b1 c1 c2
play :: TicTacToe -> Coord -> Coord -> Symb -> TicTacToe
play b c1 c2 player | b c1 c2 == Nada = composeBoard b b'
| otherwise = b
where b' c1' c2' = if c1'==c1 && c2'==c2
then player
else Nada
emptyBoard :: TicTacToe
emptyBoard = (const . const) Nada
main = do
let
b1 = play emptyBoard Two Two O
b2 = play b1 One One X
b3 = play b2 One One O
b4 = play b3 Three One X
putStr $ showBoard b4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment