checkmove

This commit is contained in:
Edi De Candido 2023-05-18 11:59:36 +02:00
parent 77a16c43a3
commit 9ecee70798
3 changed files with 52 additions and 35 deletions

View File

@ -1,17 +1,9 @@
- magari fare che (alcune delle) print vengano messe su tutti e tre i terminali - magari fare che (alcune delle) print vengano messe su tutti e tre i terminali
(motivo per cui ho messo le funzioni in nonsodovemetterle) (motivo per cui ho messo le funzioni in nonsodovemetterle)
(forse basta tipo duplicare il fd ma sono stanco ci penso domani) (forse basta tipo duplicare il fd ma sono stanco ci penso domani)
- controllo input dei client
- sistemare include (FARE SOLO ALLA FINE ALTRIMENTI SPRECO DI TEMPO COME LA TUA VITA) - sistemare include (FARE SOLO ALLA FINE ALTRIMENTI SPRECO DI TEMPO COME LA TUA VITA)
- invece di fare tutti if == -1 si potrebbe fare come semOp e creare una funzione identica - invece di fare tutti if == -1 si potrebbe fare come semOp e creare una funzione identica
che fa gia il controllo (ha come svantaggio che non puoi specifare dove va in errore con l'errexit) che fa gia il controllo (ha come svantaggio che non puoi specifare dove va in errore con l'errexit)
(anche se forse basta passare la/le stringe ma vedi riga 3) (anche se forse basta passare la/le stringe ma vedi riga 3) EDI APPROVA
- killare il server deve uccidere i figli
- isValid è corretto?
- getIndex con define?

View File

@ -9,20 +9,20 @@ void serverSigHandler(int sig) {
static int sigint_count = DEFAULT_SIGINT; static int sigint_count = DEFAULT_SIGINT;
switch (sig) { switch (sig) {
case SIGINT: case SIGINT:
sigint_count--; sigint_count--;
if (sigint_count > 0) { if (sigint_count > 0) {
printf("<Server> Press again Ctrl^C to exit (whitin %d sec)\n", TIME_TO_RESET); printf("<Server> Press again Ctrl^C to exit (whitin %d sec)\n", TIME_TO_RESET);
alarm(TIME_TO_RESET); alarm(TIME_TO_RESET);
} else { } else {
// TODO bisogna fare un "at exit" // TODO bisogna fare un "at exit"
exit(0); exit(0);
} }
break; break;
default: //SIGALRM default: //SIGALRM
printf("<Server> Time to exit (%d sec) expired\n", TIME_TO_RESET); printf("<Server> Time to exit (%d sec) expired\n", TIME_TO_RESET);
sigint_count = DEFAULT_SIGINT; sigint_count = DEFAULT_SIGINT;
break; break;
} }
} }

View File

@ -1,6 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <forza4.h> #include <forza4.h>
#include <errExit.h>
#include <structures.h> #include <structures.h>
void setRows(int rows) { void setRows(int rows) {
@ -11,13 +11,18 @@ void setCollums(int collums) {
_COLLUMS = collums; _COLLUMS = collums;
} }
void setDimension(int rows, int collums) {
setRows(rows);
setCollums(collums);
}
void setTokens(char t1, char t2) { void setTokens(char t1, char t2) {
_TOKEN1 = t1; _TOKEN1 = t1;
_TOKEN2 = t2; _TOKEN2 = t2;
} }
int getIndex(int r, int c) { int getIndex(int i, int j) {
return r * _COLLUMS + c; return i * _COLLUMS + j;
} }
@ -73,10 +78,10 @@ int checkWinAll(tile_t *board){
return EMPTY; return EMPTY;
} }
void printBoard(tile_t *board){ void printBoard(tile_t *board) {
for (int i=0; i < _ROWS; i++) { for (int i=0; i < _ROWS; i++) {
for (int j=0; j < _COLLUMS; j++) { for (int j=0; j < _COLLUMS; j++) {
printTile(board[i * _COLLUMS + j]); printTile(board[getIndex(i, j)]);
if (j != _COLLUMS - 1) { if (j != _COLLUMS - 1) {
printf(" | "); printf(" | ");
} else { } else {
@ -84,7 +89,7 @@ void printBoard(tile_t *board){
} }
} }
for(int j=0; i != _ROWS - 1 && j < _COLLUMS - 1; j++) { for (int j=0; i != _ROWS - 1 && j < _COLLUMS - 1; j++) {
printf("----"); printf("----");
} }
if (i < _ROWS - 1) { if (i < _ROWS - 1) {
@ -93,7 +98,7 @@ void printBoard(tile_t *board){
} }
} }
void printTile(tile_t t){ void printTile(tile_t t) {
switch(t) { switch(t) {
case EMPTY: case EMPTY:
printf(" "); printf(" ");
@ -107,9 +112,29 @@ void printTile(tile_t t){
} }
} }
int getHeight(tile_t *board, int col){ int checkMove(tile_t *board, int collums) {
int height = _ROWS - 1; if (collums < 0 || collums > _COLLUMS) {
while (height-- >= 0 && board[getIndex(height, col)] == EMPTY); char buf[100];
sprintf(buf, "the collums must be between 1 and %d", _COLLUMS);
warningMsg(buf);
return -1;
}
return height; if (board[collums] != EMPTY) {
char buf[100];
sprintf(buf, "the collum %d is full", collums + 1);
warningMsg(buf);
return -1;
}
int pos = collums;
while (isValid(pos + VERTICAL) && board[pos + VERTICAL] == EMPTY) {
pos += VERTICAL;
}
return pos;
}
void insertCoin() {
} }