Skip to content

Commit

Permalink
More descriptive names
Browse files Browse the repository at this point in the history
  • Loading branch information
norm2782 committed Aug 24, 2010
1 parent 9969baf commit d97cf44
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions HaskBan.hs
Expand Up @@ -34,21 +34,21 @@ module HaskBan (main) where
progLoop

keyPressed :: Key -> SokobanState()
keyPressed k = do map <- getMap
pos <- getPlayerPosition
let t = getTranslation k
if canMoveTo map pos t
then do movePlayer map t
if isBox (t pos) map
then moveBox map (t pos) t
else return ()
else return ()
keyPressed key = do map <- getMap
pos <- getPlayerPosition
let transl = getTranslation key
if canMoveTo map pos transl
then do movePlayer map transl
if isBox (transl pos) map
then moveBox map (transl pos) transl
else return ()
else return ()

getTranslation :: Key -> Translation
getTranslation k | k == KeyUp || k == (KeyChar 'k') = translateUp
| k == KeyDown || k == (KeyChar 'j') = translateDown
| k == KeyLeft || k == (KeyChar 'h') = translateLeft
| k == KeyRight || k == (KeyChar 'l') = translateRight
getTranslation key | key == KeyUp || key == (KeyChar 'k') = translateUp
| key == KeyDown || key == (KeyChar 'j') = translateDown
| key == KeyLeft || key == (KeyChar 'h') = translateLeft
| key == KeyRight || key == (KeyChar 'l') = translateRight

translateUp :: Translation
translateUp (x, y) = (x, y - 1)
Expand All @@ -72,10 +72,10 @@ module HaskBan (main) where
isPath = isCellType Path

isCellType :: CellType -> Point -> SokoMap -> Bool
isCellType c p m = getCellType p m == c
isCellType cType point sMap = getCellType point sMap == cType

getCellType :: Point -> SokoMap -> CellType
getCellType p m = m ! p
getCellType point sMap = sMap ! point

getPlayerPosition :: SokobanState Point
getPlayerPosition = player `liftM` get
Expand All @@ -90,8 +90,8 @@ module HaskBan (main) where
putMap map = get >>= \state -> put (state {sokoMap = map})

movePlayer :: SokoMap -> Translation -> SokobanState ()
movePlayer g t = liftM t getPlayerPosition >>= \position ->
when (canMoveTo g position t) (putPlayerPosition position)
movePlayer sMap transl = liftM transl getPlayerPosition >>= \position ->
when (canMoveTo sMap position transl) (putPlayerPosition position)

moveBox :: SokoMap -> Point -> Translation -> SokobanState()
moveBox = undefined
Expand All @@ -100,9 +100,9 @@ module HaskBan (main) where
-- In case the new pointis a box, the next position needs to be
-- checked as well. Hence, the original translation function is provided as well.
canMoveTo :: SokoMap -> Point -> Translation -> Bool
canMoveTo g p t | isPath p g = True
| isBox p g && not (isWall (t p) g) = True
| otherwise = False
canMoveTo sMap point transl | isPath point sMap = True
| isBox point sMap && not (isWall (transl point) sMap) = True
| otherwise = False

shouldTerminate :: Key -> Bool
shouldTerminate (KeyChar '\ESC') = True
Expand Down

0 comments on commit d97cf44

Please sign in to comment.