-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameState.hs
34 lines (29 loc) · 1.15 KB
/
GameState.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
module GameState
where
import Board
import Helper (getSpace, subSet)
checkGameState :: Grid -> (Bool, Maybe Space)
checkGameState board
| checkWin (getSpace board X) = (True, Just X) -- X Wins
| checkWin (getSpace board O) = (True, Just O) -- O Wins
| fullBoard board = (False, Just Empty)-- Draw
| otherwise = (False, Nothing) -- Game Ongoing
where
fullBoard :: Grid -> Bool
fullBoard (Square Empty End) = False
fullBoard (Square _ End) = True
fullBoard (Square x xs)
| x == Empty = False
| otherwise = fullBoard xs
checkWin :: [Int] -> Bool
checkWin [] = False
checkWin x
| winByRow x || winByColumn x || winByCross x = True
| otherwise = False
where
winByRow :: [Int] -> Bool
winByRow x = subSet [0,1,2] x || subSet [3,4,5] x || subSet [6,7,8] x
winByColumn :: [Int] -> Bool
winByColumn x = subSet [0,3,6] x || subSet [1,4,7] x || subSet [2,5,8] x
winByCross :: [Int] -> Bool
winByCross x = subSet [0,4,8] x || subSet [2,4,6] x