|
~ Introduction to Programming: C ~ Session 7 - Example Exam: Bombs |
The following is the sample exam question given in the
previous session to take away with you to attempt for this session. Read through
it again before working through walkthrough sample examination that follows.
9531 – INTRODUCTORY COMPUTER PROGRAMMING: STRUCTURED ASSIGNMENT
Topic Games and Quizzes
Title of this Assignment Bombs
Introduction
The game of Bombs is a one-player game on a hidden grid.
Assignment
Specification
Devise a program that will play the game "Bombs", with a single player.
The game should be played on a grid that is 10x10 in size, using 1 to 10 down and A to J across to identify each square in the grid.
The computer places ten bombs randomly in the grid, but which are not shown on the screen.
The grid is then shown on the screen (blank at first). The grid should display 1 to 10 down the left hand side of the grid and A to J across the top.
The player chooses an option from one of the following choices: R for reveal, M for Mark, U for Unmark or X to exit.
The player types out a grid reference (e.g. A1 gives the top-left grid position, J10 gives the bottom-right). This is used to identify a square on the grid. If anything outside this range is typed, then the player is prompted to choose one of the choices (R/M/U/X) again.
If R was chosen, then if the square contained a bomb, the player is told GAME OVER, and after pressing a key, the program exits. If the square contained nothing, then the eight squares surrounding that square are checked. If any of them contain a bomb, they should be counted, and the number of bombs surrounding the square should be placed in the revealed square.
If M was chosen, the square is marked with an X. If U was chosen, the square is marked with a space.
If X was chosen, then the program exits, after the user is prompted about whether they are sure they want to quit.
Check all squares. If all squares have been revealed or marked (i.e. not a space), and all marked squares have a bomb underneath them, then display GAME OVER. WELL DONE. and after the user presses a key, end the program.
Otherwise, loop back to clear the screen, re-display the board, and ask what the user wants to do next.
It is recommended that you
use two arrays, one for the location of the bombs, one to show the user marks /
revealed squares. For example, a square marked with a digit as a character (1 to
8) would show a revealed square with the number of unrevealed bombs next to it.
An X would show a marked square. A space would show an unrevealed square.
a) Describe briefly in writing for a possible user
i) The purpose of your program
ii) The limitations of your program
b) Construct (on paper) a screen layout indicating what will appear on the screen – prompts, input, output with annotations, any other messages.
c) Describe (on paper) the main stages of the program - by means of a flowchart or an alternative technique.
d) Produce a listing of the program. This should have adequate comment lines and should demonstrate to the examiner that the program solves the problem set.
e) Include in the program statements to display adequate instruction to the user of the program – e.g. prompts for data entry and appropriate error messages for invalid input.
f) Key in and run the program, producing and using simple test data if necessary. Check that the program's output agrees with the expected results and correct any errors.
g) Use appropriate language facilities.
Hint – Two-Dimensional Arrays
We have talked so far about single-dimension arrays –
e.g.
int i[3]; i[0]=1; i[1]=2; i[2]=i[0]+i[1];
which leaves us with i[2] containing the value of 3.
However, we can reproduce a grid, which is effectively an
array of an array. If you take a row on our grid as an array of ten characters (char
grid_row[10]) then the whole grid can be taken as an array of rows (char
grid[10][10]) which gives us 10x10 characters. To access each element in the
two-dimensional array, refer to the grid position as each dimension's position
(from 0 to 9) in separate square brackets. See diagram above for graphical
representation of this concept.
The following is a sample exam that you might submit. In the time given, you cannot be expected to produce 100% perfect results. Your aim is to complete at least some of each section. Even if you cannot get your program to work exactly as you want, you can show that you know how to go through the process of analysing the problem, documenting it, structuring it correctly with comments and indentation to make it easier to understand and locate any problems, understanding the limitations and possible improvements that could be made in the program, designing the inputs and screen output, and designing / using a test plan to show that you are able to locate any problems, even if you run out of the time in which to correct those problems (in real life, such problems, or 'bugs' can be difficult to track down, and can cost a lot in terms of time and effort – a good reason to properly document and structure your program – it will make debugging very much easier).
a) i) User's Description
The game of "Bombs" start with a 10x10 grid
of squares. Bombs are hidden underneath ten of these hundred squares.
The aim of the game is to reveal squares to see how many bombs are hidden
in adjacent squares (including diagonals), and use this to judge where other
bombs may be. If you choose a square with a bomb, the game is over. If you
correctly mark all ten bombs without revealing them, and reveal all other
squares, you win.
a)
ii) Restrictions
1. The grid can only be 10 x 10 squares, and there can only be 10 bombs in the grid, so it is not possible to have a harder or easier game, or introduce progressively harder "levels".
2. There is no scoring, nor high scoring.
3. There is no facility to start the game again
4. The display is text-based, so is less attractive than a typical commercially sold game. It can also make the jumble of Xs and digits look quite confusing.
5. Using a space to denote a hidden grid position does not clearly show the individual grid positions
b) Screen Display
Given an example 10x10 grid with the following bomb
positions:-
|
|
A |
B |
C |
D |
E |
F |
G |
H |
I |
J |
|
1 |
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
B |
|
|
|
|
|
|
|
|
3 |
|
|
|
|
|
|
|
B |
|
|
|
4 |
|
|
|
|
|
B |
|
|
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
6 |
|
|
|
B |
|
B |
|
B |
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
8 |
B |
|
|
|
|
B |
|
B |
|
|
|
9 |
|
|
|
|
|
|
|
|
|
|
|
10 |
|
|
|
|
|
|
|
|
|
B |
Text output is shown as white text on black. Input is
shown as black text on white.
Bombs
Game
==========
A B C D E F G H I J
--+-------------------
1|
2|
3|
4|
5|
6|
7|
8|
9|
10+-------------------
Choose from: [R]eveal, [M]ark, [U]nmark or e[X]it: R
Enter a grid reference (eg. E10): E5
E5 is clear. However, the following squares around E5 have bombs in them: D6, F6, F4. Thus, the square will be shown with a 3 in it, as there are three bombs adjacent to that square.
The screen may
now look as follows:-
Bombs
Game
==========
A B C D E F G H I J
--+-------------------
1|
2|
3|
4|
5| 3
6|
7|
8|
9|
10+-------------------
Choose from: [R]eveal, [M]ark, [U]nmark or e[X]it: X
Are you sure you wish to quit? Y for Yes or N for No: Y
In this example, the user has quit the program. Notice the check. If the user had pressed N, the screen would have been redisplayed with the choices displayed again.
In the following example, the user has chosen a square with
a bomb on it:-
Bombs
Game
==========
A B C D E F G H I J
--+-------------------
1|
2|
3|
4|
5| 3
6|
7|
8|
9|
10+-------------------
Choose from: [R]eveal, [M]ark, [U]nmark or e[X]it: R
Enter a grid reference (eg. E10): D6
BOOM! You hit a bomb. GAME OVER.
Press a key to exit. x
In the following example, all of the bombs have been marked
with Xs and all other squares have been revealed. The last square to be marked
is A1. The user has won the game. The display would look like this:-
Bombs
Game
==========
A B C D E F G H I J
--+-------------------
1|
1 1 1 0 0 0 0 0 0
2|0 1 X 1 0 0 1 1 1 0
3|0 1 1 1 1 1 2 X 1 0
4|0 0 0 0 1 X 2 1 1 0
5|0 0 1 1 2 2 3 1 1 0
6|0 0 1 X 2 X 2 X 1 0
7|1 1 1 1 2 2 3 2 2 0
8|X 1 0 0 1 X 2 X 2 1
9|1 1 0 0 1 1 2 1 2 X
10+-------------------
Choose from: [R]eveal, [M]ark, [U]nmark or e[X]it: M
Enter a grid reference (eg. E10): A1
GAME OVER. Well Done. All bombs found. Press
a key. x
d) Stages of Program
The following is a JSP diagram representing the structure and flow of the program.
N.b.
it is recommended that in the exam, you write these diagrams by hand – it's a lot
quicker!

