-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.hs
82 lines (60 loc) · 2.34 KB
/
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
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
module Game where
import Data.Maybe
type Height = Int
type Width = Int
type XIndex = Int
type YIndex = Int
data GameStatus = Won | Lost | Running deriving (Eq, Show)
data Player = Self | Opponent deriving (Eq, Show)
data Chip = Empty | Chip Player deriving (Eq, Show)
type Column = [Chip]
data Grid = Grid Height [Column] deriving (Eq, Show)
type RowCount = [Int]
type RowCountGrid = [[RowCount]]
defaultWidth = 7
defaultHeight = 6
defaultGrid :: Grid
defaultGrid = genEmptyGrid defaultWidth defaultHeight
genEmptyGrid :: Width -> Height -> Grid
genEmptyGrid width height = Grid height (replicate width ([] :: Column))
insertChip :: Grid -> XIndex -> Player -> Maybe Grid
insertChip grid@(Grid height cols) col player
| col < 0 = Nothing
| col >= length cols = Nothing
| length (cols !! col) >= height = Nothing
| otherwise = Just (Grid height (take col cols ++ ((Chip player : (cols !! col)) : drop (col + 1) cols)))
getChipAt :: Grid -> XIndex -> YIndex -> Maybe Chip
getChipAt grid@(Grid height cols) x y
| x < 0 = Nothing
| x >= width = Nothing
| y < 0 = Nothing
| y >= height = Nothing
| y >= length (cols !! x) = Just Empty
| otherwise = Just (col !! ((length col - 1) - y))
where
col = cols !! x
width = length cols
rowCount :: Grid -> RowCountGrid
rowCount grid@(Grid height cols) =
[[[(rowCount' x y a b 1) - 1 | a <- [-1 .. 1], b <- [-1 .. 1]] | y <- [0 .. height - 1]] | x <- [0 .. length cols - 1]]
where
rowCount' :: XIndex -> YIndex -> Int -> Int -> Int -> Int
rowCount' x y a b n
| isNothing chip = n
| isNothing neighbor = n
| a == 0 && b == 0 = n
| neighbor /= chip = n
| otherwise = rowCount' (x + a) (y + b) a b (n + 1)
where
chip = getChipAt grid x y
neighbor = getChipAt grid (x + a) (y + b)
checkGameStatus :: Grid -> GameStatus
checkGameStatus grid@(Grid height cols)
| null results = Running
| otherwise = let firstResult : _ = results in firstResult
where
results = [checkGameStatus' x y | x <- [0 .. length cols - 1], y <- [0 .. (height - 1)], not (null (relevantRows x y)) && getChipAt grid x y /= Just Empty && isJust (getChipAt grid x y)]
relevantRows x y = filter (>= 3) (rowCount grid !! x !! y)
checkGameStatus' x y
| getChipAt grid x y == Just (Chip Self) = Won
| getChipAt grid x y == Just (Chip Opponent) = Lost