以下是一个简单的C++围棋游戏示例:

#include <iostream>
#include <vector>

const int BOARD_SIZE = 19;

enum class Stone {
    EMPTY,
    BLACK,
    WHITE
};

class GoGame {
private:
    std::vector<std::vector<Stone>> board;
    Stone currentPlayer;

public:
    GoGame() {
        board.resize(BOARD_SIZE, std::vector<Stone>(BOARD_SIZE, Stone::EMPTY));
        currentPlayer = Stone::BLACK; // 黑棋先行
    }

    void printBoard() {
        for (int i = 0; i < BOARD_SIZE; ++i) {
            for (int j = 0; j < BOARD_SIZE; ++j) {
                if (board[i][j] == Stone::EMPTY) {
                    std::cout << "+ ";
                } else if (board[i][j] == Stone::BLACK) {
                    std::cout << "● ";
                } else if (board[i][j] == Stone::WHITE) {
                    std::cout << "○ ";
                }
            }
            std::cout << std::endl;
        }
    }

    bool isValidMove(int row, int col) {
        if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
            return false;
        }
        return board[row][col] == Stone::EMPTY;
    }

    void makeMove(int row, int col) {
        if (isValidMove(row, col)) {
            board[row][col] = currentPlayer;
            currentPlayer = (currentPlayer == Stone::BLACK) ? Stone::WHITE : Stone::BLACK;
        } else {
            std::cout << "Invalid move!" << std::endl;
        }
    }
};

int main() {
    GoGame game;

    int row, col;
    while (true) {
        game.printBoard();

        std::cout << "Current player: " << ((game.getCurrentPlayer() == Stone::BLACK) ? "Black" : "White") << std::endl;
        std::cout << "Enter row and column (0-18) to make a move: ";
        std::cin >> row >> col;

        game.makeMove(row, col);
    }

    return 0;
}

这个程序创建了一个GoGame类来表示围棋游戏。游戏板被表示为一个二维向量,其中每个元素是一个Stone枚举类型(EMPTY,BLACK,WHITE)。游戏通过在空位置上放置黑色和白色棋子进行进行。isValidMove函数用于检查给定的位置是否可以进行有效的移动,makeMove函数用于在棋盘上放置棋子,并切换当前玩家。

在主函数中,游戏对象被创建,并通过循环接受用户输入的行和列来进行游戏。游戏板和当前玩家状态在每次移动后进行打印

用c++写一个围棋游戏。

原文地址: https://www.cveoy.top/t/topic/hSv7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录