以下是一个简单的C++五子棋游戏的实现:

#include <iostream>
#include <vector>

// 定义棋盘的大小
const int BOARD_SIZE = 15;

// 定义棋子类型
enum class Piece {
    EMPTY,
    BLACK,
    WHITE
};

// 棋盘类
class Board {
public:
    Board() {
        // 初始化棋盘
        for (int i = 0; i < BOARD_SIZE; i++) {
            std::vector<Piece> row(BOARD_SIZE, Piece::EMPTY);
            board.push_back(row);
        }
    }

    // 打印棋盘
    void print() const {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                if (board[i][j] == Piece::EMPTY) {
                    std::cout << ".";
                } else if (board[i][j] == Piece::BLACK) {
                    std::cout << "X";
                } else if (board[i][j] == Piece::WHITE) {
                    std::cout << "O";
                }
            }
            std::cout << std::endl;
        }
    }

    // 下棋
    void makeMove(int row, int col, Piece piece) {
        board[row][col] = piece;
    }

    // 判断是否胜利
    bool isWin(int row, int col, Piece piece) const {
        // 判断水平方向
        int count = 0;
        for (int i = col - 4; i <= col + 4; i++) {
            if (i < 0 || i >= BOARD_SIZE) continue;
            if (board[row][i] == piece) {
                count++;
                if (count == 5) return true;
            } else {
                count = 0;
            }
        }

        // 判断竖直方向
        count = 0;
        for (int i = row - 4; i <= row + 4; i++) {
            if (i < 0 || i >= BOARD_SIZE) continue;
            if (board[i][col] == piece) {
                count++;
                if (count == 5) return true;
            } else {
                count = 0;
            }
        }

        // 判断斜向(左上到右下)
        count = 0;
        for (int i = -4; i <= 4; i++) {
            int r = row + i;
            int c = col + i;
            if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE) continue;
            if (board[r][c] == piece) {
                count++;
                if (count == 5) return true;
            } else {
                count = 0;
            }
        }

        // 判断斜向(右上到左下)
        count = 0;
        for (int i = -4; i <= 4; i++) {
            int r = row + i;
            int c = col - i;
            if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE) continue;
            if (board[r][c] == piece) {
                count++;
                if (count == 5) return true;
            } else {
                count = 0;
            }
        }

        return false;
    }

private:
    std::vector<std::vector<Piece>> board;
};

int main() {
    Board board;
    Piece currentPlayer = Piece::BLACK;

    while (true) {
        // 打印当前棋盘
        board.print();

        // 提示当前玩家下棋
        if (currentPlayer == Piece::BLACK) {
            std::cout << "当前玩家:黑棋(X)" << std::endl;
        } else if (currentPlayer == Piece::WHITE) {
            std::cout << "当前玩家:白棋(O)" << std::endl;
        }

        // 等待玩家输入下棋位置
        int row, col;
        std::cout << "请输入下棋位置(行 列):";
        std::cin >> row >> col;

        // 判断下棋位置是否有效
        if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE || board.makeMove(row, col, currentPlayer) == false) {
            std::cout << "无效的下棋位置,请重新输入!" << std::endl;
            continue;
        }

        // 判断当前玩家是否胜利
        if (board.isWin(row, col, currentPlayer)) {
            std::cout << "玩家 ";
            if (currentPlayer == Piece::BLACK) {
                std::cout << "黑棋(X)";
            } else if (currentPlayer == Piece::WHITE) {
                std::cout << "白棋(O)";
            }
            std::cout << " 胜利!" << std::endl;
            break;
        }

        // 切换到下一个玩家
        if (currentPlayer == Piece::BLACK) {
            currentPlayer = Piece::WHITE;
        } else if (currentPlayer == Piece::WHITE) {
            currentPlayer = Piece::BLACK;
        }
    }

    return 0;
}

这是一个简单的控制台五子棋游戏,玩家可以通过输入行和列来下棋,当有一方连成5子时,游戏结束并宣布胜利

用c++写一个简单的五子棋游戏。

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

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