-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
41 lines (37 loc) · 967 Bytes
/
Main.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
module Main where
import Logic (maybeUpdateGame)
import Rendering (firstTurn, showGame)
import Text.Read (readMaybe)
import Types
( Game (Game, state),
GameState (GameOver),
Player (..),
Position,
)
loop :: Game -> IO ()
loop g@Game {state = GameOver _} = putStrLn $ showGame g
loop gs = do
putStrLn $ showGame gs
move <- getLine
let parsedMove = readMaybe move :: Maybe Position
in case parsedMove of
Nothing -> do
putStrLn "Invalid input, try again"
loop gs
Just position ->
case maybeUpdateGame gs position of
Nothing -> do
putStrLn "Invalid move, try again"
loop gs
Just nextGameState -> loop nextGameState
testState :: [[Maybe Player]]
testState = map (map Just) state
where
state =
[ [First, First, Second],
[Second, First, Second],
[First, Second, Second]
]
main :: IO ()
main = do
loop firstTurn