2023-05-15 11:59:18 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <structures.h>
|
|
|
|
|
|
|
|
// checks board for a win
|
|
|
|
// returns player number on win, otherwise 0
|
|
|
|
// x e y come cartesiani, rows e collums le dimensioni
|
|
|
|
int checkWin(tile_t *board, int x, int y, int rows, int collums) {
|
|
|
|
int i;
|
|
|
|
tile_t current = board[x + y * collums];
|
|
|
|
|
|
|
|
// horrizontal
|
|
|
|
for (i=0; i<4; i++) {
|
|
|
|
if (x + i - 3 >= 0 && x + i < collums) {
|
|
|
|
if (board[x + i - 3 + y * collums] == board[x + i + y * collums] &&
|
|
|
|
board[x + i - 2 + y * collums] == board[x + i + y * collums] &&
|
|
|
|
board[x + i - 1 + y * collums] == board[x + i + y * collums]) {
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vertical
|
|
|
|
for (i=0; i<4; i++) {
|
|
|
|
if (y + i - 3 >= 0 && y + i < rows) {
|
|
|
|
if (board[x + (y + i - 3) * collums] == board[x + (y + i) * collums] &&
|
|
|
|
board[x + (y + i - 2) * collums] == board[x + (y + i) * collums] &&
|
|
|
|
board[x + (y + i - 1) * collums] == board[x + (y + i) * collums]) {
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// diagonal
|
2023-05-15 12:05:29 +02:00
|
|
|
for (i=0; i<4; i++) {
|
|
|
|
if (y + i - 3 >= 0 && y + i < rows && x + i - 3 >= 0 && x + i < collums) {
|
|
|
|
if (board[x - 3 + i + (y + i - 3) * collums] == board[x + i + (y + i) * collums] &&
|
|
|
|
board[x - 2 + i + (y + i - 2) * collums] == board[x + i + (y + i) * collums] &&
|
|
|
|
board[x - 1 + i + (y + i - 1) * collums] == board[x + i + (y + i) * collums]) {
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// manca seconda diagonal
|
2023-05-15 11:59:18 +02:00
|
|
|
}
|