doobdood

Minesweeper.cpp

May 9th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Minesweeper.h"
  2.  
  3. Minesweeper::Minesweeper()
  4. {
  5.     mt19937 generator(time(0)); //create the rng w/ time as our seed
  6.     uniform_int_distribution<int> board_init(1, 15);
  7.  
  8.     for(int i = 0; i < 17; i++) { //set everything in the board to 0
  9.         for(int j = 0; j < 17; j++) {
  10.             board[i][j][0] = 0;
  11.             board[i][j][1] = 0;
  12.         }
  13.     }
  14.  
  15.     int boardY; //will be used as coordinates for mines
  16.     int boardX;
  17.  
  18.     for(int i = 0; i < mines; i++) { //create all the mines in random spots
  19.         boardY = board_init(generator) + 1;
  20.         boardX = board_init(generator) + 1;
  21.  
  22.         if(board[boardY][boardX][0] == 0) board[boardY][boardX][0] = 9;
  23.                                         //the number 9 marks a mine
  24.         else                              i--; //repeat a loop if the location
  25.                                                //is already a mine
  26.  
  27.     }
  28.  
  29.     for(int i = 1; i < 16; i++) { //the double for goes through each point
  30.         for(int j = 1; j < 16; j++) {
  31.             if(board[i][j][0] == 9) pointCheck(i, j);
  32.         }
  33.     }
  34. }
  35.  
  36. void Minesweeper::runGame()
  37. {
  38.     printBoard(); //just so I don't print board twice in the while loop
  39.  
  40.     while(gameRunning) {
  41.         checkInput(_getch()); //waiting for input...
  42.  
  43.         printBoard();
  44.         checkGameWon(); //make sure you haven't won yet
  45.     }
  46.  
  47.     printBoard(); //prints final board onto screen
  48.  
  49.     if(gameWon) cout << "\nCongratulations! You won! "; //Nice job!
  50.     else        cout << "\nOh no! You stepped on a land mine! "; //lol u lost nub
  51. }
  52.  
  53. void Minesweeper::pointCheck(int y, int x) //only runs if it's
  54. {                                          //assingning values to numbers-to-be
  55.     for(int i = -1; i < 2; i++) { //both loops represent distance in x/y
  56.         for(int j = -1; j < 2; j++) {
  57.             if(board[y + i][x + j][0] != 9) board[y + i][x + j][0]++;
  58.                                 //adjacent squares have values increased by 1
  59.         }
  60.     }
  61. }
  62.  
  63. void Minesweeper::printBoard()
  64. {
  65.     system("cls"); //clears screen
  66.  
  67.     for(int i = 1; i < 16; i++) { //goes through entire board
  68.         for(int j = 1; j < 16; j++) {
  69.             if(i == positionY && j == positionX) { //this is how I mark the cursor
  70.                 if(gameRunning)   cout << '+';
  71.                 else if(!gameWon) cout << 'x'; //shows where you died
  72.                 else              cout << board[i][j][0]; //nothing happens if you won
  73.             } else {
  74.                 switch(board[i][j][1]) { //checks status of each cell
  75.                 case 0: //this means it's not revealed yet
  76.                     cout << '.';
  77.                     break;
  78.                 case 1: //this means it's revealed!
  79.                     if(board[i][j][0] == 0)      wcout << ' ';
  80.                     else if(board[i][j][0] == 9) cout << '*'; //for when you win the game only
  81.                     else                         cout << board[i][j][0];
  82.                     break;
  83.                 case 2: //this means there's a flag
  84.                     cout << '!';
  85.                     break;
  86.                 case 3: //this means it's marked as unknown
  87.                     cout << '?';
  88.                     break;
  89.                 }
  90.             }
  91.         }
  92.  
  93.         cout << endl;
  94.     }
  95.  
  96.     if(gameRunning) { //prints instructions while you are playing. How nice!
  97.         cout << "\nUse WASD to move the cursor (+) around.\n"
  98.          << "Use the Q key to create or remove a flag (!), \n"
  99.          << "use the Q key on a flag to mark it as unknown (?), \n"
  100.          << "and use the Q key a third time to remove the mark. \n"
  101.          << "Use the Spacebar to reveal the square the cursor is on. \n"
  102.          << "There are currently " << mines << " mines left. \n"
  103.          << "Please enter your command: ";
  104.     }
  105. }
  106.  
  107. void Minesweeper::checkInput(int key) //processes each key stroke
  108. {
  109.     switch(key) {
  110.     case 'w': //these 4 cases are for movement
  111.         if(positionY != 1) positionY--;
  112.         break;
  113.     case 's':
  114.         if(positionY != 15) positionY++;
  115.         break;
  116.     case 'a':
  117.         if(positionX != 1) positionX--;
  118.         break;
  119.     case 'd':
  120.         if(positionX != 15) positionX++;
  121.         break;
  122.     case ' ': //equivalence to clicking a square!
  123.         checkClick();
  124.         break;
  125.     case 'q': //this is for marking as flag, unknown, or removing mark
  126.         switch(board[positionY][positionX][1]) {
  127.         case 0:
  128.             board[positionY][positionX][1] = 2;
  129.             mines--;
  130.             break;
  131.         case 2:
  132.             board[positionY][positionX][1] = 3;
  133.             mines++;
  134.             break;
  135.         case 3:
  136.             board[positionY][positionX][1] = 0;
  137.             break;
  138.         }
  139.         break;
  140.     }
  141. }
  142.  
  143. void Minesweeper::checkClick() //processes your "clicks"
  144. {
  145.     if(board[positionY][positionX][1] != 1) { //only does stuff if square was not revealed yet
  146.         if(board[positionY][positionX][0] == 9) { //This happens when you step on a mine!
  147.             board[positionY][positionX][1] = 1;
  148.             gameRunning = false; //exits loop in runGame();
  149.             gameWon = false; //u lost scurb
  150.         } else if(board[positionY][positionX][0] != 0) { //reveals square if it's a number
  151.             board[positionY][positionX][1] = 1;
  152.         } else if(board[positionY][positionX][0] == 0) {
  153.             for(int i = -1; i < 2; i++) { //prepare yourselves...
  154.                 for(int j = -1; j < 2; j++) {
  155.                     if(board[positionY + i][positionX + j][0] == 0 && //marks squares for when you click
  156.                        board[positionY + i][positionX + j][1] == 0) { //an empty cell and everything around it
  157.                                                                       //gets clicked too
  158.                         board[positionY + i][positionX + j][1] = 4;
  159.  
  160.                     } else board[positionY + i][positionX + j][1] = 1; //reveals squares otherwise
  161.                 }
  162.             }
  163.  
  164.             board[positionY][positionX][1] = 1; //reveals empty cell you clicked
  165.         }
  166.     }
  167.  
  168.     checkPendingSpace();
  169. }
  170.  
  171. void Minesweeper::checkPendingSpace() //looks for marked squares and acts accordingly
  172. {
  173.     while(checkPendingAmt() > 0) { //keep looking for marked squares if they exist
  174.         for(int i = 1; i < 16; i++) {
  175.             for(int j = 1; j < 16; j++){ //runs through entire board
  176.                 if(board[i][j][1] == 4) { //takes action if square was marked
  177.                     board[i][j][1] = 1; //reveals said marked square
  178.  
  179.                     for(int a = -1; a < 2; a++) { //checks surroundings of said square
  180.                         for(int b = -1; b < 2; b++) {
  181.  
  182.                             if(board[i + a][j + b][0] == 0 && //marks empty squares
  183.                                board[i + a][j + b][1] != 1) {
  184.  
  185.                                 board[i + a][j + b][1] = 4;
  186.                             } else { //reveals everything else
  187.                                 board[i + a][j + b][1] = 1;
  188.                             }
  189.                         }
  190.                     }
  191.                 }
  192.             }
  193.         }
  194.     }
  195. }
  196.  
  197. int Minesweeper::checkPendingAmt() //searches entire board for marked squares
  198. {                                  //returns amt of marked squares
  199.     int pendingAmt = 0;
  200.  
  201.     for(int i = 1; i < 16; i++) {
  202.         for(int j = 1; j < 16; j++) {
  203.             if(board[i][j][1] == 4) pendingAmt++;
  204.         }
  205.     }
  206.  
  207.     return pendingAmt;
  208. }
  209.  
  210. void Minesweeper::checkGameWon() //checks if you have won the game or not
  211. {
  212.     int safeSquares = 15 * 15 - 35; //number of "safe" squares remaining
  213.     int minesFound = 35; //number of mines you have not found
  214.                          //I don't know why I chose such a bad name for that...
  215.     for(int i = 1; i < 16; i++) { //runs through entire board
  216.         for(int j = 1; j < 16; j++) {
  217.             if(board [i][j][0] < 9 && board [i][j][1] == 1) { //if number or empty and
  218.                 safeSquares--;                                //has been revealed, remaining safe
  219.             }                                                 //squares are reduced by 1
  220.  
  221.             if(board [i][j][0] == 9 && board [i][j][1] == 2) { //if mine was correctly marked,
  222.                 minesFound--;                                  //minesFound is reduced by 1
  223.             }
  224.         }
  225.     }
  226.  
  227.     if(safeSquares == 0 || minesFound == 0) { //if you revealed all safe squares
  228.         gameRunning = false;                  //or marked all mines, you win!
  229.         gameWon = true;
  230.     }
  231. }
Add Comment
Please, Sign In to add comment