getwin
This commit is contained in:
parent
23d3be833a
commit
8a6bf13e77
|
@ -7,6 +7,6 @@ static int col;
|
|||
|
||||
void setCollums(int collums);
|
||||
int getIndex(int i, int j);
|
||||
int checkWin(tile_t *board, int x, int y, int rows, int collums);
|
||||
tile_t checkWin(tile_t *board, int pos, int rows, int collums);
|
||||
|
||||
#endif
|
94
src/forza4.c
94
src/forza4.c
|
@ -1,66 +1,68 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include <structures.h>
|
||||
|
||||
static int col;
|
||||
|
||||
void setCollums(int collums) {
|
||||
col = collums;
|
||||
}
|
||||
|
||||
int getIndex(int i, int j) {
|
||||
return i * col + j;
|
||||
int getIndex(int r, int c) {
|
||||
return r * col + c;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// returns player tile_t on win, otherwise 0
|
||||
tile_t checkWin(tile_t *board, int pos, int rows, int collums) {
|
||||
int i;
|
||||
tile_t current = board[x + y * collums];
|
||||
|
||||
// horrizontal
|
||||
tile_t current = board[pos];
|
||||
int r = pos / collums;
|
||||
int c = pos % collums;
|
||||
|
||||
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
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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]) {
|
||||
|
||||
// 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;
|
||||
}
|
Loading…
Reference in New Issue