Class MineMap


  • public class MineMap
    extends Object
    The representation of a mine map. Allows random generation of mines as well as copying from other mine maps. The main operations to be used by AIs are pickASpot and flagASpot. These operations are expected to be used in a sequential fashion (no multithreaded access is allowed to them). Apart from these two important methods, a minemap also contains some functionality for limiting AI's to speeds perceivable in UIs.
    Author:
    "Gabor Kecskemeti, Department of Computer Science, Liverpool John Moores University, (c) 2019"
    • Constructor Summary

      Constructors 
      Constructor Description
      MineMap​(int rows, int cols, double mineRatio, int uidelay)
      Enables the creation of completely randomly generated MineMaps
      MineMap​(MineMap otherToCopy)
      This copy constructor acts like MineMap(MineMap, boolean) but always assumes that the copy is not allowed to be copied anymore.
      MineMap​(MineMap otherToCopy, boolean allowCopy)
      The copy constructor of the MineMap.
      MineMap​(MineMap otherToCopy, int newUIdelay)
      This copy constructor acts like MineMap(MineMap, int, boolean) but always assumes that the copy is not allowed to be copied anymore.
      MineMap​(MineMap otherToCopy, int newUIdelay, boolean allowCopy)
      A slight variant of the copy constructor (MineMap(MineMap)).
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean checkOutOfRange​(int rowCoord, int colCoord)
      Allows to determine if a given spot lies outside the perimeter of this map or not.
      private void checkRightToCopy​(MineMap otherToCopy, boolean allowCopy)
      This method is a helper for the copy constructors and ensures that only those copies are done which are allowed to be copied.
      private void copyHelper​(MineMap otherToCopy)
      Makes sure the deep copy phase of the copy constructors is done well
      private void delayForUI()
      Allows an introduction of some delay in the minemap automated operations.
      void flagASpot​(int rowCoord, int colCoord)
      This is one of the main interaction points for AIs.
      int getCorrectlyIdentifiedMineCount()
      Determines how many flags were placed on mines
      int getExploredAreaSize()
      Tells how big of an area have been picked or flagged in this map
      private int getFlaggedMineCount()
      Calculates the number of mines that have a flag on them
      int getInCorrectlyIdentifiedMineCount()
      Determines how many flags were placed on spots without mines
      int getLastCol()
      Queries the X position of the last picked/flagged spot on the map
      int getLastRow()
      Queries the Y position of the last picked/flagged spot on the map
      ExploredSpot getPos​(int rowCoord, int colCoord)
      This is one of the main interaction points for AIs.
      boolean isEnded()
      Determines if the game has been played to its final steps or not.
      boolean isWon()
      Determines if the game has been won.
      private void markPicked​(int rowCoord, int colCoord)
      Helper method to change the last touched coordinate (this is usually used by flagASpot/pickASpot.
      boolean pickASpot​(int rowCoord, int colCoord)
      This is one of the main interaction points for AIs.
      private void sweepAround​(int rowCoord, int colCoord)
      Helper for the tracing and mine counting operations.
      private void traceFrom​(int rowCoord, int colCoord)
      Allows discovering the largest extent of unexplored, but completely safe (i.e., spots with no mines on them) area
    • Field Detail

      • r

        public static final Random r
      • completeMap

        private final Spot[][] completeMap
      • howManyAround

        private final int[][] howManyAround
      • rows

        public final int rows
        The number of rows and columns in this minefield.
      • cols

        public final int cols
        The number of rows and columns in this minefield.
      • fieldSize

        public final int fieldSize
        The total area of the minefield (in square spots).
      • mineCount

        public final int mineCount
        Very helpful to tell how many mines are contained in a given map. This knowledge is often times essential for mine sweeper AIs.
      • uidelay

        public final int uidelay
      • gameEnded

        private boolean gameEnded
      • preparedMap

        private boolean preparedMap
      • allowCopy

        private boolean allowCopy
      • lastCol

        private int lastCol
      • lastRow

        private int lastRow
    • Constructor Detail

      • MineMap

        public MineMap​(int rows,
                       int cols,
                       double mineRatio,
                       int uidelay)
        Enables the creation of completely randomly generated MineMaps
        Parameters:
        rows - How many rows should the map have.
        cols - How many columns should the map have.
        mineRatio - What's the percentage of the mines to the total number of spots in the whole map.
        uidelay - How long should we wait before each AI operation takes action.
      • MineMap

        public MineMap​(MineMap otherToCopy,
                       boolean allowCopy)
                throws MineMap.MapCopyException
        The copy constructor of the MineMap. It allows to make an exact copy of another MineMap. This is helpful if one wants to test multiple algorithms with the exact same maps. It is important that this map copies the exact state. Thus it is possible to make a copy of a half solved mine map and allow an alternative solver to tackle the rest of the map independently from the original.
        Parameters:
        otherToCopy - The original map to copy from.
        allowCopy - Specify if we allow further copies of this map. True if we allow them, false otherwise.
        Throws:
        MineMap.MapCopyException - If the map in the first parameter is not supposed to be copied.
      • MineMap

        public MineMap​(MineMap otherToCopy,
                       int newUIdelay,
                       boolean allowCopy)
                throws MineMap.MapCopyException
        A slight variant of the copy constructor (MineMap(MineMap)). This variant allows uiDelays to be changed while the rest of the map is copied just like in the other case. For relevant use cases check the description of the other copy constructor as well.
        Parameters:
        otherToCopy - The original map to copy from.
        allowCopy - Specify if we allow further copies of this map. True if we allow them, false otherwise.
        newUIdelay - The alternative UI delay to be used with this map instance
        Throws:
        MineMap.MapCopyException - If the map in the first parameter is not supposed to be copied.
    • Method Detail

      • checkRightToCopy

        private void checkRightToCopy​(MineMap otherToCopy,
                                      boolean allowCopy)
                               throws MineMap.MapCopyException
        This method is a helper for the copy constructors and ensures that only those copies are done which are allowed to be copied. It also maintains the class's allowCopy data member.
        Parameters:
        otherToCopy - The source of the copying
        allowCopy - if the copy is allowed then this value will be the new copy's allowCopy data member. True if further copies are allowed, false otherwise.
        Throws:
        MineMap.MapCopyException - If the source is not allowed to be copied.
      • copyHelper

        private void copyHelper​(MineMap otherToCopy)
        Makes sure the deep copy phase of the copy constructors is done well
        Parameters:
        otherToCopy - The original map to copy from.
      • checkOutOfRange

        public boolean checkOutOfRange​(int rowCoord,
                                       int colCoord)
        Allows to determine if a given spot lies outside the perimeter of this map or not.
        Parameters:
        rowCoord - The row coordinate to test
        colCoord - The column coordinate to test
        Returns:
        true if the spot asked about is not within the range of this map. false otherwise.
      • sweepAround

        private void sweepAround​(int rowCoord,
                                 int colCoord)
        Helper for the tracing and mine counting operations. Allows the tracing and mine counting to take affect in the immediate vicinity of a given spot.
        Parameters:
        rowCoord - The row coordinate of the spot to be looked around.
        colCoord - The column coordinate of the spot to be looked around.
      • markPicked

        private void markPicked​(int rowCoord,
                                int colCoord)
        Helper method to change the last touched coordinate (this is usually used by flagASpot/pickASpot.
        Parameters:
        rowCoord - The row coordinate of the spot that changed last time
        colCoord - The column coordinate of the spot that changed last time
      • flagASpot

        public void flagASpot​(int rowCoord,
                              int colCoord)
        This is one of the main interaction points for AIs. Allows telling the map that the AI thinks this spot is unsafe (i.e., there is a suspected mine underneath it). It also allows the AI to change its mind: a flagged spot can always be turned back to unexplored.
        Parameters:
        rowCoord - the row coordinate of a suspected spot
        colCoord - the column coordinate of a suspected spot
      • delayForUI

        private void delayForUI()
        Allows an introduction of some delay in the minemap automated operations. This makes sure the UI can catch up with all the updates the AI does.
      • pickASpot

        public boolean pickASpot​(int rowCoord,
                                 int colCoord)
        This is one of the main interaction points for AIs. Allows telling the map that the AI thinks this part is safe to step on and expects to reveal what is underneath. If the spot chosen has 0 adjacent mines, then the surrounding area is automatically explored. This method only functions if the game has not ended yet or if the specified x and y coordinates are within the map.
        Parameters:
        rowCoord - the rows address coordinate of the spot to be uncovered
        colCoord - the coloumn address coordinate of the spot to be uncovered
        Returns:
        true if the game has ended/if there is no reason to call pick a spot again. false otherwise.
      • traceFrom

        private void traceFrom​(int rowCoord,
                               int colCoord)
        Allows discovering the largest extent of unexplored, but completely safe (i.e., spots with no mines on them) area
        Parameters:
        rowCoord - the row coordinate where the tracing should happen from
        colCoord - the column coordinate where the tracing should happen from
      • isEnded

        public boolean isEnded()
        Determines if the game has been played to its final steps or not. If this returns true, the pickASpot and flagASpot methods don't function anymore!
        Returns:
        true if the game has no more moves. false otherwise.
      • isWon

        public boolean isWon()
        Determines if the game has been won.
        Returns:
        true if the game is won, false if the game is still ongoing or the game is lost
      • getExploredAreaSize

        public int getExploredAreaSize()
        Tells how big of an area have been picked or flagged in this map
        Returns:
        the number of spots that have been interacted with
      • getFlaggedMineCount

        private int getFlaggedMineCount()
        Calculates the number of mines that have a flag on them
        Returns:
        the number of correctly flagged mines
      • getCorrectlyIdentifiedMineCount

        public int getCorrectlyIdentifiedMineCount()
        Determines how many flags were placed on mines
        Returns:
        the number of flags correctly placed or -1 if the game has not ended yet
      • getInCorrectlyIdentifiedMineCount

        public int getInCorrectlyIdentifiedMineCount()
        Determines how many flags were placed on spots without mines
        Returns:
        Either the number of flags incorrectly placed or -1 (if the game is still ongoing)
      • getPos

        public ExploredSpot getPos​(int rowCoord,
                                   int colCoord)
                            throws ArrayIndexOutOfBoundsException
        This is one of the main interaction points for AIs. Allows to query the explored map internally maintained by this MineMap.
        Parameters:
        rowCoord - the row coordinate of the requested spot
        colCoord - the column coordinate of the requested spot
        Returns:
        The details about the given spot in the explored map
        Throws:
        ArrayIndexOutOfBoundsException - if the requested position is not within the the map, if you want to avoid this exception you can check if your coordinates are within range with checkOutOfRange method.
      • getLastCol

        public int getLastCol()
        Queries the X position of the last picked/flagged spot on the map
        Returns:
      • getLastRow

        public int getLastRow()
        Queries the Y position of the last picked/flagged spot on the map
        Returns: