As a way to learn Haskell I decided to make a Minesweeper clone. I have already written some code and the main structure of the program is ready. It is based on:
- A
Board
, containing theamount
of mines, the list offields
and thewidth
andheight
of the map. - A
Field
, containing the number of adjacent mines or a value indicating that the field is a mine itself. TheField
also contains aFieldState
(Marked
,Hidden
orShown
).
The game loop looks like the following:
play board = do
-- Clear the screen
putStrLn $ replicate 40 '\n'
-- Show the board
print board
-- Get and process user input
putStr "Command: "
-- We are generating a new board to reflect the changes!
newBoard <- parseInput board <$> getLine
-- If the game is not ended, go to next step
if gameEnded newBoard
then return $ result newBoard
else play newBoard
This works quite good right now, but I don't like the fact that after each turn I have to generate a new Board
. It looks inefficient coming from an imperative language like C++, where you would just need to mutate the existing board.
To the point:
- Is this the correct way to do it?
- Will it be optimized by the compiler?