int HandleMessage(MessageType* m, int incomeSd, int userID) { if (m->type == login) //добавить пользователя и найти ему пару { return UserLogin(m, incomeSd, userID); } else if (m->type == logout) { return UserLogout(incomeSd, userID); } if (m->type == disposition) { return UserDisposition(incomeSd, userID); } if (m->type == try_turn) { return UserTryTurn(incomeSd, userID); } if (m->type == turn) { return UserTurn(m, incomeSd, userID); } return 0; }
int UserTryTurn(int incomeSd, int userID) { printf("\n\nUser %s is trying to make a turn!\n", users[userID].name); fflush(stdout); int result_val; if (userGame[userID] == -1) { printf("The game of %s is over, he will be disconnected", users[userID].name); fflush(stdout); result_val = 0; } else { int gameID = userGame[userID]; Game game = games[gameID]; printf("The id of the game is: %d\n", gameID); printf("UserID: %d\n", userID); printf("WhiteID: %d\n", game.white_user.userID); printf("BlackID: %d\n", game.black_user.userID); if (game.current == white) printf("Current turn is whites'\n"); else printf("Current turn is blacks'\n"); if ((game.current == white && game.white_user.userID != userID) || (game.current == black && game.black_user.userID != userID)) { printf("Not time for turn!\n"); result_val = 1; } else result_val = 2; } MessageType mes = composeMessage(result, sizeof(int), &result_val); sendMessage(incomeSd, &mes); if (result_val == 2) { getMessage(incomeSd, &mes); UserTurn(&mes, incomeSd, userID); } clearMessage(&mes); return 1; }
void DoGame(game_state_t * game) { //white always start color_t current_player = COLOR_WHITE; //print board first time PrintBoard(game); //play until somebody won while (!GameWinning(game,COLOR_BLACK) && !GameWinning(game,COLOR_WHITE)) { if (Settings_UserColor_Get()==current_player) { UserTurn (game); } else { CPUTurn(game); } //print board after turn PrintBoard(game); //switch player current_player = (current_player==COLOR_WHITE) ? COLOR_BLACK : COLOR_WHITE; } //print winning side if (GameWinning(game,COLOR_BLACK)) { printf("black player wins!\n"); } else { printf("white player wins!\n"); } }