Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- using namespace std;
- const int W = 20;
- const int H = 20;
- const int MINES = 60;
- char map[W][H];
- char opened_map[W][H];
- void create() {
- for (int i = 0; i < W; i++)
- for (int j = 0; j < H; j++) {
- map[i][j] = '.';
- opened_map[i][j] = '#';
- }
- for (int i = 0; i < MINES; i++) {
- int x, y;
- do {
- x = rand() % W;
- y = rand() % H;
- } while (map[x][y] != '.');
- map[x][y] = '*';
- }
- }
- bool is_available(int x, int y) {
- if (x < 0 || y < 0) return false;
- if (x >= W || y >= H) return false;
- return true;
- }
- bool is_bomb(int x, int y) {
- if (map[x][y] == '*')
- return true;
- return false;
- }
- int count_bombs(int i, int j) {
- int result = 0;
- for (int i1 = -1; i1 <= 1; i1++) {
- for (int j1 = -1; j1 <= 1; j1++) {
- if (is_available(i + i1, j + j1)) {
- if (is_bomb(i + i1, j + j1)) {
- result++;
- }
- }
- }
- }
- return result;
- }
- void recursive_open(short i, short j) {
- if (opened_map[i][j] != '#') return;
- int bombs = count_bombs(i, j);
- opened_map[i][j] = '0' + bombs;
- if (bombs == 0) {
- for (short i1 = -1; i1 <= 1; i1++) {
- for (short j1 = -1; j1 <= 1; j1++) {
- if (is_available(i + i1, j + j1)) {
- recursive_open(i + i1, j + j1);
- }
- }
- }
- }
- }
- bool toggle(int i, int j) {
- if (!is_available(i, j)) return true;
- if (map[i][j] == '*') {
- opened_map[i][j] = '*';
- return false;
- } else {
- recursive_open(i, j);
- }
- return true;
- }
- void draw_opened() {
- for (int i = 0; i < W; i++) {
- for (int j = 0; j < H; j++)
- cout << opened_map[i][j] << " ";
- cout << endl;
- }
- }
- void draw_map() {
- for (int i = 0; i < W; i++) {
- for (int j = 0; j < H; j++)
- cout << map[i][j] << " ";
- cout << endl;
- }
- }
- int main() {
- create();
- int x, y;
- do {
- draw_opened();
- cout << endl;
- draw_map();
- cout << endl;
- cin >> x >> y;
- } while (toggle(x, y));
- cout << "end";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement