This commit is contained in:
Lorenzo Bianchi 2023-05-15 11:59:18 +02:00
parent 91fd355aca
commit 775aebae46
1 changed files with 37 additions and 0 deletions

37
src/forza4.c Normal file
View File

@ -0,0 +1,37 @@
#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
}