In the auto-generated code for many Haskell puzzels, you can’t access the initialization input (the input before turn 1) from the main loop.
Suggested fix: replace the loop
function with a forever $ do
block. All other languages use a while (true)
for the game loop, and the forever
function is a better way to emulate that than explicit recursion.
Example (Taken from Don’t panic):
Replace this:
main :: IO ()
main = do
-- ...
input_line <- getLine
let input = words input_line
let nbfloors = read (input!!0) :: Int -- number of floors
-- ...
let nbelevators = read (input!!7) :: Int -- number of elevators
-- ...
loop
loop :: IO ()
loop = do
input_line <- getLine
let input = words input_line
let clonefloor = read (input!!0) :: Int -- floor of the leading clone
let clonepos = read (input!!1) :: Int -- position of the leading clone on its floor
let direction = input!!2 -- direction of the leading clone: LEFT or RIGHT
-- TODO Can't access initialization variables. For example:
hPrint stderr nbfloors -- XXX can't access nbfloors
-- action: WAIT or BLOCK
putStrLn "WAIT"
loop
With this:
main :: IO ()
main = do
-- ...
input_line <- getLine
let input = words input_line
let nbfloors = read (input!!0) :: Int -- number of floors
-- ...
let nbelevators = read (input!!7) :: Int -- number of elevators
-- ...
forever $ do
input_line <- getLine
let input = words input_line
let clonefloor = read (input!!0) :: Int -- floor of the leading clone
let clonepos = read (input!!1) :: Int -- position of the leading clone on its floor
let direction = input!!2 -- direction of the leading clone: LEFT or RIGHT
-- TODO Now we can access initialization vars:
hPrint stderr nbfloors
-- action: WAIT or BLOCK
putStrLn "WAIT"