For this assignment you will construct a single python file called minesweeper.py , that will operate a text-based version of the popular number-puzzle gameMinesweeper. The rules for the game are as...



For this assignment you will construct a single python file calledminesweeper.py, that will operate a text-based version of the popular number-puzzle gameMinesweeper.


The rules for the game are as follows:



  • The computer generates a 2D grid, and places several secret mines into it.

  • The player then guesses locations on the grid, trying to guess every location except the ones with mines.

  • If the player guesses a location that's already revealed, nothing happens.

  • If the player guesses a location that has a mine, the game is over and the player loses.

  • If the player guesses a location that does not have a mine, the computer reveals how many mines are inadjacentlocations on the grid. This number is displayed on the grid at the location the user guessed.

  • If there arezeromines in adjacent locations the computer reveals a blank space, and then subsequently reveals all adjacent cells according to these rules as well. (I.e., If any of those locations are also adjacent to zero mines, they trigger a similar reaction to their adjacent cells.)

  • Once the user has revealed all locations except for the mines, they win!


Below, the game development is divided into parts to aid your process. Read through all of it before beginning. You are free to build the program in any order you see fit.


Part 1: Helper functions


It is recommended that you begin your game by writingand testingthe following functions. You should create any additional helper functions you might need in order to keep your program well organized and easy to read. The following functions are required components in your solution for full marks.






  • placeMines()
    - This function takes zero arguments and returns a grid (i.e. a square 2D list) with randomly placed mines. The size of the grid should be determined by global constant called GRID_SIZE (where both the number of rows and columns equal GRID_SIZE). The grid will be filled randomly with either empty strings ("") or mines ("x"). For each cell in the grid there should be a 1 in 10 chance of placing a mine. (If you would like, this too can be a global constant making it a 1 in MINE_CHANCE chance of placing a mine. For submission, please set this constant to 10).



  • makeBoard()
    - This function takes zero arguments and returns a grid (ie. a 2D list) representing the player's game board. Initially all cells in the game board should be unrevealed, represented as "#" characters. The size of the game board should be the same as the size of the grid of mines.



  • showBoard()
    - This function takes a 2D grid representing the player's game board as argument and prints it to the screen. The printout of the board should include row and column labels (see examples below). You may assume that GRID_SIZE will always be a number in the range [2-10] (ie, all row/column labels will be one digit numbers).



  • countHiddenCells()
    - This function takes a 2D grid representing the player's game board as argument, and returns the number of cells in the board that are unrevealed ("#"). Initially this will be all of them, but as the game progresses this value should get smaller and smaller with each turn. Note the player wins when the number of hidden cells matches the number of mines.



  • countAllMines()
    - This function takes a 2D grid representing the minefield as argument and returns the number of cells in the grid that contain mines. This value should not change once the minefield is initialized.



  • isMineAt()
    - This function takes as arguments a 2D grid representing the minefield and two integers representing a row and column respectively, and returns True if the cell indicated by the row and column contains a mine in the grid, and False otherwise. This function should validate the inputted row and column values to ensure that they are on the gameboard, and return False if they are not.



  • countAdjacentMines()
    - This function takes as arguments, a 2D grid representing the minefield and two integers representing a row and column respectively, and returns the number of mines in adjacent cells. Adjacent cells are the 8 cells surrounding the provided cell, including diagonals, as illustrated in the figure to the right.



Part 2: Game loop


Next build the
main()
function and game loop.



  • The main() function should initialize two grids representing the minefield and player's gameboard respectively.

  • The game loop should repeat the following actions each round, until the user wins or loses:


    • Get the user's move

    • Check if they lost

    • If not, reveal the cell (see part 3)

    • Display the updated game board


  • The user should be prompted to input two integers in a single prompt, separated by a comma. (e.g. > 3,4)

  • You maynotassume that the user will enter good input. Any input that does not consist of two integers separated by a comma should result in the prompt repeating. Integers that are too big or small for the gameboard, can either be rejected as bad input, or accepted as valid input, but then subsequently have no effect on the game.

  • If the user loses (ie, reveals one mine), then the location ofallmines should be revealed and displayed, accompanied by a "Game Over!" message.

  • If the user wins, a congratulatory "You Win!!" message should be displayed.


Part 3: Reveal()


Write a recursive method called
reveal()
that takes as arguments: the player's gameboard, the grid of mines, and the user's choice (row & column values). This function should reveal the selected cell on the user's gameboard according to the rules of the game as laid out in the instructions above. Some additional notes:



  • Cells that are revealed as being blank should be displayed as empty spaces (" ").

  • The function should do input validation on the provided row and column values to verify that they indicate a valid location on the board. (If not, the function should do nothing).

  • The function should not need to return anything since its work is done in mutating the player's gameboard.




Some sample runs:


