# Python辅导 | Minesweeper Game Assignment

Marking Criteria:
Total: 70 marks
B. Game logic (25 marks)
C. brute-force approach (30 marks)
D. decomposition, variable names and documentation (10 marks)
Assignment code interview
Each student will be interviewed during a lab session regarding their submission to gauge your personal understanding of your Assignment code. The purpose of this is to ensure that you have completed the code yourself and that you understand the code submitted. Your assignment mark will be scaled according to the responses provided.
Interview Rubric
0
The student cannot answer even the simplest of questions There is no sign of preparation
They probably haven’t seen the code before
0.25
There is some evidence the student has seen the code
The answer to a least one question contained some correct points
But it’s clear they cannot engage in a knowledgeable discussion about the code
0.5
The student seems underprepared
Answers are long winded and only partly correct
They seem to be trying to work out the code as they read it
They seem to be trying to remember something they were told but now can’t remember However they clearly know something about the code
With prompting they fail to improve on a partial or incorrect answer
0.75
The student seems reasonably well prepared
Questions are answered correctly for the most part but the speed and/or confidence they are answered with is not 100%
1
The student is fully prepared
All questions were answered quickly and confidently
It’s absolutely clear that the student has performed all of the coding themselves.
2

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:
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.
3

Create a menu with the following options which you will use for the remaining tasks.
1- Read board (from assignment one)
2- New board (from assignment one)
3- Mine Count (from assignment one)
4- Play
5- Brute force
6- Quit
Note: The board size in this assignment can take any value between 2 and 10 inclusive.
Task 2: Game logic (25 marks)
This task is about programming the game logic. You will need two new boards, userBoard and mineBoard.
The game begins with an empty list of lists, userBoard that tracks all the users’ play entries. The second board, mineBoard, contains all the mines and the mine count as completed in assignment 1.
Part A: Check Function (5 marks)
Write a function check(userBoard, mineBoard) that takes userBoard and mineBoard and checks if the user
wins, loses or is still playing . If userBoard contains an ‘x’, when compared to mineBoard, the player loses. To determine if a player wins we need to check if all empty squares have been revealed in the userBoard. If neither of the two cases above happened, then the user still playing. The function should return -1 if the user loses, 1 if the user wins and 0 if still playing.
Part B: Play (20 marks)
In this task you are required to write a function play(userBoard, mineBoard,row,col) that would take the row
and col as input from the user and update userBoard with what is present at that location, a number 0-8 or an ‘x’. Use check(userBoard, mineBoard) from Part A to update the user of their play progress.
Bonus (5 Marks): If the revealed square has 0 mines surrounding it then the function will recursively call itself for all the neighbouring squares. By doing so an entire region of contiguous empty squares will be revealed.
Example 1:
What would you like to do?
2- New board
3- Mine count
4- Play
5- Brute force
6- Quit
?2
board size from 2 to 10: 2
number of mines less than size^2: 1
0| | |
1| | |
|0|1|
What would you like to do?
2- New board
3- Mine count
4- Play
5- Brute force
6- Quit
?4
4

row: 0 col: 0
0|1| |
1| | |
|0|1|
row: 0
col: 1
0|1|1|
1| | |
|0|1|
row: 1
col: 0
0|1|1|
1|1| |
|0|1|
you win
0|1|1|
1|1|x|
|0|1|
Example 2:
What would you like to do?
2- New board
3- Mine count
4- Play
5- Brute force
6- Quit
?2
board size from 2 to 10: 3
number of mines less than size^2: 3
0| | | |
1| | | |
2| | | |
|0|1|2|
What would you like to do?
2- New board
3- Mine count
4- Play
5- Brute force
6- Quit
?4
row: 0
col: 0
0|x| | |
1| | | |
2| | | |
|0|1|2|
you lose
0|x|1|0|
1|2|3|2|
2|1|x|x|
|0|1|2|
5

This task is about applying the brute-force approach to find a solution for minesweeper.
PartA: Chosenapproach(5marks)
In a separate word or pdf document, prepare a written discussion of the manner in which you intend to apply brute-force to this problem. Be sure to respond to the following in your discussion
1. how each individual possible solution is represented
2. the complexity of your brute force algorithm
3. how you can be certain it will give you the correct solution
Part B: Implementation (25 marks)
Prepare a python function bruteForce(mineBoard) which has mineBoard as input. The function will generate all possible solutions based on extracting the number of mines and size of the board from mineBoard and then starting with an empty board, will each time generate and check if there is a correct answer. The function will display each possibility until the first correct solution is found.
Example 1
What would you like to do?
2- New board
3- Mine count
4- Play
5- Brute force
6- Quit
?2
board size from 2 to 10: 3
number of mines less than size^2: 5
0| | | |
1| | | |
2| | | |
|0|1|2|
What would you like to do?
2- New board
3- Mine count
4- Play
5- Brute force
6- Quit
?5
==>
0|x|x|x|
1|x|x| |
2| | | |
|0|1|2|
> Incorrect
==>
0|x|x|x|
1|x| |x|
2| | | |
|0|1|2|
> Incorrect
6

==>
0|x|x|x|
1|x| | |
2|x| | |
|0|1|2|
> Incorrect
==>
0|x|x|x|
1|x| | |
2| |x| |
|0|1|2|
> Incorrect
==>
0|x|x|x|
1|x| | |
2| | |x|
|0|1|2|
> Incorrect
==>
0|x|x|x|
1| |x|x|
2| | | |
|0|1|2|
> Incorrect
==>
0|x|x|x|
1| |x| |
2|x| | |
|0|1|2|
> correct
What would you like to do?
2- New board
3- Mine count
4- Play
5- Brute force
6- Quit
?3
0|x|x|x|
1|4|x|3|
2|x|2|1|
|0|1|2|
Example 2
What would you like to do?
2- New board
3- Mine count
4- Play
5- Brute force
6- Quit
7

?2
board size from 2 to 10: 2
number of mines less than size^2: 2
0| | |
1| | |
|0|1|
What would you like to do?
2- New board
3- Mine count
4- Play
5- Brute force
6- Quit
?5
==>
0|x|x|
1| | |
|0|1|
> Incorrect
==>
0|x| |
1|x| |
|0|1|
> Incorrect
==>
0|x| |
1| |x|
|0|1|
> Incorrect
==>
0| |x|
1|x| |
|0|1|
> Incorrect
==>
0| |x|
1| |x|
|0|1|
> correct
What would you like to do?
2- New board
3- Mine count
4- Play
5- Brute force
6- Quit
?3
0|2|x|
1|2|x|
|0|1|
8

What would you like to do?