Advertisement
Peaser

ms

Dec 24th, 2014
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <conio.h>
  4. #include <cstdlib>
  5. #include <cctype>
  6. #include <ctime>
  7. #include <vector>
  8. #include <limits>
  9. #include <iomanip>
  10. #include <fstream>
  11.  
  12. using namespace std;
  13.  
  14. void main_menu();
  15. void set_level();
  16. int choose_level();
  17. void show_score();
  18. void show_board(vector<vector <int> >&, bool show_mies = false);
  19. void goto_xy(int, int);
  20. void save_score(const int&);
  21. void save_score(const int&);
  22. void set_boards(vector <vector <int> >&);
  23. void set_mines(vector<vector <int> >&, const int&);
  24. int get_input (string, const int, const int lower_limit = 0);
  25.  
  26. /**
  27.     Struct For Make Limits Of Curcor In Console...!!
  28.     left_x_limit, right_x_limit They Specify The x Co-ordinates Of The Console Between Which Cursor Can Move. Horizontally..!!
  29.     up_y_limit, down_y_limit They Specify The y Co-ordinates Of The Console Between Which Cursor Can Move Vertically...!!
  30. */
  31. struct ConsoleLimits
  32. {
  33.     int left_x_limit,
  34.          right_x_limit,
  35.          up_y_limit,
  36.          down_y_limit;
  37.  
  38. } console_limits;
  39.  
  40. /**
  41.     This Struct Is For Different Type Of Data Used In The Game...!!
  42.     1. no_of_row No. Of Rows Of The Board Display On The Screen....!!!
  43.     2. no_of_cols No. Of Columns Of The Board Display On The Screen....!!!
  44.     3. no_of_mines No. Of Mines That Will Set On The Board...!!
  45.     4. x_pos The x Co-Ordinate Of Cusor On Console...!!
  46.     5. y_pos The y Co-Ordinate Of Cusor On Console...!!
  47. */
  48. struct GameData
  49. {
  50.     int no_of_rows,
  51.         no_of_cols,
  52.         no_of_mines,
  53.         x_pos,
  54.         y_pos;
  55.  
  56.     static const int mine_symbol = 0;
  57.  
  58. } game_date;
  59.  
  60.  
  61. int main()
  62. {
  63.     system("COLOR 1F");
  64.     SetConsoleTitle("Mine Sweeper Game..!!");              // Change Title Of The Console...!!
  65.     typedef vector < vector <int > > Board;                 // Define Own Type Of 2-D Vector...!!
  66.     do
  67.     {
  68.         // Playing Main Backgroung Music For The Game...!!
  69.         // PlaySound(TEXT("BackGround Music!_1.wav"), NULL, SND_FILENAME | SND_LOOP | SND_ASYNC);
  70.         // Calling Main Menu...!!
  71.         main_menu();
  72.         // Play Game Sound...!!
  73.         // PlaySound(TEXT("BackGround Music!.wav"), NULL, SND_FILENAME | SND_LOOP | SND_ASYNC);
  74.         // Ask From User To Slect Level...!!
  75.         set_level();
  76.  
  77.         // The No Of Place That Has No Mines i.e., Opened...!!
  78.         int no_of_unmines = (game_date.no_of_rows * game_date.no_of_cols) - game_date.no_of_mines;
  79.  
  80.         int row_pos = 0,        // Row Postion Of Board On Which Cusor Is Currently Is....!!!
  81.              col_pos = 0,         // Column Postion Of Board On Which Cusor Is Currently Is....!!!
  82.              score = 0;             // Score Made By User...!!
  83.  
  84.         char ch;                            // Charecter Entered By User....!! Valid Are (A, S, D, W, a, s, d, w)...!!
  85.         bool game_over = false;         // If User Hit The Mine, Then It Will Be True...!!!
  86.  
  87.         // Make A Board For Playing Game...!!
  88.         Board board(game_date.no_of_rows,  vector <int > (game_date.no_of_cols));
  89.  
  90.         // Set Board With Random Numbers [1, 9]
  91.         set_boards(board);
  92.         // Set Mines On Board...!!!
  93.         set_mines(board, game_date.no_of_mines);
  94.         show_board(board);
  95.  
  96.         game_date.x_pos = console_limits.left_x_limit - 1;
  97.         game_date.y_pos = console_limits.up_y_limit - 1;
  98.         goto_xy(game_date.x_pos, game_date.y_pos);
  99.  
  100.         while (!game_over && score < no_of_unmines)
  101.         {
  102.                 goto_xy(15, console_limits.down_y_limit + 3);
  103.                 cout <<setw(15) << "Row Position" << setw(15) << "Col Position\n";
  104.                 goto_xy(15, console_limits.down_y_limit + 4);
  105.                 cout << setw(10) << row_pos << setw(13) << col_pos << endl;
  106.                 goto_xy(25, console_limits.down_y_limit + 6);
  107.                 cout << "Score: " << score;
  108.  
  109.                 goto_xy(game_date.x_pos, game_date.y_pos);
  110.                 ch = _getch();
  111.  
  112.                 switch (toupper(ch))
  113.                 {
  114.                     case 'D':
  115.                         {
  116.                             if (game_date.x_pos < console_limits.right_x_limit)
  117.                             {
  118.                                 game_date.x_pos += 2;
  119.                                 col_pos++;
  120.                             }
  121.                         }
  122.                     break;
  123.  
  124.                     case 'A':
  125.                         {
  126.                             if (game_date.x_pos > console_limits.left_x_limit)
  127.                             {
  128.                                 game_date.x_pos -= 2;
  129.                                 col_pos--;
  130.                             }
  131.                         }
  132.                     break;
  133.  
  134.                     case 'W':
  135.                         {
  136.                             if(game_date.y_pos > console_limits.up_y_limit)
  137.                             {
  138.                                 game_date.y_pos -= 2;
  139.                                 row_pos--;
  140.                             }
  141.                         }
  142.                         break;
  143.  
  144.                     case 'S':
  145.                         {
  146.                             if (game_date.y_pos < console_limits.down_y_limit)
  147.                             {
  148.                                 game_date.y_pos += 2;
  149.                                 row_pos++;
  150.                             }
  151.                         }
  152.                         break;
  153.  
  154.                     case '\r':
  155.                         {
  156.                             if (board[row_pos][col_pos] == 0)
  157.                             {
  158. //                                PlaySound(TEXT("Explosion Sound Effects.wav"), NULL, SND_FILENAME | SND_ASYNC);
  159.                                 game_over = true;
  160.                             }
  161.                             else if (board[row_pos][col_pos] != -1)
  162.                             {
  163.                                 cout << board[row_pos][col_pos];
  164.                                 board[row_pos][col_pos] = -1;
  165.                                 score++;
  166.                             }
  167.                         }
  168.                         break;
  169.  
  170.                         goto_xy(game_date.x_pos, game_date.y_pos);
  171.                 }
  172.                 // End Of (toupper(ch))...!!
  173.             }
  174.                 // End Of (!game_over && score < no_of_unmines)...!!!
  175.  
  176.                 game_date.x_pos = 10, game_date.y_pos = 10;
  177.                 system("CLS");
  178.                 goto_xy(game_date.x_pos, game_date.y_pos);
  179.  
  180.                 if (!game_over && score == no_of_unmines)
  181.                 {
  182.                     show_board(board, true);
  183.                     goto_xy(game_date.x_pos, game_date.y_pos + 3);
  184.                     cout << "YOU WIN\n";
  185.                     goto_xy(game_date.x_pos, game_date.y_pos + 4);
  186.                     cout << "YOU SELECT ALL THE BOX THAT ARE NOT A MINE\n";
  187.                     goto_xy(game_date.x_pos, game_date.y_pos + 5);
  188.                 }
  189.                 // End Of (!game_over && score == no_of_unmines)...!!
  190.                 else if (score < no_of_unmines)
  191.                 {
  192.                     show_board(board, true);
  193.                     goto_xy(game_date.x_pos, game_date.y_pos + 1);
  194.                     cout << "GAME OVER...!!\n\n";
  195.                     goto_xy(game_date.x_pos, game_date.y_pos + 2);
  196.                     cout << "YOU LOSE...!!YOU HIT A MINE\n";
  197.                     goto_xy(game_date.x_pos, game_date.y_pos + 3);
  198.                 }
  199.                 // End Of if (score < no_of_unmines)...!!
  200.  
  201.                 goto_xy(game_date.x_pos, game_date.y_pos + 1);
  202.                 cout << "YOUR SCORE IS: " << score;
  203.                 goto_xy(game_date.x_pos, game_date.y_pos + 4);
  204.                 save_score(score);
  205.                 system("PAUSE");
  206.             }
  207.         while (true);
  208.  
  209.  
  210.     return 0;
  211. }
  212. // End Of main()
  213.  
  214.  
  215. /**
  216.     Set Mines On The Board upto num_of_mines...!!
  217.     @Preconditions
  218.     1-->  0 < num_of_mines < (rows * col) Of board...!!
  219.     @param board A 2-D int Vector On Which Mines Will Be Placed...!!
  220.     @param num_of_mies Number Of Mines That Will Be Set Up On board...!!
  221. */
  222. void set_mines(vector<vector <int> >& board, const int& num_of_mines)
  223. {
  224.     srand(time(0));
  225.     int total_rows = board.size(),
  226.          total_cols = board[0].size(),
  227.          row_location,
  228.          col_location;
  229.  
  230.     for (int mine = 0; mine < num_of_mines; mine++)
  231.     {
  232.         row_location = rand () % total_rows;
  233.         col_location = rand() % total_cols;
  234.         if (board[row_location][col_location] != game_date.mine_symbol)
  235.         {
  236.             board[row_location][col_location] = game_date.mine_symbol;
  237.         }
  238.         else
  239.             mine--;
  240.     }
  241. } // end set_mines...!!
  242. // eNd oF void set_mines(vector<vector <int> >& board, const int& num_of_mines)
  243.  
  244. /**
  245.     Set Each Index Of Board With A Random Numbers B/W 1 AND 8 Inclusive....!!!
  246.     Preconditions:
  247.     1 -->  board Size Must Be Greater Then Zero...!!
  248.     @param board A 2-D int Vecotor On Which Random Numbers Will Be Setting Up...!!
  249. */
  250. void set_boards(vector <vector <int> >& board)
  251. {
  252.     srand (time(0));
  253.  
  254.     int rows = board.size(),
  255.          cols = board[0].size();
  256.  
  257.     for (int row_pos = 0; row_pos < rows; row_pos++)
  258.     {
  259.         for (int col_pos = 0; col_pos < cols; col_pos++)
  260.         {
  261.             board[row_pos][col_pos] = 1 + rand() % 9;
  262.         }
  263.     }
  264.  
  265. } // end of set_board
  266. // eND oF void set_boards(vector <vector <int> >& board)
  267.  
  268. void show_board(vector <vector <int> >& board, bool show_mines)
  269. {
  270.     char ch = 178;
  271.     game_date.x_pos = 10;
  272.     game_date.y_pos = 3;
  273.  
  274.     for (int row_pos = 0; row_pos < game_date.no_of_rows; row_pos++)
  275.     {
  276.         goto_xy(game_date.x_pos, game_date.y_pos);
  277.         for (int col_pos = 0; col_pos < game_date.no_of_cols; col_pos++)
  278.         {
  279.             if (show_mines)
  280.             {
  281.                 board[row_pos][col_pos] == 0 ? cout << board[row_pos][col_pos] << " " : cout << ch << " ";
  282.             }
  283.             else
  284.                 cout << ch << " ";
  285.         }
  286.         game_date.y_pos += 2;
  287.     }
  288. }
  289. // eNd oF void show_board()
  290.  
  291. void goto_xy(int x_pos, int y_pos)
  292. {
  293.     COORD coord;
  294.     coord.X = x_pos;
  295.     coord.Y = y_pos;
  296.     HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE);
  297.     SetConsoleCursorPosition(h_console, coord);
  298. }
  299. // eND oF void goto_xy(int x_pos, int y_pos)
  300.  
  301. /**
  302.     Retrun An Integer With  lower_limt < int <= upper_limit
  303.     @param str A String That Is To Be Displayed!! It Will Describes That For What Input Is Being Taking..!!
  304.     e.g., If str == "Rows", Then For Row Input Is Being Taking...!!
  305.     @param upper_limits Maximum Number User Can Enter...!!
  306.     @param lower_limt Minimum Number User Can Enter...!! By Default It Is Zero...!!
  307.     @return num An Integer With  lower_limt < num <= upper_limit
  308. */
  309. int get_input (string str, const int upper_limit, const int lower_limit)
  310. {
  311.     int num;
  312.     do
  313.     {
  314.         cout << "\neNTER A nUMBER fOR " << str << "  (" << (lower_limit + 1) << " " << upper_limit << ")  ";
  315.         cin >> num;
  316.  
  317.         if (num <= lower_limit && !cin.fail())
  318.         {
  319.             MessageBox(NULL, "iNPUT mUST bE pOSITIVE.!!\n", "eRROR..!!", MB_OK | MB_ICONERROR);
  320.         }
  321.  
  322.         if (num > upper_limit  && !cin.fail())
  323.         {
  324.             MessageBox(NULL, "iNPUT mUST bE iN LIMITS.!!\ntRY aGain!!", "eRROR!", MB_OK | MB_ICONERROR);
  325.         }
  326.  
  327.         if (cin.fail())
  328.         {
  329.             cin.clear();
  330.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  331.             MessageBox(NULL, "iNPUT mUST bE aN iNTEGER..!!", "eRROR..!!", MB_OK | MB_ICONERROR);
  332.         }
  333.     }
  334.     while ((num <= lower_limit || num > upper_limit) && !cin.fail());
  335.  
  336.     system("CLS");
  337.  
  338.     return num;
  339. }
  340. // eND oF int get_input (string str, const int upper_limit, const int lower_limit)
  341.  
  342. int choose_level()
  343. {
  344.     SetConsoleTitle("MINE SPEEPRER GAME -> CHOSE LEVEL..!!!");
  345.  
  346.     cout << "\n\n\n\n\n\n\n\t\t|--------------------------------------------------------------------|\n"
  347.            << "\t\t|                    pRESS 1 fOR LEVEL 1 (eASY mODE)                 |\n\n"
  348.            << "\t\t|                    pRESS 2 fOR LEVEL 2 (mEDUM mODE)                |\n\n"
  349.            << "\t\t|                    pRESS 1 fOR LEVEL 3 (hARD mODE)                 |\n\n"
  350.            << "\t\t|--------------------------------------------------------------------|\n";
  351.  
  352.     int level = get_input("Level", 3);
  353.  
  354.     return level;
  355. }
  356. // eND oF int choose_level()
  357.  
  358. void set_level()
  359. {
  360.     int level = choose_level();
  361.     SetConsoleTitle("Mine Sweeper Game -> Chooce Level...!!");
  362.     switch (level)
  363.     {
  364.     case 1:
  365.         {
  366.             game_date.no_of_rows = 12;
  367.             game_date.no_of_cols = 15;
  368.             game_date.no_of_mines = 75;
  369.             SetConsoleTitle("MINE SWEEPER GAME -> EASY MODE....!!!");
  370.             system("MODE 50, 40");
  371.         }
  372.         break;
  373.         // End Of Case 1...!!!
  374.  
  375.     case 2:
  376.         {
  377.             game_date.no_of_rows = 17;
  378.             game_date.no_of_cols = 20;
  379.             game_date.no_of_mines = 100;
  380.             SetConsoleTitle("MINE SWEEPER GAME -> MEDIUM MODE....!!!");
  381.             system("MODE 60, 50");
  382.         }
  383.         break;
  384.         // End Of Case 2...!!!
  385.  
  386.     case 3:
  387.         {
  388.             game_date.no_of_rows = 22;
  389.             game_date.no_of_cols = 25;
  390.             game_date.no_of_mines = 200;
  391.             SetConsoleTitle("MINE SWEEPER GAME -> HARD MODE....!!!");
  392.             system("MODE 77, 55");
  393.         }
  394.         break;
  395.         // End Of Case 3...!!!
  396.     }
  397.  
  398.     game_date.x_pos = 10;
  399.     game_date.y_pos = 3;
  400.     console_limits.left_x_limit = game_date.x_pos + 1;
  401.     console_limits.right_x_limit = console_limits.left_x_limit + game_date.no_of_cols + game_date.no_of_rows;
  402.     console_limits.up_y_limit = game_date.y_pos + 1;
  403.     console_limits.down_y_limit = console_limits.right_x_limit - 13;
  404.  
  405. }
  406. // eND oF void set_level()
  407.  
  408. void show_score()
  409. {
  410.     ifstream file;
  411.     file.open("Scores!.txt");
  412.     string name;
  413.     int score;
  414.     char line_end;
  415.  
  416.     cout << setw(50) << "nAME" << setw(10) << " sCORE\n";
  417.     while (getline(file, name) && file >> score)
  418.     {
  419.         cout << setw(50) << name << setw(10) << score << endl;
  420.         file.get(line_end);
  421.     }
  422.  
  423.     file.close();
  424.  
  425. }
  426. // eND oF void show_score()
  427.  
  428. void save_score(const int& score)
  429. {
  430.     ofstream file;
  431.     file.open("Scores!.txt", ios::app);
  432.     string name;
  433.  
  434.     cin.ignore();
  435.     do
  436.     {
  437.         cout << "\teNTER yOUR nAME\n>>> ";
  438.         getline(cin, name);
  439.     }
  440.     while (name.length() == 0);
  441.  
  442.     file << name << endl << score << endl;
  443.  
  444.     file.close();
  445.     MessageBox(NULL, "sCORE sAVED", "O.K.,", MB_OK | MB_ICONMASK);
  446. }
  447. // eND oF void save_score(const int& score)
  448.  
  449. /**
  450.     Main Menus Of The Game...!! Runs When Game Is Start....!!
  451. */
  452. void main_menu()
  453. {
  454.     string game = "WELCOME TO MINE SWEEPER GAME\n";
  455.     system("MODE 100, 40");
  456.     Sleep(250);
  457.     cout << "\n\n\n\n";
  458.     for (int ch = 0; ch < game.length(); ch++)
  459.     {
  460.         cout << game[ch] << "  ";
  461.         Sleep(500);
  462.     }
  463.  
  464.     // An Infinte Loop That Will Display Main Options Of The Game...!!
  465.     // This Will Return From Funtion If User Select Choice 2, Otherwise Always Display Main Options...!!
  466.     while(true)
  467.     {
  468.         system("CLS");
  469.         cout << "\n\n\n\n\n\t\t|-----------------------------------------------------------|\n\n";
  470.         cout << "\t\t|                    eNTER 1 fOR vIEW sCORE                 |\n\n";
  471.         cout << "\t\t|                    eNTER 2 fOR plAY gAME!                 |\n\n";
  472.         cout << "\t\t|-----------------------------------------------------------|\n";
  473.         int choice = get_input("cHOICE", 2);
  474.  
  475.         if (choice == 1)
  476.             {
  477.                 show_score();
  478.                 system("PAUSE");
  479.             }
  480.         else
  481.             return;
  482.     }
  483. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement