This commit is contained in:
Jack Harley 2021-01-28 14:17:59 +00:00
commit 65c8035820
9 changed files with 202 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.stack-work

26
Minesweeper.cabal Normal file
View File

@ -0,0 +1,26 @@
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.34.3.
--
-- see: https://github.com/sol/hpack
name: Minesweeper
version: 1.0.0.0
description: CSU44012 Assignment 2
author: Jack Harley
maintainer: jackpharley@gmail.com
copyright: 2020 Jack Harley
license: AllRightsReserved
build-type: Simple
executable Minesweeper
main-is: Main.hs
other-modules:
Paths_Minesweeper
hs-source-dirs:
src
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base
, random
default-language: Haskell2010

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# Shape Server
Author: Jack Harley jackpharley@gmail.com / jharley@tcd.ie
This is my submission for Assignment 1 for CSU44012 Topics in Functional Programming 2020/21
My report containing background information, design choices, reflection, etc. is submitted separately as a PDF to Blackboard.
Build/Run
----------------------
Build the project with `stack build` and execute with `stack exec ShapeServer`.
Then visit http://localhost:3000/ in a web browser.

2
Setup.hs Normal file
View File

@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

2
hie.yaml Normal file
View File

@ -0,0 +1,2 @@
cradle:
stack:

23
package.yaml Normal file
View File

@ -0,0 +1,23 @@
name: Minesweeper
version: 1.0.0.0
license: AllRightsReserved
author: Jack Harley
maintainer: jackpharley@gmail.com
copyright: 2020 Jack Harley
description: CSU44012 Assignment 2
dependencies:
- base
- random
#extra-source-files:
executables:
Minesweeper:
main: Main.hs
source-dirs: src
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N

55
src/Main.hs Normal file
View File

@ -0,0 +1,55 @@
module Main where
import System.Random
main :: IO ()
main = do
rng <- newStdGen
putStrLn (printBoard $ createBoard 12 0.2 rng)
type Square = (Int, Int)
type Grid = [[Bool]]
data Board = Board { size :: Int
, mines :: Grid
, uncovered :: Grid
, flagged :: Grid
} deriving (Show)
-- Creates a board given a size (width/height), mine ratio and random generator
createBoard :: Int -> Float -> StdGen -> Board
createBoard size mineRatio rng = Board size (seedGrid rng mineRatio (createGrid size)) (createGrid size) (createGrid size)
-- Creates a 2D list of booleans of given size, initialised to False
createGrid :: Int -> Grid
createGrid size = replicate size (replicate size False)
-- Seeds True statuses to a 2D grid randomly with a given probability, requires a random generator
seedGrid :: StdGen -> Float -> Grid -> Grid
seedGrid _ _ [] = []
seedGrid rng p (l:ls) = newL : seedGrid newRng p ls
where (newL, newRng) = seedList rng p l
seedList :: StdGen -> Float -> [Bool] -> ([Bool], StdGen)
seedList rng p (l:ls) = (newBool : seedListDiscard newRng p ls, newRng)
where (newBool, newRng) = randomlyTrue rng p
seedListDiscard :: StdGen -> Float -> [Bool] -> [Bool]
seedListDiscard _ _ [] = []
seedListDiscard rng p (l:ls) = newBool : seedListDiscard newRng p ls
where (newBool, newRng) = randomlyTrue rng p
randomlyTrue :: StdGen -> Float -> (Bool, StdGen)
randomlyTrue rng p = let (generatedFloat, newRng) = randomR (0.0, 1.0) rng
in (generatedFloat <= p, newRng)
printBoard :: Board -> String
printBoard b = printBoardGrid (mines b)
printBoardGrid :: Grid -> String
printBoardGrid [] = ""
printBoardGrid (l:ls) = printBoardLine l ++ "\n" ++ printBoardGrid ls
printBoardLine :: [Bool] -> String
printBoardLine [] = ""
printBoardLine (x:xs) = (if x then " x" else " .") ++ printBoardLine xs

67
stack.yaml Normal file
View File

@ -0,0 +1,67 @@
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# resolver: lts-3.5
# resolver: nightly-2015-09-21
# resolver: ghc-7.10.2
#
# The location of a snapshot can be provided as a file or url. Stack assumes
# a snapshot provided as a file might change, whereas a url resource does not.
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
resolver:
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/24.yaml
# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
# subdirs:
# - auto-update
# - wai
packages:
- .
# Dependency packages to be pulled from upstream that are not in the resolver.
# These entries can reference officially published versions as well as
# forks / in-progress versions pinned to a git hash. For example:
#
# extra-deps:
# - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#
# extra-deps: []
# Override default flag values for local packages and extra-deps
# flags: {}
# Extra package databases containing global packages
# extra-package-dbs: []
# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=2.5"
#
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor

13
stack.yaml.lock Normal file
View File

@ -0,0 +1,13 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files
packages: []
snapshots:
- completed:
size: 532835
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/24.yaml
sha256: cf2b52420b2262fe9cf0f6744929120131abd6675b1c3fb2d8b155a47f80d103
original:
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/24.yaml