Game won with GRID_SIZE: 5




> python minesweeper.py |01234 ------- 0|##### 1|##### 2|##### 3|##### 4|##### Select a cell (row,col) > 1,1 |01234 ------- 0|##### 1|#2### 2|##### 3|##### 4|##### Select a cell (row,col) > 3,3 |01234 ------- 0|##### 1|#2### 2|##### 3|###1# 4|##### Select a cell (row,col) > 0,4 |01234 ------- 0|####1 1|#2### 2|##### 3|###1# 4|##### Select a cell (row,col) > 4,0 |01234 ------- 0|####1 1|#2111 2|11 3| 11 4| 1# Select a cell (row,col) > 0,2 |01234 ------- 0|##1#1 1|#2111 2|11 3| 11 4| 1# Select a cell (row,col) > 0,1 |01234 ------- 0|#21#1 1|#2111 2|11 3| 11 4| 1# YOU WIN!!


Game lost with GRID_SIZE 5




> python minesweeper.py |01234 -------- 0|##### 1|##### 2|##### 3|##### 4|##### Select a cell (row,col) > 4,3 |01234 -------- 0|##### 1|##### 2|##### 3|##### 4|###2# Select a cell (row,col) > 2,2 |01234 -------- 0|##### 1|##### 2|##1## 3|##### 4|###2# Select a cell (row,col) > 1,0 |01234 -------- 0|##### 1|1#### 2|##1## 3|##### 4|###2# Select a cell (row,col) > 0,3 |01234 -------- 0|###1# 1|1#### 2|##1## 3|##### 4|###2# Select a cell (row,col) > 1,3 |01234 -------- 0|###1# 1|1##2# 2|##1## 3|##### 4|###2# Select a cell (row,col) > 3,1 |01234 -------- 0|###1# 1|1##2# 2|##1## 3|#1### 4|###2# Select a cell (row,col) > 0,0 |01234 -------- 0|1##1# 1|1##2# 2|##1## 3|#1### 4|###2# Select a cell (row,col) > 0,1 |01234 -------- 0|11#1# 1|1##2# 2|##1## 3|#1### 4|###2# Select a cell (row,col) > 0,2 |01234 -------- 0|1111# 1|1##2# 2|##1## 3|#1### 4|###2# Select a cell (row,col) > 3,2 |01234 -------- 0|1111# 1|1##2# 2|##1## 3|#11## 4|###2# Select a cell (row,col) > 2,0 |01234 -------- 0|1111# 1|1##2# 2|1#1## 3|#11## 4|###2# Select a cell (row,col) > 3,0 |01234 -------- 0|1111# 1|1##2# 2|111## 3| 11## 4| 1#2# Select a cell (row,col) > 1,2 |01234 -------- 0|1111# 1|1#12# 2|111## 3| 11## 4| 1#2# Select a cell (row,col) > 3,4 |01234 -------- 0|1111X 1|1X12# 2|111#X 3| 11#X 4| 1X2# GAME OVER!


Game won with GRID_SIZE 10



>python minesweeper.py   |0123456789 ------------- 0|########## 1|########## 2|########## 3|########## 4|########## 5|########## 6|########## 7|########## 8|########## 9|########## Select a cell (row,col) > 2,3   |0123456789 ------------- 0|########## 1|########## 2|###1###### 3|########## 4|########## 5|########## 6|########## 7|########## 8|########## 9|########## Select a cell (row,col) > 2,1   |0123456789 ------------- 0|  1####### 1|  1####### 2|  11###### 3|111####### 4|########## 5|########## 6|########## 7|########## 8|########## 9|########## Select a cell (row,col) > 4,3   |0123456789 ------------- 0|  1####### 1|  1####### 2|  112###11 3|111 112#1 4|##1   1#1 5|111   111 6| 7|11 8|#1    111 9|#1    1#1 Select a cell (row,col) > 4,0   |0123456789 ------------- 0|  1####### 1|  1####### 2|  112###11 3|111 112#1 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|#1    1#1 Select a cell (row,col) > 9,0   |0123456789 ------------- 0|  1####### 1|  1####### 2|  112###11 3|111 112#1 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 Select a cell (row,col) > 3,7   |0123456789 ------------- 0|  1####### 1|  1####### 2|  112###11 3|111 11211 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 Select a cell (row,col) > 1,4   |0123456789 ------------- 0|  1####### 1|  1#3##### 2|  112###11 3|111 11211 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 Select a cell (row,col) > 2,6   |0123456789 ------------- 0|  1####### 1|  1#3##### 2|  112#2#11 3|111 11211 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 Select a cell (row,col) > 2,7   |0123456789 ------------- 0|  1####### 1|  1#3##### 2|  112#2211 3|111 11211 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 Select a cell (row,col) > 0,0   |0123456789 ------------- 0|  1####### 1|  1#3##### 2|  112#2211 3|111 11211 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 Select a cell (row,col) > 0,3   |0123456789 ------------- 0|  12###### 1|  1#3##### 2|  112#2211 3|111 11211 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 Select a cell (row,col) > 0,5   |0123456789 ------------- 0|  12#3#### 1|  1#3##### 2|  112#2211 3|111 11211 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 Select a cell (row,col) > 0,9   |0123456789 ------------- 0|  12#3###1 1|  1#3##### 2|  112#2211 3|111 11211 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 Select a cell (row,col) > 0,8   |0123456789 ------------- 0|  12#3##21 1|  1#3##### 2|  112#2211 3|111 11211 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 Select a cell (row,col) > 1,7   |0123456789 ------------- 0|  12#3##21 1|  1#3##4## 2|  112#2211 3|111 11211 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 Select a cell (row,col) > 1,9   |0123456789 ------------- 0|  12#3##21 1|  1#3##4#1 2|  112#2211 3|111 11211 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 Select a cell (row,col) > 1,5   |0123456789 ------------- 0|  12#3##21 1|  1#34#4#1 2|  112#2211 3|111 11211 4|1#1   1#1 5|111   111 6| 7|11 8|#1    111 9|11    1#1 YOU WIN!!


