Elaborato_SO/src/F4Client.c

108 lines
2.4 KiB
C
Raw Normal View History

2023-05-15 11:24:39 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
2023-05-16 15:55:05 +02:00
#include <sys/stat.h>
#include <sys/msg.h>
2023-05-16 16:10:43 +02:00
#include <signal.h>
#include <sys/sem.h>
2023-05-16 20:21:15 +02:00
#include <sys/shm.h>
2023-05-16 23:42:42 +02:00
#include <string.h>
2023-05-17 15:46:51 +02:00
#include <unistd.h>
2023-06-17 12:46:59 +02:00
#include <time.h>
2023-05-15 11:24:39 +02:00
#include <custom_sem.h>
#include <structures.h>
#include <custom_shm.h>
2023-05-16 16:10:43 +02:00
#include <errExit.h>
2023-05-19 15:21:15 +02:00
#include <custom_msgq.h>
#include <forza4.h>
2023-05-29 12:16:36 +02:00
#include <client.h>
2023-05-16 18:43:56 +02:00
2023-05-18 11:23:45 +02:00
void checkClientinput(int argc, char *argv[]);
2023-06-17 11:10:30 +02:00
int generateMove();
2023-05-18 11:23:45 +02:00
2023-05-16 18:46:41 +02:00
int main(int argc, char *argv[]){
2023-06-17 12:46:59 +02:00
srand(time(0));
2023-06-17 12:05:56 +02:00
2023-05-29 15:01:11 +02:00
setupClient(argc, argv);
2023-05-16 15:55:05 +02:00
2023-05-18 11:23:45 +02:00
// aspetto che il server mi dia il permesso di collegarmi e segnalo collegamento al server
2023-05-29 16:14:01 +02:00
semOp(_SEMID, ID, -1);
2023-05-22 11:03:36 +02:00
printf(PCLIENT "Connecting to server...\n");
2023-05-29 13:12:38 +02:00
semOp(_SEMID, 2, 1);
2023-05-15 11:24:39 +02:00
2023-05-29 16:14:01 +02:00
2023-05-17 15:57:10 +02:00
// mando i miei dati al server
2023-06-17 11:10:30 +02:00
player_ds player = {.mtype = 3, .id = ID, .pid = getpid(), .bot = 0};
if (argc == 3) {
2023-06-17 11:39:54 +02:00
if (!strcmp(argv[2], "bot")) {
2023-06-17 11:10:30 +02:00
player.bot = 1;
}
2023-06-17 11:39:54 +02:00
if (!strcmp(argv[2], "auto")) {
2023-06-17 11:10:30 +02:00
clientBot = 1;
}
}
2023-05-17 15:57:10 +02:00
strcpy(player.name_player, argv[1]);
2023-05-19 15:21:15 +02:00
sndPlayer(&player);
2023-05-16 17:07:35 +02:00
2023-06-17 12:05:56 +02:00
if (!clientBot) {
printf(PCLIENT "Searching for oponent...\n");
}
2023-05-29 16:14:01 +02:00
semOp(_SEMID, ID, -1);
2023-05-16 18:43:56 +02:00
2023-06-17 12:05:56 +02:00
if (!clientBot) {
printf(PCLIENT "Opponent found\n");
printBoard();
printf(PCLIENT "Waiting for oponent\n");
}
2023-05-31 11:00:59 +02:00
2023-05-16 23:27:27 +02:00
2023-05-16 17:07:35 +02:00
2023-05-18 11:23:45 +02:00
// PARTITA
move_t move = {.mtype = 2};
2023-05-16 23:27:27 +02:00
2023-05-29 11:49:35 +02:00
while (1) {
2023-05-18 11:23:45 +02:00
// aspetto il mio turno
2023-05-31 11:13:23 +02:00
semOp(_SEMID, ID, -1);
2023-05-22 11:03:36 +02:00
2023-06-17 12:05:56 +02:00
if (!clientBot) {
printBoard();
// input e controllo
printf(PCLIENT "Your Turn: ");
}
2023-05-22 17:22:17 +02:00
2023-05-18 11:23:45 +02:00
do {
2023-06-17 11:10:30 +02:00
if (!clientBot) {
scanf("%d", &move.move);
} else {
2023-06-18 15:54:51 +02:00
move.move = generateMove();
2023-06-17 11:10:30 +02:00
}
2023-05-31 11:00:59 +02:00
move.move = checkMove(move.move - 1);
2023-05-22 17:22:17 +02:00
} while (move.move == -1);
2023-05-18 11:23:45 +02:00
// mando mossa al server
2023-05-19 15:21:15 +02:00
sndMove(&move);
2023-05-26 10:58:57 +02:00
2023-05-29 16:14:01 +02:00
semOp(_SEMID, ID, -1);
2023-06-17 12:05:56 +02:00
if (!clientBot) {
printBoard();
printf(PCLIENT "Waiting for opponent\n");
}
2023-05-18 11:23:45 +02:00
}
2023-05-16 23:27:27 +02:00
2023-05-18 11:23:45 +02:00
return 0;
2023-06-17 11:10:30 +02:00
}
int generateMove() {
2023-06-17 14:31:24 +02:00
int collums;
2023-06-18 11:42:41 +02:00
// genera la mossa casuale finchè la colonna è piena
// appena trova una colonna con spazio libero la ritorna
2023-06-17 14:31:24 +02:00
do {
collums = rand() % _COLLUMS;
} while (_BOARD[collums] != 0);
2023-06-18 15:54:51 +02:00
return collums + 1;
2023-05-15 11:24:39 +02:00
}