-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.hs
executable file
·78 lines (57 loc) · 2.44 KB
/
Board.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module Board (emptyBoard,
boardValue,
xWins,
oWins,
tie,
isEmpty,
movesFor,
placeToken,
indexFromTuple,
tokens,
Board(Board)) where
import Token
import Tree
import Valuable
import Prelude
emptyBoard = Board [Empty | x <- [0..8]]
boardValue board | xWins board = Max
| oWins board = Min
| otherwise = foldr addValues (Value 0) (lineValues board)
xWins board = any (== Max) (lineValues board)
oWins board = any (== Min) (lineValues board)
tie board = all (not . isEmpty board) [0..8]
isEmpty (Board a) index = (a !! index) == Empty
movesFor board token = map (placeTokenAtIndex board token) validIndicies
where validIndicies = filter (isEmpty board) [0..8]
placeToken board t token = placeTokenAtIndex board token (indexFromTuple t)
indexFromTuple (column, row) = row * 3 + column
tokens (Board list) = list
data Board = Board [Token] deriving (Eq)
instance Ord Board where
compare a b = compare (boardValue a) (boardValue b)
instance Show Board where
show board = " (a,b,c)\n" ++
"1" ++ (show $ row 0 board) ++ "\n" ++
"2" ++ (show $ row 1 board) ++ "\n" ++
"3" ++ (show $ row 2 board) ++ "\n"
addValues (Value a) (Value b) = Value (a + b)
lineValue (X, X, X) = Max
lineValue (O, O, O) = Min
lineValue (a, b, c) = Value (tokenValue a + tokenValue b + tokenValue c)
lineValues board = map lineValue (boardLines board)
boardLines board = [row 0 board,
row 1 board,
row 2 board,
column 0 board,
column 1 board,
column 2 board,
diagonalRight board,
diagonalLeft board]
token (Board a) t = a !! (indexFromTuple t)
row x board = (token board (0, x), token board (1, x), token board (2, x))
column x board = (token board (x, 0), token board (x, 1), token board (x, 2))
diagonalRight board = (token board (0, 0), token board (1, 1), token board (2, 2))
diagonalLeft board = (token board (2, 0), token board (1, 1), token board (0, 2))
placeTokenAtIndex (Board a) token index = Board $ concat [startingSpaces, [token], endingSpaces]
where startingSpaces = take index a
endingSpaces = drop (index + 1) a