diff --git a/app/Main.hs b/app/Main.hs index a34a838..8b5f6a2 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,12 +1,15 @@ module Main where import Lib +import System.Environment +import System.Random main :: IO () -main = - putStrLn $ - printBoard - [ [Bomb Revealed, Value 1 Revealed, Value 0 Revealed] - , [Value 1 Revealed, Value 1 Revealed, Value 0 Revealed] - , [Value 0 Revealed, Value 0 Revealed, Value 0 Revealed] - ] +main = do + args <- getArgs + let boardWidth = read (head args) + boardLength = read (args !! 1) + bombs = read (args !! 2) + generator <- newStdGen + putStrLn . printBoard . revealAll . createBoard boardWidth boardLength $ + generateRandomCoordinates (boardWidth-1) (boardLength-1) bombs generator diff --git a/minesweeper.cabal b/minesweeper.cabal index 71b96ea..140acb1 100644 --- a/minesweeper.cabal +++ b/minesweeper.cabal @@ -1,10 +1,10 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.31.1. +-- This file has been generated from package.yaml by hpack version 0.31.2. -- -- see: https://github.com/sol/hpack -- --- hash: 73d25bfaacbe65c083033b14864aa14b133058ca0e7c877622a08afb39ca460c +-- hash: 512c036467601196724c0e9412fc6175989ea1a8c48bf158d2cf62aa1c0b4311 name: minesweeper version: 0.1.0.0 @@ -19,8 +19,12 @@ library src/ ghc-options: -Wall build-depends: - base + array + , base , containers + , mtl + , random + , split default-language: Haskell2010 executable minesweeper @@ -33,6 +37,7 @@ executable minesweeper build-depends: base , minesweeper + , random default-language: Haskell2010 test-suite minesweeper-test diff --git a/package.yaml b/package.yaml index 5d2173c..bf66d5f 100644 --- a/package.yaml +++ b/package.yaml @@ -12,13 +12,18 @@ library: Lib dependencies: - containers + - array + - split + - mtl + - random executables: minesweeper: source-dirs: app/ main: Main.hs dependencies: - minesweeper + - minesweeper + - random tests: minesweeper-test: diff --git a/src/Lib.hs b/src/Lib.hs index baf23fd..98a51c5 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -1,13 +1,18 @@ module Lib ( createBoard + , generateRandomCoordinates , flagTile , revealTile , printBoard + , revealAll , Board , Tile(..) , TileState(..) ) where +import Data.List +import System.Random + type Board = [[Tile]] data Tile @@ -97,3 +102,10 @@ mapAroundTile :: (Coordinates -> Board -> Board) -> Coordinates -> Board -> Boar mapAroundTile f (x, y) = f (x - 1, y - 1) . f (x, y - 1) . f (x + 1, y - 1) . f (x - 1, y) . f (x + 1, y) . f (x - 1, y + 1) . f (x, y + 1) . f (x + 1, y + 1) + +generateRandomCoordinates :: Int -> Int -> Int -> StdGen -> [Coordinates] +generateRandomCoordinates maxX maxY count generator = take count . nub $ zip randomXs randomYs + where + (xGen, yGen) = split generator + randomXs = randomRs (0, maxX) xGen + randomYs = randomRs (0, maxY) yGen