bool DiagonalPosCheck(int row, int col) { int botX = col - RANGE >= 0 ? RANGE : col; int botY = row + RANGE <= MAX_INDEX ? RANGE : MAX_INDEX - row; int topX = col + RANGE <= MAX_INDEX ? RANGE : MAX_INDEX - col; int topY = row - RANGE >= 0 ? RANGE : row; int pos = Mathf.Min(botX, botY); int neg = Mathf.Min(topX, topY); ESlotStatus status = current_turn == Turn.Player ? ESlotStatus.Player : ESlotStatus.AI; int points = 0; int max = 0; for (int i = 0; i <= pos + neg; i++) { if (board[row + pos - i, col - pos + i] == status) { points++; max = Mathf.Max(max, points); } else { max = Mathf.Max(max, points); points = 0; } } return(max >= 5); }
bool VerticalCheck(int row, int col) { int begin = row - RANGE > 0 ? row - RANGE : 0; int end = row + RANGE <= MAX_INDEX ? row + RANGE : MAX_INDEX; int points = 0; int max = 0; ESlotStatus status = current_turn == Turn.Player ? ESlotStatus.Player : ESlotStatus.AI; for (int i = begin; i <= end; i++) { if (board[i, col] == status) { points++; max = Mathf.Max(max, points); } else { max = Mathf.Max(max, points); points = 0; } } return(max >= 5); }
ESlotStatus Enemy(ESlotStatus status) { if (status == ESlotStatus.Player) { return(ESlotStatus.AI); } else { return(ESlotStatus.Player); } }
DangerEvaluation HorizontalDanger(int row, int col, DangerEvaluation evaluation, ESlotStatus status) { int t_min = col - SEARCH_RANGE; int t_max = col + SEARCH_RANGE; int min = ValidIndex(t_min) ? t_min : 0; int max = ValidIndex(t_max) ? t_max : MAX_INDEX; // Evaluate the left side of the piece int begin = col - 1; int left_danger = 0; bool at_corner = begin < 0; if (!at_corner) { for (int i = begin; i >= min; i--) { if (playing_board.board[row, i] == status) { left_danger++; } if (playing_board.board[row, i] == Enemy(status)) { break; } } } // Evaluate the right side of the piece int r_begin = col + 1; int right_danger = 0; at_corner = r_begin > MAX_INDEX; if (!at_corner) { for (int i = r_begin; i <= max; i++) { if (playing_board.board[row, i] == status) { right_danger++; } if (playing_board.board[row, i] == Enemy(status)) { break; } } } evaluation.total_horizontal_danger = (left_danger + right_danger + 1); evaluation.left_danger = left_danger; evaluation.right_danger = right_danger; return(evaluation); }
DangerEvaluation VerticalDanger(int row, int col, DangerEvaluation evaluation, ESlotStatus status) { int t_min = row - SEARCH_RANGE; int t_max = row + SEARCH_RANGE; int min = ValidIndex(t_min) ? t_min : 0; int max = ValidIndex(t_max) ? t_max : MAX_INDEX; // Evaluate the left side of the piece int top = row - 1; int top_danger = 0; bool at_corner = top < 0; if (!at_corner) { for (int i = top; i >= min; i--) { if (playing_board.board[i, col] == status) { top_danger++; } if (playing_board.board[i, col] == Enemy(status)) { break; } } } // Evaluate the right side of the piece int bot = row + 1; int down_danger = 0; at_corner = bot > MAX_INDEX; if (!at_corner) { for (int i = bot; i <= max; i++) { if (playing_board.board[i, col] == status) { down_danger++; } if (playing_board.board[i, col] == Enemy(status)) { break; } } } evaluation.total_vertical_danger = (top_danger + down_danger + 1); evaluation.up_danger = top_danger; evaluation.down_danger = down_danger; return(evaluation); }
DangerEvaluation DiagonalPositiveDanger(int row, int col, DangerEvaluation evaluation, ESlotStatus status) { // Left max int y_min = row + SEARCH_RANGE; int x_min = col - SEARCH_RANGE; int l_y_min = ValidIndex(y_min) ? SEARCH_RANGE : MAX_INDEX - row; int l_x_min = ValidIndex(x_min) ? SEARCH_RANGE : col; int left_range = Mathf.Min(l_y_min, l_x_min); // Do the left side bool at_corner = row == MAX_INDEX || col == 0; int top_danger = 0; if (!at_corner) { int new_row = 0; int new_col = 0; for (int i = 0; i < left_range; i++) { new_row = row + 1 + i; new_col = col - 1 - i; if (playing_board.board[new_row, new_col] == status) { top_danger++; } if (playing_board.board[new_row, new_col] == Enemy(status)) { break; } } } int y_max = row - SEARCH_RANGE; int x_max = col + SEARCH_RANGE; int l_y_max = ValidIndex(y_max) ? SEARCH_RANGE : row; int l_x_max = ValidIndex(x_max) ? SEARCH_RANGE : MAX_INDEX - col; int rigth_range = Mathf.Min(l_y_max, l_x_max); at_corner = row == 0 || col == MAX_INDEX; // Do the right side int down_danger = 0; if (!at_corner) { int new_row = 0; int new_col = 0; for (int i = 0; i < rigth_range; i++) { new_row = row - 1 - i; new_col = col + 1 + i; if (playing_board.board[new_row, new_col] == status) { down_danger++; } if (playing_board.board[new_row, new_col] == Enemy(status)) { break; } } } evaluation.total_diagonal_positive_danger = (top_danger + down_danger + 1); evaluation.up_danger = top_danger; evaluation.down_danger = down_danger; return(evaluation); }