Elaborato_SO/src/forza4.c

68 lines
1.2 KiB
C

#include <stdio.h>
#include <forza4.h>
#include <structures.h>
void setCollums(int rows) {
_ROWS = rows;
}
void setCollums(int collums) {
_COLLUMS = collums;
}
int getIndex(int r, int c) {
return r * _COLLUMS + c;
}
int checkLine(tile_t *board, int pos, int delta) {
int count = 1, i;
i = pos - delta;
while (isValid(i) && board[pos] == board[i]) {
count++;
i -= delta;
}
i = pos + delta;
while (isValid(i) && board[pos] == board[i]) {
count++;
i += delta;
}
return count >= 4;
}
// checks board for a win
// returns player tile_t on win, otherwise 0
int checkWin(tile_t *board, int pos, int rows, int collums) {
int result = checkLine(board, pos, ORIZONTAL);
if (result) {
return board[pos];
}
int result = checkLine(board, pos, VERTICAL);
if (result) {
return board[pos];
}
int result = checkLine(board, pos, DIAGONAL);
if (result) {
return board[pos];
}
int result = checkLine(board, pos, DIAGONAL_INV);
if (result) {
return board[pos];
}
return 0;
}
int checkWinAll(tile_t *board, int rows, int collums){
for (int i=0; i<rows*collums; i++) {
if (checkWin(board, i, rows, collums) != EMPTY) {
return checkWin(board, i, rows, collums);
}
}
return EMPTY;
}