Advertisement
TeRackSito

Docstring

Apr 20th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | Source Code | 0 0
  1. """
  2.    generate_map function
  3.  
  4.    Generates a MineSweeper map within given parameters using a custom algorithm.
  5.  
  6.    This generator has implemented some debugging and anti-hung tools to make computationally
  7.    secure generations and easily modifiable algorithms.
  8.  
  9.    Algorithm
  10.    ---------
  11.  
  12.        This algorithm `does not check if` the generated map `has at least one solution`,
  13.        as in the original game. Instead, it checks a series of conditions for each mine placed using
  14.        recursive methods.
  15.  
  16.        For each mine to be placed, the steps below are followed:
  17.  
  18.        - First, a `cell is randomly chosen` considering the entire grid. Fails and tries again if the cell is not empty.
  19.  
  20.        - Second, the `range of all contiguous cells` to the chosen cell is computed. Then each contiguous cell
  21.        is iterated and checked. `Two main conditions` are checked, the number of surrounding `valid empty cells` and
  22.        if every surrounding `mine is still valid` if the current one were placed. If one of the conditions fails,
  23.        then the cell is discarded as invalid and failed attempts counter increases by one.
  24.  
  25.        - A `valid empty cell` is one that is surrounded by at least `three empty cells`. Note that, due to recursive
  26.        limit, the surrounding empty cells to the empty cell being checked are not also checked. This means that the
  27.        surrounding empty cells to the empty cell being checked could not have at least three surrounding empty cells.
  28.  
  29.        - A `valid mine` is one that has at least three surrounding `valid empty cells`. Note that, when three surrounding
  30.        valid empty cells are already found while checking a mine, then no more empty cells will be checked,
  31.        but mines will continue to be searched to validate.
  32.  
  33.        - When a `mine` is found while checking the surrounding cells, then that mine will be checked recursively having as a
  34.        parent the mine before it. If another mine is found, then again it will be checked recursively having as parents the mines
  35.        before it. This recursive search will stop if more than four cells are found straight, reaching the concatenated mine limit.
  36.        Then the main parent cell that is being checked will be discarded as invalid for new mines.
  37.        Note that the algorithm does not register which cells were discarded as invalid.
  38.        So, if the same cell is chosen again, the recursive search will be performed again.
  39.  
  40.        - Finally, if all surrounding mines and those surrounding them are valid and the concatenated mine limit was not reached,
  41.        the current checked cell will be set as a mine.
  42.  
  43.        - This process will stop in two cases. First, if no more mines have to be placed and all mines have been placed
  44.        correctly. Second, if the failed attempts counter reaches a certain limit, by default 500. The failed attempts limit
  45.        should be set larger if large grids with higher proportions of mines are computed.
  46. """
Tags: docstring
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement