Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
cpp 1
#include <iostream>
#include <conio.h>
#include <time.h>
void mostrarMenu() {
system("cls");
cout
<< "(1) Tengo mucho miedo (10 minas en un tablero de 9 x 9)" << endl
<< "(2) Estoy listo (40 minas en un tablero de 16 x 16)" << endl
<< "(3) Soy un demente (80 minas en un tablero de 16 x 16)" << endl
<< endl
<< "(0) Salir, mi mama no me dio permiso" << endl
<< endl
<< ": ";
}
int** crearTablero(int s) {
int **n = new int *[s];
for (int x=0; x<s; x++) { n[x] = new int[s]; for (int y=0; y<s; y++) { n[x][y] = 0; } }
return n;
}
while (m > 0) {
int x = rand()%s;
int y = rand()%s;
if (tablero[x][y] == 0) {
tablero[x][y] = 9;
m--;
}
}
}
int estadoCelda(int n) {
int r = 0;
if (n == 0) { r = 1; } // cerrado vacio
else if ((n >= 1) & (n <= 8)) { r = 2; } // cerrado pista
else if (n == 9) { r = 3; } // cerrado mina
return r;
C:\Users\Arturo\Downloads\Minesweeper.cpp 2
}
bool validarPosicion(int x, int y, int s) { return ((x >= 0) & (x <= s-1) & (y >= 0) & (y <= s-1)); }
bool estaCerrado(int c) { return (estadoCelda(c) >= 10) & (estadoCelda(c) <= 12); }
bool estaMarcado(int c) { return (estadoCelda(c) >= 16) & (estadoCelda(c) <= 18); }
bool estaConMina(int c) { return (estadoCelda(c) == 3) | (estadoCelda(c) == 6) | (estadoCelda(c) == 9) |
(estadoCelda(c) == 12) | (estadoCelda(c) == 15) | (estadoCelda(c) == 18); }
return 1; // ganado
}
void mostrarCelda(int c) {
int e = estadoCelda(c);
if (e <= 3) { cout << char(219); } // cerrado
else if (e == 4) { cout << char(176); } // vacio
else if (e == 5) { cout << c-10; } // pista
else if (e == 6) { cout << char(15); } // mina
else if ((e >= 7) & (e <= 9)) { cout << char(240); } // marcado
else if ((e >= 10) & (e <= 18)) { cout << char(254); } // ubicado
}
int main() {
int k = 0;
int s = 0;
int m = 0;
int **tablero;
C:\Users\Arturo\Downloads\Minesweeper.cpp 4
mostrarMenu();
do {
k = _getch();
tablero = crearTablero(s);
ponerMinas(tablero, s, m);
ponerPistas(tablero, s);
int tx = 0;
int ty = 0;
tablero[tx][ty] += 1000;
mostrarTablero(tablero, s);
do {
k = _getch();
switch (k) {
case 75: case 72: case 77: case 80: mover(tablero, s, k, tx, ty); mostrarTablero
(tablero, s); break;
case 32: marcarCelda(tablero, tx, ty); mostrarTablero(tablero, s); break;
case 13: abrirCelda(tablero, s, tx, ty); mostrarTablero(tablero, s); break;
}
if (estadoJuego(tablero, s) >= 1) { _getch(); k = 27; }
} while (k != 27);
borrarTablero(tablero, s);
mostrarMenu();
}
} while (k != '0');
return 0;
}