1
2
3
4
5
6
7
8
9
1011
12
13
14
15
16
17
18
19
2021
22
23
24
25
26
27
28
29
3031
32
33
34
35
36
37
38
39
4041
42
43
44
45
46
47
48
49
5051
52
53
54
55
56
57
58
59
6061
62
63
64
65
66
67
68
69
7071
72
73
74
75
76
77
78
79
8081
82
83
84
85
86
87
88
89
9091
92
93
94
95
96
97
98
99
100101
102
103
104
105
106
107
108
109
110111
112
113
114
115
116
117
118
119
120121
122
123
124
125
126
127
128
129
130131
132
133
134
135
136
137
138
139
140141
142
143
144
145
146
147
148
149
150151
152
153
154
155
156
157
158
159
160161
162
163
164
165
166
| import Prelude
-- Excercise of Lecture 9 of "Programming Haskell"
data Player = PlayerOne | PlayerTwo deriving Show
nextPlayer PlayerOne = PlayerTwo
nextPlayer _ = PlayerOne
nim :: Player -> [Int] -> IO ()
nim player xs | sum xs == 0 = putStrLn $ show (nextPlayer player) ++ " has won!"
| otherwise =
let rmStar :: [Int] -> Int -> Maybe [Int]
rmStar xs n
| xs !! n == 0 = Nothing
| otherwise = Just (rmStar' xs n)
where
rmStar' xs 0 = xs !! 0 - 1 : drop 1 xs
rmStar' (x:xs) (pos+1) = x : rmStar' xs pos
dsplStandings :: [Int] -> IO ()
dsplStandings = dsplStandings' 0
where
dsplStandings' 5 _ = return ()
dsplStandings' n xs = do
putStr $ show (n+1) ++ ": "
putStrLn $ take (xs !! n) (repeat '*')
dsplStandings' (n+1) xs
in do dsplStandings xs
putStr $ show player ++ ", "
putStr "From which row do you want to remove a star?: "
x <- getChar
let pos = read (x:[]) - 1
if or [(pos < 0),(pos >= 5)] then do
putChar '\n'
putStrLn "This row is out of range!"
nim player xs
else do putChar '\n'
case rmStar xs pos of
Just decr -> nim (nextPlayer player) decr
Nothing -> do
putStr "This row is already empty!"
putStrLn " Take another one!"
nim player xs
nimTest = nim PlayerOne [1..5]{- Bsp.:
*Main> nimTest
1: *
2: **
3: ***
4: ****
5: *****
PlayerOne, From which row do you want to remove a star?: 1
1:
2: **3: ***
4: ****
5: *****
PlayerTwo, From which row do you want to remove a star?: 6
This row is out of range!
1:
2: **
3: ***
4: ****
5: *****PlayerTwo, From which row do you want to remove a star?: 4
1:
2: **
3: ***
4: ***
5: *****
PlayerOne, From which row do you want to remove a star?: 3
1:
2: **
3: **4: ***
5: *****
PlayerTwo, From which row do you want to remove a star?: 2
1:
2: *
3: **
4: ***
5: *****
PlayerOne, From which row do you want to remove a star?: 3
1: 2: *
3: *
4: ***
5: *****
PlayerTwo, From which row do you want to remove a star?: 5
1:
2: *
3: *
4: ***
5: ****PlayerOne, From which row do you want to remove a star?: 6
This row is out of range!
1:
2: *
3: *
4: ***
5: ****
PlayerOne, From which row do you want to remove a star?: 7
This row is out of range!
1: 2: *
3: *
4: ***
5: ****
PlayerOne, From which row do you want to remove a star?: 5
1:
2: *
3: *
4: ***
5: ***PlayerTwo, From which row do you want to remove a star?: 5
1:
2: *
3: *
4: ***
5: **
PlayerOne, From which row do you want to remove a star?: 5
1:
2: *
3: *4: ***
5: *
PlayerTwo, From which row do you want to remove a star?: 5
1:
2: *
3: *
4: ***
5:
PlayerOne, From which row do you want to remove a star?: 4
1: 2: *
3: *
4: **
5:
PlayerTwo, From which row do you want to remove a star?: 3
1:
2: *
3:
4: **
5: PlayerOne, From which row do you want to remove a star?: 2
1:
2:
3:
4: **
5:
PlayerTwo, From which row do you want to remove a star?: 4
1:
2:
3: 4: *
5:
PlayerOne, From which row do you want to remove a star?: 4
PlayerOne has won!
*Main>
-} |