int check_SDL_events() { SDL_Event event; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) return 1; else if(event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE) return 1; else if(event.key.keysym.sym == SDLK_RETURN) { if(is_paused()) { if(!is_started()) { set_started(1); } else if(is_game_over()) { set_game_over(0); ent_table_shutdown(); ent_table_init(); create_entities(); } } toggle_paused(); } } } return 0; }
void play_game(const GameState* state) { while (!is_game_over(state)) { print_board(state); play_turn(state); } }
void play_turn(const GameState* state) { int winner = is_game_over(state); if (winner) { // player won ai_game_over(); } else { // roll dice ai_chance(state, sizeof(GameState), die_rolled, 0, 6); } }
void play_turn(const GameState* state) { int winner = is_game_over(state); if (winner) { // player won DEBUG("Player %d won\n", winner-1); ai_game_over(); } else { // roll dice (2d6 -> 0-35) ai_chance(state, 0, roll_dice, 0, 36); } }
int main(int argc, char **argv) { SDL_Surface *screen, *background, *pause_text, *press_enter_text, *game_over_text; const SDL_VideoInfo *video_info; Uint32 frame_start, frame_end = 0, game_frame_end = 0; systems_init(); screen = SDL_SetVideoMode(800, 600, 0, SDL_HWSURFACE|SDL_DOUBLEBUF); video_info = SDL_GetVideoInfo(); create_images(&press_enter_text, &pause_text, &game_over_text, &background); create_entities(); for(;;) { start_frame(&frame_start, &frame_end); if(check_SDL_events()) break; draw_background(background, screen, video_info); update_entities(game_frame_end ); if(is_paused()) { if(is_game_over()) { draw_centered(screen, video_info, game_over_text); } else if(is_started()) { draw_centered(screen, video_info, pause_text); } else { draw_centered(screen, video_info, press_enter_text); } } SDL_Flip(screen); finish_frame(&frame_start, &frame_end, &game_frame_end); } SDL_FreeSurface(background); SDL_FreeSurface(pause_text); SDL_FreeSurface(press_enter_text); systems_shutdown(); return 0; }
/* Make a move, update the log, display board, etc. */ void update_game(Chess *chess, U32 move) { Board *board; ASSERT(1, chess != NULL); ASSERT(1, chess->game_over != true); board = &chess->board; make_move(board, move); if (chess->protocol == PROTO_NONE) print_board(board); if (is_game_over(board)) chess->game_over = true; }
void play_game(const GameState* state) { while (!is_game_over(state)) { print_board(state); /* // AI solve for this player? if (ai_set_mode_search()) { play_turn(state); ai_print_stats(); } // commit the best move, or prompt human for move if (!ai_set_mode_play()) break; */ play_turn(state); } }
/* Requirement 3 - controls the flow of play in the game */ void play_game(void) { do { enum move_result move_result = SUCCESSFUL_MOVE; enum cell_contents board[BOARD_HEIGHT][BOARD_WIDTH]; init_board(board); /* game loop */ while (!is_game_over(board) && move_result != QUIT_GAME) move_result = player_move(board); /* if result is QUIT_GAME, break prematurely */ if (move_result == QUIT_GAME) break; display_result(board); } while (process_ask_play_again()); }
void make_guess(unsigned int *xGuess, unsigned int *yGuess, Grid *board, int **answer) { if(*xGuess >= board->width || *yGuess >= board->height) { printf("Bad guess\n"); } else if(answer[*xGuess][*yGuess] == 1 || answer[*xGuess][*yGuess] == -1) { printf("Miss\n"); answer[*xGuess][*yGuess] = -1; } else { printf("Hit\n"); if(answer[*xGuess][*yGuess] > 0) { answer[*xGuess][*yGuess] = -answer[*xGuess][*yGuess]; if(is_sunk(-answer[*xGuess][*yGuess], board, answer)) { printf("Ship sunk\n"); } } if(is_game_over(board, answer)) { printf("Game over\n"); exit(0); } } }
// Evaluate the move by performing a search. void evaluateMove(searchNode *node, move_t mv, move_t killer_a, move_t killer_b, searchType_t type, uint64_t *node_count_serial, moveEvaluationResult* result) { int ext = 0; // extensions bool blunder = false; // shoot our own piece result->next_node.subpv[0] = 0; result->next_node.parent = node; // Make the move, and get any victim pieces. bool isko = make_move(&(node->position), &(result->next_node.position), mv); // Check whether this move changes the board state. // such moves are not legal. if (isko) { result->type = MOVE_ILLEGAL; return; } victims_t* victims = &result->next_node.position.victims; // Check whether the game is over. if (is_game_over(victims, node->pov, node->ply)) { // Compute the end-game score. result->type = MOVE_GAMEOVER; result->score = get_game_over_score(victims, node->pov, node->ply); return; } // Ignore noncapture moves when in quiescence. if (zero_victims(victims) && node->quiescence) { result->type = MOVE_IGNORE; return; } // Check whether the board state has been repeated, this results in a draw. if (is_repeated(&(result->next_node.position), node->ply)) { result->type = MOVE_GAMEOVER; result->score = get_draw_score(&(result->next_node.position), node->ply); return; } tbassert(victims->stomped == 0 || color_of(victims->stomped) != node->fake_color_to_move, "stomped = %d, color = %d, fake_color_to_move = %d\n", victims->stomped, color_of(victims->stomped), node->fake_color_to_move); // Check whether we caused our own piece to be zapped. This isn't considered // a blunder if we also managed to stomp an enemy piece in the process. if (victims->stomped == 0 && victims->zapped > 0 && color_of(victims->zapped) == node->fake_color_to_move) { blunder = true; } // Do not consider moves that are blunders while in quiescence. if (node->quiescence && blunder) { result->type = MOVE_IGNORE; return; } // Extend the search-depth by 1 if we captured a piece, since that means the // move was interesting. if (victim_exists(victims) && !blunder) { ext = 1; } // Late move reductions - or LMR. Only done in scout search. int next_reduction = 0; if (type == SEARCH_SCOUT && node->legal_move_count + 1 >= LMR_R1 && node->depth > 2 && zero_victims(victims) && mv != killer_a && mv != killer_b) { if (node->legal_move_count + 1 >= LMR_R2) { next_reduction = 2; } else { next_reduction = 1; } } result->type = MOVE_EVALUATED; int search_depth = ext + node->depth - 1; // Check if we need to perform a reduced-depth search. // // After a reduced-depth search, a full-depth search will be performed if the // reduced-depth search did not trigger a cut-off. if (next_reduction > 0) { search_depth -= next_reduction; int reduced_depth_score = -scout_search(&(result->next_node), search_depth, node_count_serial); if (reduced_depth_score < node->beta) { result->score = reduced_depth_score; return; } search_depth += next_reduction; } // Check if we should abort due to time control. if (abortf) { result->score = 0; result->type = MOVE_IGNORE; return; } if (type == SEARCH_SCOUT) { result->score = -scout_search(&(result->next_node), search_depth, node_count_serial); } else { if (node->legal_move_count == 0 || node->quiescence) { result->score = -searchPV(&(result->next_node), search_depth, node_count_serial); } else { result->score = -scout_search(&(result->next_node), search_depth, node_count_serial); if (result->score > node->alpha) { result->score = -searchPV(&(result->next_node), node->depth + ext - 1, node_count_serial); } } } }
void server_main(int argc, char* argv[]) { /////////////////////////////////////////////////////////////// //set up global data structures Syncronizer<Env::K> syncronizer; std::cout <<"before env"<<std::endl; Env env(argc,argv); std::cout <<"after env"<<std::endl; Time_frame::Interval_color player_a_color = PLAYER_A_COLOR; Time_frame::Interval_color player_b_color = PLAYER_B_COLOR; Time_frame time_frame( player_a_color, //player a is red get_colored_sleep_time()); File_names file_names(&time_frame); Targets_manager target_configurations_manager(env.get_target_configurations(), env.get_additional_target_configurations()); Additional_target_configurations_manager additional_target_configurations_manager(5,&time_frame); //add additional targets at fifth interval Robot_location robot_location_a(env.get_source_configuration_a()); Robot_location robot_location_b(env.get_source_configuration_b()); Writen_paths_filenames writen_paths_filenames_a; Writen_paths_filenames writen_paths_filenames_b; Safe_bool is_game_over(false); Result result; /////////////////////////////////////////////////////////////// //set up sockets Socket* socket_g= set_up_connection__gui(robot_location_a, robot_location_b, &target_configurations_manager); #if USE_PLAYER_A Socket* socket_a= set_up_connection__player_a(); #endif //USE_PLAYER_A #if USE_PLAYER_B Socket* socket_b= set_up_connection__player_b(); #endif //USE_PLAYER_B /////////////////////////////////////////////////////////////// //set up request handlers #if USE_PLAYER_A Client_request_handler request_handler_a; request_handler_a.start(socket_a, player_a_color, &time_frame, &file_names, &additional_target_configurations_manager, &writen_paths_filenames_a, &robot_location_b, &is_game_over); #endif //USE_PLAYER_A #if USE_PLAYER_B Client_request_handler request_handler_b; request_handler_b.start(socket_b, player_b_color, &time_frame, &file_names, &additional_target_configurations_manager, &writen_paths_filenames_b, &robot_location_a, &is_game_over); #endif //USE_PLAYER_B /////////////////////////////////////////////////////////////// //start up syncronizer std::cout <<"in main thread : " <<boost::this_thread::get_id()<<std::endl; std::cout <<"before synchronizer"<<std::endl<<std::endl; syncronizer.start(&time_frame, &env, &result, socket_g, &target_configurations_manager, &additional_target_configurations_manager, &robot_location_a, &robot_location_b, &writen_paths_filenames_a, &writen_paths_filenames_b, &is_game_over); syncronizer.join(); std::cout <<"player a score: " <<result.first <<"player b score: " <<result.second <<std::endl; #if USE_PLAYER_A request_handler_a.join(); #endif //USE_PLAYER_A #if USE_PLAYER_B request_handler_b.join(); #endif //USE_PLAYER_B end_connection__gui(socket_g); return ; }
int main() { //shell vars bool render = false; //allegro vars ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; //allegro init functions printf ("Initializing allegro\n"); if (!al_init()) { al_show_native_message_box(NULL, NULL, NULL, "failed", NULL, 0); return -1; } printf("Creating display\n"); display = al_create_display(WIDTH, HEIGHT); if (!display) { al_show_native_message_box(NULL, NULL, NULL, "failed", NULL, 0); return -1; } printf("Installing addons\n"); al_init_font_addon(); al_init_ttf_addon(); al_init_primitives_addon(); al_init_image_addon(); al_install_keyboard(); al_install_mouse(); al_install_audio(); al_init_acodec_addon(); al_reserve_samples(10); //project inits srand(time(NULL)); printf("Initializing timer\n"); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); printf("Registering event sources\n"); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_start_timer(timer); printf("Init mouse and keyboard\n"); init_keyboard(); init_mouse(); printf("Loading assets\n"); load_bitmaps(); load_fonts(); load_samples(); printf ("Creating manager\n"); push_state(new TitleMenu()); printf("Beginning game\n"); while (!is_game_over()) { //declare an event ALLEGRO_EVENT event; //monitor event sources al_wait_for_event(event_queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { end_game(); } else if (event.type == ALLEGRO_EVENT_TIMER) { render = true; update_mouse(); update_keyboard(); handle_key(); update_game(); } // Render screen if (render && al_event_queue_is_empty(event_queue)) { render = false; render_game(); al_flip_display(); } } unload_assets(); al_destroy_event_queue(event_queue); al_destroy_display(display); al_destroy_timer(timer); return 0; }
bool Reversi::make_move(string move){ //check size of move //cout << "Making move!\n"; //cout << "size of move:" << sizeof(move) << endl; if(move.size() != 2) return false; //check if user has chosen side //cout << "Move sized correctly!\n"; //cout << "current player:" << current_player << endl; if(current_player == 'n') return false; //split move into correct data types //cout << "Player has been set!\n"; //cout << "Move string: " << move << endl; Position current_move; if(isalpha(move[0])) { current_move.column = get_number_of_letter(move[0]); current_move.row = move[1]-'0'-1; } else{ current_move.row = get_number_of_letter(move[0]); current_move.column = move[1]-'0'-1; } //cout << "row: " << current_move.row << endl; //cout << "column: " << current_move.column << endl; // check if valid move bool possible_move_check = false; for(unsigned int i=0; i< available_moves.size(); i++) if(available_moves[i].row == current_move.row && available_moves[i].column == current_move.column) possible_move_check = true; if(!possible_move_check) return false; //cout << "Move is valid!\n"; // save previous state // only need to support 10 undos (20 total saved states) if(previous_states.size() >= 20) previous_states.pop_back(); previous_states.push_front({board, available_moves, white_score, black_score, current_player}); previous_move = move; //check all directions //if valid in a direction flip all appropriate tiles vector<Position> all_positions; vector<Position> temp_positions; all_positions.push_back(current_move); int x_step = 0; int y_step = -1; temp_positions = get_tiles(current_move, x_step, y_step); //check above for(unsigned int i=0; i<temp_positions.size(); i++) all_positions.push_back(temp_positions[i]); y_step = 1; temp_positions = get_tiles(current_move, x_step, y_step); //check below for(unsigned int i=0; i<temp_positions.size(); i++) all_positions.push_back(temp_positions[i]); y_step = 0; x_step = 1; temp_positions = get_tiles(current_move, x_step, y_step); //check right for(unsigned int i=0; i<temp_positions.size(); i++) all_positions.push_back(temp_positions[i]); x_step = -1; temp_positions = get_tiles(current_move, x_step, y_step); //check left for(unsigned int i=0; i<temp_positions.size(); i++) all_positions.push_back(temp_positions[i]); y_step = -1; temp_positions = get_tiles(current_move, x_step, y_step); //check top left for(unsigned int i=0; i<temp_positions.size(); i++) all_positions.push_back(temp_positions[i]); x_step = 1; temp_positions = get_tiles(current_move, x_step, y_step); //check top right for(unsigned int i=0; i<temp_positions.size(); i++) all_positions.push_back(temp_positions[i]); y_step = 1; temp_positions = get_tiles(current_move, x_step, y_step); //check bottom right for(unsigned int i=0; i<temp_positions.size(); i++) all_positions.push_back(temp_positions[i]); x_step = -1; temp_positions = get_tiles(current_move, x_step, y_step); //check bottom left for(unsigned int i=0; i<temp_positions.size(); i++) all_positions.push_back(temp_positions[i]); for(unsigned int i=0; i<all_positions.size(); i++) board[all_positions[i].row][all_positions[i].column] = current_player; update_score(); toggle_player(); available_moves = get_available_moves(); if(available_moves.size() == 0 && !(is_game_over())) { toggle_player(); available_moves = get_available_moves(); } }
bool Start_Menu_Splash::Run() { Start(); //start the splash loop return is_game_over(); //return whether the game should quit or not }
score_t searchRoot(position_t *p, score_t alpha, score_t beta, int depth, int ply, move_t *pv, uint64_t *node_count_serial, FILE *OUT) { static int num_of_moves = 0; // number of moves in list // hopefully, more than we will need static sortable_move_t move_list[MAX_NUM_MOVES]; if (depth == 1) { // we are at depth 1; generate all possible moves num_of_moves = generate_all_opt(p, move_list, false); // shuffle the list of moves for (int i = 0; i < num_of_moves; i++) { int r = myrand() % num_of_moves; sortable_move_t tmp = move_list[i]; move_list[i] = move_list[r]; move_list[r] = tmp; } } searchNode rootNode; rootNode.parent = NULL; initialize_root_node(&rootNode, alpha, beta, depth, ply, p); assert(rootNode.best_score == alpha); // initial conditions searchNode next_node; next_node.subpv[0] = 0; next_node.parent = &rootNode; score_t score; for (int mv_index = 0; mv_index < num_of_moves; mv_index++) { move_t mv = get_move(move_list[mv_index]); if (TRACE_MOVES) { print_move_info(mv, ply); } (*node_count_serial)++; // make the move. victims_t x = make_move(&(rootNode.position), &(next_node.position), mv); if (is_KO(x)) { continue; // not a legal move } if (is_game_over(x, rootNode.pov, rootNode.ply)) { score = get_game_over_score(x, rootNode.pov, rootNode.ply); next_node.subpv[0] = 0; goto scored; } if (is_repeated(&(next_node.position), rootNode.ply)) { score = get_draw_score(&(next_node.position), rootNode.ply); next_node.subpv[0] = 0; goto scored; } if (mv_index == 0 || rootNode.depth == 1) { // We guess that the first move is the principle variation score = -searchPV(&next_node, rootNode.depth-1, node_count_serial); // Check if we should abort due to time control. if (abortf) { return 0; } } else { score = -scout_search(&next_node, rootNode.depth-1, node_count_serial); // Check if we should abort due to time control. if (abortf) { return 0; } // If its score exceeds the current best score, if (score > rootNode.alpha) { score = -searchPV(&next_node, rootNode.depth-1, node_count_serial); // Check if we should abort due to time control. if (abortf) { return 0; } } } scored: // only valid for the root node: tbassert((score > rootNode.best_score) == (score > rootNode.alpha), "score = %d, best = %d, alpha = %d\n", score, rootNode.best_score, rootNode.alpha); if (score > rootNode.best_score) { tbassert(score > rootNode.alpha, "score: %d, alpha: %d\n", score, rootNode.alpha); rootNode.best_score = score; pv[0] = mv; memcpy(pv+1, next_node.subpv, sizeof(move_t) * (MAX_PLY_IN_SEARCH - 1)); pv[MAX_PLY_IN_SEARCH - 1] = 0; // Print out based on UCI (universal chess interface) double et = elapsed_time(); char pvbuf[MAX_PLY_IN_SEARCH * MAX_CHARS_IN_MOVE]; getPV(pv, pvbuf, MAX_PLY_IN_SEARCH * MAX_CHARS_IN_MOVE); if (et < 0.00001) { et = 0.00001; // hack so that we don't divide by 0 } uint64_t nps = 1000 * *node_count_serial / et; fprintf(OUT, "info depth %d move_no %d time (microsec) %d nodes %" PRIu64 " nps %" PRIu64 "\n", depth, mv_index + 1, (int) (et * 1000), *node_count_serial, nps); fprintf(OUT, "info score cp %d pv %s\n", score, pvbuf); // Slide this move to the front of the move list for (int j = mv_index; j > 0; j--) { move_list[j] = move_list[j - 1]; } move_list[0] = mv; } // Normal alpha-beta logic: if the current score is better than what the // maximizer has been able to get so far, take that new value. Likewise, // score >= beta is the beta cutoff condition if (score > rootNode.alpha) { rootNode.alpha = score; } if (score >= rootNode.beta) { tbassert(0, "score: %d, beta: %d\n", score, rootNode.beta); break; } } return rootNode.best_score; }