UE CS 215 - CS 215 – Fundamentals of Programming II

Unformatted text preview:

CS 215 – Fundamentals of Programming IIFall 2007 (Harlaxton) – Project 430 pointsOut: October 4, 2007Due: October 18, 2007Reminder: Programming Projects (as opposed to Homework problems) are to be your own work. See syllabus for definitions of acceptable assistance from others.Minesweeper is game with a two-dimensional grid playing field. Each grid location (called a cell) may contain a mine. Initially, all cells are covered, hiding any mines from the player. Play is conducted as follows: ● A cell may be uncovered. If a mine is uncovered, the game is lost. Otherwise, the total number of mines in any adjacent grid cells (all 8 directions) is shown in the cell. Using these numbers, together with skill, judgment and a sprinkling of luck, a player should be able to determine the locations of all mines. ● A covered cell may be marked. This is usually used to indicate a grid cell where the player thinks a mine is. ● A covered cell may be unmarked. This removes a mark from a grid cell. ● The game is won if all grid cells have been either marked or uncovered, and the number of marked grid cells is the same as the number of mines. I.e. the player has found all the mines. Consider the following specification for a SweeperGrid class that represents the Minesweeper playing field. Each grid cell contains a SweeperCell object, that contains information regarding the state of the cell. (The SweeperCell class will be provided and is explained below.) Cell locations are given as coordinates (row, col) where (0,0) is the upper left-hand corner of the grid. Specification for SweeperGrid Class Data Attributes For this project, a SweeperGrid is modeled using a dynamically-allocated two-dimensional array of SweeperCells to allow grids of differing sizes to be created. Thus the data attributes include at least those shown below. You may add additional appropriate attributes.Objects Type Namenumber of rows int numRowsnumber of columns int numColumnsnumber of bombs in the grid int numBombspointer to gridSweeperCell**gridOperations ● Explicit-value constructor - receives the number of rows and columns, and a density percentage. Default values are given below. Throws a RangeError exception if initialRows or initialCols is less than 4 (to make sure the grid is large enough) or if the density is not between 20 and 80 (to make sure not all cells are empty or all cells contain bombs). It should dynamically allocate a two-dimensional SweeperCell grid of the specified size. The constructor places density percentage bombs in the grid, and computes the number of adjacent cells containing bombs for each cell. 10/03/07 1 of 8Bombs are to be placed randomly into the grid in the density specified. Density is specified as an integer between 20 and 80 that represents the percentage of cells that will contain bombs. (E.g. 25 means 25%, or one-fourth, of the cells will contain bombs.) The easiest way to do this is to loop through the grid cells, and for each cell use the built-in random number generator to pick a number between 1 and 100. If the number generated is less than the density percentage, then place a bomb in the cell. Note: the random number generator under g++ works as follows. The generator is seeded using srand() (defined in <cstdlib>), which receives a long integer. Usually we want the game to be different each time, so most programmers use the result of the time function (defined in <ctime>) to seed the generator by using: srand(time(0));This should be done once before the first use of the random number generator. The function rand returns the next random integer between 0 and RAND_MAX. Scaling this result to numbers between 1 and 100 can be done by the following code fragment: rand() % 100 + 1Recall that % is the remainder operator in C++. (As shown in the sample run below, the actual density percentage produced by this method can be off by quite a bit, but it should be within 10% or so.) Analysis Objects Default Type Kind Movement Namenumber of rows 4 int variable received initialRowsnumber of columns 4 int variable received initialColsdensity percentage 20 int variable received density● Copy constructor - creates a new SweeperGrid that is identical to an existing one. Analysis Objects Type Kind Movement Nameoriginal SweeperGrid object SweeperGrid variable received original● Destructor - deallocates the grid Analysis - no objects ● operator= - overloaded assignment operator function. Makes an existing SweeperGrid object identical to the original SweeperGrid object. Analysis Objects Type Kind Movement Nameoriginal SweeperGrid object SweeperGrid variable received originalthis SweeperGrid object SweeperGrid variable returned *this10/03/07 2 of 8● GetRows - returns the number of rows in the grid Analysis Objects Type Kind Movement Namenumber of rows int variable returned numRows● GetColumns - returns the number of columns in the grid AnalysisObjects Type Kind Movement Namenumber of columns int variable returned numColumns● GetBombs - returns the number of bombs in the grid Analysis Objects Type Kind Movement Namenumber of bombs int variable returned numBombs● GameWon - returns true if the game has been won Analysis Objects Type Kind Movement Namewin result bool variable returned ---● PlaceBomb - place a bomb in the grid cell at location (row, col). Note this function must recompute the number of adjacent bombs of the cell's neighbors (all 8 directions). If the location (row, col) is not within the bounds of the grid, throws a RangeError exception. Analysis Objects Type Kind Movement Namerow index int variable received rowcolumn index int variable received col● RemoveBomb - remove a bomb in the grid cell at location (row, col). Note this function must recompute the number of adjacent bombs of the cell's neighbors (all 8 directions). If the location (row, col) is not within the bounds of the grid, throws a RangeError exception. Analysis Objects Type Kind Movement Namerow index int variable received rowcolumn index int variable received col10/03/07 3 of 8● Uncover - uncovers the grid cell at location (row, col). Returns true if a bomb is uncovered. If the location (row, col) is not within the bounds of the grid, throws a RangeError exception. Analysis Objects Type Kind Movement Namerow index int variable received rowcolumn index int variable received colbomb check bool variable returned ---● Mark - marks the grid cell at location (row,


View Full Document

UE CS 215 - CS 215 – Fundamentals of Programming II

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download CS 215 – Fundamentals of Programming II
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 215 – Fundamentals of Programming II and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 215 – Fundamentals of Programming II 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?