Place bombs randomly
This commit is contained in:
parent
0b20ec98e3
commit
fd1aef44ef
4 changed files with 36 additions and 11 deletions
17
app/Main.hs
17
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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
12
src/Lib.hs
12
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue