Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 12, 2020 01: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/e3d48d620a49821901bf6b7f9f61fcb3 to your computer and use it in GitHub Desktop.
Save parzibyte/e3d48d620a49821901bf6b7f9f61fcb3 to your computer and use it in GitHub Desktop.
/*
____ _____ _ _ _
| _ \ | __ \ (_) | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___|
__/ | __/ |
|___/ |___/
____________________________________
/ Si necesitas ayuda, contáctame en \
\ https://parzibyte.me /
------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Creado por Parzibyte (https://parzibyte.me). Este encabezado debe mantenerse intacto,
excepto si este es un proyecto de un estudiante.
*/
#include "conecta4.h"
int contarArriba(int x, int y, char jugador, char tablero[FILAS][COLUMNAS]) {
int yInicio = (y - CONECTA >= 0) ? y - CONECTA + 1 : 0;
int contador = 0;
for (; yInicio <= y; yInicio++) {
if (tablero[yInicio][x] == jugador) {
contador++;
} else {
contador = 0;
}
}
return contador;
}
int contarDerecha(int x, int y, char jugador, char tablero[FILAS][COLUMNAS]) {
int xFin = (x + CONECTA < COLUMNAS) ? x + CONECTA - 1 : COLUMNAS - 1;
int contador = 0;
for (; x <= xFin; x++) {
if (tablero[y][x] == jugador) {
contador++;
} else {
contador = 0;
}
}
return contador;
}
int contarArribaDerecha(int x, int y, char jugador, char tablero[FILAS][COLUMNAS]) {
int xFin = (x + CONECTA < COLUMNAS) ? x + CONECTA - 1 : COLUMNAS - 1;
int yInicio = (y - CONECTA >= 0) ? y - CONECTA + 1 : 0;
int contador = 0;
while (x <= xFin && yInicio <= y) {
if (tablero[y][x] == jugador) {
contador++;
} else {
contador = 0;
}
x++;
y--;
}
return contador;
}
int contarAbajoDerecha(int x, int y, char jugador, char tablero[FILAS][COLUMNAS]) {
int xFin = (x + CONECTA < COLUMNAS) ? x + CONECTA - 1 : COLUMNAS - 1;
int yFin = (y + CONECTA < FILAS) ? y + CONECTA - 1 : FILAS - 1;
int contador = 0;
while (x <= xFin && y <= yFin) {
if (tablero[y][x] == jugador) {
contador++;
} else {
contador = 0;
}
x++;
y++;
}
return contador;
}
int ganador(char jugador, char tablero[FILAS][COLUMNAS]) {
/*
* Solo necesitamos
* Arriba
* Derecha
* Arriba derecha
* Abajo derecha
*
* */
int y;
for (y = 0; y < FILAS; y++) {
int x;
for (x = 0; x < COLUMNAS; x++) {
int conteoArriba = contarArriba(x, y, jugador, tablero);
if (conteoArriba >= CONECTA) {
return CONECTA_ARRIBA;
}
if (contarDerecha(x, y, jugador, tablero) >= CONECTA) {
return CONECTA_DERECHA;
}
if (contarArribaDerecha(x, y, jugador, tablero) >= CONECTA) {
return CONECTA_ARRIBA_DERECHA;
}
if (contarAbajoDerecha(x, y, jugador, tablero) >= CONECTA) {
return CONECTA_ABAJO_DERECHA;
}
}
}
return NO_CONECTA;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment