-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTTTCLI.hs
45 lines (36 loc) · 1.02 KB
/
TTTCLI.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
import TicTacToe
import System.Exit
import Text.Read
import Board
main = tictactoe $ Right initialBoard
tictactoe :: BoardOrMsg -> IO ()
tictactoe bmsg = case bmsg of
(Left msg) -> putStrLn msg
(Right b) -> nextRound b
nextRound :: Board -> IO ()
nextRound b = do
-- check if one of the players won the game
case checkWinnerPure b of
Nothing -> putStrLn $ "Player's turn: " ++ (show $ currentPlayer b)
Just p -> do
putStrLn $ "Player " ++ show p ++ " won the game"
exitSuccess
-- determine current player and output info
let p = currentPlayer b
putStrLn $ show b
-- get choice from input
col <- getInput "Input column: "
row <- getInput "Input row: "
-- make choice on board
case mkChoice (col,row,p) b of
Left msg -> do
putStrLn msg
nextRound b
Right b' -> nextRound b'
getInput :: String -> IO Int
getInput msg = do
putStr msg
inp <- getLine
case readMaybe inp of
Nothing -> getInput msg
Just n -> return n