Advertisement
ZhilinskiyG

Saper

May 14th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. const int SIZE = 5;
  8. const int SETW = 2;//(int)log10(SIZE*SIZE - 1) + 1;
  9. const char BOMB = '*';
  10. const char UNKNOWN = ' ';
  11. const int COUNT_BOMB = 10;
  12.  
  13. char table[SIZE*SIZE];
  14. int game[SIZE*SIZE];
  15.  
  16. void Clear_Console() { system("cls"); }
  17. void Clear_Table();
  18. void Print_Line();
  19. void Print_Table();
  20. void Create_Game();
  21.  
  22. using namespace std;
  23.  
  24. int main(){
  25.  
  26.     srand(time(0));
  27.     bool exit;
  28.     do{
  29.         exit = true;
  30.  
  31.         Clear_Console();
  32.         Clear_Table();
  33.         Create_Game();
  34.         Print_Table();
  35.  
  36.         bool win = true;
  37.         int input;
  38.         for (int i = 0; i < SIZE*SIZE - COUNT_BOMB; ++i){
  39.  
  40.             do{
  41.                 cout << "Input number: "; cin >> input;
  42.                 --input;
  43.             } while (input < 0 || SIZE*SIZE <= input || table[input] != UNKNOWN);
  44.  
  45.             if (game[input] == 9){
  46.                 cout << "Big bang!" << endl;
  47.                 win = false;
  48.                 break;
  49.             }
  50.  
  51.             table[input] = '0' + game[input];
  52.             Clear_Console();
  53.             Print_Table();
  54.         }
  55.  
  56.         if (win)
  57.             cout << "You Win!" << endl;
  58.         else{
  59.             for (int i = 0; i < SIZE*SIZE; ++i)
  60.                 if (game[i] == 9)
  61.                     table[i] = BOMB;
  62.                 else
  63.                     table[i] = '0' + game[i];
  64.             Print_Table();
  65.         }
  66.  
  67.         cout << "0 - exit." << endl;
  68.         cin >> input;
  69.         if (input)
  70.             exit = false;
  71.  
  72.     } while (!exit);
  73. }
  74.  
  75. void Clear_Table(){
  76.  
  77.     for (int i = 0; i < SIZE*SIZE; ++i)
  78.         table[i] = UNKNOWN;
  79. }
  80.  
  81. void Print_Line(){
  82.  
  83.     cout << char(197);
  84.     for (int i = 0; i < SIZE; ++i){
  85.         for (int j = 0; j < SETW; ++j)
  86.             cout << char(196);
  87.         cout << char(197);
  88.     }
  89. }
  90.  
  91. void Print_Table(){
  92.  
  93.     Print_Line();
  94.     cout << '\t';
  95.     Print_Line();
  96.     cout << endl;
  97.     for (int i = 0; i < SIZE; ++i){
  98.  
  99.         cout << char(179);
  100.         for (int j = 0; j < SIZE; ++j)
  101.             cout << setw(SETW) << table[i*SIZE + j] << char(179);
  102.  
  103.         cout << '\t' << char(179);
  104.         for (int j = 0; j < SIZE; ++j)
  105.             cout << setw(SETW) << i*SIZE + j + 1 << char(179);
  106.  
  107.         cout << endl;
  108.  
  109.         Print_Line();
  110.         cout << '\t';
  111.         Print_Line();
  112.         cout << endl;
  113.     }
  114. }
  115.  
  116. void Create_Game(){
  117.  
  118.     for (int i = 0; i < SIZE*SIZE; ++i)
  119.         game[i] = 0;
  120.  
  121.     int count_bomb = COUNT_BOMB;
  122.     for (int i = 0; i < SIZE*SIZE && count_bomb > 0; ++i){
  123.  
  124.         if (rand() % (SIZE*SIZE - i) + 1 <= count_bomb){
  125.  
  126.             game[i] = 9;
  127.             --count_bomb;
  128.         }
  129.     }
  130.  
  131.     for (int i = 0; i < SIZE*SIZE; ++i)
  132.         if (game[i] == 9){
  133.  
  134.             int x = i%SIZE;
  135.             int y = i / SIZE;
  136.  
  137.             if ((x + 1 < SIZE) && (game[i + 1] != 9))
  138.                 ++game[i + 1];
  139.             if ((x - 1 >= 0) && (game[i - 1] != 9))
  140.                 ++game[i - 1];
  141.             if ((y + 1 < SIZE) && (game[i + SIZE] != 9))
  142.                 ++game[i + SIZE];
  143.             if ((y - 1 >= 0) && (game[i - SIZE] != 9))
  144.                 ++game[i - SIZE];
  145.             if ((x + 1 < SIZE) && (y + 1 < SIZE) && (game[i + SIZE + 1] != 9))
  146.                 ++game[(y + 1)*SIZE + x + 1];
  147.             if ((x - 1 >= 0) && (y - 1 >= 0) && (game[i - SIZE - 1] != 9))
  148.                 ++game[i - SIZE - 1];
  149.             if ((x + 1 < SIZE) && (y - 1 >= 0) && (game[i - SIZE + 1] != 9))
  150.                 ++game[i - SIZE + 1];
  151.             if ((x - 1 >= 0) && (y + 1 < SIZE) && (game[i + SIZE - 1] != 9))
  152.                 ++game[i + SIZE - 1];
  153.         }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement