原文地址:扫雷
Introduction
实现扫雷游戏Minesweeper.
Objectives
The objectives of this assignment are:
- To gain experience in designing algorithms for a given problem description and implementing those algorithms in Python 3.
-
To demonstrate your understanding of:
- how to decompose code into functions in Python. - how to read from text files using Python.
- how to manipulate lists using basic operations.
- brute-force approaches to problems
Submission Procedure
- Create a single python file containing all your solutions with your name and studentID as comments.
- Save your file into a zip file called YourStudentID.zip
- Submit your zip file containing your solution to Moodle.
- Your assignment will not be accepted unless it is a readable zip file.
Marking Criteria:
A. representation and display
B. Mine Count
C. generating a board of size N x N with random placement of mines
D. decomposition, variable names and documentation
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.
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 size.
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 don't 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.
(本文出自csprojectedu.com,转载请注明出处)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。