Elaborato_SO/src/forza4.c

156 lines
2.6 KiB
C
Raw Normal View History

2023-05-15 11:59:18 +02:00
#include <stdio.h>
2023-05-29 11:37:43 +02:00
#include <stdlib.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-23 15:47:34 +02:00
tile_t *_BOARD;
2023-05-29 11:37:43 +02:00
int _ROWS;
int _COLLUMS;
char _TOKEN1;
char _TOKEN2;
2023-05-16 18:43:56 +02:00
void setRows(int rows) {
2023-05-16 16:55:56 +02:00
_ROWS = rows;
}
void setCollums(int collums) {
2023-05-16 16:55:56 +02:00
_COLLUMS = collums;
}
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-15 11:59:18 +02:00
2023-05-17 15:04:42 +02:00
2023-05-31 11:00:59 +02:00
int checkLine(int pos, int delta) {
2023-05-16 16:55:56 +02:00
int count = 1, i;
i = pos - delta;
2023-05-31 11:00:59 +02:00
while (isValid(i) && _BOARD[pos] == _BOARD[i]) {
2023-05-16 16:55:56 +02:00
count++;
i -= delta;
}
i = pos + delta;
2023-05-31 11:00:59 +02:00
while (isValid(i) && _BOARD[pos] == _BOARD[i]) {
2023-05-16 16:55:56 +02:00
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-31 11:00:59 +02:00
// checks _BOARD for a win
2023-06-01 14:39:04 +02:00
// returns player id on win, otherwise -1
2023-05-31 11:00:59 +02:00
int checkWin(int pos) {
int result = checkLine(pos, ORIZONTAL);
2023-05-16 16:55:56 +02:00
if (result) {
2023-05-31 11:13:23 +02:00
return _BOARD[pos] - 1;
2023-05-16 16:55:56 +02:00
}
2023-05-15 11:59:18 +02:00
2023-05-31 11:00:59 +02:00
result = checkLine(pos, VERTICAL);
2023-05-16 16:55:56 +02:00
if (result) {
2023-05-31 11:13:23 +02:00
return _BOARD[pos] - 1;
2023-05-16 16:55:56 +02:00
}
2023-05-15 11:59:18 +02:00
2023-05-31 11:00:59 +02:00
result = checkLine(pos, DIAGONAL);
2023-05-16 16:55:56 +02:00
if (result) {
2023-05-31 11:13:23 +02:00
return _BOARD[pos] - 1;
2023-05-16 16:55:56 +02:00
}
2023-05-15 12:05:29 +02:00
2023-05-31 11:00:59 +02:00
result = checkLine(pos, DIAGONAL_INV);
2023-05-16 16:55:56 +02:00
if (result) {
2023-05-31 11:13:23 +02:00
return _BOARD[pos] - 1;
2023-05-15 16:24:10 +02:00
}
2023-05-16 16:55:56 +02:00
2023-06-01 14:39:04 +02:00
return -1;
2023-05-16 11:45:16 +02:00
}
2023-05-31 11:00:59 +02:00
int checkWinAll(){
2023-05-17 15:04:42 +02:00
for (int i=0; i <_ROWS *_COLLUMS; i++) {
2023-05-31 11:00:59 +02:00
if (checkWin(i) != EMPTY) {
return checkWin(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-31 11:00:59 +02:00
void printBoard() {
2023-05-29 11:37:43 +02:00
if (CLEAR) {
system("clear");
}
2023-05-17 15:04:42 +02:00
for (int i=0; i < _ROWS; i++) {
2023-05-25 14:28:45 +02:00
printf("|");
2023-05-17 15:04:42 +02:00
for (int j=0; j < _COLLUMS; j++) {
2023-05-25 14:28:45 +02:00
printf(" ");
2023-05-31 11:00:59 +02:00
printTile(_BOARD[getIndex(i, j)]);
2023-05-25 14:28:45 +02:00
printf(" ");
2023-05-17 15:04:42 +02:00
}
2023-05-25 14:28:45 +02:00
printf("|\n");
}
2023-06-18 15:54:51 +02:00
for (int i=0; i<_COLLUMS; i++) {
printf("---");
2023-05-17 15:04:42 +02:00
}
2023-06-18 15:54:51 +02:00
printf("--\n");
2023-05-17 15:04:42 +02:00
}
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-06-01 14:39:04 +02:00
printf("\033[94m%c\033[39m", _TOKEN1);
2023-05-17 15:04:42 +02:00
break;
case PLAYER2:
2023-06-01 14:39:04 +02:00
printf("\033[91m%c\033[39m", _TOKEN2);
2023-05-17 15:04:42 +02:00
break;
}
2023-05-17 15:15:02 +02:00
}
2023-06-17 12:05:56 +02:00
// ritorna la posizione in cui il giocatore potra iserire la sua mossa
2023-05-31 11:00:59 +02:00
int checkMove(int collums) {
2023-05-25 14:28:45 +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;
}
2023-05-31 11:00:59 +02:00
if (_BOARD[collums] != EMPTY) {
2023-05-18 11:59:36 +02:00
char buf[100];
sprintf(buf, "the collum %d is full", collums + 1);
warningMsg(buf);
return -1;
}
int pos = collums;
2023-05-31 11:00:59 +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-31 11:00:59 +02:00
void insertMove(int pos, int turn) {
2023-05-26 10:58:57 +02:00
printf("playing %d in %d\n", (turn == 0) ? PLAYER1 : PLAYER2, pos);
2023-06-01 14:39:04 +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
}