Skip to content

Commit 975a153

Browse files
committed
Add IO functions for game of nim
1 parent eb107ce commit 975a153

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Data.Char
2+
3+
next :: Int -> Int
4+
next 1 = 2
5+
next 2 = 1
6+
7+
type Board = [Int]
8+
9+
initial :: Board
10+
initial = [5,4,3,2,1]
11+
12+
finished :: Board -> Bool
13+
finished = all (== 0)
14+
15+
valid :: Board -> Int -> Int -> Bool
16+
valid board row num = board !! (row-1) >= num
17+
18+
move :: Board -> Int -> Int -> Board
19+
move board row num = [update r n | (r, n) <- zip [1..] board]
20+
where
21+
update r n = if r == row then n - num else n
22+
23+
putRow :: Int -> Int -> IO ()
24+
putRow row num = do putStr (show row)
25+
putStr ": "
26+
putStrLn (concat (replicate num "* "))
27+
28+
putBoard :: Board -> IO ()
29+
putBoard [a,b,c,d,e] = do putRow 1 a
30+
putRow 2 b
31+
putRow 3 c
32+
putRow 4 d
33+
putRow 5 e
34+
35+
getDigit :: String -> IO Int
36+
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

10.8-nim.hs

-15
This file was deleted.

0 commit comments

Comments
 (0)