2019-11-24 02:49:57 +01:00
|
|
|
module Main where
|
|
|
|
|
|
|
|
|
|
import Lib
|
2019-12-04 00:13:57 +01:00
|
|
|
import System.Environment
|
|
|
|
|
import System.Random
|
2019-12-07 02:51:12 +01:00
|
|
|
import Text.Read
|
2019-11-24 02:49:57 +01:00
|
|
|
|
|
|
|
|
main :: IO ()
|
2019-12-04 00:13:57 +01:00
|
|
|
main = do
|
|
|
|
|
args <- getArgs
|
|
|
|
|
let boardWidth = read (head args)
|
|
|
|
|
boardLength = read (args !! 1)
|
|
|
|
|
bombs = read (args !! 2)
|
|
|
|
|
generator <- newStdGen
|
2019-12-07 02:51:12 +01:00
|
|
|
gameStep . createBoard boardWidth boardLength $
|
|
|
|
|
generateRandomCoordinates (boardWidth - 1) (boardLength - 1) bombs 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!"
|