A very unlucky first guess



>python minsweeper.py   |0123456789 ------------- 0|########## 1|########## 2|########## 3|########## 4|########## 5|########## 6|########## 7|########## 8|########## 9|########## Select a cell (row,col) > 0,0   |0123456789 ------------- 0|X######X## 1|X##X###X## 2|##X####XXX 3|#########X 4|#####X#### 5|X###X#X### 6|###X##X### 7|##X##X#X## 8|######X### 9|##X#####XX GAME OVER!


Bad inputs and another win on GRID_SIZE 10



>python minesweeper.py   |0123456789 ------------- 0|########## 1|########## 2|########## 3|########## 4|########## 5|########## 6|########## 7|########## 8|########## 9|########## Select a cell (row,col) > 12,12   |0123456789 ------------- 0|########## 1|########## 2|########## 3|########## 4|########## 5|########## 6|########## 7|########## 8|########## 9|########## Select a cell (row,col) > a,b Select a cell (row,col) > one,two Select a cell (row,col) > 3 Select a cell (row,col) >  Select a cell (row,col) > 0,0   |0123456789 ------------- 0| 1|       111 2|  111  1## 3|222#2111## 4|########## 5|########## 6|########## 7|########## 8|########## 9|########## Select a cell (row,col) > 3,8   |0123456789 ------------- 0| 1|       111 2|  111  1## 3|222#21112# 4|########## 5|########## 6|########## 7|########## 8|########## 9|########## Select a cell (row,col) > 2,9   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|########## 5|########## 6|########## 7|########## 8|########## 9|########## Select a cell (row,col) > 4,9   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|#########1 5|########## 6|########## 7|########## 8|########## 9|########## Select a cell (row,col) > 5,9   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|######1 11 5|######1 6|######11 7|#######211 8|########## 9|########## Select a cell (row,col) > 7,5   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|######1 11 5|######1 6|######11 7|#####2#211 8|########## 9|########## Select a cell (row,col) > 6,5   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|######1 11 5|######1 6|#####111 7|#####2#211 8|########## 9|########## Select a cell (row,col) > 6,4   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|##212#1 11 5|221 111 6|     111 7|    12#211 8|   12##### 9|   1###### Select a cell (row,col) > 9,5   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|##212#1 11 5|221 111 6|     111 7|    12#211 8|   12##### 9|   1#2#### Select a cell (row,col) > 9,6   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|##212#1 11 5|221 111 6|     111 7|    12#211 8|   12##### 9|   1#21### Select a cell (row,col) > 9,7   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|##212#1 11 5|221 111 6|     111 7|    12#211 8|   12##### 9|   1#211## Select a cell (row,col) > 8,6   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|##212#1 11 5|221 111 6|     111 7|    12#211 8|   12#2### 9|   1#211## Select a cell (row,col) > 8,7   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|##212#1 11 5|221 111 6|     111 7|    12#211 8|   12#22## 9|   1#211## Select a cell (row,col) > 9,8   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|##212#1 11 5|221 111 6|     111 7|    12#211 8|   12#22## 9|   1#2111# Select a cell (row,col) > 9,9   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|##212#1 11 5|221 111 6|     111 7|    12#211 8|   12#22## 9|   1#21111 Select a cell (row,col) > 8,9   |0123456789 ------------- 0| 1|       111 2|  111  1#2 3|222#21112# 4|##212#1 11 5|221 111 6|     111 7|    12#211 8|   12#22#1 9|   1#21111 YOU WIN!!



Apr 06, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here