/** * @brief Move generator performance test function. * * @param board * @param depth * @param global_stats statistics */ static void count_game(const Board *board, const int depth, GameStatistics *global_stats) { GameStatistics stats = GAME_STATISTICS_INIT; unsigned long long moves; int x; Board next[1]; if (depth == 1) { moves = get_moves(board->player, board->opponent); stats.n_moves = stats.max_mobility = stats.min_mobility = bit_count(moves); if (moves == 0) { if (can_move(board->opponent, board->player)) { stats.n_passes = 1; } else { const int n_player = bit_count(board->player); const int n_opponent = bit_count(board->opponent); if (n_player > n_opponent) stats.n_wins = 1; else if (n_player == n_opponent) stats.n_draws = 1; else stats.n_losses = 1; } } } else { moves = get_moves(board->player, board->opponent); if (moves) { foreach_bit (x, moves) { board_next(board, x, next); count_game(next, depth - 1, &stats); } } else {
int main(int argc, char **argv) { if (argc < 3) { printf("Usage: ./gol <infile> <print>\n"); return 0; } FILE *f = fopen(argv[1], "r"); int to_print = strtol(argv[2], NULL, 10); // Check if we opened the file if (f != NULL) { int num_rows = 0; int num_cols = 0; int num_iterations = 0; int num_pairs = 0; double total_time = 0.0; // read numbers fscanf(f, "%d\n%d\n%d\n%d", &num_rows, &num_cols, &num_iterations, &num_pairs); // set up board board_t *board = init_board(f, num_rows, num_cols, num_pairs); // set up timing struct timeval start_time; struct timeval end_time; gettimeofday(&start_time, NULL); // run iterations int i = num_iterations; while (board_next(board, &i)) { if (to_print) { print_board(board, num_iterations - i); usleep(200000); clear(num_rows + 4); } i--; } // get end time gettimeofday(&end_time, NULL); total_time = ((end_time.tv_sec + (end_time.tv_usec/1000000.0)) - (start_time.tv_sec + (start_time.tv_usec/1000000.0))); // print final board print_board(board, num_iterations); printf("total time for %d iteration%s of %dx%d world is %f sec\n", num_iterations, (num_iterations != 1) ? "s" : "", num_rows, num_cols, total_time); fclose(f); board_free(board); } else { printf("There was an error opening '%s'\n", argv[1]); return 1; } return 0; }