e)
Program Listing
This
program does pretty much exactly what is required, and follows the structure of
the JSP diagram using functions for each main section of the JSP diagram.
You can follow how it splits down. You
are unlikely to find the time to be this exhaustive, but it gives you an idea of
what can be done. See below this
listing for something a bit more reasonable for you to produce.
/*
**
Bombs Game
**
by Simon Huggins - 9th April 2002
**
Introduction to Programming Course (C Element)
**
*/
#include
<stdio.h>
#include
<stdlib.h>
#include
<conio.h>
#include
<ctype.h>
#include
<string.h>
/*
Define function prototypes */
void
set_up_arrays();
char
main_loop();
void
display_visible_array();
void
get_prompt(char *prompt, int *rowpos, int *colpos);
int
bomb_revealed(int row, int col);
int
check_game_over();
int
exit_check();
/*
Define constants for grid size and number of bombs */
int
const rows = 10, cols = 10, bombs = 10;
/*
Define global variable arrays for visible & invisible grids */
char
visible[rows][cols],invisible[rows][cols];
/*
Main Loop */
void
main() {
randomize(); /* Randomize generated numbers */
set_up_arrays(); /* Set up arrays with blanks and bombs */
while (main_loop() != 'X'); /* Keeps getting user option */
/* Closing message and await a keypress */
printf("Press a key to finish. ");
getch();
}
/*
Set visible & invisible arrays to blank and
set up bombs in invisible array */
void
set_up_arrays() {
/* Fill both grids with spaces */
for (int i=0; i<rows; i++)
for (int j=0; j<cols; j++) {
visible[i][j] = ' ';
invisible[i][j] = ' ';
}
/* Set up bombs in invisible array */
for (int i=0; i<bombs; i++) {
int rowpos, colpos;
/* Loop while random cell has a bomb in it */
do {
rowpos = rand() % rows;
colpos = rand() % cols;
} while (invisible[rowpos][colpos] == 'B');
/* Set invisible square to 'B' for bomb */
invisible[rowpos][colpos] = 'B';
}
}
/*
Show grid, prompt for option / grid position.
Call appropriate function according to option chosen.
Return option chosen (or 'X' if game over).
Called from main() function */
char
main_loop() {
/* Local variables [visible only in main_loop()] */
char prompt;
int rowpos,colpos;
/* Print title and columns */
clrscr();
printf("Bombs Game\n==========\n\n");
/* This would be the easy way of doing it...
printf(" A B C D E F G H I J\n"); */
/* ... but so that the number of letters matches the number of columns: */
printf(" ");
for (int i=0; i<cols; i++) printf("%c ",'A'+i);
printf("\n");
printf("--+-------------------\n");
display_visible_array();
printf("--+-------------------\n\n");
/* If board has all been marked, return an 'X' to finish game
Note chose to do this before anything else after some testing,
otherwise final revealed square is not shown when game over */
if (check_game_over()) return 'X';
get_prompt(&prompt, &rowpos, &colpos);
switch(prompt) {
case('M') : visible[rowpos][colpos] = 'X'; break;
case('U') : visible[rowpos][colpos] = ' '; break;
/* If reveal returns true (non-zero) then break, otherwise
return with an 'exit' */
case('R') : if (bomb_revealed(rowpos,colpos)) return 'X'; else break;
/* If user doesn't want to exit, return a non-X character */
case('X') : if (!exit_check()) return '?';
}
return prompt;
}