Background:

In this assignment, you are required to write a program for the game Minesweeper. The game starts with a grid of unmarked squares. The game is played by asking the user to either step on one of these squares to reveal its content or flag that square as a mine. If a square containing a mine is revealed, the player loses the game otherwise a number is displayed in the square, indicating how many adjacent squares contain mines. If no mines are adjacent, then all adjacent squares will be revealed. The player uses the numbers displayed in the squares to deduce the contents of other squares, and may either safely reveal each square or flag the squares containing a mine, as shown below: see image.

Note: The size of the board will never be greater than 10 x 10 and the number of mines squares will be less than the size2.

Task 1: Representation and display

The simplest representation of this problem would be to represent the board as a table, where 'x' marks the squares with mines, - marks the squares that dont have any mines.

The integer values represents how many mines surround a specific square. The value can be from 0 to 8 because each square is surrounded by eight squares.

Part A: Initial setup

Write a python function readBoard(fileName) which takes the name of a file as input and produces a two- dimensional table (board) to represent this board as a list of lists. The following example demonstrates a 5x5 board.

x-x--
-x---
---x-
----x

Part B: Display

Extend your program to print the board on the screen using printBoard(board) function. This function prints the contents of the board to the screen. It will replace '- with spaces as shown in the example. The board square will contain either x', -, or integer value between 0 to 8.

Example 1

0| | | | |x|
1| | | | | |
2|x| | | | |
3| | | | |x|
4| | |x|x| |
_|0|1|2|3|4|

Example 2

0| |1|0|0|0|0|1| | |x|
1| |1|0|0|0|0|1| | | |
2|1|1|0|0|0|0|1|2| | |
3|0|0|0|0|0|0|0|1| | |
4|1|1|1|0|0|0|0|1|1|1|
5| | |1|0|0|0|0|0|0|0|
6|1|1|1|0|0|1|1|1|0|0|
7|0|0|0|0|0|1| |1|0|0|
8|0|0|0|0|0|1|1|1|0|0|
9|0|0|0|0|0|0|0|0|0|0|
_|0|1|2|3|4|5|6|7|8|9|

Part C: File input/output

Extend your program so that you can do the following:

  • Write a function saveBoard(fileName,board) that writes the current board state (board) in its original format to a fileName entered by the user. Note: if you read from the file you just wrote to, your board state will be unchanged.

Part D: Menu

Write a function menu( ) which allows the user to choose from a set of options to perform . An example of the menu is given below:

What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?3
0| | | | | |
1| | | | | |
2| | | | | |
3| | | | | |
4| | | | | |
_|0|1|2|3|4|
What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?1
Please enter a valid filename: example.txt
example.txt has been successfully read.
What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?3
0| | | | |x|
1| | | | | |
2|x| | | | |
3| | | | |x|
4| | |x|x| |
_|0|1|2|3|4|
What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?2
Please enter a valid filename: example.txt
example.txt has been successfully saved.
What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?6
Thanks for playing, bye

Task 2: Mine Count:

This task is about finding the number of mines that surrounds each square.

0 1 1 2 x 2
1 1 x 2 2 x
2 2 2 3 2 2
3 1 x 2 x 1
4 1 1 2 1 1
0 1 2 3 4

For the example above, the square [2, 2] has three squares surrounding it that contain mines, namely, [1, 1], [3, 1], and [3, 3]. Similarly, square [0, 4] has two neighbouring mines at [0, 3] and [1, 4]. Square [2, 2] has eight neighbouring squares that need to be checked, while square [0, 4] has only three.

You need to write a function mineCount(board) that returns a new list of lists with each square representing the count of neighbouring mines, while keeping the "x" for the squares that have a mine.

What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?1
Please enter a valid filename: example.txt
example.txt has been successfully read.
What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?3
0| | | | | |
1| | |x| |x|
2| | | |x| |
3| |x| |x| |
4| | | | | |
_|0|1|2|3|4|
What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?4
0|0|1|1|2|1|
1|0|1|x|3|x|
2|1|2|4|x|3|
3|1|x|3|x|2|
4|1|1|2|1|1|
_|0|1|2|3|4|

Task 3: Generating random boards

In this task you will need to ask the user for the board size N. You will also ask the user to enter the number of mines they want placed on the board. You will use these values to generate a new board of size N x N in which the mines are randomly placed in the board. N should be any value between 5 and 10 inclusive. You should validate the input. The minimum number of mines allowed should be between 0 and N x N inclusive.

What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?5
Please enter a board size from 5 to 10: 3
Please enter a board size from 5 to 10: 2
Please enter a board size from 5 to 10: 5
Please enter mines number less than size^2: 25
Please enter mines number less than size^2: 26
Please enter mines number less than size^2: -4
Please enter mines number less than size^2: 24
What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?3
0|x| |x|x|x|
1|x|x|x|x|x|
2|x|x|x|x|x|
3|x|x|x|x|x|
4|x|x|x|x|x|
_|0|1|2|3|4|
What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?5
Please enter a board size from 5 to 10: 11
Please enter a board size from 5 to 10: 12
Please enter a board size from 5 to 10: 10
Please enter mines number less than size^2: 100
Please enter mines number less than size^2: 50
What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?3
0|x| |x| | |x|x|x|x| |
1| |x|x| | |x|x| | | |
2| |x| |x| | |x| | | |
3|x| | |x|x|x|x|x| |x|
4|x|x| |x| |x| |x|x|x|
5| |x| | | |x|x|x|x| |
6|x| | | | | |x|x| |x|
7| | | | |x| | | |x| |
8| |x| |x|x|x|x|x| |x|
9|x|x| | | | |x| |x|x|
_|0|1|2|3|4|5|6|7|8|9|
What would you like to do?
1- Read board
2- Save board
3- Print board
4- Mine count
5- New board
6- Quit
?6
Thanks for playing, bye

Task 4: Decomposition, variable names and code documentation

Marks will be allocated for good use of variable names, code documentation and proper decomposition.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.