using global var _BOARD
This commit is contained in:
parent
a7a5582087
commit
f40ecead80
8
Makefile
8
Makefile
|
@ -69,12 +69,14 @@ MKFILE_DIR := $(dir $(mkfile_path))
|
|||
ARGSSERVER := 6 7 O X
|
||||
ARGSCLIENT1 := Test1
|
||||
ARGSCLIENT2 := Test2
|
||||
OPEN_NEW_TERMINAL := x-terminal-emulator -e
|
||||
OPEN_NEW_TERMINAL := x-terminal-emulator
|
||||
TERMINAL_ARGS1 := --geometry=90x30 -e
|
||||
TERMINAL_ARGS2 := --geometry=10x30 -e
|
||||
# OPEN_NEW_TERMINAL := kitty
|
||||
|
||||
test: comp
|
||||
$(OPEN_NEW_TERMINAL) $(MKFILE_DIR) $(TARGET2) $(ARGSCLIENT1) |\
|
||||
$(OPEN_NEW_TERMINAL) $(MKFILE_DIR) $(TARGET2) $(ARGSCLIENT2) |\
|
||||
$(OPEN_NEW_TERMINAL) $(TERMINAL_ARGS1) $(MKFILE_DIR) $(TARGET2) $(ARGSCLIENT1)
|
||||
$(OPEN_NEW_TERMINAL) $(TERMINAL_ARGS2) $(MKFILE_DIR) $(TARGET2) $(ARGSCLIENT2)
|
||||
./$(TARGET1) $(ARGSSERVER)
|
||||
|
||||
comp: server_c client_c
|
||||
|
|
10
inc/forza4.h
10
inc/forza4.h
|
@ -22,13 +22,13 @@ void setRows(int rows);
|
|||
void setTokens(char t1, char t2);
|
||||
int getIndex(int i, int j);
|
||||
|
||||
int checkWin(tile_t *board, int pos);
|
||||
int checkWinAll(tile_t *board);
|
||||
int checkMove(tile_t *board, int collums);
|
||||
int checkWin(int pos);
|
||||
int checkWinAll();
|
||||
int checkMove(int collums);
|
||||
|
||||
void printBoard(tile_t *board);
|
||||
void printBoard();
|
||||
void printTile(tile_t t);
|
||||
void insertMove(tile_t *board, int pos, int turn);
|
||||
void insertMove(int pos, int turn);
|
||||
|
||||
int isValid(int pos);
|
||||
|
||||
|
|
|
@ -28,8 +28,6 @@ int main(int argc, char *argv[]){
|
|||
setupClient(argc, argv);
|
||||
|
||||
// aspetto che il server mi dia il permesso di collegarmi e segnalo collegamento al server
|
||||
printf("fin qua funzia (spero) %d\n", ID);
|
||||
scanf("%d");
|
||||
semOp(_SEMID, ID, -1);
|
||||
printf(PCLIENT "Connecting to server...\n");
|
||||
semOp(_SEMID, 2, 1);
|
||||
|
@ -46,6 +44,9 @@ int main(int argc, char *argv[]){
|
|||
|
||||
printf(PCLIENT "Opponent found\n");
|
||||
|
||||
printBoard();
|
||||
printf(PCLIENT "Waiting for oponent\n");
|
||||
|
||||
|
||||
|
||||
// PARTITA
|
||||
|
@ -54,7 +55,7 @@ int main(int argc, char *argv[]){
|
|||
while (1) {
|
||||
// aspetto il mio turno
|
||||
semOp(_SEMID, ID, -1); // mettere id eccetera in var
|
||||
printBoard(_BOARD);
|
||||
printBoard();
|
||||
|
||||
// input e controllo
|
||||
printf(PCLIENT "Your Turn: ");
|
||||
|
@ -62,14 +63,14 @@ int main(int argc, char *argv[]){
|
|||
int pos;
|
||||
do {
|
||||
scanf("%d", &move.move);
|
||||
move.move = checkMove(_BOARD, move.move - 1);
|
||||
move.move = checkMove(move.move - 1);
|
||||
} while (move.move == -1);
|
||||
|
||||
// mando mossa al server
|
||||
sndMove(&move);
|
||||
|
||||
semOp(_SEMID, ID, -1);
|
||||
printBoard(_BOARD);
|
||||
printBoard();
|
||||
|
||||
printf(PCLIENT "Waiting for oponent\n"); //FIXME: solo se la partita non è finita
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ int main(int argc, char *argv[]){
|
|||
// aspetto che mi mandi una mossa
|
||||
rcvMove(&move);
|
||||
|
||||
insertMove(_BOARD, move.move, turn);
|
||||
insertMove(move.move, turn);
|
||||
|
||||
// info
|
||||
printf("move: %d\n", move.move);
|
||||
|
@ -86,7 +86,7 @@ int main(int argc, char *argv[]){
|
|||
// next turn
|
||||
turn ^= 1;
|
||||
|
||||
} while (--turns_left && !(result = checkWin(_BOARD, move.move)));
|
||||
} while (--turns_left && !(result = checkWin(move.move)));
|
||||
|
||||
|
||||
if (!result) {
|
||||
|
|
|
@ -109,7 +109,7 @@ void sigHandlerClient(int sig) {
|
|||
if (sig == SIGINT) {
|
||||
//TODO: abbandono
|
||||
|
||||
sig = SIGTERM;
|
||||
kill(getpid(), SIGINT);
|
||||
}
|
||||
|
||||
// Terminazione e chiusura terminale
|
||||
|
|
46
src/forza4.c
46
src/forza4.c
|
@ -36,15 +36,15 @@ int getIndex(int i, int j) {
|
|||
}
|
||||
|
||||
|
||||
int checkLine(tile_t *board, int pos, int delta) {
|
||||
int checkLine(int pos, int delta) {
|
||||
int count = 1, i;
|
||||
i = pos - delta;
|
||||
while (isValid(i) && board[pos] == board[i]) {
|
||||
while (isValid(i) && _BOARD[pos] == _BOARD[i]) {
|
||||
count++;
|
||||
i -= delta;
|
||||
}
|
||||
i = pos + delta;
|
||||
while (isValid(i) && board[pos] == board[i]) {
|
||||
while (isValid(i) && _BOARD[pos] == _BOARD[i]) {
|
||||
count++;
|
||||
i += delta;
|
||||
}
|
||||
|
@ -52,43 +52,43 @@ int checkLine(tile_t *board, int pos, int delta) {
|
|||
return count >= 4;
|
||||
}
|
||||
|
||||
// checks board for a win
|
||||
// checks _BOARD for a win
|
||||
// returns player tile_t on win, otherwise 0
|
||||
int checkWin(tile_t *board, int pos) {
|
||||
int result = checkLine(board, pos, ORIZONTAL);
|
||||
int checkWin(int pos) {
|
||||
int result = checkLine(pos, ORIZONTAL);
|
||||
if (result) {
|
||||
return board[pos];
|
||||
return _BOARD[pos];
|
||||
}
|
||||
|
||||
result = checkLine(board, pos, VERTICAL);
|
||||
result = checkLine(pos, VERTICAL);
|
||||
if (result) {
|
||||
return board[pos];
|
||||
return _BOARD[pos];
|
||||
}
|
||||
|
||||
result = checkLine(board, pos, DIAGONAL);
|
||||
result = checkLine(pos, DIAGONAL);
|
||||
if (result) {
|
||||
return board[pos];
|
||||
return _BOARD[pos];
|
||||
}
|
||||
|
||||
result = checkLine(board, pos, DIAGONAL_INV);
|
||||
result = checkLine(pos, DIAGONAL_INV);
|
||||
if (result) {
|
||||
return board[pos];
|
||||
return _BOARD[pos];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int checkWinAll(tile_t *board){
|
||||
int checkWinAll(){
|
||||
for (int i=0; i <_ROWS *_COLLUMS; i++) {
|
||||
if (checkWin(board, i) != EMPTY) {
|
||||
return checkWin(board, i);
|
||||
if (checkWin(i) != EMPTY) {
|
||||
return checkWin(i);
|
||||
}
|
||||
}
|
||||
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
void printBoard(tile_t *board) {
|
||||
void printBoard() {
|
||||
if (CLEAR) {
|
||||
system("clear");
|
||||
}
|
||||
|
@ -97,7 +97,7 @@ void printBoard(tile_t *board) {
|
|||
printf("|");
|
||||
for (int j=0; j < _COLLUMS; j++) {
|
||||
printf(" ");
|
||||
printTile(board[getIndex(i, j)]);
|
||||
printTile(_BOARD[getIndex(i, j)]);
|
||||
printf(" ");
|
||||
}
|
||||
printf("|\n");
|
||||
|
@ -125,7 +125,7 @@ void printTile(tile_t t) {
|
|||
// ritorna la posizione in cui il giocqtore potra iserire la sua mossa
|
||||
// gli si passa la colona partendo da 1
|
||||
// se
|
||||
int checkMove(tile_t *board, int collums) {
|
||||
int checkMove(int collums) {
|
||||
if (collums < 0 || collums >= _COLLUMS) {
|
||||
char buf[100];
|
||||
sprintf(buf, "the collums must be between 1 and %d", _COLLUMS);
|
||||
|
@ -133,7 +133,7 @@ int checkMove(tile_t *board, int collums) {
|
|||
return -1;
|
||||
}
|
||||
|
||||
if (board[collums] != EMPTY) {
|
||||
if (_BOARD[collums] != EMPTY) {
|
||||
char buf[100];
|
||||
sprintf(buf, "the collum %d is full", collums + 1);
|
||||
warningMsg(buf);
|
||||
|
@ -141,16 +141,16 @@ int checkMove(tile_t *board, int collums) {
|
|||
}
|
||||
|
||||
int pos = collums;
|
||||
while (isValid(pos + VERTICAL) && board[pos + VERTICAL] == EMPTY) {
|
||||
while (isValid(pos + VERTICAL) && _BOARD[pos + VERTICAL] == EMPTY) {
|
||||
pos += VERTICAL;
|
||||
}
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
void insertMove(tile_t *board, int pos, int turn) {
|
||||
void insertMove(int pos, int turn) {
|
||||
printf("playing %d in %d\n", (turn == 0) ? PLAYER1 : PLAYER2, pos);
|
||||
board[pos] = (turn == 0) ? PLAYER2 : PLAYER1;
|
||||
_BOARD[pos] = (turn == 0) ? PLAYER2 : PLAYER1;
|
||||
}
|
||||
|
||||
int isValid(int pos) {
|
||||
|
|
Loading…
Reference in New Issue