//tries to insert a hash at a certain point in hash table. If full, checks if duplicate, if not, puts in next available chain position //returns a 1 if duplicate found, 0 if not. Returns 3 if there is an error int insert_hash (hash *hash_array, Node *current, int key, int new_board[BOARD_HEIGHT][BOARD_WIDTH]){ hash *chain = (hash_array + key); if (chain->board_ref == NULL){ chain->board_ref = current; printf("no collision\n"); return 0; } else if (duplicate_board (chain->board_ref->board, new_board)){ printf("no collision\n"); return 1; } else while (chain != NULL){ printf("collision\n"); if (chain->next == NULL){ chain->next = allocate_new_chain (current); return 0; } if (duplicate_board (chain->board_ref->board, new_board)){ return 1; } chain = chain->next; } return 3; }
void clear_illegal_moves(char board[BOARD_SIZE][BOARD_SIZE], COLOR player){ Move *prev_move = moves_head, *curr_move = moves_head; char tmp_board[BOARD_SIZE][BOARD_SIZE]; while (curr_move != NULL){ duplicate_board(board, tmp_board); exc_move(tmp_board, curr_move, player); if (is_check(tmp_board, player) != 0){ if (curr_move == moves_head){ moves_head = curr_move->next; free(curr_move); curr_move = moves_head, prev_move = moves_head; } else{ prev_move->next = curr_move->next; free(curr_move); curr_move = prev_move->next; } } else { prev_move = curr_move; curr_move = curr_move->next; } } }
// minimax recursive func, using alpha-beta pruning int alpha_beta_minimax(char board[BOARD_SIZE][BOARD_SIZE], COLOR player, int depth, int alpha, int beta){ Move* move_list = get_all_moves(board, player); Move* curr_move = move_list; if (depth == 0 && curr_move == NULL){ best_move = NULL; return -500; } if (curr_move == NULL && player != curr_player){ if (!is_check(board, !curr_player)) return -450; else return 500; } if (curr_move == NULL && player == curr_player){ if (!is_check(board, curr_player)) return 450; else return -500; } if (depth == minimax_depth || curr_move == NULL){ clear_old_moves(move_list); return calc_score(board, curr_player); } if (depth == 0){ best_move = curr_move; if (curr_move->next == NULL){ best_move->score = 500; return 500; } } char init_board[BOARD_SIZE][BOARD_SIZE]; duplicate_board(board, init_board); // MAX if (depth % 2 == 0){ while (curr_move != NULL){ exc_move(board, curr_move, player); curr_move->score = alpha_beta_minimax(board, (player == 0), depth + 1, alpha, beta); if (curr_move->score > alpha){ alpha = curr_move->score; if (depth == 0) best_move = curr_move; } if (alpha >= beta){ if (depth != 0) clear_old_moves(move_list); else moves_head = move_list; duplicate_board(init_board, board); return alpha; } curr_move = curr_move->next; duplicate_board(init_board, board); } if (depth != 0) clear_old_moves(move_list); else moves_head = move_list; return alpha; } // MIN else{ while (curr_move != NULL){ exc_move(board, curr_move, player); curr_move->score = alpha_beta_minimax(board, (player == 0), depth + 1, alpha, beta); if (curr_move->score < beta){ beta = curr_move->score; if (depth == 0) best_move = curr_move; } if (alpha >= beta){ if (depth != 0) clear_old_moves(move_list); else moves_head = move_list; duplicate_board(init_board, board); return beta; } curr_move = curr_move->next; duplicate_board(init_board, board); } if (depth != 0) clear_old_moves(move_list); else moves_head = move_list; return beta; } }