Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Minesweeper.h"
- Minesweeper::Minesweeper()
- {
- mt19937 generator(time(0)); //create the rng w/ time as our seed
- uniform_int_distribution<int> board_init(1, 15);
- for(int i = 0; i < 17; i++) { //set everything in the board to 0
- for(int j = 0; j < 17; j++) {
- board[i][j][0] = 0;
- board[i][j][1] = 0;
- }
- }
- int boardY; //will be used as coordinates for mines
- int boardX;
- for(int i = 0; i < mines; i++) { //create all the mines in random spots
- boardY = board_init(generator) + 1;
- boardX = board_init(generator) + 1;
- if(board[boardY][boardX][0] == 0) board[boardY][boardX][0] = 9;
- //the number 9 marks a mine
- else i--; //repeat a loop if the location
- //is already a mine
- }
- for(int i = 1; i < 16; i++) { //the double for goes through each point
- for(int j = 1; j < 16; j++) {
- if(board[i][j][0] == 9) pointCheck(i, j);
- }
- }
- }
- void Minesweeper::runGame()
- {
- printBoard(); //just so I don't print board twice in the while loop
- while(gameRunning) {
- checkInput(_getch()); //waiting for input...
- printBoard();
- checkGameWon(); //make sure you haven't won yet
- }
- printBoard(); //prints final board onto screen
- if(gameWon) cout << "\nCongratulations! You won! "; //Nice job!
- else cout << "\nOh no! You stepped on a land mine! "; //lol u lost nub
- }
- void Minesweeper::pointCheck(int y, int x) //only runs if it's
- { //assingning values to numbers-to-be
- for(int i = -1; i < 2; i++) { //both loops represent distance in x/y
- for(int j = -1; j < 2; j++) {
- if(board[y + i][x + j][0] != 9) board[y + i][x + j][0]++;
- //adjacent squares have values increased by 1
- }
- }
- }
- void Minesweeper::printBoard()
- {
- system("cls"); //clears screen
- for(int i = 1; i < 16; i++) { //goes through entire board
- for(int j = 1; j < 16; j++) {
- if(i == positionY && j == positionX) { //this is how I mark the cursor
- if(gameRunning) cout << '+';
- else if(!gameWon) cout << 'x'; //shows where you died
- else cout << board[i][j][0]; //nothing happens if you won
- } else {
- switch(board[i][j][1]) { //checks status of each cell
- case 0: //this means it's not revealed yet
- cout << '.';
- break;
- case 1: //this means it's revealed!
- if(board[i][j][0] == 0) wcout << ' ';
- else if(board[i][j][0] == 9) cout << '*'; //for when you win the game only
- else cout << board[i][j][0];
- break;
- case 2: //this means there's a flag
- cout << '!';
- break;
- case 3: //this means it's marked as unknown
- cout << '?';
- break;
- }
- }
- }
- cout << endl;
- }
- if(gameRunning) { //prints instructions while you are playing. How nice!
- cout << "\nUse WASD to move the cursor (+) around.\n"
- << "Use the Q key to create or remove a flag (!), \n"
- << "use the Q key on a flag to mark it as unknown (?), \n"
- << "and use the Q key a third time to remove the mark. \n"
- << "Use the Spacebar to reveal the square the cursor is on. \n"
- << "There are currently " << mines << " mines left. \n"
- << "Please enter your command: ";
- }
- }
- void Minesweeper::checkInput(int key) //processes each key stroke
- {
- switch(key) {
- case 'w': //these 4 cases are for movement
- if(positionY != 1) positionY--;
- break;
- case 's':
- if(positionY != 15) positionY++;
- break;
- case 'a':
- if(positionX != 1) positionX--;
- break;
- case 'd':
- if(positionX != 15) positionX++;
- break;
- case ' ': //equivalence to clicking a square!
- checkClick();
- break;
- case 'q': //this is for marking as flag, unknown, or removing mark
- switch(board[positionY][positionX][1]) {
- case 0:
- board[positionY][positionX][1] = 2;
- mines--;
- break;
- case 2:
- board[positionY][positionX][1] = 3;
- mines++;
- break;
- case 3:
- board[positionY][positionX][1] = 0;
- break;
- }
- break;
- }
- }
- void Minesweeper::checkClick() //processes your "clicks"
- {
- if(board[positionY][positionX][1] != 1) { //only does stuff if square was not revealed yet
- if(board[positionY][positionX][0] == 9) { //This happens when you step on a mine!
- board[positionY][positionX][1] = 1;
- gameRunning = false; //exits loop in runGame();
- gameWon = false; //u lost scurb
- } else if(board[positionY][positionX][0] != 0) { //reveals square if it's a number
- board[positionY][positionX][1] = 1;
- } else if(board[positionY][positionX][0] == 0) {
- for(int i = -1; i < 2; i++) { //prepare yourselves...
- for(int j = -1; j < 2; j++) {
- if(board[positionY + i][positionX + j][0] == 0 && //marks squares for when you click
- board[positionY + i][positionX + j][1] == 0) { //an empty cell and everything around it
- //gets clicked too
- board[positionY + i][positionX + j][1] = 4;
- } else board[positionY + i][positionX + j][1] = 1; //reveals squares otherwise
- }
- }
- board[positionY][positionX][1] = 1; //reveals empty cell you clicked
- }
- }
- checkPendingSpace();
- }
- void Minesweeper::checkPendingSpace() //looks for marked squares and acts accordingly
- {
- while(checkPendingAmt() > 0) { //keep looking for marked squares if they exist
- for(int i = 1; i < 16; i++) {
- for(int j = 1; j < 16; j++){ //runs through entire board
- if(board[i][j][1] == 4) { //takes action if square was marked
- board[i][j][1] = 1; //reveals said marked square
- for(int a = -1; a < 2; a++) { //checks surroundings of said square
- for(int b = -1; b < 2; b++) {
- if(board[i + a][j + b][0] == 0 && //marks empty squares
- board[i + a][j + b][1] != 1) {
- board[i + a][j + b][1] = 4;
- } else { //reveals everything else
- board[i + a][j + b][1] = 1;
- }
- }
- }
- }
- }
- }
- }
- }
- int Minesweeper::checkPendingAmt() //searches entire board for marked squares
- { //returns amt of marked squares
- int pendingAmt = 0;
- for(int i = 1; i < 16; i++) {
- for(int j = 1; j < 16; j++) {
- if(board[i][j][1] == 4) pendingAmt++;
- }
- }
- return pendingAmt;
- }
- void Minesweeper::checkGameWon() //checks if you have won the game or not
- {
- int safeSquares = 15 * 15 - 35; //number of "safe" squares remaining
- int minesFound = 35; //number of mines you have not found
- //I don't know why I chose such a bad name for that...
- for(int i = 1; i < 16; i++) { //runs through entire board
- for(int j = 1; j < 16; j++) {
- if(board [i][j][0] < 9 && board [i][j][1] == 1) { //if number or empty and
- safeSquares--; //has been revealed, remaining safe
- } //squares are reduced by 1
- if(board [i][j][0] == 9 && board [i][j][1] == 2) { //if mine was correctly marked,
- minesFound--; //minesFound is reduced by 1
- }
- }
- }
- if(safeSquares == 0 || minesFound == 0) { //if you revealed all safe squares
- gameRunning = false; //or marked all mines, you win!
- gameWon = true;
- }
- }
Add Comment
Please, Sign In to add comment