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

View file

@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 512c036467601196724c0e9412fc6175989ea1a8c48bf158d2cf62aa1c0b4311
-- hash: ae0d0337fef94e96da9aaca377817a7cbca70851243db8fe2a6c435fe06f0cfa
name: minesweeper
version: 0.1.0.0
@ -37,6 +37,7 @@ executable minesweeper
build-depends:
base
, minesweeper
, optparse-generic
, random
default-language: Haskell2010

View file

@ -24,6 +24,7 @@ executables:
dependencies:
- minesweeper
- random
- optparse-generic
tests:
minesweeper-test: