-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.hs
More file actions
89 lines (64 loc) · 2.15 KB
/
Board.hs
File metadata and controls
89 lines (64 loc) · 2.15 KB
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
78
79
80
81
82
83
84
85
86
87
module Board where
import Data.List
import Position
type Board = [[XO]]
type Column = Int
type BoardState = (Board,XO)
data XO = X | O
deriving (Eq,Ord,Show)
data Turn = ToPlay XO
| HasWon XO (Pos,Pos)
deriving (Eq,Show)
data Cell = Token XO
| Empty
| Invalid
deriving (Eq)
instance Show Cell where
show (Token x) = show x
show (Empty) = " "
show (Invalid) = "!"
swap :: XO -> XO
swap X = O
swap O = X
newBoard :: Board
newBoard = [[] | x <- [1..7]]
-- Update the board with the latest move
updateBoard :: Board -> XO -> Column -> Board
updateBoard b t col = [if i==col then c ++ [t] else c
| (c,i) <- zip b [0..]
]
--------------------------------------------------------------------------------
-- Board Queries
--------------------------------------------------------------------------------
nextCell :: Board -> Int -> Int
nextCell b col = length (b !! col)
topFilled :: Board -> Int -> Int
topFilled b col = let l = length (b !! col)
in case l of
0 -> 0
otherwise -> l-1
filled :: Board -> Column -> Bool
filled b col = length (b !! col) >= 6
isEmpty :: Board -> Pos -> Bool
isEmpty b (row,col) = length (b !! col) <= row
canPick :: Board -> Pos -> Bool
canPick b (row,col) = if isValid (row,col) then
length (b !! col) == row
else
False
getCell :: Board -> Pos -> Cell
getCell b (row,col) = case isValid (row,col) of
False -> Invalid
True -> case (isEmpty b (row,col)) of
True -> Empty
False -> Token $ ((b !! col ) !! row)
validChoice :: Board -> Column -> Bool
validChoice b col = let row = nextCell b col in
row < 6
--------------------------------------------------------------------------------
-- Test Items
--------------------------------------------------------------------------------
testBoard1 :: Board
testBoard1 = [[O,X,O,X,O,O],[O,X,O,X,X,O],[O,X,X],[X,O,X,O,X,O],[X,X,O,O,X,X],[O,X,O,X,O,X],[X,O,X,O,X,O]]
testBoard2 :: Board
testBoard2 = [[X,O,X,O,O,O],[O,X,X,O,X],[O,X,O,X,O,O],[X,X,O,X,X,O],[X,O,X,O],[O,X],[O,X,X,O,X]]