Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <ctime>
- const int SIZE = 5;
- const int SETW = 2;//(int)log10(SIZE*SIZE - 1) + 1;
- const char BOMB = '*';
- const char UNKNOWN = ' ';
- const int COUNT_BOMB = 10;
- char table[SIZE*SIZE];
- int game[SIZE*SIZE];
- void Clear_Console() { system("cls"); }
- void Clear_Table();
- void Print_Line();
- void Print_Table();
- void Create_Game();
- using namespace std;
- int main(){
- srand(time(0));
- bool exit;
- do{
- exit = true;
- Clear_Console();
- Clear_Table();
- Create_Game();
- Print_Table();
- bool win = true;
- int input;
- for (int i = 0; i < SIZE*SIZE - COUNT_BOMB; ++i){
- do{
- cout << "Input number: "; cin >> input;
- --input;
- } while (input < 0 || SIZE*SIZE <= input || table[input] != UNKNOWN);
- if (game[input] == 9){
- cout << "Big bang!" << endl;
- win = false;
- break;
- }
- table[input] = '0' + game[input];
- Clear_Console();
- Print_Table();
- }
- if (win)
- cout << "You Win!" << endl;
- else{
- for (int i = 0; i < SIZE*SIZE; ++i)
- if (game[i] == 9)
- table[i] = BOMB;
- else
- table[i] = '0' + game[i];
- Print_Table();
- }
- cout << "0 - exit." << endl;
- cin >> input;
- if (input)
- exit = false;
- } while (!exit);
- }
- void Clear_Table(){
- for (int i = 0; i < SIZE*SIZE; ++i)
- table[i] = UNKNOWN;
- }
- void Print_Line(){
- cout << char(197);
- for (int i = 0; i < SIZE; ++i){
- for (int j = 0; j < SETW; ++j)
- cout << char(196);
- cout << char(197);
- }
- }
- void Print_Table(){
- Print_Line();
- cout << '\t';
- Print_Line();
- cout << endl;
- for (int i = 0; i < SIZE; ++i){
- cout << char(179);
- for (int j = 0; j < SIZE; ++j)
- cout << setw(SETW) << table[i*SIZE + j] << char(179);
- cout << '\t' << char(179);
- for (int j = 0; j < SIZE; ++j)
- cout << setw(SETW) << i*SIZE + j + 1 << char(179);
- cout << endl;
- Print_Line();
- cout << '\t';
- Print_Line();
- cout << endl;
- }
- }
- void Create_Game(){
- for (int i = 0; i < SIZE*SIZE; ++i)
- game[i] = 0;
- int count_bomb = COUNT_BOMB;
- for (int i = 0; i < SIZE*SIZE && count_bomb > 0; ++i){
- if (rand() % (SIZE*SIZE - i) + 1 <= count_bomb){
- game[i] = 9;
- --count_bomb;
- }
- }
- for (int i = 0; i < SIZE*SIZE; ++i)
- if (game[i] == 9){
- int x = i%SIZE;
- int y = i / SIZE;
- if ((x + 1 < SIZE) && (game[i + 1] != 9))
- ++game[i + 1];
- if ((x - 1 >= 0) && (game[i - 1] != 9))
- ++game[i - 1];
- if ((y + 1 < SIZE) && (game[i + SIZE] != 9))
- ++game[i + SIZE];
- if ((y - 1 >= 0) && (game[i - SIZE] != 9))
- ++game[i - SIZE];
- if ((x + 1 < SIZE) && (y + 1 < SIZE) && (game[i + SIZE + 1] != 9))
- ++game[(y + 1)*SIZE + x + 1];
- if ((x - 1 >= 0) && (y - 1 >= 0) && (game[i - SIZE - 1] != 9))
- ++game[i - SIZE - 1];
- if ((x + 1 < SIZE) && (y - 1 >= 0) && (game[i - SIZE + 1] != 9))
- ++game[i - SIZE + 1];
- if ((x - 1 >= 0) && (y + 1 < SIZE) && (game[i + SIZE - 1] != 9))
- ++game[i + SIZE - 1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement