To turn in: - University of Delaware



Lab2: Minesweeper(85 pts, Due Sunday, Mar 1, midnight)Note: you may work with a partner or you may work alone. If you choose to work with a partner, make sure you both turn in the lab, and make sure you include both names on the lab. Equally, note your partner’s name in canvas. Please be aware that if your partner flakes on you, you are still responsible for completing the lab and turning it in on time.To turn in:Screenshot of running codeCode that compiles Your partner’s name (if you worked with a partner)Functions are scored as below, and then there are 24 points for getting everything working together as a game.Note that this lab took me about 3 hours to code. The rule of thumb is to double that time for students, but you’re probably more cognizant of your programming speed than I am, so plan accordingly.Contents TOC \o "1-3" \h \z \u Instructions: PAGEREF _Toc33130383 \h 1Code: PAGEREF _Toc33130384 \h 2Function Descriptions: PAGEREF _Toc33130385 \h 2// getSize: (2 pts) PAGEREF _Toc33130386 \h 2// makeBoard: ( 5 pts) PAGEREF _Toc33130387 \h 2// printBoard: (5 pts) PAGEREF _Toc33130388 \h 2//placeBombs: (4 pts) PAGEREF _Toc33130389 \h 2//placeCounts: (9 pts) PAGEREF _Toc33130390 \h 3//makeVisibleBoard: (3 pts) PAGEREF _Toc33130391 \h 3// printVisible: (3 pts) PAGEREF _Toc33130392 \h 3//chooseSquare: (6 pts) PAGEREF _Toc33130393 \h 3//addBomb: (5 pts) PAGEREF _Toc33130394 \h 4//removeBomb: (4 pts) PAGEREF _Toc33130395 \h 4//checkForWin: (7 pts) PAGEREF _Toc33130396 \h 4//removeBoard: (5 pts) PAGEREF _Toc33130397 \h 4//removeVisible: (3 pts) PAGEREF _Toc33130398 \h 4/*Function Definitions: */ PAGEREF _Toc33130399 \h 6/*Output */ PAGEREF _Toc33130400 \h 6Instructions:For this lab you’ll be creating the game, Minesweeper.Minesweeper is a game in which you’ve got a board with hidden bombs. As you choose squares, the number of bombs adjacent to the square is revealed. You can place bomb markers on the board where you think bombs are located. After you’ve placed a marker on the board where you think every bomb is, the game checks to see if you are correct. If you are, you win. If not, you lose. If, as you are playing, you accidentally choose to reveal a square where a bomb is located, you blow up and lose. Note that you can also change your mind and remove a bomb marker from the board if you think you were wrong.I have included a main function plus descriptions of the functions you are to write for this game. I’ve also included output from my playing the game at the bottom of the lab. You should place the function declarations above the main function, and the function definitions below the main function. Note that I’ve also included some test code if you want to test your functions as you go.Have fun!Code:Function Descriptions:* minesweeper.cpp * * Created on: Feb 20, 2020 * Author: Debra */#include <iostream>#include <time.h>#include <stdlib.h>using namespace std;/*FUNCTION DECLARATIONS GO HERE!!!! */// getSize: (2 pts)// function getSize uses call by reference to modify the integer input parameter to a random// number between 7 and 20 (not including 20). This will be the size of your board// makeBoard: ( 5 pts)// Function makeboard takes as input an integer: which is the length and width of the board// (it's a square board). The function should create an integer matrix (a 2-d array) on the heap, fill// matrix with 0s (that's the number, not the letter 'O') and return a pointer to the matrix// printBoard: (5 pts)// This method takes as input a pointer to a matrix (a 2-D function) of integers and an// integer for the size (the length and the width - it's a square). It returns nothing.// It should print out the matrix, only it should print a space instead of a 0 (in other words, I don't// want to see a board of 0's - I'd rather just see blank spaces for the 0s).// NOTE: to print out the board, I used tabs instead of endl's, e.g., cout << x << "\t";// NOTE2: I also printed out the indices around the edges so the game would be// easier to check when I was debugging, not to mention easier to play.//placeBombs: (4 pts)//This function takes as an input parameter a pointer to the 2-D matrix of integers (the board) and an// integer (the size).//It places size + 1 "bombs" randomly on the 2-D matrix. My bombs were the 9 value (because the board // is a board of integers and that kind of looks like an upside down 'b'). It made sure that a bomb hadn't// already been placed in the randomly selected place//on the board before placing it, and, if it had, it chose another spot to place the bomb// NOTE: if you'd like to write a helper function for this, I'm okay with that.//placeCounts: (9 pts)//This function takes as input parameters a pointer to the 2-D integer matrix that is the board, an int// representing the size of the board. It returns nothing.//This function places the counts on the board - in minesweeper, when you select a square, that square//becomes "visible" and should contain the numbe of "bombs" that are adjacent to that square. So, for// instance, if the board was a 7 x 7 matrix with 8 bombs, it would look like this:/* * 01234560991122123 111 114 1922395 2392996 192222 *///This function calculates the number of adjacent bombs for each square on the matrix and places those//counts on the matrix, making sure not to overwrite a bomb.//Again, if you'd like to write a helper function for this one, feel free.//makeVisibleBoard: (3 pts)//This function takes as an input parameter the integer for the size of the 2-D matrix of characters that will // be created in this function and returned from this function. The 2-D matrix will be initialized to all '-'//This function is similar to the makeBoard function. It will be the board that is displayed to the user as // the user plays the game (because you don't want the user to see all the bombs and counts)// printVisible: (3 pts)// Almost exactly like printMatrix (templates anyone??):// This method takes as input a pointer to a matrix (a 2-D function) of Chars and an// integer for the size (the length and the width - it's a square). It returns nothing.// It should print out the matrix.// NOTE: like with printMatrix, I printed out the indices around the edges so the game would be// easier to check when I was debugging, not to mention easier to play.//chooseSquare: (6 pts)//This function takes as input parameters the pointer to the board matrix of ints, the pointer to the visible// matrix of chars, and the size.//It uses cout and cin to allow the user to choose a square (the x and the y position on the matrix // respectively) and then sets the visible matrix at that square to be the value in the board matrix.//This function returns a boolean - false if the square chosen was a bomb, and true otherwise.//NOTE: the quick way to convert a single integer to a char is to do the following://int x = 7;//char c = '0' + x;// now c holds '7' (not the int 7).//addBomb: (5 pts)//This function takes as input parameters the pointer to the visible matrix of chars, the size int, and a pointer// to the number of bombs found. It returns a boolean value (true if the number of bombs found is equal// to size + 1, false otherwise.//this function is allowing the user to choose a square where they think a bomb is and mark it as bomb in // the visible matrix.//It allows the user to choose a square, using cout and cin to get the x and y values, respectively, and then//modifies that square in the visible matrix (I marked it with the '9' character, but you could make it be an // X or anything you feel makes it obvious that you think there's abomb there. It then increases the third // parameter by 1 (the pointer to the number of bombs found so far). It checks if the number of bombs // found is equal to the total number of bombs and, if so, returns True. Otherwise it returns false//removeBomb: (4 pts)//This function takes as input parameters the pointer to the visible matrix of chars, the size int, and a pointer//to the number of bombs found. It returns nothing.//this function is allowing the user to choose a square where they previously placed a bomb and unmark the//square.//It allows the user to choose a square, using cout and cin to get the x and y values, respectively, and then//modifies that square if it is a '9' (how I marked my bombs) to be a '-'. It also decreases the count of the//number of bombs found.//Note that it checks to make sure the user had previously marked that square as where they thought a bomb//was.//checkForWin: (7 pts) //this function takes as input the pointer to the board matrix of integers, the pointer to the visible matrix//of characters, and the int size.//It checks to make sure that each bomb in the board matrix has been marked as a bomb on the visible//matrix. It returns a boolean value - true if all the bombs have been found, and false otherwise.//removeBoard: (5 pts)//This function takes as input parameters the pointer to the 2-D integer matrix that is the board, along with the// size integer, and removes the matrix from the heap. It returns nothing.//removeVisible: (3 pts)//This function takes as input parameters the pointer to the 2-D integer matrix that is the board, along with the// size integer, and removes the matrix from the heap. It returns nothing./******************************END OF FUNCTION DECLARATIONS ******************************/int main() {srand(time(NULL));int size = 0;int bombsfound = 0;getSize(size);cout << "Size is " << size << endl;int **mat = makeBoard(size);//printBoard(mat, size); - for testing purposesplaceBombs(mat,size);//printBoard(mat, size); - for testing purposesplaceCounts(mat, size);//printBoard(mat, size); - for testing purposeschar **visible = makeVisibleBoard(size);/* For testing purposes: */printVisible(visible,size);chooseSquare(mat,visible,size);printVisible(visible,size);addBomb(visible, size, &bombsfound);printVisible(visible,size);removeBomb(visible, size, &bombsfound);printVisible(visible,size);checkForWin(mat, visible, size);/* End of testing */char c;bool flag = true;char again = 'n';while (flag) {printVisible(visible,size);cout << "Choose: A for choose square, B for add Bomb, C for remove bomb: " << endl;cin >> c;if (c == 'A' || c == 'a') {flag = chooseSquare(mat, visible, size);if (!flag) {cout << "YOU LOSE! YOU HIT A BOMB!!" << endl;printBoard(mat, size);cout << "\n\nDo you want to play again?" <<endl;cin >> again;}}if (c == 'B' || c == 'b') {if (addBomb(visible, size, &bombsfound)) {cout << "Bombs found: " << bombsfound << endl;cout << "You appear to think you've found all the bombs" << endl;cout << "Choose: D for check for Win, or C for remove bomb:" << endl;cin >> c;if (c == 'D' || c == 'd') {if (checkForWin(mat, visible, size)) {cout <<"YOU WON!!! WOO WOO!!!" << endl;printBoard(mat, size);}else {cout <<"Sorry, you were wrong. Boo hoo." << endl;printBoard(mat, size);}removeBoard(mat, size);removeVisible(visible, size);bombsfound = 0;flag = false;}cout << "\n\nDo you want to play again? (y or n)" <<endl;cin >> again;}}if (c == 'C' || c == 'c') {removeBomb(visible, size, &bombsfound);}if (!flag && (again == 'y' || again == 'Y')) {flag = true;bombsfound = 0;getSize(size);cout << "Size is " << size << endl;mat = makeBoard(size);//printBoard(mat, size); - for testing purposesplaceBombs(mat,size);//printBoard(mat, size); - for testing purposesplaceCounts(mat, size);//printBoard(mat, size); - for testing purposesvisible = makeVisibleBoard(size);}}return 0;}/********************************FUNCTION DEFINITIONS GO HERE***************************************//*Function Definitions: *//********************************OUTPUT OF GAME********************************************//*Output */Size is 701234560-------1-------2-------3-------4-------5-------6-------Choose square x (between 0 and 7): 3Choose square y (between 0 and 7): 8Invalid square, try again:Choose square x (between 0 and 7): 3Choose square y (between 0 and 7): 201234560-------1-------2-------3--3----4-------5-------6-------Add bomb: choose square x(between 0 and 7): 4Add bomb: choose square y(between 0 and 7): 601234560-------1-------2-------3--3----4------95-------6-------Remove bomb: choose square x(between 0 and 7): 4Remove bomb: choose square y (between 0 and 7): 601234560-------1-------2-------3--3----4-------5-------6-------01234560-------1-------2-------3--3----4-------5-------6-------Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 6Choose square y (between 0 and 7): 601234560-------1-------2-------3--3----4-------5-------6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 5Choose square y (between 0 and 7): 501234560-------1-------2-------3--3----4-------5-----1-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 4Choose square y (between 0 and 7): 401234560-------1-------2-------3--3----4----1--5-----1-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 3Choose square y (between 0 and 7): 301234560-------1-------2-------3--32---4----1--5-----1-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 2Choose square y (between 0 and 7): 201234560-------1-------2--3----3--32---4----1--5-----1-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 1Choose square y (between 0 and 7): 101234560-------1-2-----2--3----3--32---4----1--5-----1-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 0Choose square y (between 0 and 7): 0012345601------1-2-----2--3----3--32---4----1--5-----1-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 3Choose square y (between 0 and 7): 1YOU LOSE! YOU HIT A BOMB!!0123456019211 1223921 2293291 3393211 4939211 5121291 6 111 Do you want to play again?ySize is 701234560-------1-------2-------3-------4-------5-------6-------Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 6Choose square y (between 0 and 7): 601234560-------1-------2-------3-------4-------5-------6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 5Choose square y (between 0 and 7): 501234560-------1-------2-------3-------4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 0Choose square y (between 0 and 7): 0012345600------1-------2-------3-------4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 1Choose square y (between 0 and 7): 1012345600------1-0-----2-------3-------4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 2Choose square y (between 0 and 7): 2012345600------1-0-----2--1----3-------4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 2Choose square y (between 0 and 7): 1012345600------1-0-----2-01----3-------4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 3Choose square y (between 0 and 7): 1012345600------1-0-----2-01----3-1-----4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 3Choose square y (between 0 and 7): 0012345600------1-0-----2-01----311-----4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 0Choose square y (between 0 and 7): 2012345600-0----1-0-----2-01----311-----4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 0Choose square y (between 0 and 7): 3012345600-00---1-0-----2-01----311-----4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 0Choose square y (between 0 and 7): 4012345600-001--1-0-----2-01----311-----4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 1Choose square y (between 0 and 7): 2012345600-001--1-01----2-01----311-----4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 1Choose square y (between 0 and 7): 3012345600-001--1-011---2-01----311-----4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 1Choose square y (between 0 and 7): 4012345600-001--1-0113--2-01----311-----4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 3Choose square y (between 0 and 7): 2012345600-001--1-0113--2-01----3112----4-------5-----2-6------0Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 6Choose square y (between 0 and 7): 5012345600-001--1-0113--2-01----3112----4-------5-----2-6-----10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 5Choose square y (between 0 and 7): 6012345600-001--1-0113--2-01----3112----4-------5-----206-----10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 4Choose square y (between 0 and 7): 6012345600-001--1-0113--2-01----3112----4------05-----206-----10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 3Choose square y (between 0 and 7): 6012345600-001--1-0113--2-01----3112---14------05-----206-----10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 4Choose square y (between 0 and 7): 5012345600-001--1-0113--2-01----3112---14-----205-----206-----10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 3Choose square y (between 0 and 7): 5012345600-001--1-0113--2-01----3112--314-----205-----206-----10Choose: A for choose square, B for add Bomb, C for remove bomb: bAdd bomb: choose square x(between 0 and 7): 2Add bomb: choose square y(between 0 and 7): 3012345600-001--1-0113--2-019---3112--314-----205-----206-----10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 2Choose square y (between 0 and 7): 4012345600-001--1-0113--2-0193--3112--314-----205-----206-----10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 3Choose square y (between 0 and 7): 3012345600-001--1-0113--2-0193--31123-314-----205-----206-----10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 6Choose square y (between 0 and 7): 2012345600-001--1-0113--2-0193--31123-314-----205-----206--1--10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 5Choose square y (between 0 and 7): 0012345600-001--1-0113--2-0193--31123-314-----2051----206--1--10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 6Choose square y (between 0 and 7): 0012345600-001--1-0113--2-0193--31123-314-----2051----2060-1--10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 6Choose square y (between 0 and 7): 1012345600-001--1-0113--2-0193--31123-314-----2051----206001--10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 5Choose square y (between 0 and 7): 1012345600-001--1-0113--2-0193--31123-314-----20511---206001--10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 5Choose square y (between 0 and 7): 2012345600-001--1-0113--2-0193--31123-314-----205112--206001--10Choose: A for choose square, B for add Bomb, C for remove bomb: bAdd bomb: choose square x(between 0 and 7): 4Add bomb: choose square y(between 0 and 7): 1012345600-001--1-0113--2-0193--31123-314-9---205112--206001--10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 4Choose square y (between 0 and 7): 2012345600-001--1-0113--2-0193--31123-314-92--205112--206001--10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 4Choose square y (between 0 and 7): 3012345600-001--1-0113--2-0193--31123-314-923-205112--206001--10Choose: A for choose square, B for add Bomb, C for remove bomb: bAdd bomb: choose square x(between 0 and 7): 3Add bomb: choose square y(between 0 and 7): 4012345600-001--1-0113--2-0193--311239314-923-205112--206001--10Choose: A for choose square, B for add Bomb, C for remove bomb: bAdd bomb: choose square x(between 0 and 7): 4Add bomb: choose square y(between 0 and 7): 4012345600-001--1-0113--2-0193--311239314-9239205112--206001--10Choose: A for choose square, B for add Bomb, C for remove bomb: bAdd bomb: choose square x(between 0 and 7): 5Add bomb: choose square y(between 0 and 7): 3012345600-001--1-0113--2-0193--311239314-92392051129-206001--10Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 6Choose square y (between 0 and 7): 3012345600-001--1-0113--2-0193--311239314-92392051129-2060012-10Choose: A for choose square, B for add Bomb, C for remove bomb: bAdd bomb: choose square x(between 0 and 7): 6Add bomb: choose square y(between 0 and 7): 4012345600-001--1-0113--2-0193--311239314-92392051129-2060012910Choose: A for choose square, B for add Bomb, C for remove bomb: aChoose square x (between 0 and 7): 0Choose square y (between 0 and 7): 6012345600-001-11-0113--2-0193--311239314-92392051129-2060012910Choose: A for choose square, B for add Bomb, C for remove bomb: bAdd bomb: choose square x(between 0 and 7): 2Add bomb: choose square y(between 0 and 7): 5012345600-001-11-0113--2-01939-311239314-92392051129-2060012910Choose: A for choose square, B for add Bomb, C for remove bomb: bAdd bomb: choose square x(between 0 and 7): 0Add bomb: choose square y(between 0 and 7): 5Bombs found: 8You appear to think you've found all the bombsChoose: D for check for Win, or C for remove bomb:dYOU WON!!! WOO WOO!!!01234560 1911 113222 19391311239314192392 5112932 6 1291 Do you want to play again? (y or n)n ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download