Elaborato_SO/src/F4Client.c

108 lines
2.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/stat.h>
#include <sys/msg.h>
#include <signal.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <custom_sem.h>
#include <structures.h>
#include <custom_shm.h>
#include <errExit.h>
#include <custom_msgq.h>
#include <forza4.h>
#include <client.h>
void checkClientinput(int argc, char *argv[]);
int generateMove();
int main(int argc, char *argv[]){
srand(time(0));
setupClient(argc, argv);
// aspetto che il server mi dia il permesso di collegarmi e segnalo collegamento al server
semOp(_SEMID, ID, -1);
printf(PCLIENT "Connecting to server...\n");
semOp(_SEMID, 2, 1);
// mando i miei dati al server
player_ds player = {.mtype = 3, .id = ID, .pid = getpid(), .bot = 0};
if (argc == 3) {
if (!strcmp(argv[2], "bot")) {
player.bot = 1;
}
if (!strcmp(argv[2], "auto")) {
clientBot = 1;
}
}
strcpy(player.name_player, argv[1]);
sndPlayer(&player);
if (!clientBot) {
printf(PCLIENT "Searching for oponent...\n");
}
semOp(_SEMID, ID, -1);
if (!clientBot) {
printf(PCLIENT "Opponent found\n");
printBoard();
printf(PCLIENT "Waiting for oponent\n");
}
// PARTITA
move_t move = {.mtype = 2};
while (1) {
// aspetto il mio turno
semOp(_SEMID, ID, -1);
if (!clientBot) {
printBoard();
// input e controllo
printf(PCLIENT "Your Turn: ");
}
do {
if (!clientBot) {
scanf("%d", &move.move);
} else {
move.move = generateMove();
}
move.move = checkMove(move.move - 1);
} while (move.move == -1);
// mando mossa al server
sndMove(&move);
semOp(_SEMID, ID, -1);
if (!clientBot) {
printBoard();
printf(PCLIENT "Waiting for opponent\n");
}
}
return 0;
}
int generateMove() {
int collums;
// genera la mossa casuale finchè la colonna è piena
// appena trova una colonna con spazio libero la ritorna
do {
collums = rand() % _COLLUMS;
} while (_BOARD[collums] != 0);
return collums + 1;
}