116 lines
2.4 KiB
C
116 lines
2.4 KiB
C
#include <sys/types.h>
|
|
#include <sys/ipc.h>
|
|
#include <sys/shm.h>
|
|
#include <sys/stat.h>
|
|
#include <signal.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <sys/msg.h>
|
|
#include <sys/sem.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include <server.h>
|
|
#include <structures.h>
|
|
#include <custom_sem.h>
|
|
#include <custom_shm.h>
|
|
#include <errExit.h>
|
|
#include <forza4.h>
|
|
#include <custom_msgq.h>
|
|
#include <custom_sig.h>
|
|
|
|
int main(int argc, char *argv[]){
|
|
printf("Il mio pid per killarmi: %d\n", getpid()); //FIXME: TMP
|
|
|
|
setupServer(argc, argv);
|
|
|
|
|
|
// aperta semaforo per connessione dei due client
|
|
printf(PSERVER "Waiting for players...\n");
|
|
semOp(_SEMID, 0, 1);
|
|
semOp(_SEMID, 1, 1);
|
|
|
|
|
|
// aspetto che si connettano i due client
|
|
semOp(_SEMID, 2, -1);
|
|
|
|
player_ds player;
|
|
char name[2][MAX_NAME];
|
|
|
|
// ricevuta nome primo client
|
|
rcvPlayer(&player);
|
|
|
|
strcpy(name[player.id], player.name_player);
|
|
_PIDS[player.id] = player.pid;
|
|
|
|
printf(PSERVER "Player %s connected\n", name[player.id]);
|
|
|
|
// aspetto secondo client
|
|
semOp(_SEMID, 2, -1);
|
|
// ricevuta nome secondo client
|
|
rcvPlayer(&player);
|
|
|
|
strcpy(name[player.id], player.name_player);
|
|
_PIDS[player.id] = player.pid;
|
|
|
|
printf(PSERVER "Player %s connected\n", name[player.id]);
|
|
|
|
semOp(_SEMID, 0, 1);
|
|
semOp(_SEMID, 1, 1);
|
|
|
|
|
|
// PARTITA
|
|
printf(PSERVER "Starting game\n");
|
|
|
|
printf("%d %d\n", _ROWS, _COLLUMS);
|
|
for (int i=0; i < _ROWS * _COLLUMS; i++) { //FIXME: tmp
|
|
_BOARD[i] = EMPTY;
|
|
}
|
|
|
|
|
|
int turns_left = _INPUT_S.rows * _INPUT_S.collums;
|
|
int turn = 0;
|
|
tile_t result;
|
|
move_t move;
|
|
|
|
do {
|
|
semOp(_SEMID, turn, 1);
|
|
|
|
printf(PSERVER "%s's turn\n", name[turn]);
|
|
|
|
rcvMove(&move);
|
|
|
|
//TMP
|
|
printf("move: %d\n", move.move);
|
|
|
|
insertMove(_BOARD, move.move, turn);
|
|
|
|
printf("turns left: %d\n", turns_left - 1);
|
|
|
|
semOp(_SEMID, turn, 1);
|
|
|
|
|
|
turn ^= 1;
|
|
} while (--turns_left && !(result = checkWin(_BOARD, move.move)));
|
|
|
|
|
|
if (!result) { //FIXME: change printf
|
|
printf(PSERVER "Game ended in a draw\n");
|
|
|
|
} else {
|
|
printf(PSERVER "Game ended\n");
|
|
}
|
|
|
|
game_end_t game_end = {.mtype = 4, .winner = result};
|
|
sndGame_end(&game_end);
|
|
sndGame_end(&game_end);
|
|
|
|
kill(_PIDS[0], SIGUSR1);
|
|
kill(_PIDS[1], SIGUSR1);
|
|
|
|
|
|
//TODO: end of game
|
|
|
|
return 0;
|
|
}
|