Skip to content

Commit

Permalink
Only a single goal generated per map.
Browse files Browse the repository at this point in the history
  • Loading branch information
steveklabnik committed Sep 25, 2010
1 parent 4b59493 commit c38b7ce
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions behind.hs
Expand Up @@ -37,17 +37,31 @@ data World = MakeWorld [[Cell]] Player
initWorld :: StdGen -> IO World
initWorld gen = do
(width, gen) <- randomNum 3 75 gen
(height, gen') <- randomNum 3 23 gen
(height, gen) <- randomNum 3 23 gen
(goalX, gen) <- randomNum 2 width gen
(goalY, gen) <- randomNum 2 height gen
cells <- return $ map (genCell) (randoms gen :: [Int])
return $ MakeWorld (map (\i -> take width (drop (i * width) cells)) [0,1 .. height]) initPlayer
board <- return $ (map (\i -> take width (drop (i * width) cells)) [0,1 .. height])
board <- return $ addGoal goalX goalY board
return $ MakeWorld board initPlayer
where
genCell :: Int -> Cell
genCell n = case x of
0 -> Wall
3 -> Goal
otherwise -> Empty
where x = mod n 4


addGoal :: Int -> Int -> [[Cell]] -> [[Cell]]
addGoal x y (b:board)
| y == 1 = (addGoal' x b) : board
| otherwise = b : addGoal x (y-1) board

addGoal' :: Int -> [Cell] -> [Cell]
addGoal' x (r:row)
| x == 1 = Goal : row
| otherwise = r : addGoal' (x-1) row


type GeneratorState = State StdGen

randomGen :: Int -> Int -> GeneratorState Int
Expand Down

0 comments on commit c38b7ce

Please sign in to comment.