board change_board_size(board b, int diff_x, int diff_y) { // takes a board, and some x,y values which alter the size // of said board, growing/shrinking the boards dimentions // according to them // NOTE: this breaks when the board grows as cloning // a board to a size larger than itself is undefined behavior b->width = b->width + diff_x; b->height = b->height + diff_y; return clone_board(b); }
board iterate_board(board b) { // because the whole board needs to tick // at the same time as a result of eachother, // we can't modify the cells in place // NOTE: actually we could as long as we // only modify the alive state and not the // neighbors state board newboard = clone_board(b); int i,j; for(i=0; i<b->height; i++) { for(j=0; j<b->width; j++) { newboard->cells[i][j] = iterate_cell(b->cells[i][j]); } } return newboard; }
int move(board_t* board, symbol_t symbol, int depth, int alpha, int beta) { int n, i; move_t* max_move; int score = get_score(board, depth, symbol); if(score != 0) { return score; } move_t** moves = get_all_possible_moves(board, symbol, &n); if(depth == 0) { int max_score = -9999; board_t* b; #pragma omp parallel for private(i, score, b) shared(board, alpha, beta, moves, depth, symbol, max_score) schedule(guided, CHUNK_SIZE) for(i = 0; i < n; i++) { b = clone_board(board); put_symbol(b, symbol, moves[i]); score = -move(b, other_symbol(symbol), depth + 1, -beta, -max_score); #pragma omp critical { if(score > max_score) { max_score = score; max_move = moves[i]; } } free(b); } alpha = max_score; } else { for(i = 0; i < n; i++) { put_symbol(board, symbol, moves[i]); score = -move(board, other_symbol(symbol), depth + 1, -beta, -alpha); clear_symbol(board, moves[i]); if(score > alpha) { alpha = score; max_move = moves[i]; } if(alpha >= beta) { if(depth == 0) { printf("%i %i %i\n", i, beta, alpha); } break; } } } if(depth == 0) { put_symbol(board, symbol, max_move); } for(i = 0; i < n; i++) { free(moves[i]); } free(moves); return alpha; }