Created
February 1, 2015 13:32
-
-
Save choro3/8905a4c54cc5b85c8152 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import System.Environment | |
import Data.List | |
import Control.Applicative | |
data Field = Field (Int, Int) (Int, Int) | |
size :: Field -> (Int, Int) | |
size (Field (w, h) _) = (w, h) | |
pos :: Field -> (Int, Int) | |
pos (Field _ (x, y)) = (x, y) | |
moveTo :: Field -> (Int, Int) -> Field | |
moveTo (Field (w, h) (x, y)) (nx, ny) | |
| nx < 0 || nx >= w = (Field (w, h) (x, y)) | |
| ny < 0 || ny >= h = (Field (w, h) (x, y)) | |
| otherwise = (Field (w, h) (nx, ny)) | |
showField :: Field -> String | |
showField (Field (w, h) (x, y)) = | |
concat $ replRow $ replicate h $ (replicate w '.') ++ "\n" | |
where | |
replCell row = map (\(c, i) -> if i == x then '@' else c) $ zip row [0..w] | |
replRow lst = map (\(r, i) -> if i == y then (replCell r) else r) $ zip lst [0..h - 1] | |
move :: Field -> String -> Field | |
move field "j" = moveTo field (fst $ pos field, (snd $ pos field) + 1) | |
move field "l" = moveTo field ((fst $ pos field) + 1, snd $ pos field) | |
move field "h" = moveTo field ((fst $ pos field) - 1, snd $ pos field) | |
move field "k" = moveTo field (fst $ pos field, (snd $ pos field) - 1) | |
move field _ = field | |
-- main = forever $ interact $ showField . (move (Field (20, 20) (0, 0))) | |
mainLoop field = do | |
putStrLn $ showField field | |
newField <- move field <$> getLine | |
mainLoop newField | |
main = mainLoop $ Field (20, 20) (0, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment