Place bombs randomly

This commit is contained in:
Paul-Henri Froidmont 2019-12-04 00:13:57 +01:00 committed by Paul-Henri Froidmont
parent 0b20ec98e3
commit fd1aef44ef
4 changed files with 36 additions and 11 deletions

View file

@ -1,12 +1,15 @@
module Main where module Main where
import Lib import Lib
import System.Environment
import System.Random
main :: IO () main :: IO ()
main = main = do
putStrLn $ args <- getArgs
printBoard let boardWidth = read (head args)
[ [Bomb Revealed, Value 1 Revealed, Value 0 Revealed] boardLength = read (args !! 1)
, [Value 1 Revealed, Value 1 Revealed, Value 0 Revealed] bombs = read (args !! 2)
, [Value 0 Revealed, Value 0 Revealed, Value 0 Revealed] generator <- newStdGen
] putStrLn . printBoard . revealAll . createBoard boardWidth boardLength $
generateRandomCoordinates (boardWidth-1) (boardLength-1) bombs generator

View file

@ -1,10 +1,10 @@
cabal-version: 1.12 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 -- see: https://github.com/sol/hpack
-- --
-- hash: 73d25bfaacbe65c083033b14864aa14b133058ca0e7c877622a08afb39ca460c -- hash: 512c036467601196724c0e9412fc6175989ea1a8c48bf158d2cf62aa1c0b4311
name: minesweeper name: minesweeper
version: 0.1.0.0 version: 0.1.0.0
@ -19,8 +19,12 @@ library
src/ src/
ghc-options: -Wall ghc-options: -Wall
build-depends: build-depends:
base array
, base
, containers , containers
, mtl
, random
, split
default-language: Haskell2010 default-language: Haskell2010
executable minesweeper executable minesweeper
@ -33,6 +37,7 @@ executable minesweeper
build-depends: build-depends:
base base
, minesweeper , minesweeper
, random
default-language: Haskell2010 default-language: Haskell2010
test-suite minesweeper-test test-suite minesweeper-test

View file

@ -12,13 +12,18 @@ library:
Lib Lib
dependencies: dependencies:
- containers - containers
- array
- split
- mtl
- random
executables: executables:
minesweeper: minesweeper:
source-dirs: app/ source-dirs: app/
main: Main.hs main: Main.hs
dependencies: dependencies:
minesweeper - minesweeper
- random
tests: tests:
minesweeper-test: minesweeper-test:

View file

@ -1,13 +1,18 @@
module Lib module Lib
( createBoard ( createBoard
, generateRandomCoordinates
, flagTile , flagTile
, revealTile , revealTile
, printBoard , printBoard
, revealAll
, Board , Board
, Tile(..) , Tile(..)
, TileState(..) , TileState(..)
) where ) where
import Data.List
import System.Random
type Board = [[Tile]] type Board = [[Tile]]
data Tile data Tile
@ -97,3 +102,10 @@ mapAroundTile :: (Coordinates -> Board -> Board) -> Coordinates -> Board -> Boar
mapAroundTile f (x, y) = mapAroundTile f (x, y) =
f (x - 1, y - 1) . 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) 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