Minesweeper C++

Solved
Pipette -  
 Pipette -
Hello, for a project I need to design a minesweeper game. The problem arises when I have to determine the mines around each square; I can't get the function to work correctly. If someone with a fresh perspective could take a look to see if they can identify the source of the problem, as I really don't understand the method that seems correct...
Thank you for your help
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; //Permits creating a 2D array int * *creer_tab2D(int n, int m){ int * *p=new int *[n] ; for(int i=0;i<n;i++) p[i]=new int[m] ; return (p); } //Structure of the matrix struct matrice { int nbl; int nbc ; int * *T ; }; //Creation of the matrix matrice *Creer_matrice(int n, int m){ matrice *p=new matrice; (*p).nbl=n; (*p).nbc=m; (*p).T=creer_tab2D(n,m); return (p); } //Initialization of the matrix to zero void initZero_mat(matrice &J){ for(int i=0;i<J.nbl;i++){ for(int j=0;j<J.nbc;j++){ J.T[i][j]=0; } } } //Creation of the table with mines int Int_jeu(matrice & J, int Mines, int n){ srand(time(NULL)); int i,j; for(int cpt=0;cpt<Mines;cpt++){ i = rand()%n; j = rand()%n; if (J.T[i][j]==2){ while(J.T[i][j]==2){ i = rand()%n; j = rand()%n; } J.T[i][j]=2; } else{ J.T[i][j]=2; } } return(0); } bool possible(int z, int e, int n){ if((z==-1 or z>=n) and(e==-1 or e>=n)) return(false); else return(true); } //Determines the mines adjacent to each case void mines_autour(matrice &J, matrice &M, int n){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ int cpt=0; if(J.T[i][j]!=2){ for(int a=-1; a!=1; a++){ for(int b=-1; b!=1; b++){ if(a!=0 and b!=0){ if(possible(a+i,b+j,n)){ if(J.T[a+i][b+j]==2){ cpt++; } } } } } } M.T[i][j]=cpt; } } } //Function to display the mine table // Affichage_TM(*J,n); void Affichage_TM(matrice & J, int n){ for(int i=0; i<n; i++){ for(int j=0;j<n;j++){ cout<<J.T[i][j]<<" "; } cout<<endl; } } int main(){ int n=5; int m=5; int Mines=5; matrice *J=Creer_matrice(n,m); initZero_mat(*J); Int_jeu(*J,Mines,n); matrice *M=Creer_matrice(n,m); initZero_mat(*M); Affichage_TM(*J,n); cout<<endl; Affichage_TM(*M,n); return(0); }

2 answers

Dalfab Posted messages 638 Registration date   Status Member Last intervention   102
 
Hello,

The code you write is 95% C. Only
using namespace std
(which is a very old command to forget), the use of
std::cout
,
std::cin
, the use of
new
(which should NEVER be used) and references are C++. The language you are learning is therefore not really C++!

The errors I have seen
-
if((z==-1 or z>=n) and(e==-1 or e>=n)) return(false);
. No: an error on z OR an error on e results in an invalid position!
 for(int a=-1; a!=1; a++){ for(int b=-1; b!=1; b++){ if(a!=0 and b!=0){

These 3 lines each have an error:
- we want to go from -1 to +1, and you indicate to exit the loop when
a
equals 1!
- in the 3rd, you still confuse AND and OR. It suffices that one among
a
OR
b
is non-zero.
0
Pipette
 
Thank you for your help, everything works perfectly now, thank you very much!
I couldn't see the errors after getting so worked up about it.
I am normally supposed to learn C++, but I believe you if it's mostly C, I'm just going over my lessons.
Thanks again for your help!!
0