52700.fb2
|
5 |
6 |
7 |
8 |
--
+----+----+----+----+
--
|
9 | 10 | 11 | 12 |
--
+----+----+----+----+
--
| 13 | 14 | 15 |
|
--
+----+----+----+----+
--
instance Show Game where
show (Game _ board) = ”\n” ++ space ++ line ++
(foldr (\a b -> a ++ space ++ line ++ b) ”\n” $ map column [0 .. 3])
where post id = showLabel $ board ! id
showLabel n
= cell $ show $ case n of
15 -> 0
n
-> n+1
cell ”0”
= ”
”
cell [x]
= ’ ’:’ ’: x :’ ’:[]
cell [a,b] = ’ ’: a : b :’ ’:[]
line = ”+----+----+----+----+\n”
nums = ((space ++ ”|”) ++ ) . foldr (\a b -> a ++ ”|” ++ b) ”\n” .
map post
column i = nums $ map (\x -> (i, x)) [0 .. 3]
space = ”\t”
Теперь мы можем загрузить модуль Loop в интерпретатор и набрать play. Немного отвлечёмся и поигра-
ем.
Prelude> :l Loop
[1 of 2] Compiling Game
( Game. hs, interpreted )
[2 of 2] Compiling Loop
( Loop. hs, interpreted )
Ok, modules loaded: Loop, Game.
*Loop> play
Привет! Это игра пятнашки
+----+----+----+----+