void global_setup( int use_random, int hash_bits ) { FILE *log_file; time_t timer; /* Clear the log file. No error handling done. */ #if defined(ANDROID) sprintf(log_file_path, "%s/%s", android_files_dir, LOG_FILE_NAME); #elif defined(__linux__) strcpy( log_file_path, LOG_FILE_NAME ); #else char directory_buffer[MAX_PATH_LENGTH]; getcwd( directory_buffer, MAX_PATH_LENGTH ); if ( directory_buffer[strlen( directory_buffer ) - 1] == '\\' ) sprintf( log_file_path, "%s%s", directory_buffer, LOG_FILE_NAME ); else sprintf( log_file_path, "%s\\%s", directory_buffer, LOG_FILE_NAME ); #endif if ( use_log_file ) { log_file = fopen( log_file_path, "w" ); if ( log_file != NULL ) { time( &timer ); fprintf( log_file, "%s %s\n", LOG_TEXT, ctime( &timer ) ); fprintf( log_file, "%s %s %s\n", ENGINE_TEXT, __DATE__, __TIME__ ); fclose( log_file ); } } if ( use_random ) { time( &timer ); my_srandom( timer ); } else my_srandom( 1 ); init_hash( hash_bits ); init_bitboard(); init_moves(); init_patterns(); init_coeffs(); init_timer(); init_probcut(); init_stable(); setup_search(); }
int compute_move( int side_to_move, int update_all, int my_time, int my_incr, int timed_depth, int book, int mid, int exact, int wld, int search_forced, EvaluationType *eval_info ) { FILE *log_file; EvaluationType book_eval_info, mid_eval_info, end_eval_info; char *eval_str; double midgame_diff; enum { INTERRUPTED_MOVE, BOOK_MOVE, MIDGAME_MOVE, ENDGAME_MOVE } move_type; int i; int curr_move, midgame_move; int empties; int midgame_depth, interrupted_depth, max_depth; int book_move_found; int endgame_reached; int offset; log_file = NULL; if ( use_log_file ) log_file = fopen( log_file_path, "a" ); if ( log_file ) display_board( log_file, board, side_to_move, FALSE, FALSE, FALSE ); /* Initialize various components of the move system */ piece_count[BLACKSQ][disks_played] = disc_count( BLACKSQ ); piece_count[WHITESQ][disks_played] = disc_count( WHITESQ ); init_moves(); generate_all( side_to_move ); determine_hash_values( side_to_move, board ); calculate_perturbation(); if ( log_file ) { fprintf( log_file, "%d %s: ", move_count[disks_played], MOVE_GEN_TEXT ); for ( i = 0; i < move_count[disks_played]; i++ ) fprintf( log_file, "%c%c ", TO_SQUARE( move_list[disks_played][i] ) ); fputs( "\n", log_file ); } if ( update_all ) { reset_counter( &evaluations ); reset_counter( &nodes ); } for ( i = 0; i < 100; i++ ) evals[disks_played][i] = 0; max_depth_reached = 1; empties = 60 - disks_played; reset_buffer_display(); determine_move_time( my_time, my_incr, disks_played + 4 ); if ( !get_ponder_move() ) clear_ponder_times(); remove_coeffs( disks_played ); /* No feasible moves? */ if ( move_count[disks_played] == 0 ) { *eval_info = create_eval_info( PASS_EVAL, UNSOLVED_POSITION, 0.0, 0.0, 0, FALSE ); set_current_eval( *eval_info ); if ( echo ) { eval_str = produce_eval_text( *eval_info, FALSE ); send_status( "--> " ); send_status( "%-8s ", eval_str ); display_status( stdout, FALSE ); free( eval_str ); } if ( log_file ) { fprintf( log_file, "%s: %s\n", BEST_MOVE_TEXT, PASS_TEXT ); fclose( log_file ); } last_time_used = 0.0; clear_pv(); return PASS; } /* If there is only one move available: Don't waste any time, unless told so or very close to the end, searching the position. */ if ( (empties > DISABLE_FORCED_MOVES) && (move_count[disks_played] == 1) && !search_forced ) { /* Forced move */ *eval_info = create_eval_info( FORCED_EVAL, UNSOLVED_POSITION, 0.0, 0.0, 0, FALSE ); set_current_eval( *eval_info ); if ( echo ) { eval_str = produce_eval_text( *eval_info, FALSE ); send_status( "--> " ); send_status( "%-8s ", eval_str ); free( eval_str ); send_status( "%c%c ", TO_SQUARE( move_list[disks_played][0] ) ); display_status( stdout, FALSE ); } if ( log_file ) { fprintf( log_file, "%s: %c%c (%s)\n", BEST_MOVE_TEXT, TO_SQUARE(move_list[disks_played][0]), FORCED_TEXT ); fclose( log_file ); } last_time_used = 0.0; return move_list[disks_played][0]; } /* Mark the search as interrupted until a successful search has been performed. */ move_type = INTERRUPTED_MOVE; interrupted_depth = 0; curr_move = move_list[disks_played][0]; /* Check the opening book for midgame moves */ book_move_found = FALSE; midgame_move = PASS; if ( forced_opening != NULL ) { /* Check if the position fits the currently forced opening */ curr_move = check_forced_opening( side_to_move, forced_opening ); if ( curr_move != PASS ) { book_eval_info = create_eval_info( UNDEFINED_EVAL, UNSOLVED_POSITION, 0, 0.0, 0, TRUE ); midgame_move = curr_move; book_move_found = TRUE; move_type = BOOK_MOVE; if ( echo ) { send_status( "--> Forced opening move " ); if ( get_ponder_move() ) send_status( "{%c%c} ", TO_SQUARE( get_ponder_move() ) ); send_status( "%c%c", TO_SQUARE( curr_move ) ); display_status( stdout, FALSE ); } clear_pv(); pv_depth[0] = 1; pv[0][0] = curr_move; } } if ( !book_move_found && play_thor_match_openings ) { /* Optionally use the Thor database as opening book. */ int threshold = 2; database_search( board, side_to_move ); if ( get_match_count() >= threshold ) { int game_index = (my_random() >> 8) % get_match_count(); curr_move = get_thor_game_move( game_index, disks_played ); if ( valid_move( curr_move, side_to_move ) ) { book_eval_info = create_eval_info( UNDEFINED_EVAL, UNSOLVED_POSITION, 0, 0.0, 0, TRUE ); midgame_move = curr_move; book_move_found = TRUE; move_type = BOOK_MOVE; if ( echo ) { send_status( "--> %s ", THOR_TEXT ); if ( get_ponder_move() ) send_status( "{%c%c} ", TO_SQUARE( get_ponder_move() ) ); send_status( "%c%c", TO_SQUARE( curr_move ) ); display_status( stdout, FALSE ); } clear_pv(); pv_depth[0] = 1; pv[0][0] = curr_move; } else fatal_error( "Thor book move %d is invalid!", curr_move ); }
int main () { state_t *start; state_t *end; state_t *s; int i; float metric; init_moves(); init_state_space(); /* Insert the Start node */ start = get_state(120,180,0,0); list_push_front(&pending, &start->node); start->dt = 0; start->color = 1; /* Insert the end node */ end = get_state(320,200,0,0); int now = -1; int num_visited = 0; int num_pending = 1; while (!list_is_empty(&pending)) { s = CONTAINER_OF(list_pop_front(&pending), state_t, node); num_visited++; num_pending--; if (s->dt > now-1) { now = s->dt + 1; printf("Next step: %d\n", now); printf("Visited: %d\n", num_visited); printf("Pending: %d\n", num_pending); } for (i=0; i<num_moves; i++) { move_t *m = &moves[i]; uint16_t x1 = s->x + s->vx + m->ax; uint16_t y1 = s->y + s->vy + m->ay; int16_t vx1 = s->vx + m->ax; int16_t vy1 = s->vy + m->ay; /* */ if (now < steps[x1][y1]) steps[x1][y1] = now; else if (now > steps[x1][y1] + 1) continue; /* */ state_t *d = get_state(x1,y1,vx1,vy1); if (d->color == 0) { /* Not yet reached state */ d->color = s->color; d->dt = now; d->metric = s->metric + delta_metric(s, d, m); d->from = *m; /* Add destination state into pending list */ list_push_back(&pending, &d->node); num_pending++; } else if (d == end) { /* Reached the end state. Clear the list. */ printf("Reached end node after %d steps.\n", now); list_init(&pending); } else if (d->color == s->color && d->dt == s->dt) { metric = s->metric + delta_metric(s,d,m); if (metric < d->metric) { /* Store better path */ d->metric = metric; d->from = *m; } } } } return 0; }