Improve structure using Polysemy
This commit is contained in:
parent
0ddc766aea
commit
2c6fba8312
6 changed files with 133 additions and 53 deletions
42
app/Main.hs
42
app/Main.hs
|
|
@ -7,10 +7,12 @@
|
|||
|
||||
module Main where
|
||||
|
||||
import Lib
|
||||
import Data.Function ((&))
|
||||
import Minesweeper
|
||||
import Options.Generic
|
||||
import System.Random
|
||||
import Text.Read
|
||||
import Polysemy
|
||||
import Random
|
||||
import Terminal
|
||||
|
||||
data Parameters w =
|
||||
Parameters
|
||||
|
|
@ -27,36 +29,4 @@ deriving instance Show (Parameters Unwrapped)
|
|||
main :: IO ()
|
||||
main = do
|
||||
(Parameters boardWidth boardLength bombsCount) <- unwrapRecord "Minesweeper"
|
||||
generator <- newStdGen
|
||||
gameStep . createBoard boardWidth boardLength $
|
||||
generateRandomCoordinates (boardWidth - 1) (boardLength - 1) bombsCount generator
|
||||
|
||||
gameStep :: Board -> IO ()
|
||||
gameStep board = do
|
||||
putStrLn . convertBoardToString $ board
|
||||
coordinates <- getCoordinates
|
||||
let nextBoard = revealTile coordinates board
|
||||
if isGameLost nextBoard
|
||||
then gameLost nextBoard
|
||||
else if isGameWon nextBoard
|
||||
then gameWon nextBoard
|
||||
else gameStep nextBoard
|
||||
|
||||
getCoordinates :: IO (Int, Int)
|
||||
getCoordinates = do
|
||||
putStrLn "Enter the coordinates of the tile to reveal (X then Y, zero indexed):"
|
||||
xString <- getLine
|
||||
yString <- getLine
|
||||
case (readMaybe xString, readMaybe yString) of
|
||||
(Just x, Just y) -> return (x, y)
|
||||
_ -> putStrLn "Invalid coordinates!" >> getCoordinates
|
||||
|
||||
gameLost :: Board -> IO ()
|
||||
gameLost board = do
|
||||
putStrLn . convertBoardToString . revealAll $ board
|
||||
putStrLn "Boom! You lost!"
|
||||
|
||||
gameWon :: Board -> IO ()
|
||||
gameWon board = do
|
||||
putStrLn . convertBoardToString $ board
|
||||
putStrLn "You won! All the bombs were found!"
|
||||
runMinesweeper boardWidth boardLength bombsCount & runTerminalIO & runRandomIO & runM
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue