【五子棋 v1.0.3】

· · 个人记录

#include <bits/stdc++.h>
#include <windows.h>

using namespace std;

const int BOARD_SIZE = 15;
const int EMPTY = 0;
const int BLACK = 1;  // 电脑
const int WHITE = 2;  // 玩家

class Gomoku {
    private:
        vector<vector<int>> board;
        bool gameOver;
        int currentPlayer;
        pair<int, int> lastMove;
        int moveCount;

    public:
        Gomoku() : board(BOARD_SIZE, vector<int>(BOARD_SIZE, EMPTY)),
            gameOver(false), currentPlayer(BLACK), lastMove(-1, -1), moveCount(0) {
            srand(time(0));
        }

        void clearScreen() {
            system("cls");
        }

        void printBoard() {
            cout << "  ";
            for (int i = 0; i < BOARD_SIZE; i++) {
                cout << (i < 10 ? " " : "") << i << " ";
            }
            cout << endl;

            for (int i = 0; i < BOARD_SIZE; i++) {
                cout << (i < 10 ? " " : "") << i << " ";
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] == EMPTY) {
                        cout << ".  ";
                    } else if (board[i][j] == BLACK) {
                        cout << "#  ";
                    } else {
                        cout << "@  ";
                    }
                }
                cout << endl;
            }
        }

        bool isValidPosition(int x, int y) {
            return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE;
        }

        bool placeStone(int x, int y, int player) {
            if (!isValidPosition(x, y) || board[x][y] != EMPTY) {
                return false;
            }
            board[x][y] = player;
            lastMove = {x, y};
            moveCount++;
            return true;
        }

        // 胜利判断
        bool checkWin(int player) {
            if (lastMove.first == -1)
                return false;

            int x = lastMove.first;
            int y = lastMove.second;

            int directions[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};

            for (auto &dir : directions) {
                int count = 1;

                // 正向检查
                for (int i = 1; i <= 4; i++) {
                    int nx = x + dir[0] * i;
                    int ny = y + dir[1] * i;
                    if (isValidPosition(nx, ny) && board[nx][ny] == player) {
                        count++;
                    } else {
                        break;
                    }
                }

                // 反向检查
                for (int i = 1; i <= 4; i++) {
                    int nx = x - dir[0] * i;
                    int ny = y - dir[1] * i;
                    if (isValidPosition(nx, ny) && board[nx][ny] == player) {
                        count++;
                    } else {
                        break;
                    }
                }

                if (count >= 5) {
                    return true;
                }
            }
            return false;
        }

        // 检查特定方向的连续棋子
        int checkDirection(int x, int y, int dx, int dy, int player) {
            int count = 0;
            for (int i = 1; i <= 4; i++) {
                int nx = x + dx * i;
                int ny = y + dy * i;
                if (isValidPosition(nx, ny) && board[nx][ny] == player) {
                    count++;
                } else {
                    break;
                }
            }
            return count;
        }

        // 获取立即获胜的位置
        vector<pair<int, int>> getWinPositions(int player) {
            vector<pair<int, int>> positions;

            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] != EMPTY)
                        continue;

                    board[i][j] = player;
                    if (checkWin(player)) {
                        positions.push_back({i, j});
                    }
                    board[i][j] = EMPTY;
                }
            }
            return positions;
        }

        // 获取需要立即防守的位置(对方下一步获胜)
        vector<pair<int, int>> getImmediateThreats(int player) {
            return getWinPositions(3 - player);
        }

        // 修复的活四检测
        vector<pair<int, int>> getLiveFourPositions(int player) {
            vector<pair<int, int>> positions;
            int opponent = 3 - player;

            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] != EMPTY)
                        continue;

                    // 检查四个方向
                    int directions[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};

                    for (auto &dir : directions) {
                        // 检查连续四子且两端都为空
                        int forward = checkDirection(i, j, dir[0], dir[1], opponent);
                        int backward = checkDirection(i, j, -dir[0], -dir[1], opponent);

                        // 检查是否形成四连珠
                        if (forward + backward >= 3) {
                            // 检查两端情况
                            int end1_x = i + dir[0] * (forward + 1);
                            int end1_y = j + dir[1] * (forward + 1);
                            int end2_x = i - dir[0] * (backward + 1);
                            int end2_y = j - dir[1] * (backward + 1);

                            bool end1_empty = isValidPosition(end1_x, end1_y) && board[end1_x][end1_y] == EMPTY;
                            bool end2_empty = isValidPosition(end2_x, end2_y) && board[end2_x][end2_y] == EMPTY;

                            // 两端都为空才是活四
                            if (end1_empty && end2_empty) {
                                // 添加两个防守位置
                                positions.push_back({end1_x, end1_y});
                                positions.push_back({end2_x, end2_y});
                            }
                        }
                    }
                }
            }

            // 去重
            sort(positions.begin(), positions.end());
            positions.erase(unique(positions.begin(), positions.end()), positions.end());
            return positions;
        }

        // 修复的死四检测
        vector<pair<int, int>> getDeadFourPositions(int player) {
            vector<pair<int, int>> positions;
            int opponent = 3 - player;

            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] != EMPTY)
                        continue;

                    // 检查四个方向
                    int directions[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};

                    for (auto &dir : directions) {
                        // 检查连续四子但一端被堵
                        int forward = checkDirection(i, j, dir[0], dir[1], opponent);
                        int backward = checkDirection(i, j, -dir[0], -dir[1], opponent);

                        if (forward + backward >= 3) {
                            // 检查两端情况
                            int end1_x = i + dir[0] * (forward + 1);
                            int end1_y = j + dir[1] * (forward + 1);
                            int end2_x = i - dir[0] * (backward + 1);
                            int end2_y = j - dir[1] * (backward + 1);

                            bool end1_blocked = !isValidPosition(end1_x, end1_y) ||
                                                (isValidPosition(end1_x, end1_y) && board[end1_x][end1_y] == player);
                            bool end2_blocked = !isValidPosition(end2_x, end2_y) ||
                                                (isValidPosition(end2_x, end2_y) && board[end2_x][end2_y] == player);

                            // 只有一端被堵才是死四
                            if (end1_blocked != end2_blocked) {
                                if (!end1_blocked)
                                    positions.push_back({end1_x, end1_y});
                                if (!end2_blocked)
                                    positions.push_back({end2_x, end2_y});
                            }
                        }

                        // 检查间隔死四模式:X X _ X X
                        if (forward == 1 && backward == 1) {
                            int next2_x = i + dir[0] * 2;
                            int next2_y = j + dir[1] * 2;
                            int prev2_x = i - dir[0] * 2;
                            int prev2_y = j - dir[1] * 2;

                            if (isValidPosition(next2_x, next2_y) && board[next2_x][next2_y] == opponent &&
                                    isValidPosition(prev2_x, prev2_y) && board[prev2_x][prev2_y] == opponent) {
                                positions.push_back({i, j});
                            }
                        }
                    }
                }
            }

            // 去重
            sort(positions.begin(), positions.end());
            positions.erase(unique(positions.begin(), positions.end()), positions.end());
            return positions;
        }

        // 修复的活三检测
        vector<pair<int, int>> getLiveThreePositions(int player) {
            vector<pair<int, int>> positions;
            int opponent = 3 - player;

            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] != EMPTY)
                        continue;

                    // 检查四个方向
                    int directions[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};

                    for (auto &dir : directions) {
                        // 检查连续活三
                        int forward = checkDirection(i, j, dir[0], dir[1], opponent);
                        int backward = checkDirection(i, j, -dir[0], -dir[1], opponent);

                        if (forward + backward == 2) {
                            // 检查两端是否都为空
                            int end1_x = i + dir[0] * (forward + 1);
                            int end1_y = j + dir[1] * (forward + 1);
                            int end2_x = i - dir[0] * (backward + 1);
                            int end2_y = j - dir[1] * (backward + 1);

                            bool end1_empty = isValidPosition(end1_x, end1_y) && board[end1_x][end1_y] == EMPTY;
                            bool end2_empty = isValidPosition(end2_x, end2_y) && board[end2_x][end2_y] == EMPTY;

                            if (end1_empty && end2_empty) {
                                positions.push_back({end1_x, end1_y});
                                positions.push_back({end2_x, end2_y});
                            }
                        }

                        // 检查跳活三模式:X X _ X 或 X _ X X
                        // 模式1: X X _ X
                        if (forward == 2 && backward == 0) {
                            int gap_x = i + dir[0];
                            int gap_y = j + dir[1];
                            int next3_x = i + dir[0] * 3;
                            int next3_y = j + dir[1] * 3;

                            if (isValidPosition(gap_x, gap_y) && board[gap_x][gap_y] == EMPTY &&
                                    isValidPosition(next3_x, next3_y) && board[next3_x][next3_y] == opponent) {

                                int prev_x = i - dir[0];
                                int prev_y = j - dir[1];
                                bool prev_empty = isValidPosition(prev_x, prev_y) && board[prev_x][prev_y] == EMPTY;
                                bool next4_empty = isValidPosition(i + dir[0] * 4, j + dir[1] * 4) &&
                                                   board[i + dir[0] * 4][j + dir[1] * 4] == EMPTY;

                                if (prev_empty && next4_empty) {
                                    positions.push_back({prev_x, prev_y});
                                    positions.push_back({i + dir[0] * 4, j + dir[1] * 4});
                                }
                            }
                        }

                        // 模式2: X _ X X
                        if (forward == 1 && backward == 0) {
                            int next1_x = i + dir[0];
                            int next1_y = j + dir[1];
                            int next2_x = i + dir[0] * 2;
                            int next2_y = j + dir[1] * 2;

                            if (isValidPosition(next1_x, next1_y) && board[next1_x][next1_y] == EMPTY &&
                                    isValidPosition(next2_x, next2_y) && board[next2_x][next2_y] == opponent) {

                                int prev_x = i - dir[0];
                                int prev_y = j - dir[1];
                                bool prev_empty = isValidPosition(prev_x, prev_y) && board[prev_x][prev_y] == EMPTY;
                                bool next3_empty = isValidPosition(i + dir[0] * 3, j + dir[1] * 3) &&
                                                   board[i + dir[0] * 3][j + dir[1] * 3] == EMPTY;

                                if (prev_empty && next3_empty) {
                                    positions.push_back({prev_x, prev_y});
                                    positions.push_back({i + dir[0] * 3, j + dir[1] * 3});
                                }
                            }
                        }
                    }
                }
            }

            // 去重
            sort(positions.begin(), positions.end());
            positions.erase(unique(positions.begin(), positions.end()), positions.end());
            return positions;
        }

        // 检查活二模式
        vector<pair<int, int>> getLiveTwoPositions(int player) {
            vector<pair<int, int>> positions;
            int opponent = 3 - player;

            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] != EMPTY)
                        continue;

                    int directions[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};

                    for (auto &dir : directions) {
                        int forward = checkDirection(i, j, dir[0], dir[1], opponent);
                        int backward = checkDirection(i, j, -dir[0], -dir[1], opponent);

                        if (forward + backward == 1) {
                            // 检查两端是否都为空
                            int end1_x = i + dir[0] * (forward + 1);
                            int end1_y = j + dir[1] * (forward + 1);
                            int end2_x = i - dir[0] * (backward + 1);
                            int end2_y = j - dir[1] * (backward + 1);

                            bool end1_empty = isValidPosition(end1_x, end1_y) && board[end1_x][end1_y] == EMPTY;
                            bool end2_empty = isValidPosition(end2_x, end2_y) && board[end2_x][end2_y] == EMPTY;

                            if (end1_empty && end2_empty) {
                                positions.push_back({end1_x, end1_y});
                                positions.push_back({end2_x, end2_y});
                            }
                        }
                    }
                }
            }

            // 去重
            sort(positions.begin(), positions.end());
            positions.erase(unique(positions.begin(), positions.end()), positions.end());
            return positions;
        }

        // 检查自己能否形成活四
        vector<pair<int, int>> getOwnLiveFourPositions(int player) {
            vector<pair<int, int>> positions;

            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] != EMPTY)
                        continue;

                    board[i][j] = player;

                    // 检查四个方向
                    int directions[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};
                    bool found = false;

                    for (auto &dir : directions) {
                        int forward = checkDirection(i, j, dir[0], dir[1], player);
                        int backward = checkDirection(i, j, -dir[0], -dir[1], player);

                        if (forward + backward == 3) {
                            // 检查两端是否都为空
                            int end1_x = i + dir[0] * (forward + 1);
                            int end1_y = j + dir[1] * (forward + 1);
                            int end2_x = i - dir[0] * (backward + 1);
                            int end2_y = j - dir[1] * (backward + 1);

                            bool end1_empty = isValidPosition(end1_x, end1_y) && board[end1_x][end1_y] == EMPTY;
                            bool end2_empty = isValidPosition(end2_x, end2_y) && board[end2_x][end2_y] == EMPTY;

                            if (end1_empty && end2_empty) {
                                positions.push_back({i, j});
                                found = true;
                                break;
                            }
                        }
                    }

                    board[i][j] = EMPTY;
                    if (found)
                        break;
                }
            }
            return positions;
        }

        // 检查自己能否形成死四
        vector<pair<int, int>> getOwnDeadFourPositions(int player) {
            vector<pair<int, int>> positions;

            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] != EMPTY)
                        continue;

                    board[i][j] = player;

                    // 检查四个方向
                    int directions[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};
                    bool found = false;

                    for (auto &dir : directions) {
                        int forward = checkDirection(i, j, dir[0], dir[1], player);
                        int backward = checkDirection(i, j, -dir[0], -dir[1], player);

                        if (forward + backward == 3) {
                            // 检查是否只有一端可下
                            int end1_x = i + dir[0] * (forward + 1);
                            int end1_y = j + dir[1] * (forward + 1);
                            int end2_x = i - dir[0] * (backward + 1);
                            int end2_y = j - dir[1] * (backward + 1);

                            bool end1_ok = isValidPosition(end1_x, end1_y) && board[end1_x][end1_y] == EMPTY;
                            bool end2_ok = isValidPosition(end2_x, end2_y) && board[end2_x][end2_y] == EMPTY;

                            if (end1_ok != end2_ok) { // 只有一端可下
                                positions.push_back({i, j});
                                found = true;
                                break;
                            }
                        }
                    }

                    board[i][j] = EMPTY;
                    if (found)
                        break;
                }
            }
            return positions;
        }

        // 检查自己能否形成活三
        vector<pair<int, int>> getOwnLiveThreePositions(int player) {
            vector<pair<int, int>> positions;

            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] != EMPTY)
                        continue;

                    board[i][j] = player;

                    // 检查四个方向
                    int directions[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};
                    bool found = false;

                    for (auto &dir : directions) {
                        int forward = checkDirection(i, j, dir[0], dir[1], player);
                        int backward = checkDirection(i, j, -dir[0], -dir[1], player);

                        if (forward + backward == 2) {
                            // 检查两端是否都为空
                            int end1_x = i + dir[0] * (forward + 1);
                            int end1_y = j + dir[1] * (forward + 1);
                            int end2_x = i - dir[0] * (backward + 1);
                            int end2_y = j - dir[1] * (backward + 1);

                            bool end1_empty = isValidPosition(end1_x, end1_y) && board[end1_x][end1_y] == EMPTY;
                            bool end2_empty = isValidPosition(end2_x, end2_y) && board[end2_x][end2_y] == EMPTY;

                            if (end1_empty && end2_empty) {
                                positions.push_back({i, j});
                                found = true;
                                break;
                            }
                        }
                    }

                    board[i][j] = EMPTY;
                    if (found)
                        break;
                }
            }
            return positions;
        }

        // 检查自己能否形成双活三
        vector<pair<int, int>> getOwnDoubleLiveThreePositions(int player) {
            vector<pair<int, int>> positions;

            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] != EMPTY)
                        continue;

                    board[i][j] = player;

                    // 检查是否能形成两个活三
                    auto liveThrees = getOwnLiveThreePositions(player);
                    int count = 0;
                    for (auto &pos : liveThrees) {
                        if (pos.first == i && pos.second == j) {
                            count++;
                        }
                    }

                    // 如果能在多个方向形成活三,则是双活三
                    if (count >= 2) {
                        positions.push_back({i, j});
                    }

                    board[i][j] = EMPTY;
                }
            }
            return positions;
        }

        // 检查自己能否形成双活二
        vector<pair<int, int>> getOwnDoubleLiveTwoPositions(int player) {
            vector<pair<int, int>> positions;

            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] != EMPTY)
                        continue;

                    board[i][j] = player;

                    // 统计能形成的活二数量
                    int liveTwoCount = 0;
                    int directions[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};

                    for (auto &dir : directions) {
                        int forward = checkDirection(i, j, dir[0], dir[1], player);
                        int backward = checkDirection(i, j, -dir[0], -dir[1], player);

                        if (forward + backward == 1) {
                            // 检查两端是否都为空
                            int end1_x = i + dir[0] * (forward + 1);
                            int end1_y = j + dir[1] * (forward + 1);
                            int end2_x = i - dir[0] * (backward + 1);
                            int end2_y = j - dir[1] * (backward + 1);

                            bool end1_empty = isValidPosition(end1_x, end1_y) && board[end1_x][end1_y] == EMPTY;
                            bool end2_empty = isValidPosition(end2_x, end2_y) && board[end2_x][end2_y] == EMPTY;

                            if (end1_empty && end2_empty) {
                                liveTwoCount++;
                            }
                        }
                    }

                    // 如果能形成两个或以上的活二,则是双活二
                    if (liveTwoCount >= 2) {
                        positions.push_back({i, j});
                    }

                    board[i][j] = EMPTY;
                }
            }
            return positions;
        }

        // 获取相邻位置
        vector<pair<int, int>> getAdjacentPositions() {
            vector<pair<int, int>> positions;

            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] == EMPTY) {
                        for (int dx = -1; dx <= 1; dx++) {
                            for (int dy = -1; dy <= 1; dy++) {
                                if (dx == 0 && dy == 0)
                                    continue;
                                int nx = i + dx;
                                int ny = j + dy;
                                if (isValidPosition(nx, ny) && board[nx][ny] != EMPTY) {
                                    positions.push_back({i, j});
                                    goto next_position;
                                }
                            }
                        }
                    }
next_position:
                    ;
                }
            }
            return positions;
        }

        // 检查组合威胁(双活三、双死四、活三+死四等)
        vector<pair<int, int>> getComboThreats(int player) {
            vector<pair<int, int>> positions;
            int opponent = 3 - player;

            // 收集所有威胁位置
            auto liveFours = getLiveFourPositions(player);
            auto deadFours = getDeadFourPositions(player);
            auto liveThrees = getLiveThreePositions(player);

            // 统计每个位置出现的威胁类型数量
            map<pair<int, int>, int> threatCount;

            for (auto &pos : liveFours) {
                threatCount[pos]++;
            }
            for (auto &pos : deadFours) {
                threatCount[pos]++;
            }
            for (auto &pos : liveThrees) {
                threatCount[pos]++;
            }

            // 找出有多个威胁的位置
            for (auto &entry : threatCount) {
                if (entry.second >= 2) {
                    positions.push_back(entry.first);
                }
            }

            return positions;
        }

        // 电脑AI逻辑 - 修复的优先级
        void computerMove() {
            // 若是第一步,落在 (7,7)
            if (moveCount == 0) {
                placeStone(7, 7, BLACK);
                return;
            }

            vector<pair<int, int>> candidatePositions;
            int opponent = WHITE;

            // 1. 自己立即获胜
            auto winPositions = getWinPositions(BLACK);
            if (!winPositions.empty()) {
                int idx = rand() % winPositions.size();
                placeStone(winPositions[idx].first, winPositions[idx].second, BLACK);
                return;
            }

            // 2. 对方立即获胜(包括活4、死4)
            auto immediateThreats = getImmediateThreats(BLACK);
            if (!immediateThreats.empty()) {
                int idx = rand() % immediateThreats.size();
                placeStone(immediateThreats[idx].first, immediateThreats[idx].second, BLACK);
                return;
            }

            // 3. 对方活四防守 - 新增,提高优先级
            auto liveFours = getLiveFourPositions(BLACK);
            if (!liveFours.empty()) {
                int idx = rand() % liveFours.size();
                placeStone(liveFours[idx].first, liveFours[idx].second, BLACK);
                return;
            }

            // 4. 自己形成活四
            auto ownLiveFours = getOwnLiveFourPositions(BLACK);
            if (!ownLiveFours.empty()) {
                int idx = rand() % ownLiveFours.size();
                placeStone(ownLiveFours[idx].first, ownLiveFours[idx].second, BLACK);
                return;
            }

            // 5. 自己形成死四
            auto ownDeadFours = getOwnDeadFourPositions(BLACK);
            if (!ownDeadFours.empty()) {
                int idx = rand() % ownDeadFours.size();
                placeStone(ownDeadFours[idx].first, ownDeadFours[idx].second, BLACK);
                return;
            }

            // 6. 对方死四防守
            auto deadFours = getDeadFourPositions(BLACK);
            if (!deadFours.empty()) {
                int idx = rand() % deadFours.size();
                placeStone(deadFours[idx].first, deadFours[idx].second, BLACK);
                return;
            }

            // 7. 组合威胁防守
            auto comboThreats = getComboThreats(BLACK);
            if (!comboThreats.empty()) {
                int idx = rand() % comboThreats.size();
                placeStone(comboThreats[idx].first, comboThreats[idx].second, BLACK);
                return;
            }

            // 8. 对方活三防守
            auto liveThrees = getLiveThreePositions(BLACK);
            if (!liveThrees.empty()) {
                int idx = rand() % liveThrees.size();
                placeStone(liveThrees[idx].first, liveThrees[idx].second, BLACK);
                return;
            }

            // 9. 自己双活三
            auto ownDoubleLiveThrees = getOwnDoubleLiveThreePositions(BLACK);
            if (!ownDoubleLiveThrees.empty()) {
                int idx = rand() % ownDoubleLiveThrees.size();
                placeStone(ownDoubleLiveThrees[idx].first, ownDoubleLiveThrees[idx].second, BLACK);
                return;
            }

            // 10. 自己形成活三
            auto ownLiveThrees = getOwnLiveThreePositions(BLACK);
            if (!ownLiveThrees.empty()) {
                int idx = rand() % ownLiveThrees.size();
                placeStone(ownLiveThrees[idx].first, ownLiveThrees[idx].second, BLACK);
                return;
            }

            // 11. 对方活二防守
            auto liveTwos = getLiveTwoPositions(BLACK);
            if (!liveTwos.empty()) {
                int idx = rand() % liveTwos.size();
                placeStone(liveTwos[idx].first, liveTwos[idx].second, BLACK);
                return;
            }

            // 12. 自己双活二
            auto ownDoubleLiveTwos = getOwnDoubleLiveTwoPositions(BLACK);
            if (!ownDoubleLiveTwos.empty()) {
                int idx = rand() % ownDoubleLiveTwos.size();
                placeStone(ownDoubleLiveTwos[idx].first, ownDoubleLiveTwos[idx].second, BLACK);
                return;
            }

            // 13. 相邻位置落子
            auto adjacentPositions = getAdjacentPositions();
            if (!adjacentPositions.empty()) {
                int idx = rand() % adjacentPositions.size();
                placeStone(adjacentPositions[idx].first, adjacentPositions[idx].second, BLACK);
                return;
            }

            // 14. 随机落子
            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] == EMPTY) {
                        candidatePositions.push_back({i, j});
                    }
                }
            }

            if (!candidatePositions.empty()) {
                int idx = rand() % candidatePositions.size();
                placeStone(candidatePositions[idx].first, candidatePositions[idx].second, BLACK);
            }
        }

        void playerMove() {
            int x, y;
            while (true) {
                cout << "请输入您的落子位置 (行 列, 如: 7 7): ";
                cin >> x >> y;

                if (placeStone(x, y, WHITE)) {
                    break;
                } else {
                    cout << "无效位置,请重新输入!" << endl;
                }
            }
        }

        void run() {
            cout << "欢迎来到五子棋游戏!" << endl;
            cout << "电脑: #   玩家: @" << endl;
            cout << "按回车键开始...";
            cin.ignore();
            cin.get();

            while (!gameOver) {
                clearScreen();
                printBoard();

                if (currentPlayer == BLACK) {
                    cout << "电脑思考中..." << endl;
                    Sleep(500);
                    computerMove();

                    if (checkWin(BLACK)) {
                        clearScreen();
                        printBoard();
                        cout << "电脑获胜!" << endl;
                        gameOver = true;
                    }
                    currentPlayer = WHITE;
                } else {
                    playerMove();

                    if (checkWin(WHITE)) {
                        clearScreen();
                        printBoard();
                        cout << "恭喜您获胜!" << endl;
                        gameOver = true;
                    }
                    currentPlayer = BLACK;
                }

                // 检查平局
                bool isFull = true;
                for (int i = 0; i < BOARD_SIZE; i++) {
                    for (int j = 0; j < BOARD_SIZE; j++) {
                        if (board[i][j] == EMPTY) {
                            isFull = false;
                            break;
                        }
                    }
                    if (!isFull)
                        break;
                }

                if (isFull) {
                    clearScreen();
                    printBoard();
                    cout << "平局!" << endl;
                    gameOver = true;
                }
            }
        }
};

int main() {
    Gomoku game;
    game.run();
    return 0;
}