From 65c8035820243873ce0670b61f4c2986e152333e Mon Sep 17 00:00:00 2001 From: Jack Harley Date: Thu, 28 Jan 2021 14:17:59 +0000 Subject: [PATCH] init --- .gitignore | 1 + Minesweeper.cabal | 26 ++++++++++++++++++ README.md | 13 +++++++++ Setup.hs | 2 ++ hie.yaml | 2 ++ package.yaml | 23 ++++++++++++++++ src/Main.hs | 55 ++++++++++++++++++++++++++++++++++++++ stack.yaml | 67 +++++++++++++++++++++++++++++++++++++++++++++++ stack.yaml.lock | 13 +++++++++ 9 files changed, 202 insertions(+) create mode 100644 .gitignore create mode 100644 Minesweeper.cabal create mode 100644 README.md create mode 100644 Setup.hs create mode 100644 hie.yaml create mode 100644 package.yaml create mode 100644 src/Main.hs create mode 100644 stack.yaml create mode 100644 stack.yaml.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c1d9b4c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.stack-work \ No newline at end of file diff --git a/Minesweeper.cabal b/Minesweeper.cabal new file mode 100644 index 0000000..35ea5a8 --- /dev/null +++ b/Minesweeper.cabal @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..9b4b1b7 --- /dev/null +++ b/README.md @@ -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. \ No newline at end of file diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/hie.yaml b/hie.yaml new file mode 100644 index 0000000..142e69f --- /dev/null +++ b/hie.yaml @@ -0,0 +1,2 @@ +cradle: + stack: \ No newline at end of file diff --git a/package.yaml b/package.yaml new file mode 100644 index 0000000..e73c7b5 --- /dev/null +++ b/package.yaml @@ -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 \ No newline at end of file diff --git a/src/Main.hs b/src/Main.hs new file mode 100644 index 0000000..cd81e70 --- /dev/null +++ b/src/Main.hs @@ -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 diff --git a/stack.yaml b/stack.yaml new file mode 100644 index 0000000..40f511c --- /dev/null +++ b/stack.yaml @@ -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 diff --git a/stack.yaml.lock b/stack.yaml.lock new file mode 100644 index 0000000..8c81e60 --- /dev/null +++ b/stack.yaml.lock @@ -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