2023-05-15 11:59:18 +02:00
|
|
|
#include <stdio.h>
|
2023-05-16 16:55:56 +02:00
|
|
|
#include <forza4.h>
|
2023-05-18 11:59:36 +02:00
|
|
|
#include <errExit.h>
|
2023-05-16 11:39:09 +02:00
|
|
|
#include <structures.h>
|
|
|
|
|
2023-05-16 18:43:56 +02:00
|
|
|
void setRows(int rows) {
|
2023-05-16 16:55:56 +02:00
|
|
|
_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-18 11:59:36 +02:00
|
|
|
void setDimension(int rows, int collums) {
|
|
|
|
setRows(rows);
|
|
|
|
setCollums(collums);
|
|
|
|
}
|
|
|
|
|
2023-05-17 15:04:42 +02:00
|
|
|
void setTokens(char t1, char t2) {
|
|
|
|
_TOKEN1 = t1;
|
|
|
|
_TOKEN2 = t2;
|
|
|
|
}
|
|
|
|
|
2023-05-18 11:59:36 +02:00
|
|
|
int getIndex(int i, int j) {
|
|
|
|
return i * _COLLUMS + j;
|
2023-05-16 11:03:51 +02:00
|
|
|
}
|
2023-05-15 11:59:18 +02:00
|
|
|
|
2023-05-17 15:04:42 +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
|
2023-05-17 15:04:42 +02:00
|
|
|
int checkWin(tile_t *board, int pos) {
|
2023-05-16 16:55:56 +02:00
|
|
|
int result = checkLine(board, pos, ORIZONTAL);
|
|
|
|
if (result) {
|
|
|
|
return board[pos];
|
|
|
|
}
|
2023-05-15 11:59:18 +02:00
|
|
|
|
2023-05-16 18:43:56 +02:00
|
|
|
result = checkLine(board, pos, VERTICAL);
|
2023-05-16 16:55:56 +02:00
|
|
|
if (result) {
|
|
|
|
return board[pos];
|
|
|
|
}
|
2023-05-15 11:59:18 +02:00
|
|
|
|
2023-05-16 18:43:56 +02:00
|
|
|
result = checkLine(board, pos, DIAGONAL);
|
2023-05-16 16:55:56 +02:00
|
|
|
if (result) {
|
|
|
|
return board[pos];
|
|
|
|
}
|
2023-05-15 12:05:29 +02:00
|
|
|
|
2023-05-16 18:43:56 +02:00
|
|
|
result = checkLine(board, pos, DIAGONAL_INV);
|
2023-05-16 16:55:56 +02:00
|
|
|
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-17 15:04:42 +02:00
|
|
|
int checkWinAll(tile_t *board){
|
|
|
|
for (int i=0; i <_ROWS *_COLLUMS; i++) {
|
|
|
|
if (checkWin(board, i) != EMPTY) {
|
|
|
|
return checkWin(board, i);
|
2023-05-16 11:45:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 11:39:09 +02:00
|
|
|
return EMPTY;
|
2023-05-17 15:04:42 +02:00
|
|
|
}
|
|
|
|
|
2023-05-18 11:59:36 +02:00
|
|
|
void printBoard(tile_t *board) {
|
2023-05-17 15:04:42 +02:00
|
|
|
for (int i=0; i < _ROWS; i++) {
|
|
|
|
for (int j=0; j < _COLLUMS; j++) {
|
2023-05-18 11:59:36 +02:00
|
|
|
printTile(board[getIndex(i, j)]);
|
2023-05-17 15:04:42 +02:00
|
|
|
if (j != _COLLUMS - 1) {
|
|
|
|
printf(" | ");
|
|
|
|
} else {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-18 11:59:36 +02:00
|
|
|
for (int j=0; i != _ROWS - 1 && j < _COLLUMS - 1; j++) {
|
2023-05-17 15:04:42 +02:00
|
|
|
printf("----");
|
|
|
|
}
|
|
|
|
if (i < _ROWS - 1) {
|
|
|
|
printf("---\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-18 11:59:36 +02:00
|
|
|
void printTile(tile_t t) {
|
2023-05-17 15:04:42 +02:00
|
|
|
switch(t) {
|
|
|
|
case EMPTY:
|
|
|
|
printf(" ");
|
|
|
|
break;
|
|
|
|
case PLAYER1:
|
2023-05-23 12:35:25 +02:00
|
|
|
printf("\033[91m%c\033[39m", _TOKEN1);
|
2023-05-17 15:04:42 +02:00
|
|
|
break;
|
|
|
|
case PLAYER2:
|
2023-05-23 12:35:25 +02:00
|
|
|
printf("\033[94m%c\033[39m", _TOKEN2);
|
2023-05-17 15:04:42 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-05-17 15:15:02 +02:00
|
|
|
}
|
|
|
|
|
2023-05-22 17:22:17 +02:00
|
|
|
// ritorna la posizione in cui il giocqtore potra iserire la sua mossa
|
|
|
|
// gli si passa la colona partendo da 1
|
|
|
|
// se
|
2023-05-18 11:59:36 +02:00
|
|
|
int checkMove(tile_t *board, int collums) {
|
2023-05-22 17:22:17 +02:00
|
|
|
if (collums <= 0 || collums > _COLLUMS) {
|
2023-05-18 11:59:36 +02:00
|
|
|
char buf[100];
|
|
|
|
sprintf(buf, "the collums must be between 1 and %d", _COLLUMS);
|
|
|
|
warningMsg(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (board[collums] != EMPTY) {
|
|
|
|
char buf[100];
|
|
|
|
sprintf(buf, "the collum %d is full", collums + 1);
|
|
|
|
warningMsg(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pos = collums;
|
2023-05-22 17:22:17 +02:00
|
|
|
while (isValid(pos + VERTICAL) && board[pos + VERTICAL] == EMPTY) {
|
2023-05-18 11:59:36 +02:00
|
|
|
pos += VERTICAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
2023-05-17 15:15:02 +02:00
|
|
|
|
2023-05-18 12:07:19 +02:00
|
|
|
void insertMove(tile_t *board, int pos, int turn) {
|
2023-05-23 12:11:17 +02:00
|
|
|
printf("playing %d in %d\n", (turn == 0) ? PLAYER2 : PLAYER1 , pos);
|
2023-05-18 12:07:19 +02:00
|
|
|
board[pos] = (turn == 0) ? PLAYER1 : PLAYER2;
|
2023-05-22 17:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int isValid(int pos) {
|
|
|
|
return pos >= 0 && pos < _ROWS * _COLLUMS;
|
2023-05-15 11:59:18 +02:00
|
|
|
}
|