bUSCAMINAS Hola, necesito hacer un buscaminas por medio de matrices dispersas o tambien lo llaman matrices poco densas. Si alguien me puede ayudar gracias. Si es en java mejor. Gracias. :rolleyes: |
Respuesta: bUSCAMINAS Este programa lo hice hace unos años y funcionaba perfectamente para el compilador Borland C++. No creo que sea muy difícil pasarlo a Java. Lo de las matrices ralas es fácil... debes crear una función matriz_rala_get y matriz_rala_set o algo así para escribir y leer los datos a la matriz. Saludos! (y no olvides ponerme puntos dándome las gracias) #include<stdio.h> #include<stdlib.h> #include<iostream.h> #include<conio.h> // Constantes #define bool int #define false 0 #define true 1 #define ESC 27 #define F_IZQ 75 #define F_DER 77 #define F_ARR 72 #define F_ABA 80 #define ESPACIO 32 #define ENTER 13 const int MAXFIL = 25; const int MAXCOL = 80; const int NMINAS = 150; // Variables globales bool destapado[MAXCOL+2][MAXFIL+2] = {false}; bool protegido[MAXCOL+2][MAXFIL+2] = {false}; bool esmina[MAXCOL+2][MAXFIL+2] = {false}; // Prototipos bool destapar(int x, int y); int sumaalrededor(int x, int y); void perdio(); void crearminas(int n); // Declaración de las funciones bool destapar(int x, int y){ if (x<1) return false; if (x>MAXCOL) return false; if (y<1) return false; if (y>MAXFIL) return false; if (destapado[x][y] || protegido[x][y]) return false; if (esmina[x][y]) perdio(); destapado[x][y] = true; int suma = sumaalrededor(x,y); gotoxy(x,y); if (suma==0){ putch(' '); destapar(x-1,y-1); destapar(x,y-1); destapar(x+1,y-1); destapar(x-1,y); destapar(x+1,y); destapar(x-1,y+1); destapar(x,y+1); destapar(x+1,y+1); } else { putch(suma+int('0')); } gotoxy(x,y); return true; } int sumaalrededor(int x, int y){ int i, j, suma=0; for(i=x-1;i<=x+1;i++) for(j=y-1;j<=y+1;j++) suma += int(esmina[i][j]); return suma; } void perdio(){ //mostrar todas las minas gotoxy(1,25); cout << "Perdiste"; getch(); exit(EXIT_FAILURE); } void crearminas(int n){ for(int i=0;i<n;i++) esmina[random(MAXCOL)+1][random(MAXFIL)+1] = true; return; } int main(){ char c; int suma, i, j, x=MAXCOL/2, y=MAXFIL/2+1; bool marcadas; randomize(); crearminas(NMINAS); _wscroll = 0; clrscr(); _setcursortype(_NOCURSOR); //esto es para que el cursor no moleste for(i=1;i<=MAXCOL;i++){ for(j=1;j<=MAXFIL;j++){ gotoxy(i,j); putch('.'); } } gotoxy(x,y); putch((int)'#'); gotoxy(x,y); do{ c = (char)getch(); if (!c){ c = (char)getch(); switch((int)c){ case F_IZQ: if (x>1) x--; break; case F_DER: if (x<MAXCOL) x++; break; case F_ARR: if (y>1) y--; break; case F_ABA: if (y<MAXFIL) y++; break; } } else { switch((int)c){ case ENTER: destapar(x,y); break; case ESPACIO: if (!destapado[x][y]){ gotoxy(x,y); if (protegido[x][y]){ cout << '.'; protegido[x][y] = false; } else{ cout << '@'; protegido[x][y] = true; } gotoxy(x,y); } break; case ESC: exit(EXIT_SUCCESS); break; //esta nunca se ejecutara } } if (protegido[wherex()][wherey()]) putch('@'); else { if (destapado[wherex()][wherey()]){ suma = sumaalrededor(wherex(),wherey()); if (suma == 0) putch(' '); else cout << suma; } else putch('.'); } gotoxy(x,y); putch('#'); gotoxy(x,y); marcadas = true; for(i=1;i<=MAXCOL;i++){ for(j=1;j<=MAXFIL;j++){ if ((esmina[i][j] != protegido[i][j])){ // && // (esmina[i][j] != !destapado[i][j])){ marcadas = false; break; } } } }while(!marcadas); gotoxy(1,25); cout << "Ganaste "; // system("pause"); getch(); return 0; } |
Respuesta: bUSCAMINAS hola, muchas gracias por tu aporte, lo voy a ensayar en java. Y te cuento que como soy nueva por aqui, no se como darte puntos. Si me dices como, claro que si. |
La franja horaria es GMT -5. Ahora son las 04:54:24. |
Desarrollado por: vBulletin® Versión 3.8.9
Derechos de Autor ©2000 - 2024, Jelsoft Enterprises Ltd.
DeNunCianDo.CoM ©