2023-05-15 11:59:18 +02:00
|
|
|
#include <stdio.h>
|
2023-05-16 16:55:56 +02:00
|
|
|
#include <forza4.h>
|
2023-05-15 11:59:18 +02:00
|
|
|
|
2023-05-16 11:39:09 +02:00
|
|
|
#include <structures.h>
|
|
|
|
|
2023-05-16 16:55:56 +02:00
|
|
|
void setCollums(int rows) {
|
|
|
|
_ROWS = rows;
|
|
|
|
}
|
2023-05-16 11:03:51 +02:00
|
|
|
|
|
|
|
void setCollums(int collums) {
|
2023-05-16 16:55:56 +02:00
|
|
|
_COLLUMS = collums;
|
2023-05-16 11:03:51 +02:00
|
|
|
}
|
|
|
|
|
2023-05-16 11:39:09 +02:00
|
|
|
int getIndex(int r, int c) {
|
2023-05-16 16:55:56 +02:00
|
|
|
return r * _COLLUMS + c;
|
2023-05-16 11:03:51 +02:00
|
|
|
}
|
2023-05-15 11:59:18 +02:00
|
|
|
|
2023-05-16 16:55:56 +02:00
|
|
|
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;
|
|
|
|
}
|
2023-05-16 11:39:09 +02:00
|
|
|
|
2023-05-16 16:55:56 +02:00
|
|
|
return count >= 4;
|
|
|
|
}
|
2023-05-16 11:39:09 +02:00
|
|
|
|
2023-05-16 16:55:56 +02:00
|
|
|
// 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];
|
|
|
|
}
|
2023-05-15 11:59:18 +02:00
|
|
|
|
2023-05-16 16:55:56 +02:00
|
|
|
int result = checkLine(board, pos, VERTICAL);
|
|
|
|
if (result) {
|
|
|
|
return board[pos];
|
|
|
|
}
|
2023-05-15 11:59:18 +02:00
|
|
|
|
2023-05-16 16:55:56 +02:00
|
|
|
int result = checkLine(board, pos, DIAGONAL);
|
|
|
|
if (result) {
|
|
|
|
return board[pos];
|
|
|
|
}
|
2023-05-15 12:05:29 +02:00
|
|
|
|
2023-05-16 16:55:56 +02:00
|
|
|
int result = checkLine(board, pos, DIAGONAL_INV);
|
|
|
|
if (result) {
|
|
|
|
return board[pos];
|
2023-05-15 16:24:10 +02:00
|
|
|
}
|
2023-05-16 16:55:56 +02:00
|
|
|
|
|
|
|
return 0;
|
2023-05-16 11:45:16 +02:00
|
|
|
}
|
|
|
|
|
2023-05-16 16:55:56 +02:00
|
|
|
int checkWinAll(tile_t *board, int rows, int collums){
|
2023-05-16 11:45:16 +02:00
|
|
|
for (int i=0; i<rows*collums; i++) {
|
|
|
|
if (checkWin(board, i, rows, collums) != EMPTY) {
|
|
|
|
return checkWin(board, i, rows, collums);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 11:39:09 +02:00
|
|
|
return EMPTY;
|
2023-05-15 11:59:18 +02:00
|
|
|
}
|