C代写:CS104-Minesweeper

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:

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

Example 2:

1
2
3
4
5
6
7
8
9
10
11
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
    1. a. You must have at least 2 .c files
  3. Your program should accept the following command line arguments
    1. a. The number of rows
    2. b. The number of columns
    3. c. The number of mines
    4. d. Optionally a seed to the random number generator
    5. e. If no seed is given then you should seed the random number generator with the current time
    6. 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)
    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.
    After selecting a tile the user will be presented with a list of actions to take based on the tile they selected
    1. 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
    2. b. If the tile is marked as a mine, they may remove their marker or cancel their move.
    3. c. If the tile is marked as a possible mine, they may remove their marker or cancel their move
    4. d. If the tile is already revealed tell the user that the tile is already revealed
    5. 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
  5. We will be using the following symbols in your board
    1. a. #: a concealed tile
    2. b. *: a mine
    3. c. !: a tile marked as a mine
    4. d. ?: a tile marked as potentially being a mine
  6. 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
    1. a. For each mine that needs to be placed:
  7. randomly choose a row
  8. randomly choose a column
  9. 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
    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
  10. If a new tile is encountered that has 0 mines surrounding it, the process repeats
  11. Hint, this involves recursion
  12. The user losers if they reveal a mine
  13. The user wins if they reveal all non-mine tiles and have marked all tiles that contain a mine
  14. After the game is over the entire board should be revealed whether the user won or not should be displayed.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
./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 :(