Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- const int NB_MINES=10;
- const int TAILLE_X=9;
- const int TAILLE_Y=9;
- struct case_grille
- {
- int valeur;
- bool decouverte;
- };
- void init (struct case_grille g[TAILLE_X+2][TAILLE_Y+2])
- {
- int i, j;
- for (i=0;i<TAILLE_X+2;i++)
- {
- for (j=0;j<TAILLE_Y+2;j++)
- {
- g[i][j].valeur=0;
- g[i][j].decouverte=false;
- }
- }
- }
- void remp(struct case_grille g[TAILLE_X+2][TAILLE_Y+2])
- {
- int i, j, k;
- for (k=0;k<NB_MINES;k++)
- {
- i=rand()%TAILLE_X+1;
- j=rand()%TAILLE_Y+1;
- }
- while (g[i][j].valeur!=0)
- {
- g[i][j].valeur=(-1);
- }
- }
- void bombe (struct case_grille g[TAILLE_X+2][TAILLE_Y+2])
- {
- int i, j, k, l;
- for (i=1; i<TAILLE_X+1;i++)
- {
- for (j=1;j<TAILLE_Y+1;j++)
- {
- if (g[i][j].valeur!=1)
- {
- for (k=i-1;j<=i+1;k++)
- {
- for (l=j-1;l<=j+1;l++)
- {
- if (g[k][l].valeur==(-1)) {g[i][j].valeur++;}
- }
- }
- }
- }
- }
- }
- void affiche (struct case_grille g[TAILLE_X+2][TAILLE_Y+2])
- {
- int i, j;
- for (i=1;i<TAILLE_X+1;i++)
- {
- for (j=1;j<TAILLE_Y;j++)
- {
- if (g[i][j].decouverte)
- {
- if (g[i][j].valeur==-1)
- {
- putchar(207);
- }
- else
- cout <<g[i][j].valeur;
- }
- cout <<endl;
- }
- }
- }
- int decouvrir (struct case_grille g[TAILLE_X+2][TAILLE_Y+2], int x, int y)
- {
- // int i, j;
- int code_retour;
- do
- {
- cout<<"Donnez les coordonnees de la case"<<endl;
- cin>>x>>y;
- }
- while (g[x][y].decouverte==true);
- if (g[x][y].valeur==-1)
- code_retour = 0;
- else
- code_retour = -1;
- return code_retour;
- }
- int jouer (struct case_grille g[TAILLE_X+2][TAILLE_Y+2], int reste)
- {
- int x,y;
- int retour;
- bool bombe=false;
- while((reste!=0)&&(bombe=false))
- retour=decouvrir(g,x,y);
- if (retour==0)
- {
- bombe=true;
- g[x][y].decouverte=true;
- }
- else
- if (g[x][y].decouverte==false)
- {
- g[x][y].decouverte=true;
- reste--;
- }
- else
- {
- cout <<"Case jouer"<<endl;
- affiche(g);
- cout<<"Nombre de case restante"<<reste<<endl;
- }
- if (reste==0)
- return 0;
- else
- return 1;
- }
- int main (void)
- {
- struct case_grille jeu[TAILLE_X+2][TAILLE_Y+2];
- int nb_case=((TAILLE_X*TAILLE_Y)-NB_MINES);
- int fin_partie;
- srand(time(NULL));
- init(jeu);
- affiche (jeu);
- remp(jeu);
- bombe(jeu);
- fin_partie=jouer(jeu, nb_case);
- if (fin_partie==0)
- cout<<"BRAVO"<<endl;
- else
- cout<<"Vous avez perdu"<<endl;
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement