Implement Show instance for Karel #3
1 changed files with 18 additions and 19 deletions
|
@ -15,6 +15,10 @@ data Karel = Karel
|
|||
}
|
||||
deriving (Eq)
|
||||
|
||||
xor :: Bool -> Bool -> Bool
|
||||
xor True a = not a
|
||||
xor False a = a
|
||||
|
||||
getHeader :: Karel -> String
|
||||
getHeader karel = unlines [
|
||||
printf " %8s %s %s %s" "POSITION" "FACING" "BEEP-BAG" "BEEP-POS",
|
||||
|
@ -50,32 +54,27 @@ mapCell karel x y
|
|||
| otherwise = " . "
|
||||
where beepersAt = getBeepersAt (x, y) $ karelWorld karel
|
||||
|
||||
|
||||
mapWall :: Karel -> Int -> Int -> String
|
||||
mapWall karel x y = leftPart <> rightPart
|
||||
where
|
||||
world = karelWorld karel
|
||||
(width, height) = worldDimensions world
|
||||
wallAtNorth = hasWallAt (x, y) North world
|
||||
vWallAbove = hasWallAt (x, y + 1) East world
|
||||
vWallBelow = hasWallAt (x, y - 1) East world
|
||||
vWallAt = hasWallAt (x, y) East world
|
||||
verticalWall = vWallAt && vWallAbove
|
||||
wallEnds = wallAtNorth && (not $ hasWallAt (x + 1, y) North world)
|
||||
wallStarts = wallAtNorth && x > 1 && (not $ hasWallAt (x - 1, y) North world)
|
||||
noVWall = not (hasWallAt (x, y) West world || hasWallAt (x, y + 1) West world)
|
||||
(northWall:eastWall:westWall:_) = map (\d -> hasWallAt (x, y) d world) [North, East, West]
|
||||
(northWallOnLeft:northWallOnRight:_) = map (\d -> hasWallAt (x + d, y) North world) [-1, 1]
|
||||
(eastWallAbove:westWallAbove:_) = map (\d -> hasWallAt (x, y + 1) d world) [East, West]
|
||||
verticalWall = eastWall && eastWallAbove
|
||||
wallEnds = northWall && not northWallOnRight
|
||||
wallStarts = northWall && x > 1 && not northWallOnLeft
|
||||
leftPart
|
||||
| wallStarts && noVWall = "+-"
|
||||
| wallAtNorth = "--"
|
||||
| wallStarts && not (westWall || westWallAbove) = "+-"
|
||||
| northWall = "--"
|
||||
| otherwise = " "
|
||||
rightPart
|
||||
| wallAtNorth && wallEnds && verticalWall = "-|"
|
||||
| wallEnds = "-+"
|
||||
| wallAtNorth && (vWallAbove) = "-+"
|
||||
| wallAtNorth = "--"
|
||||
| verticalWall && hasWallAt (x+1, y) North world = " +"
|
||||
| northWall && wallEnds && verticalWall = "-|"
|
||||
| wallEnds || (northWall && eastWallAbove) = "-+"
|
||||
| northWall = "--"
|
||||
| (verticalWall && northWallOnRight) || (eastWall `xor` eastWallAbove) = " +"
|
||||
| verticalWall = " |"
|
||||
| not vWallAbove && vWallAt = " +"
|
||||
| not vWallAt && vWallAbove = " +"
|
||||
| otherwise = " "
|
||||
|
||||
getRow :: Karel -> Int -> String
|
||||
|
@ -89,7 +88,7 @@ getRow karel row
|
|||
mapWall karel column row
|
||||
| column <- [1..width]
|
||||
]
|
||||
line = (printf "%2d |" row) <> foldl1 (<>) [
|
||||
line = printf "%2d |" row <> foldl1 (<>) [
|
||||
position <> wall
|
||||
| column <- [1..width],
|
||||
let wall = if hasWallAt (column, row) East world then "|" else " ",
|
||||
|
|
Loading…
Reference in a new issue