lore smart

This commit is contained in:
Edi De Candido 2023-05-16 16:55:56 +02:00
parent 0e63c14bc7
commit 33bbc9b578
2 changed files with 57 additions and 57 deletions

View File

@ -3,12 +3,22 @@
#include <structures.h> #include <structures.h>
static int col; #define ORIZONTAL 1
#define VERTICAL _COLLUMS
#define DIAGONAL _COLLUMS - 1
#define DIAGONAL_INV _COLLUMS + 1
#define isValid(pos) (pos > 0 && pos / _COLLUMS < _ROWS)
// global variable
static int _ROWS;
static int _COLLUMS;
void setCollums(int collums); void setCollums(int collums);
int getIndex(int i, int j); int getIndex(int i, int j);
tile_t checkWin(tile_t *board, int pos, int rows, int collums); int checkWin(tile_t *board, int pos, int rows, int collums);
tile_t checkWinAll(tile_t *board, int rows, int collums); int checkWinAll(tile_t *board, int rows, int collums);
#endif #endif

View File

@ -1,73 +1,63 @@
#include <stdio.h> #include <stdio.h>
#include <forza4.h>
#include <structures.h> #include <structures.h>
static int col; void setCollums(int rows) {
_ROWS = rows;
}
void setCollums(int collums) { void setCollums(int collums) {
col = collums; _COLLUMS = collums;
} }
int getIndex(int r, int c) { int getIndex(int r, int c) {
return r * col + 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 // checks board for a win
// returns player tile_t on win, otherwise 0 // returns player tile_t on win, otherwise 0
tile_t checkWin(tile_t *board, int pos, int rows, int collums) { int checkWin(tile_t *board, int pos, int rows, int collums) {
int i; int result = checkLine(board, pos, ORIZONTAL);
if (result) {
tile_t current = board[pos]; return board[pos];
int r = pos / collums;
int c = pos % collums;
for (i=0; i<4; i++) {
// vertical
if (r + i - 3 >= 0 && r + i < rows) {
if (board[getIndex(r + i - 3, c)] == board[getIndex(r + i, c)] &&
board[getIndex(r + i - 2, c)] == board[getIndex(r + i, c)] &&
board[getIndex(r + i - 1, c)] == board[getIndex(r + i, c)]) {
return current;
}
}
// horizontal
if (c + i - 3 >= 0 && c + i < collums) {
if (board[getIndex(r, c + i - 3)] == board[getIndex(r, c + i)] &&
board[getIndex(r, c + i - 2)] == board[getIndex(r, c + i)] &&
board[getIndex(r, c + i - 1)] == board[getIndex(r, c + i)]) {
return current;
}
}
// diagonal1
if (c + i - 3 >= 0 && c + i < collums && r + i - 3 >= 0 && r + i < rows) {
if (board[getIndex(r + i - 3, c + i - 3)] == board[getIndex(r + i, c + i)] &&
board[getIndex(r + i - 2, c + i - 2)] == board[getIndex(r + i, c + i)] &&
board[getIndex(r + i - 1, c + i - 1)] == board[getIndex(r + i, c + i)]) {
return current;
}
}
// diagonal2
if (c + i + 3 >= 0 && c + i < collums && r - i + 3 < rows && r - i >= 0) {
if (board[getIndex(r - i + 3, c + i - 3)] == board[getIndex(r - i, c + i)] &&
board[getIndex(r - i + 2, c + i - 2)] == board[getIndex(r - i, c + i)] &&
board[getIndex(r - i + 1, c + i - 1)] == board[getIndex(r - i, c + i)]) {
return current;
}
}
} }
return EMPTY; 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;
} }
tile_t checkWinAll(tile_t *board, int rows, int collums){ int checkWinAll(tile_t *board, int rows, int collums){
for (int i=0; i<rows*collums; i++) { for (int i=0; i<rows*collums; i++) {
if (checkWin(board, i, rows, collums) != EMPTY) { if (checkWin(board, i, rows, collums) != EMPTY) {
return checkWin(board, i, rows, collums); return checkWin(board, i, rows, collums);