Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created March 10, 2021 00:34
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/af15f36de2256ea8bb8e62d929492f97 to your computer and use it in GitHub Desktop.
Save parzibyte/af15f36de2256ea8bb8e62d929492f97 to your computer and use it in GitHub Desktop.
/*
Funciones de conteo. Simplemente cuentan cuántas piezas del mismo jugador están
alineadas
*/
int contarHaciaArriba(int x, int y, char jugador, char tablero[FILAS][COLUMNAS])
{
int yInicio = (y - CONTEO_PARA_GANAR >= 0) ? y - CONTEO_PARA_GANAR + 1 : 0;
int contador = 0;
for (; yInicio <= y; yInicio++)
{
if (tablero[yInicio][x] == jugador)
{
contador++;
}
else
{
contador = 0;
}
}
return contador;
}
int contarHaciaDerecha(int x, int y, char jugador, char tablero[FILAS][COLUMNAS])
{
int xFin = (x + CONTEO_PARA_GANAR < COLUMNAS) ? x + CONTEO_PARA_GANAR - 1 : COLUMNAS - 1;
int contador = 0;
for (; x <= xFin; x++)
{
if (tablero[y][x] == jugador)
{
contador++;
}
else
{
contador = 0;
}
}
return contador;
}
int contarHaciaArribaDerecha(int x, int y, char jugador, char tablero[FILAS][COLUMNAS])
{
int xFin = (x + CONTEO_PARA_GANAR < COLUMNAS) ? x + CONTEO_PARA_GANAR - 1 : COLUMNAS - 1;
int yInicio = (y - CONTEO_PARA_GANAR >= 0) ? y - CONTEO_PARA_GANAR + 1 : 0;
int contador = 0;
while (x <= xFin && yInicio <= y)
{
if (tablero[y][x] == jugador)
{
contador++;
}
else
{
contador = 0;
}
x++;
y--;
}
return contador;
}
int contarHaciaAbajoDerecha(int x, int y, char jugador, char tablero[FILAS][COLUMNAS])
{
int xFin = (x + CONTEO_PARA_GANAR < COLUMNAS) ? x + CONTEO_PARA_GANAR - 1 : COLUMNAS - 1;
int yFin = (y + CONTEO_PARA_GANAR < FILAS) ? y + CONTEO_PARA_GANAR - 1 : FILAS - 1;
int contador = 0;
while (x <= xFin && y <= yFin)
{
if (tablero[y][x] == jugador)
{
contador++;
}
else
{
contador = 0;
}
x++;
y++;
}
return contador;
}
// Indica si el jugador gana
int comprobarSiGana(char jugador, char tablero[FILAS][COLUMNAS])
{
int y;
for (y = 0; y < FILAS; y++)
{
int x;
for (x = 0; x < COLUMNAS; x++)
{
if (
contarHaciaArriba(x, y, jugador, tablero) >= CONTEO_PARA_GANAR ||
contarHaciaDerecha(x, y, jugador, tablero) >= CONTEO_PARA_GANAR ||
contarHaciaArribaDerecha(x, y, jugador, tablero) >= CONTEO_PARA_GANAR ||
contarHaciaAbajoDerecha(x, y, jugador, tablero) >= CONTEO_PARA_GANAR)
{
return 1;
}
}
}
// Terminamos de recorrer y no conectó
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment