Introduction
作业需要用C编程实现 Minesweeper
,和其他的Minesweeper不同的是,这个Minesweeper的board不太一样,独树一帜。
Task description
In this assignment you will develop a variation of the Minesweeper game in the
C programming language. We recommend that you play the game to gain an overall
understanding of how it works.
In each game, mines are hidden in a two dimensional mine field and the object
of the game is to uncover the empty cells and flag the cells containing mines.
To simplify your implementation, regions without mines should not be
automatically expanded and players are not allowed to unflag cells once they
have been flagged. Each game will contain a total of 10 mines, regardless of
the grid size.
You are encouraged to ask questions on Ed using the assignments category. As
with any assignment,
make sure that your work is your own, and you do not share your code or
solutions with other students.
Working on your assignment
You can work on this assignment on your own computer or the lab machines. It
is important that you continually back up your assignment files onto your own
machine, external drives, and in the cloud.
You are encouraged to submit your assignment on Ed while you are in the
process of completing it. By submitting you will obtain some feedback of your
progress on the sample test cases provided.
Implementation details
Your program must be contained in one file called minesweeper.c and produce no
errors when compiled using clang on the lab machines and Ed. Your program will
be run from the command line using ./minesweeper and read input from standard
input and write output to standard output.
Important – your program will be marked automatically, so make sure that you
follow the assignment specifications carefully. Your program output must
match the exact output shown in the examples.
Program input
The program input consists of two sections. The first section defines the mine
field and the second section describes the moves of the player. Each line of
the input is one instruction. You are also required to check for whether any
invalid input is encountered.
- The first line of the input will be of the form g [width] [height] where width and height are integers in the range [1, 100]. This defines the size of the grid to width cells wide and height cells high. The grid must have at least 10 cells, so this instruction is only valid if width x height > 10
- The next 10 lines will be of the form b [x] [y] where x and y are integers in the range [0, width) and [0, height) respectively. This places a mine at cell (x, y) in the grid.
- The next lines will describe the moves of the player – either u [x] [y] or f [x] [y].
* A u instruction means that the player uncovers the cell (x, y)
* A f instruction means that the player flags the cell (x, y)
There must be exactly one player instruction for each grid cell. That is, if
the grid is of size width x height then there should be exactly width x height
u and f instructions in total.
No two instructions should refer to the same grid cell, and no more than 10
flags can be placed.
The input should be processed one line at a time. After reading a line of
input your program should immediately act on the line of input as described
before proceeding to the next line of input.
Sample input
In the following test case we construct a 4 x 4 mine field and then play the
game.
The player successfully flags all of the mines and uncovers the remaining
cells in this game.
g 4 4
b 0 0
b 1 0
b 3 0
b 1 1
b 1 2
b 0 2
b 3 2
b 0 3
b 1 3
b 3 3
u 2 3
u 2 0
f 0 3
u 2 1
f 3 0
f 0 2
u 0 1
f 3 2
f 3 3
f 0 0
f 1 1
f 1 0
f 1 2
u 3 1
u 2 2
f 1 3
Program output
- After reading a g [width] [height] instruction, output the instruction that was inputted.
- After reading a b [x] [y] instruction, output the instruction that was inputted. In addition, after reading the tenth b [x] [y] line, the state of the grid should be displayed.
- After reading a correct player instruction, output the same instruction.
* If the player uncovers a bomb, output the message lost and immediately exit.
* If every mine has been flagged and every other empty cell has been uncovered, then display the final state of the grid, output the message won and then exit.
* Otherwise, the state of the grid after the instruction should be outputted. - If an invalid, out of place, or otherwise incorrect instruction is read at any time, error should be outputted and your program should exit immediately. Make sure that you do not output the instruction or the grid following an erroneous instruction.
Displaying the grid
The grid is represented using 1 character per cell, with a 1 character border.
A 4 x 4 grid looks like:
+—-+
||
||
||
||
+—-+
- The border consists of - or | characters, with a + character in each corner.
- The * character represents a covered cell.
- The f character represents a flagged cell.
- An uncovered cell is represented by a number that corresponds to the total number of bombs in the 8 adjacent cells that surround the uncovered cell.
The top left corner of the grid should have coordinates (0, 0) and the bottom
right corner of the grid should have coordinates ([width] - 1, [height] - 1).
The first coordinate is the x coordinate, and the second is the y coordinate.
A 4 x 4 grid where the player has flagged (3, 1) and uncovered (0, 2) with 2
mines in the adjacent cells would look like:
+—-+
||
|f|
|2|
||
+—-+
Writing your own testcases
We have provided you with some sample testcases but these do not test all the
functionality described in the assignment. It is important that you thoroughly
test your code by writing your own testcases.
You should place all of your test cases in a tests/ directory. Ensure that
each test case has the .in input file along with a corresponding .out output
file. We recommend that the names of your test cases are descriptive so that
you know what each is testing, for example: too-many-mines.in