Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 26, 2022 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/47776e2640334038b27f7a357a611f19 to your computer and use it in GitHub Desktop.
Save parzibyte/47776e2640334038b27f7a357a611f19 to your computer and use it in GitHub Desktop.
class Juego
{
private:
Tablero tablero;
int cantidadMinas;
int aleatorio_en_rango(int minimo, int maximo)
{
return minimo + rand() / (RAND_MAX / (maximo - minimo + 1) + 1);
}
int filaAleatoria()
{
return this->aleatorio_en_rango(0, this->tablero.obtenerAltura() - 1);
}
int columnaAleatoria()
{
return this->aleatorio_en_rango(0, this->tablero.obtenerAnchura() - 1);
}
public:
Juego(Tablero tablero, int cantidadMinas)
{
this->tablero = tablero;
this->cantidadMinas = cantidadMinas;
this->colocarMinasAleatoriamente();
}
void colocarMinasAleatoriamente()
{
int x, y, minasColocadas = 0;
while (minasColocadas < this->cantidadMinas)
{
x = this->columnaAleatoria();
y = this->filaAleatoria();
if (this->tablero.colocarMina(x, y))
{
minasColocadas++;
}
}
}
/*
solicitarFila y solicitarColumna piden la fila y columna del 1 al N, pero
recordemos que los índices se manejan del 0 al N-1, por eso es que se resta 1
*/
int solicitarFila()
{
int fila = 0;
cout << "Ingresa la fila: ";
cin >> fila;
return fila - 1;
}
int solicitarColumna()
{
int columna = 0;
cout << "Ingresa la columna: ";
cin >> columna;
return columna - 1;
}
bool jugadorGana()
{
int conteo = this->tablero.contarCeldasSinMinasYSinDescubrir();
if (conteo == 0)
{
return true;
}
else
{
return false;
}
}
void iniciar()
{
int fila, columna;
while (true)
{
this->tablero.imprimir();
fila = this->solicitarFila();
columna = this->solicitarColumna();
bool ok = this->tablero.descubrir(columna, fila);
if (!ok)
{
cout << "Perdiste\n";
// El modo programador te permite ver todo. Entonces lo activamos y volvemos a imprimir. No hay problema porque el jugador ya perdió
this->tablero.setModoProgramador(true);
this->tablero.imprimir();
break;
}
if (this->jugadorGana())
{
cout << "Ganaste\n";
this->tablero.setModoProgramador(true);
this->tablero.imprimir();
break;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment