Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #define CHMAX 100
- using namespace std;
- const int DIMX = 6;
- const int DIMY = 6;
- const int BOMB = 2;
- bool cheat = true;
- int randInt(int min, int max) { return min + (rand() % (int)(max - min + 1)); }
- int glX = 0;
- int glY = 0;
- bool game = true;
- struct mCase
- {
- int x;
- int y;
- int bombesProches = 0;
- bool bombe = false;
- bool visible = true;
- bool revele = false;
- bool drapeau = false;
- void init()
- {
- x = glX;
- y = glY;
- bombesProches = 0;
- if (x == 0 || y == 0 || x == DIMX || y == DIMY) visible = false;
- drapeau = false;
- bombe = false;
- }
- };
- void init(mCase table[DIMX][DIMY])
- {
- for (glX = 0; glX < DIMX; glX++)
- {
- for (glY = 0; glY < DIMY; glY++)
- {
- table[glX][glY].init();
- }
- }
- for (int i = 0; i < BOMB; i++)
- {
- int x = randInt(1, DIMX - 2);
- int y = randInt(1, DIMY - 2);
- if (table[x][y].bombe == false && table[x][y].visible == true) table[x][y].bombe = true;
- else i--;
- }
- for (glX = 1; glX < DIMX - 1; glX++)
- {
- for (glY = 1; glY < DIMY - 1; glY++)
- {
- for (int x = glX - 1; x <= glX + 1; x++)
- {
- for (int y = glY - 1; y <= glY + 1; y++)
- {
- if (table[x][y].bombe == true)
- {
- table[glX][glY].bombesProches++;
- }
- }
- }
- }
- }
- }
- void triche(mCase table[DIMX][DIMY])
- {
- for (glX = 1; glX < DIMX - 1; glX++)
- {
- for (glY = 1; glY < DIMY - 1; glY++)
- {
- if (table[glX][glY].bombe) cout << 'B';
- else cout << table[glX][glY].bombesProches;
- cout << " ";
- }
- cout << endl;
- }
- cout << endl;
- }
- void affiche(mCase table[DIMX][DIMY])
- {
- for (int x = 1; x < DIMX - 1; x++)
- {
- for (int y = 1; y < DIMY - 1; y++)
- {
- if (table[x][y].drapeau) cout << "D";
- else if (table[x][y].revele)
- {
- if (table[x][y].bombe) cout << "B";
- else cout << table[x][y].bombesProches;
- }
- else cout << "?";
- cout << " ";
- }
- cout << endl;
- }
- cout << endl;
- }
- void entree(mCase table[DIMX][DIMY])
- {
- int x, y, action;
- cout << "X: ";
- do cin >> y;
- while (y < 1 || y > DIMX - 1);
- cout << "Y: ";
- do cin >> x;
- while (x < 1 || x > DIMY - 1);
- cout << "1: Clic | 2: Drapeau: ";
- do cin >> action;
- while (action < 1 || action > 2);
- cout << endl;
- if (action == 2)
- {
- if (table[x][y].revele)
- {
- cout << "Case deja affiche" << endl;
- return;
- }
- if (table[x][y].drapeau)
- {
- table[x][y].drapeau = false;
- cout << "Drapeau retire" << endl;
- }
- else
- {
- table[x][y].drapeau = true;
- cout << "Drapeau pose" << endl;
- }
- }
- if (action == 1)
- {
- if (table[x][y].drapeau)
- {
- cout << "Il y'a un drapeau, enlevez le avant de demine" << endl;
- return;
- }
- table[x][y].revele = true;
- if (table[x][y].bombe)
- {
- cout << "Perdu !" << endl;
- game = false;
- }
- }
- }
- void testGagne(mCase table[DIMX][DIMY])
- {
- int nombreCase = (DIMX - 2)*(DIMY - 2) - BOMB;
- for (int x = 1; x < DIMX - 1; x++)
- {
- for (int y = 1; y < DIMY - 1; y++)
- {
- if (table[x][y].revele) nombreCase--;
- }
- }
- if (nombreCase == 0)
- {
- cout << "Vous avez gagne !";
- game = false;
- }
- }
- int main(void)
- {
- srand(time(NULL));
- mCase table[DIMX][DIMY];
- init(table);
- if (cheat) triche(table);
- while (game)
- {
- affiche(table);
- entree(table);
- testGagne(table);
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement