C++围棋游戏开发简单示例(控制台版)
核心代码实现
            
            
              cpp
              
              
            
          
          #include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int SIZE = 9;  // 简化棋盘为9x9:ml-citation{ref="1" data="citationList"}
int board[SIZE][SIZE] = {0};  // 0:空 1:黑 2:白:ml-citation{ref="1,2" data="citationList"}
pair<int, int> lastRemoved = {-1, -1};  // 记录上一步提子位置(简化打劫规则):ml-citation{ref="1" data="citationList"}
// 显示棋盘
void displayBoard() {
    cout << "  ";
    for (int i = 0; i < SIZE; i++) cout << i << " ";
    cout << "\n";
    for (int y = 0; y < SIZE; y++) {
        cout << y << " ";
        for (int x = 0; x < SIZE; x++) {
            char c = (board[y][x] == 1) ? 'B' : (board[y][x] == 2) ? 'W' : '+';
            cout << c << " ";
        }
        cout << "\n";
    }
}
// 检查坐标合法性
bool isValid(int x, int y) {
    return x >= 0 && x < SIZE && y >= 0 && y < SIZE;
}
// BFS计算气(Liberty)
int calculateLiberty(int x, int y, int color) {
    bool visited[SIZE][SIZE] = {false};
    queue<pair<int, int>> q;
    q.push({x, y});
    visited[y][x] = true;
    int liberty = 0;
    vector<pair<int, int>> dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}};
    while (!q.empty()) {
        auto [cx, cy] = q.front();
        q.pop();
        for (auto [dx, dy] : dirs) {
            int nx = cx + dx, ny = cy + dy;
            if (!isValid(nx, ny) || visited[ny][nx]) continue;
            if (board[ny][nx] == 0) liberty++;
            else if (board[ny][nx] == color) {
                visited[ny][nx] = true;
                q.push({nx, ny});
            }
        }
    }
    return liberty;
}
// 提子逻辑
void removeGroup(int x, int y, int color) {
    queue<pair<int, int>> q;
    q.push({x, y});
    board[y][x] = 0;
    vector<pair<int, int>> dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}};
    while (!q.empty()) {
        auto [cx, cy] = q.front();
        q.pop();
        for (auto [dx, dy] : dirs) {
            int nx = cx + dx, ny = cy + dy;
            if (isValid(nx, ny) && board[ny][nx] == color) {
                board[ny][nx] = 0;
                q.push({nx, ny});
            }
        }
    }
}
// 落子逻辑
bool placeStone(int x, int y, int color) {
    if (!isValid(x, y) || board[y][x] != 0) return false;
    board[y][x] = color;
    vector<pair<int, int>> dirs = {{0,1}, {1,0}, {0,-1}, {-1,0}};
    // 检查周围敌方棋子是否无气
    for (auto [dx, dy] : dirs) {
        int nx = x + dx, ny = y + dy;
        if (isValid(nx, ny) && board[ny][nx] != 0 && board[ny][nx] != color) {
            if (calculateLiberty(nx, ny, board[ny][nx]) == 0) {
                removeGroup(nx, ny, board[ny][nx]);
                lastRemoved = {x, y};  // 记录提子位置:ml-citation{ref="1" data="citationList"}
            }
        }
    }
    // 检查自身棋子是否存活
    if (calculateLiberty(x, y, color) == 0) {
        board[y][x] = 0;  // 自杀规则
        return false;
    }
    return true;
}
int main() {
    int currentPlayer = 1;  // 黑方先手
    while (true) {
        displayBoard();
        int x, y;
        cout << "玩家" << ((currentPlayer == 1) ? "黑(B)" : "白(W)") << "输入坐标(x y): ";
        cin >> x >> y;
        if (placeStone(x, y, currentPlayer)) {
            currentPlayer = (currentPlayer == 1) ? 2 : 1;  // 切换玩家:ml-citation{ref="3" data="citationList"}
        } else {
            cout << "落子无效,请重新输入!\n";
        }
    }
    return 0;
}核心功能说明
1.棋盘初始化
使用9x9简化棋盘,通过二维数组board存储状态12。
符号B表示黑子,W表示白子,+表示空位3。
2.落子与提子
通过placeStone()函数实现落子合法性检查,包括自杀规则和敌方提子逻辑12。
使用BFS算法计算棋子群的气,气为0时触发removeGroup()提子14。
3.交互与显示
控制台输入坐标,动态更新棋盘状态3。
简化打劫规则:仅记录最后一次提子位置(未完全实现劫争判断)1。
4.编译与运行
环境要求
支持C++11标准的编译器(如GCC/Clang/Visual Studio)13。
控制台模式下直接运行,无需图形库依赖3。
5.操作说明
输入坐标格式为x y(例如3 4表示第3列第4行)。
若落子位置无效(如已有棋子或导致自杀),提示重新输入3。
6.扩展方向
规则完善
增加劫争判断:通过lastRemoved变量阻止立即回提1。
实现胜负判定:通过计算领地或活子数量24。