Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created March 10, 2021 00:32
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/89ab1402954e9cd94571e07f255c4472 to your computer and use it in GitHub Desktop.
Save parzibyte/89ab1402954e9cd94571e07f255c4472 to your computer and use it in GitHub Desktop.
// Hace que el CPU elija unas coordenadas para ganar
void elegirCoordenadasCpu(char jugador, char tablero[FILAS][COLUMNAS], int *yDestino, int *xDestino)
{
hablar("Estoy pensando...", jugador);
/*
El orden en el que el CPU infiere las coordenadas que toma es:
1. Ganar si se puede
2. Hacer perder al oponente si está a punto de ganar
3. Tomar el mejor movimiento del oponente (en donde obtiene el mayor puntaje)
4. Tomar mi mejor movimiento (en donde obtengo mayor puntaje)
5. Elegir la de la esquina superior izquierda (0,0)
6. Coordenadas aleatorias
*/
int y, x, conteoJugador, conteoOponente;
char oponente = oponenteDe(jugador);
// 1
coordenadasParaGanar(jugador, tablero, &y, &x);
if (y != -1 && x != -1)
{
hablar("Ganar", jugador);
*yDestino = y;
*xDestino = x;
return;
}
// 2
coordenadasParaGanar(oponente, tablero, &y, &x);
if (y != -1 && x != -1)
{
hablar("Tomar victoria de oponente", jugador);
*yDestino = y;
*xDestino = x;
return;
}
// 3
coordenadasParaMayorPuntaje(jugador, tablero, &y, &x, &conteoJugador);
coordenadasParaMayorPuntaje(oponente, tablero, &y, &x, &conteoOponente);
if (conteoOponente > conteoJugador)
{
hablar("Tomar puntaje mayor del oponente", jugador);
*yDestino = y;
*xDestino = x;
return;
}
else
{
hablar("Tomar mi mayor puntaje", jugador);
*yDestino = y;
*xDestino = x;
return;
}
// 4
if (coordenadasVacias(0, 0, tablero))
{
hablar("Tomar columna superior izquierda", jugador);
*yDestino = 0;
*xDestino = 0;
return;
}
// 5
hablar("Coordenadas aleatorias", jugador);
obtenerCoordenadasAleatorias(jugador, tablero, yDestino, xDestino);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment