int ai_play_best_move(ai* self, char** sq) { if (self->moves_len == 0) { *sq = "PA"; return ai_play(self, -1, -1); } future_move* best_move = NULL; int i, max_weight = (self->moves)[0]->weight; best_move = (self->moves)[0]; for (i=1; i<(self->moves_len); i++) { if ((self->moves)[i]->weight > max_weight) { max_weight = (self->moves)[i]->weight; best_move = (self->moves)[i]; } } (*sq)[0] = FIRST_LETTER + (best_move->col); (*sq)[1] = FIRST_DIGIT + (best_move->row); (*sq)[2] = '\n'; return ai_play(self, best_move->col, best_move->row); }
/* * This handles the game for each client. */ void *game_runner(void *client_data_pointer) { /* Get client and details */ client_nfo client = *(client_nfo*)client_data_pointer; int socket_id = client.socket_id; int status = 1, client_status = 1, move = 0, ai_move = 0, read_size; uint32_t move_nbo, ai_move_nbo; c4_t board; /* To ensure memory is freed at the end */ pthread_detach(pthread_self()); /* Set-up the game */ srand(time(NULL)); init_empty(board, NO_PRINT); /* log that client is connected */ append_log(STATUS_CONNECTED, 0, &client, STATUS_CONNECTED, 0); /* Receive move from client */ while((read_size = recv(socket_id, &move_nbo, sizeof(uint32_t), 0)) > 0) { if(status < 0) break; /* Get move from (client move in network byte order)*/ move = ntohl(move_nbo); /* Make client move */ status = human_play(board, move, NO_PRINT); client_status = status; /* needed for log */ /* Make AI move if game is not over */ if(status > 0) { ai_move = suggest_move(board, RED); status = ai_play(board, ai_move, NO_PRINT); } /* log the moves */ append_log(status, ai_move, &client, client_status, move); /* If game is over, the old ai_move will be sent to client again but game will be terminated just after the client move is made in client program, so the ai move won't be made */ /* Send the AI move to client */ ai_move_nbo = htonl(ai_move); if(send(socket_id, &ai_move_nbo, sizeof(uint32_t), 0) < 0) { perror("send error"); break; } } if(read_size == 0) { /* Client has been disconnected from server */ if (status > 0) status = STATUS_ABNORMAL; } else if(read_size == -1) { perror("recv error"); if (status > 0) status = STATUS_ABNORMAL; } /* log any abnormal end of game */ if(status == STATUS_ABNORMAL) append_log(STATUS_ABNORMAL, 0, &client, STATUS_ABNORMAL, 0); free(client_data_pointer); return 0; }
int game(int type){ int game_over = 0; { surface *background = new_image("background.png"); apply_texture(background, window); destroy_surface(background); } initialize_cameras();initialize_star();initialize_ships(2);initialize_dust(); initialize_shot();initialize_ai(); play_music("music.ogg"); draw_tank(0, (window_width / 2 - 421), window_height / 2 - 150); draw_tank(1, window_width / 2 + 401, window_height / 2 - 150); // Main loop for(;;){ int i; get_input(); if(keyboard[ESC] || game_over){ break; } for(i = 0; i < 2; i ++) if(ship[i].status == DEAD){ struct timeval now; gettimeofday(&now, NULL); if((int) (now.tv_sec - ship[i].time.tv_sec) > 2) game_over = 1; } erase_ships(); erase_shot(); if(type != CPU_X_CPU){ // Rotation: 10 Propulse: 20 Fire: 20 Hyper: 180 int my_ship; // Player 1 moves if(type == CPU_X_PLR){ ai_play(0); my_ship = 1; } else if(type == PLR_X_CPU){ my_ship = 0; ai_play(1); } else my_ship = 0; if(keyboard[LEFT]) rotate_ship(my_ship, LEFT); if(keyboard[RIGHT]) rotate_ship(my_ship, RIGHT); if(keyboard[UP]) propulse_ship(my_ship); if(keyboard[DOWN]) ship_fire(my_ship); if(keyboard[RIGHT_CTRL]) goto_hyperspace(my_ship); } else{ ai_play(0); ai_play(1); } if(type == PLR_X_PLR){ // Player 2 moves if(keyboard[A]) rotate_ship(1, LEFT); if(keyboard[D]) rotate_ship(1, RIGHT); if(keyboard[W]) propulse_ship(1); if(keyboard[L]) blow_up(1); if(keyboard[S]) ship_fire(1); if(keyboard[LEFT_CTRL]) goto_hyperspace(1); } update_star(); // 6 update_ships(); // 2 update_dust(); // 145 update_shot(); // 16 film_ships(); // Até 16 film_dust(); // 20 film_shot(); // Até 20 weaver_rest(10000000); } stop_music(); destroy_star(); destroy_ships(); destroy_cameras(); destroy_dust(); destroy_shot(); clean_keyboard(); return 0; }