-
Notifications
You must be signed in to change notification settings - Fork 4
/
Game.hs
29 lines (22 loc) · 814 Bytes
/
Game.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module Tfoo.Game where
import Tfoo.Board
import Network.Wai.EventSource (ServerEvent (..), eventSourceApp)
import Control.Concurrent.Chan
type Player = String
data Game = Game {
playerX :: Maybe Player,
playerO :: Maybe Player,
channel :: Chan ServerEvent,
board :: Board
}
setPlayer :: Game -> Mark -> Player -> Game
setPlayer game O playerId = game { playerO = Just playerId }
setPlayer game X playerId = game { playerX = Just playerId }
whoseTurn :: Game -> Maybe Player
whoseTurn g = if nextMark (board g) == O then playerO g else playerX g
gameState :: Game -> String
gameState game =
let board' = board game
in maybe (nextMove board') announceWinner (winner board')
where nextMove board = (show $ nextMark board) ++ "'s turn"
announceWinner mark = "Game won: " ++ (show mark)