Advertisement
Glenpl

sapper 1.0

Mar 30th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.42 KB | None | 0 0
  1. // sapper game 1.0
  2. // author glenpl
  3.  
  4. #include <iostream>
  5. #include <time.h>
  6. #include <windows.h>
  7. #include <cstdlib>
  8. #include <stdio.h>
  9. #include <conio.h>
  10.  
  11. using namespace std;
  12.  
  13. typedef unsigned int UI;
  14.  
  15. const UI BOARD_SIZE = 9; // board is a grid of x * x fields
  16. const UI BOMBS = 9; // number of bombs
  17. const bool SHOW_ZEROES = false; // show all zero-valued fields
  18. // special values of fields
  19. const UI CLEAR = 0;
  20. const UI BOMB = 9;
  21. const UI MARKED = 10;
  22. const UI SHOWN = 11;
  23. const UI HIDDEN = 12;
  24.  
  25. // boards
  26. string board[BOARD_SIZE][BOARD_SIZE]; // board that will be displayed to user
  27. UI board_int[BOARD_SIZE][BOARD_SIZE]; // board with actual fields' values
  28. UI shown[BOARD_SIZE][BOARD_SIZE]; // board with values: marked, hidden, shown for each field
  29.  
  30. // some global variables... :(
  31. UI flags = 0;
  32. bool lose = false;
  33. int hidden_fields = BOARD_SIZE * BOARD_SIZE;
  34.  
  35. // function index
  36. void update_board();
  37. UI count_bombs_around(int, int);
  38. void print_board();
  39. void init_game();
  40. void do_action(string, UI, UI);
  41. string itos(int);
  42.  
  43. // convert int to string
  44. string itos(int n)
  45. {
  46.     string tmp;
  47.     if(n < 0)
  48.     {
  49.         tmp = "-";
  50.         n = -n;
  51.     }
  52.     if(n > 9)
  53.         tmp += itos(n / 10);
  54.     tmp += n % 10 + 48;
  55.     return tmp;
  56. }
  57.  
  58. void print_board()
  59. {
  60.     system("CLS");
  61.     printf("                    ");
  62.     for(UI i = 0; i < BOARD_SIZE; i++)
  63.         printf("%i   ", i+1);
  64.     printf("\n                   ");
  65.     for(UI i = 0; i < BOARD_SIZE*4-1; i++)
  66.         printf("_");
  67.     printf("\n");
  68.     for(UI i = 0; i < BOARD_SIZE; i++)
  69.     {
  70.         printf("                %i | ", i+1);
  71.         for(UI j = 0; j < BOARD_SIZE; j++)
  72.             cout << board[i][j] << " | ";
  73.         printf("%i\n                  ", i+1);
  74.         if(i < BOARD_SIZE-1) printf("|");
  75.         if(i < BOARD_SIZE-1)
  76.         {
  77.             for(UI j = 0; j < BOARD_SIZE-1; j++)
  78.                 printf("---+");
  79.             printf("---|\n");
  80.         }
  81.     }
  82.     printf(" ");
  83.     for(UI i = 0; i < BOARD_SIZE*4-1; i++)
  84.         printf("-");
  85.     printf("\n");
  86. }
  87.  
  88. void init_game()
  89. {
  90.     // init formal board with 'X' characters
  91.     // init board with 0 values
  92.     // no val has been shown yet - all fields are hidden
  93.     for(UI i = 0; i < BOARD_SIZE; i++)
  94.         for(UI j = 0; j < BOARD_SIZE; j++)
  95.         {
  96.             board[i][j] = 'X';
  97.             board_int[i][j] = 0;
  98.             shown[i][j] = HIDDEN;
  99.         }
  100.  
  101.     // sets bombs on the board
  102.     UI dec_bombs = 0;
  103.     while(dec_bombs != BOMBS)
  104.     {
  105.         UI a = rand()%BOARD_SIZE;
  106.         UI b = rand()%BOARD_SIZE;
  107.         if(board_int[a][b] != BOMB)
  108.         {
  109.             board_int[a][b] = BOMB;
  110.             dec_bombs++;
  111.         }
  112.     }
  113.  
  114.     hidden_fields = BOARD_SIZE * BOARD_SIZE;
  115.  
  116.     // set values of all other fields
  117.     for(UI i = 0; i < BOARD_SIZE; i++)
  118.         for(UI j = 0; j < BOARD_SIZE; j++)
  119.             if(board_int[i][j] != BOMB)
  120.             {
  121.                 board_int[i][j] = count_bombs_around(i, j);
  122.                 // if SHOW_ZEROES is true - shows zero-valued fields
  123.                 if(SHOW_ZEROES)
  124.                     if(board_int[i][j] == 0)
  125.                     {
  126.                         shown[i][j] = SHOWN;
  127.                         --hidden_fields;
  128.                     }
  129.             }
  130.  
  131.     lose = false;
  132.     flags = 0;
  133.     update_board();
  134. }
  135.  
  136. UI count_bombs_around(int x, int y)
  137. {
  138.     // counts bombs around XY field
  139.     int a, b, num = 0;
  140.     for(int i = x - 1; i <= x + 1; i++)
  141.     {
  142.         a = i;
  143.         if(a < 0)
  144.             continue;
  145.         if(a > (BOARD_SIZE-1))
  146.             continue;
  147.         for(int j = y - 1; j <= y + 1; j++)
  148.         {
  149.             b = j;
  150.             if(b < 0)
  151.                 continue;
  152.             if(b > (BOARD_SIZE-1))
  153.                 continue;
  154.             if (board_int[a][b] == BOMB)
  155.                 num++;
  156.         }
  157.     }
  158.     return num;
  159. }
  160.  
  161. void update_board()
  162. {
  163.     // update all fields of formal board
  164.     for(UI i = 0; i < BOARD_SIZE; i++)
  165.         for(UI j = 0; j < BOARD_SIZE; j++)
  166.         {
  167.             if(shown[i][j] == HIDDEN)
  168.                 board[i][j] = 'X';
  169.             else
  170.             {
  171.                 if(shown[i][j] == MARKED)
  172.                     board[i][j] = 'M';
  173.                 else if(board_int[i][j] == BOMB)
  174.                 {
  175.                     board[i][j] = 'B';
  176.                     lose = true;
  177.                 }
  178.                 else
  179.                     board[i][j] = itos(board_int[i][j]);
  180.             }
  181.         }
  182. }
  183.  
  184. void do_action(string action, UI x, UI y)
  185. {
  186.     if(action == "mark")
  187.     {
  188.         if(shown[x][y] == HIDDEN)
  189.         {
  190.             if(flags < BOMBS)
  191.             {
  192.                 shown[x][y] = MARKED;
  193.                 flags++;
  194.             }
  195.         }
  196.         else if(shown[x][y] == MARKED)
  197.         {
  198.             shown[x][y] = HIDDEN;
  199.             flags--;
  200.         }
  201.     }
  202.     if(action == "show")
  203.         if(shown[x][y] == HIDDEN)
  204.         {
  205.             shown[x][y] = SHOWN;
  206.             --hidden_fields;
  207.         }
  208. }
  209.  
  210. int main()
  211. {
  212.     srand(time(NULL));
  213.     // initialize boards etc
  214.     init_game();
  215.     //print board for the first time
  216.     print_board();
  217.  
  218.     int x, y;
  219.     string action;
  220.     for(;;)
  221.     {
  222.         while(lose == false)
  223.         {
  224.             cout << "Flags left: " << BOMBS - flags << "\n";
  225.             cout << "Tell me what to do! (action[show/mark], row[1-9] , col[1-9]):\n>";
  226.                 cin>>action>>x>>y;
  227.             do_action(action, x-1, y-1);
  228.             update_board();
  229.             print_board();
  230.             if(flags == BOMBS && hidden_fields == BOMBS)
  231.             {
  232.                 cout << "\n\nCongratulations! You marked all bombs correctly and saved many human beings!\n";
  233.                 break;
  234.             }
  235.         }
  236.         if(lose == true)
  237.             cout << "\n\nWhat a pitty! You stepped on a bomb!\n";
  238.         cout<<"Would you like to play once again? [yes/no]\n";
  239.         string answer;
  240.         while(answer != "yes" && answer != "no")
  241.         {
  242.             cout<<">";
  243.             cin>>answer;
  244.         }
  245.         if(answer == "no")
  246.         {
  247.             cout << "Ok, goodbye! :) [press any button]";
  248.             getch();
  249.             return 0;
  250.         }
  251.         if(answer == "yes")
  252.         {
  253.             cout<<"Here we go! [press any button]";
  254.             init_game();
  255.             getch();
  256.             print_board();
  257.         }
  258.     }
  259.     return 0;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement