Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- const int DIMX = 10;
- const int DIMY = 10;
- const int BOMB = 12;
- int glX = 0;
- int glY = 0;
- int randInt(int min, int max) { return min + (rand() % (int)(max - min + 1)); }
- /*struct mCase;
- void init(mCase table[DIMX][DIMY]);
- void print(mCase table[DIMX][DIMY]);
- struct table;*/
- struct mCase
- {
- int x;
- int y;
- int nearBomb = 0;
- bool isBomb = false;
- bool isVisible = true;
- bool isDisplayed = false;
- bool isFlagged = false;
- void init()
- {
- x = glX;
- y = glY;
- nearBomb = 0;
- if (x == 0 || y == 0 || x == DIMX || y == DIMY) isVisible = false;
- isFlagged = false;
- isBomb = false;
- }
- };
- void init(mCase table[DIMX][DIMY])
- {
- // Just calling init
- for (glX = 0; glX < DIMX; glX++)
- {
- for (glY = 0; glY < DIMY; glY++)
- {
- table[glX][glY].init();
- }
- }
- // Placing bombs
- for (int i = 0; i < BOMB; i++)
- {
- int x = randInt(1, DIMX - 2);
- int y = randInt(1, DIMY - 2);
- if (table[x][y].isBomb == false && table[x][y].isVisible == true) table[x][y].isBomb = true;
- else i--;
- }
- // Count near bombs
- for (glX = 1; glX < DIMX - 1; glX++)
- {
- //cout << "glX: " << glX;
- for (glY = 1; glY < DIMY - 1; glY++)
- {
- //cout << ", glY: " << glY << endl;
- for (int x = glX - 1; x <= glX + 1; x++)
- {
- for (int y = glY - 1; y <= glY + 1; y++)
- {
- //cout << "Case [" << glX << "][" << glY << "] : Checking (" << x << "," << y << ")" << endl;
- if (table[x][y].isBomb == true)
- {
- table[glX][glY].nearBomb++;
- //cout << "BOMB" << endl;
- }
- }
- }
- }
- }
- }
- void print(mCase table[DIMX][DIMY])
- {
- for (glX = 1; glX < DIMX - 1; glX++)
- {
- for (glY = 1; glY < DIMY - 1; glY++)
- {
- if (table[glX][glY].isBomb) cout << 'B';
- else cout << table[glX][glY].nearBomb;
- cout << " ";
- }
- cout << endl;
- }
- }
- int main(void)
- {
- srand(time(NULL));
- mCase table[DIMX][DIMY];
- init(table);
- print(table);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement