-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataTypes.hs
48 lines (34 loc) · 936 Bytes
/
DataTypes.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module DataTypes
(Player (..)
, nextPlayer
, Board (..)
, listToBoard
, boardSide
, boardSize
, Position
) where
data Player = Cross | Circle | Empty deriving (Eq)
instance Show Player where
show Cross = "X"
show Circle = "O"
show Empty = "_"
makeLine :: [Player] -> String
makeLine ps = x ++ "\n\n" where
x = unwords [" " ++ show p ++ " " | p <- ps]
nextPlayer :: Player -> Player
nextPlayer p = if p == Cross then Circle else Cross
newtype Board = Board [Player]
boardSide :: Int
boardSide = 3
boardSize :: Int
boardSize = boardSide * boardSide
instance Show Board where
show = showBoard
listToBoard :: [Player] -> Maybe Board
listToBoard xs = if length xs == boardSize then Just (Board xs) else Nothing
showBoard :: Board -> String
showBoard (Board []) = []
showBoard (Board ps) = x ++ y where
x = makeLine $ take boardSide ps
y = showBoard (Board (drop boardSide ps))
type Position = Int