Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created May 21, 2021 18:38
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/193514e1de871a40ca4b444509da238e to your computer and use it in GitHub Desktop.
Save parzibyte/193514e1de871a40ca4b444509da238e to your computer and use it in GitHub Desktop.
/*
Funciones de conteo. Simplemente cuentan cuántas piezas del mismo jugador están
alineadas
*/
static int contarHaciaArriba(int x, int y, char jugador, char[][] tablero) {
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;
}
static int contarHaciaDerecha(int x, int y, char jugador, char[][] tablero) {
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;
}
static int contarHaciaArribaDerecha(int x, int y, char jugador, char[][] tablero) {
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;
}
static int contarHaciaAbajoDerecha(int x, int y, char jugador, char[][] tablero) {
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment