C代写:CS104 Minesweeper

Introduction

实现一个Minesweeper,即扫雷游戏,跑过测试即可。这个作业比较坑的地方是测试集里面采用的随机数种子,只有Linux中能过,MacOS中不能过。

Background

For your final project you will be implementing the computer game minesweeper. The game of minesweeper works as follows:

  1. There is a rectangular grid of tiles
  2. Hidden under some of the tiles are mines
  3. Your goal is to mark all the tiles that contain mines and uncover all the tiles that don’t contain mines
  4. If you uncover a tile that contains a mine you lose the game
  5. If you uncover a tile that doesn’t contain a mine, it tells you how many mines are hidden in the eight surrounding squares

Here are some example boards with all of their contents revealed so that you can get an idea as to what they look like.
Example 1:

2 * * 2 0
1 5 * 3 0
0 * * 2 0
  0 1 2 3

Example 2:

9 0 0 0 0 0 1 1 1 0 0
8 0 0 0 0 0 1 * 1 0 0
7 0 1 1 1 0 1 1 2 1 1
6 0 1 * 1 1 1 1 1 * 1
5 0 1 1 1 1 * 1 1 1 1
4 1 1 1 0 1 1 1 0 0 0
3 1 * 1 0 0 0 0 0 0 0
2 1 1 1 0 0 0 0 0 0 0
1 1 2 1 1 0 1 1 2 2 2
0 * 2 * 1 0 1 * 2 * *
  0 1 2 3 4 5 6 7 8 9

Requirement

  1. Name your executable mine_sweeper.out
  2. You must submit a makefile named Makefile, along with the necessary .c and .h files needed to compile your program
    a. You must have at least 2 .c files
  3. Your program should accept the following command line arguments
    a. The number of rows
    b. The number of columns
    c. The number of mines
    d. Optionally a seed to the random number generator
    e. If no seed is given then you should seed the random number generator with the current time
    f. If not enough command line parameters are given your program should print a usage message and terminate.
  4. You MUST USE A STRUCT to store your board state
    a. This includes but is not limited to: the size of your board, the number of mines on the board, and the status of each tile (revealed, concealed, marked, etc)
  5. During each turn the user will be asked to select a row and column number to take an action on
    a. You must validate that the tile they select is contained within the board
    b. If the row and column are not valid, you should repeatedly ask the user for a new row and column until they enter a valid one.
  6. After selecting a tile the user will be presented with a list of actions to take based on the tile they selected
    a. If the tile is concealed, they may reveal it, mark it as a mine, mark it as a possible mine, or cancel their move
    b. If the tile is marked as a mine, they may remove their marker or cancel their move.
    c. If the tile is marked as a possible mine, they may remove their marker or cancel their move
    d. If the tile is already revealed tell the user that the tile is already revealed
    e. If the user chooses to cancel their action or the tile is already revealed the user should be asked for a new tile and a new action to take on it
  7. We will be using the following symbols in your board
    a. #: a concealed tile
    b. *: a mine
    c. !: a tile marked as a mine
    d. ?: a tile marked as potentially being a mine
  8. Since mines will be randomly distributed around the board it is important that you follow the same algorithm for placing mines as I did. The algorithm is
    a. For each mine that needs to be placed:
    randomly choose a row
    randomly choose a column
    If the board at the row and column does not contain a mine place it there
    else repeat beginning at sub-step 1
    b. You should also print out where you place your mines so that you can confirm that they are being placed in the same locations as my program
  9. Each tile that does not contain a mine, once revealed, shows the number of mines contained in the 8 tiles surrounding it
    a. If the user reveals a concealed tile that has 0 mines surrounding it, then not only that tile but all concealed tiles that do not contain mines next to it should also be revealed
    If a new tile is encountered that has 0 mines surrounding it, the process repeats
    Hint, this involves recursion
  10. The user losers if they reveal a mine
  11. The user wins if they reveal all non-mine tiles and have marked all tiles that contain a mine
  12. After the game is over the entire board should be revealed whether the user won or not should be displayed.

Example

./mine_sweeper.out 4 5 2 2
Placing mine at 2, 4
Placing mine at 0, 0
There are 2 mines left
3 # # # # #
2 # # # # #
1 # # # # #
0 # # # # #
  0 1 2 3 4
Enter row a row between 0-3 and a column between 0-4: 3 0
Enter Action
0. Reveal
1. Question
2. Mark
3. Cancel
Action: 0
There are 2 mines left
3 0 0 0 1 #
2 0 0 0 1 #
1 1 1 0 1 1
0 # 1 0 0 0
  0 1 2 3 4
Enter row a row between 0-3 and a column between 0-4: 0 0
Enter Action
0. Reveal
1. Question
2. Mark
3. Cancel
Action: 2
There are 1 mines left
3 0 0 0 1 #
2 0 0 0 1 #
1 1 1 0 1 1
0 ! 1 0 0 0
  0 1 2 3 4
Enter row a row between 0-3 and a column between 0-4: 2 4
Enter Action
0. Reveal
1. Question
2. Mark
3. Cancel
Action: 1
There are 1 mines left
3 0 0 0 1 #
2 0 0 0 1 ?
1 1 1 0 1 1
0 ! 1 0 0 0
  0 1 2 3 4
Enter row a row between 0-3 and a column between 0-4: 3 4
Enter Action
0. Reveal
1. Question
2. Mark
3. Cancel
Action: 0
There are 1 mines left
3 0 0 0 1 1
2 0 0 0 1 ?
1 1 1 0 1 1
0 ! 1 0 0 0
  0 1 2 3 4
Enter row a row between 0-3 and a column between 0-4: 2 4
Enter Action
0. UnQuestion
1. Cancel
Action: 0
There are 1 mines left
3 0 0 0 1 1
2 0 0 0 1 #
1 1 1 0 1 1
0 ! 1 0 0 0
  0 1 2 3 4
Enter row a row between 0-3 and a column between 0-4: 0 0
Enter Action
0. UnMark
1. Cancel
Action: 0
There are 2 mines left
3 0 0 0 1 1
2 0 0 0 1 #
1 1 1 0 1 1
0 # 1 0 0 0
  0 1 2 3 4
Enter row a row between 0-3 and a column between 0-4: 0 0
Enter Action
0. Reveal
1. Question
2. Mark
3. Cancel
Action: 2
There are 1 mines left
3 0 0 0 1 1
2 0 0 0 1 #
1 1 1 0 1 1
0 ! 1 0 0 0
  0 1 2 3 4
Enter row a row between 0-3 and a column between 0-4: 1 1
This tile is already revealed.
Enter row a row between 0-3 and a column between 0-4: 0 2
This tile is already revealed.
Enter row a row between 0-3 and a column between 0-4: 2 99
Enter row a row between 0-3 and a column between 0-4: 2 4
Enter Action
0. Reveal
1. Question
2. Mark
3. Cancel
Action: 0
3 0 0 0 1 1
2 0 0 0 1 *
1 1 1 0 1 1
0 * 1 0 0 0
  0 1 2 3 4
You Lost :(