Buscaminas c++
Resuelto
Pipette
-
Pipette -
Pipette -
```cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//Permet de réaliser un tableau a 2D
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 de la matrice
struct matrice {
int nbl;
int nbc;
int **T;
};
//Création de la matrice
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);
}
//Initilisation de la matrice a zéro
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;
}
}
}
//Création du tableau avec les 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) {
return (z >= 0 && z < n && e >= 0 && e < n);
}
//Determine les mines a coter de chaque 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 || b != 0) {
if (possible(i + a, j + b, n)) {
if (J.T[i + a][j + b] == 2) {
cpt++;
}
}
}
}
}
M.T[i][j] = cpt;
}
}
}
}
//fonction d'affichage du tableau de mines
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;
mines_autour(*J, *M, n);
Affichage_TM(*M, n);
return (0);
}
```</ctime></cstdlib></iostream>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//Permet de réaliser un tableau a 2D
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 de la matrice
struct matrice {
int nbl;
int nbc;
int **T;
};
//Création de la matrice
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);
}
//Initilisation de la matrice a zéro
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;
}
}
}
//Création du tableau avec les 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) {
return (z >= 0 && z < n && e >= 0 && e < n);
}
//Determine les mines a coter de chaque 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 || b != 0) {
if (possible(i + a, j + b, n)) {
if (J.T[i + a][j + b] == 2) {
cpt++;
}
}
}
}
}
M.T[i][j] = cpt;
}
}
}
}
//fonction d'affichage du tableau de mines
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;
mines_autour(*J, *M, n);
Affichage_TM(*M, n);
return (0);
}
```</ctime></cstdlib></iostream>
2 respuestas
-
Hola,
El código que escribes es en un 95% en C. Solousing namespace std
(que es un comando muy antiguo que hay que olvidar), el uso destd::cout
,std::cin
, el uso denew
(que NUNCA debes usar) y las referencias son de C++. El lenguaje que estás aprendiendo, por lo tanto, no es realmente C++!
Los errores que he visto
-if((z==-1 or z>=n) and(e==-1 or e>=n)) return(false);
. No: un error en z O BIEN un error en e hace que la posición no sea válida!for(int a=-1; a!=1; a++){ for(int b=-1; b!=1; b++){ if(a!=0 and b!=0){
Estas 3 líneas tienen cada una un error:
- queremos ir de -1 a +1, y tú indicas salir del bucle cuandoa
valga 1!
- en la tercera, confundes nuevamente Y y O. Basta que uno solo dea
O BIENb
sea diferente de cero. -
¡Gracias por tu ayuda, corrigiendo todo funciona perfectamente, ¡muchas gracias!
No veía los errores por estar estresado con ello.
Normalmente se supone que debo aprender C++, pero te creo si es principalmente C, solo estoy retomando mis cursos.
¡Gracias de nuevo por tu ayuda!