-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
53 lines (43 loc) · 1.24 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
import Board
import AI
import Data.Maybe
import Text.Read
emptyBoard :: Int -> Int -> Board
emptyBoard _ 0 = []
emptyBoard width height = replicate width Nothing : emptyBoard width (height-1)
getMove :: Board -> IO Board
getMove board = do
putStr "\nYour move: "
pos <- getLine
case readMaybe pos of
Nothing -> do
putStrLn "Invalid syntax."
getMove board
Just pos' ->
maybe retry return (move pos' board X)
where
retry = do
putStrLn "Illegal move."
getMove board
play :: Board -> IO ()
play board = do
putStrLn $ showBoard board
maybe
(if fullBoard board
then putStrLn "It's a tie!"
else do
let newBoard = getMove board
let newBoard' = fmap getAIMove newBoard
newBoard' >>= play
)
(\x -> putStrLn $ show x ++ " won the game!")
(winner board)
main :: IO ()
main = do
putStrLn "Welcome to TicTacToe!\n"
putStrLn "How wide should the board be? (Write a whole number)"
width <- getLine
putStrLn "How tall should the board be? (Write a whole number)"
height <- getLine
putStrLn "\nTo make a move, write a position on the form (x,y), where (0,0) is the top left position."
play $ emptyBoard (read width) (read height)