Minesweeper
The year is 1992... Microsoft has just released Windows 3.1 and packaged with it is a beautiful game called Minesweeper. For many
people, this game is the first game they will have (and the last game they will need) on their Windows computer, and it is still a
classic.
If you'd like to try the game, it's available as a Google Search Game here.
In this assignment, you will be implementing COMP1511's version of this classic logic puzzle game. Our Minesweeper is a program
that allows us to set up and play a game using a series of commands in a terminal.
The commands are made up of integers typed
directly into our program. Each command will make some change to a minefield, a two dimensional space that hides some mines that
a player is trying not to uncover.
The aim of minesweeper is to reveal every square in the minefield except for the ones containing mines.
Minesweeper is already capable of drawing a certain view of the minefield, but it will be up to you to write code so that it can get
input from the user on where mines are initially placed, and read commands to make the correct changes to the minefield. The
finished product of Minesweeper is a simplified playable version of the game.
Note: At time of release of this assignment (end of Week 3), COMP1511 has not yet covered all of the techniques and topics
necessary to complete this assignment. At the end of Week 3, the course has covered enough content to be able to read in a single
command and process its integers, but not enough to work with two dimensional arrays like the minefield or be able to handle
multiple commands ending in End-of-Input (Ctrl-D). We will be covering these topics in the lectures, tutorials, labs, and a live stream
in Week 4.
The Minefield
The minefield is a two dimensional array (an array of arrays) of integers that represents the space that the game is played in. We will
be referring to individual elements of these arrays as squares in the minefield.
The minefield is a fixed size grid and has SIZE rows, and SIZE columns. SIZE is a #define 'd constant.
Both the rows and columns start at 0, not at 1.
The top left corner of the grid is (0, 0) and the bottom right corner of the grid is (SIZE - 1, SIZE - 1) . Note that we are
rows as the first coordinate in pairs of coordinates.
For example, if we are given an input pair of coordinates 5 6 , we will use that to find a particular square in our minefield by accessing
the individual element in the array: minefield[5][6]
In the game of minesweeper these states are displayed to the player:
A revealed square
A square that is unrevealed
Since a square that has not been revealed may or may not contain a mine, there are actually 3 values a square can take. These are
represented by the following #define 'd integers:
#define VISIBLE_SAFE 0 : this represents a square that has been revealed.
#define HIDDEN_SAFE 1 : this represents a square that has not been revealed but does not contain a mine.
#define HIDDEN_MINE 2 : this represents a square that has not been revealed and contains a mine.
When the program is started, all of the squares should be HIDDEN_SAFE . The minefield is then populated with mines (i.e.,
HIDDEN_MINE ) by scanning the locations of the mines, using the code you will write.
The way you reveal squares in the original minesweeper requires a concept not taught in COMP1511 so has been replaced by another
revealing command:
REVEAL_CROSS :
reveal the selected square, for each of the four directly connected squares, only reveal if they have no mines
adjacent. This could result in your program revealing anything from 1 - 5 squares.Please note: Connected refers to the 4 squares directly above, below, to the left, and to the rights of a square. While adjacent refers
to the 8 surrounding squares of a grid square. For example, in the diagram below, there are 8 adjacent squares (in grey) to the
square in yellow.Your minesweeper program will have a way of checking how many mines are in a set of squares in a row, or in a square section of the
minefield, and use these to give the user hints about the location of mines.
The game ends when either:
The game is won: All of the squares are revealed except for those containing mines.
The game is lost: A user attempts to reveal a square containing a mine.
column
0 1 2 3 4 5 6 7
row 0 (0,0) (0,1) (0,2) (0,3) (0,4) (0,5) (0,6) (0,7)
1 (1,0) (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7)
2 (2,0) (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7)
3 (3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7)
4 (4,0) (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7)
5 (5,0) (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7)
6 (6,0) (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7)
7 (7,0) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7)
REVEAL_CROSS
Note: If you ever have a question in the form of "What should my program do if it is given these inputs?" you can run the
minesweeper reference and copy its behaviour.
Your Task: ImplementationYour task for this assignment is to write a program that first prompts the user to enter the location of mines on the minefield, and
place them on the board. Then it should allow the user to check the number of mines in a location, and reveal spaces on thhe
minefield until the end of the game.
Your program will be given commands as a series of integers on standard input. Your program will need to scan in these integers and
then make the necessary changes in the minefield.
Allowed C Features
In this assignment, there are no restrictions on C Features, except for those in the Style Guide.
We strongly encourage you to complete the assessment using only features taught in lectures up to and including Week 4. The only
C features you will need to get full marks in the assignment are:
int variables;
if statements, including all relational and logical operators;
while loops;
int arrays, including two dimensional arrays;
printf and scanf ; and
functions.
Using any other features will not increase your marks (and will make it more likely you make style mistakes that cost you marks).
If you choose to disregard this advice, you must still follow the Style Guide. You also may be unable to get help from course staff if
you use features not taught in COMP1511.
Starter Code
minesweeper.c is the starting point for your minesweeper program. We've provided you with some constants and some starter code
to display the minefield as basic integers on the screen; you'll be completing the rest of the program.
Input Commands
The program should first ask for the number of mines as an integer. Then, the program will scan the locations of the mines as pairs
of integers in the format: row column
After specifying the location of the mines, each command given to the program will be a series of integers. These input commands
should continue to be read and executed until the game is won, lost, or when there is an EOF (Ctrl + D)
The first input will always be an integer representing the type of command, e.g. 1 means How many mines in a row?
Depending on what command the first integer specifies, you will then scan in some number of "arguments" (additional integers) that
have a specific meaning for that command.
For example, 1 5 2 5 asks for how many mines in row 5, from column 2 to 6 inclusive, checking along 5 columns in total.
$ 1511 minesweeper
Download the starter code (minesweeper.c) here or use this command on your CSE account to copy the file into your
current directory:
$ cp -n /web/cs1511/21T2/activities/minesweeper/minesweeper.c .minesweeper.c is the starting point for your minesweeper program. We've provided you with some constants and some starter code
to display the minefield as basic integers on the screen; you'll be completing the rest of the program.
Input Commands
The program should first ask for the number of mines as an integer. Then, the program will scan the locations of the mines as pairs
of integers in the format: row column
After specifying the location of the mines, each command given to the program will be a series of integers. These input commands
should continue to be read and executed until the game is won, lost, or when there is an EOF (Ctrl + D)
The first input will always be an integer representing the type of command, e.g. 1 means How many mines in a row?
Depending on what command the first integer specifies, you will then scan in some number of "arguments" (additional integers) that
have a specific meaning for that command.
For example, 1 5 2 5 asks for how many mines in row 5, from column 2 to 6 inclusive, checking along 5 columns in total.
$ 1511 minesweeper
Download the starter code (minesweeper.c) here or use this command on your CSE account to copy the file into your
current directory:
$ cp -n /web/cs1511/21T2/activities/minesweeper/minesweeper.c .
Input to your program will be via standard input (e.g. typing into the terminal).
You can assume that the input will always be integers and that you will always receive the correct number of arguments for a
command.
Details on each command that your program must implement are shown below.
………………
学霸联盟