C++圍棋游戲開發簡單示例(控制臺版)
?核心代碼實現?
#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。