-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogic.hs
62 lines (50 loc) · 1.67 KB
/
Logic.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
{-# LANGUAGE RecordWildCards #-}
module Logic where
import Data.List (transpose)
import Data.Maybe (isNothing)
import Types
isRowWin :: Board -> Player -> Bool
isRowWin b p = any (all (== Just p)) b
isColWin :: Board -> Player -> Bool
isColWin = isRowWin . transpose
isWinState :: Board -> Player -> Bool
isWinState b p = isRowWin b p || isColWin b p
isValidPosition :: Board -> Position -> Bool
isValidPosition b (x, y) = isNothing $ b !! y !! x
swapListVal :: Int -> a -> [a] -> [a]
swapListVal pos val list =
take pos list ++ [val] ++ drop (pos + 1) list
nextBoardState :: Board -> Player -> Position -> Board
nextBoardState board player (x, y) =
swapListVal y updatedRow board
where
selectedRow = board !! y
updatedRow = swapListVal x (Just player) selectedRow
applyMove :: Game -> Position -> Game
applyMove Game {..} position =
Game
{ player = if player == First then Second else First,
board = nextBoardState board player position,
state = state
}
isBoardFull :: Board -> Bool
isBoardFull = not . any isNothing . concat
maybeUpdateGame :: Game -> Position -> Maybe Game
maybeUpdateGame g@Game {state = GameOver _} _ = Just g
maybeUpdateGame g@Game {..} position
| not $ isValidPosition board position = Nothing
| isWinState nextBoard First = gameOver $ Just First
| isWinState nextBoard Second = gameOver $ Just Second
| isBoardFull nextBoard = gameOver Nothing
| otherwise = Just nextGameState
where
nextGameState@Game
{ board = nextBoard
} = applyMove g position
gameOver maybePlayer =
Just
Game
{ player = player,
board = nextBoard,
state = GameOver maybePlayer
}