Skip to content

Commit 17d1177

Browse files
committed
Add play and main function to game of nim
1 parent 975a153 commit 17d1177

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

10-interactive-programming/10.8-nim.hs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,38 @@ putBoard [a,b,c,d,e] = do putRow 1 a
3434

3535
getDigit :: String -> IO Int
3636
getDigit prompt = do putStr prompt
37-
x <- getChar
38-
newline
39-
if isDigit x the
40-
return (digitToInt x)
41-
else
42-
do putStrLn "ERROR: Invalid digit"
43-
getDigit prompt
37+
x <- getChar
38+
newline
39+
if isDigit x then
40+
return (digitToInt x)
41+
else
42+
do putStrLn "ERROR: Invalid digit"
43+
getDigit prompt
44+
45+
newline :: IO ()
46+
newline = putChar '\n'
47+
48+
play :: Board -> Int -> IO ()
49+
play board player =
50+
do newline
51+
putBoard board
52+
if finished board then
53+
do newline
54+
putStr "Player "
55+
putStr (show (next player))
56+
putStrLn " wins!"
57+
else
58+
do newline
59+
putStr "Player "
60+
putStrLn (show player)
61+
row <- getDigit "Enter a row number: "
62+
num <- getDigit "Stars to remove: "
63+
if valid board row num then
64+
play (move board row num) (next player)
65+
else
66+
do newline
67+
putStrLn "ERROR: Invalid move"
68+
play board player
69+
70+
nim :: IO ()
71+
nim = play initial 1

0 commit comments

Comments
 (0)