Add nice, documented CLI parameters

This commit is contained in:
Paul-Henri Froidmont 2019-12-10 00:09:55 +01:00 committed by Paul-Henri Froidmont
parent 9a1cf149df
commit 0ddc766aea
3 changed files with 27 additions and 9 deletions

View file

@ -1,19 +1,35 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeOperators #-}
module Main where
import Lib
import System.Environment
import Options.Generic
import System.Random
import Text.Read
data Parameters w =
Parameters
{ width :: w ::: Int <?> "Width of the board"
, length :: w ::: Int <?> "Length of the board"
, bombs :: w ::: Int <?> "Number of bombs to be placed"
}
deriving (Generic)
instance ParseRecord (Parameters Wrapped)
deriving instance Show (Parameters Unwrapped)
main :: IO ()
main = do
args <- getArgs
let boardWidth = read (head args)
boardLength = read (args !! 1)
bombs = read (args !! 2)
(Parameters boardWidth boardLength bombsCount) <- unwrapRecord "Minesweeper"
generator <- newStdGen
gameStep . createBoard boardWidth boardLength $
generateRandomCoordinates (boardWidth - 1) (boardLength - 1) bombs generator
generateRandomCoordinates (boardWidth - 1) (boardLength - 1) bombsCount generator
gameStep :: Board -> IO ()
gameStep board = do
@ -32,8 +48,8 @@ getCoordinates = do
xString <- getLine
yString <- getLine
case (readMaybe xString, readMaybe yString) of
(Just x, Just y) -> return (x,y)
_ -> putStrLn "Invalid coordinates!" >> getCoordinates
(Just x, Just y) -> return (x, y)
_ -> putStrLn "Invalid coordinates!" >> getCoordinates
gameLost :: Board -> IO ()
gameLost board = do