以下是一个简单的C++五子棋游戏的示例代码:

#include <iostream>
#include <vector>

const int BOARD_SIZE = 15;

enum class Piece {
    Empty,
    Black,
    White
};

class Game {
public:
    Game() : currentPlayer(Piece::Black), board(BOARD_SIZE, std::vector<Piece>(BOARD_SIZE, Piece::Empty)) {}

    void play() {
        while (true) {
            drawBoard();
            std::cout << "Player " << static_cast<int>(currentPlayer) << "'s turn. Enter row and column: ";
            int row, col;
            std::cin >> row >> col;
            if (isValidMove(row, col)) {
                makeMove(row, col);
                if (isWinningMove(row, col)) {
                    drawBoard();
                    std::cout << "Player " << static_cast<int>(currentPlayer) << " wins!" << std::endl;
                    break;
                }
                switchPlayer();
            } else {
                std::cout << "Invalid move. Try again." << std::endl;
            }
        }
    }

private:
    void drawBoard() const {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                switch (board[i][j]) {
                    case Piece::Empty:
                        std::cout << ".";
                        break;
                    case Piece::Black:
                        std::cout << "X";
                        break;
                    case Piece::White:
                        std::cout << "O";
                        break;
                }
            }
            std::cout << std::endl;
        }
    }

    bool isValidMove(int row, int col) const {
        return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == Piece::Empty;
    }

    void makeMove(int row, int col) {
        board[row][col] = currentPlayer;
    }

    bool isWinningMove(int row, int col) const {
        return checkHorizontal(row, col) || checkVertical(row, col) || checkDiagonal(row, col);
    }

    bool checkHorizontal(int row, int col) const {
        int count = 0;
        for (int i = col - 4; i <= col + 4; i++) {
            if (i >= 0 && i + 4 < BOARD_SIZE && board[row][i] == currentPlayer && board[row][i + 1] == currentPlayer &&
                board[row][i + 2] == currentPlayer && board[row][i + 3] == currentPlayer &&
                board[row][i + 4] == currentPlayer) {
                return true;
            }
        }
        return false;
    }

    bool checkVertical(int row, int col) const {
        int count = 0;
        for (int i = row - 4; i <= row + 4; i++) {
            if (i >= 0 && i + 4 < BOARD_SIZE && board[i][col] == currentPlayer && board[i + 1][col] == currentPlayer &&
                board[i + 2][col] == currentPlayer && board[i + 3][col] == currentPlayer &&
                board[i + 4][col] == currentPlayer) {
                return true;
            }
        }
        return false;
    }

    bool checkDiagonal(int row, int col) const {
        int count = 0;
        for (int i = -4; i <= 4; i++) {
            if (row + i >= 0 && row + i + 4 < BOARD_SIZE && col + i >= 0 && col + i + 4 < BOARD_SIZE &&
                board[row + i][col + i] == currentPlayer && board[row + i + 1][col + i + 1] == currentPlayer &&
                board[row + i + 2][col + i + 2] == currentPlayer && board[row + i + 3][col + i + 3] == currentPlayer &&
                board[row + i + 4][col + i + 4] == currentPlayer) {
                return true;
            }
        }
        for (int i = -4; i <= 4; i++) {
            if (row - i >= 0 && row - i - 4 < BOARD_SIZE && col + i >= 0 && col + i + 4 < BOARD_SIZE &&
                board[row - i][col + i] == currentPlayer && board[row - i - 1][col + i + 1] == currentPlayer &&
                board[row - i - 2][col + i + 2] == currentPlayer && board[row - i - 3][col + i + 3] == currentPlayer &&
                board[row - i - 4][col + i + 4] == currentPlayer) {
                return true;
            }
        }
        return false;
    }

    void switchPlayer() {
        currentPlayer = (currentPlayer == Piece::Black) ? Piece::White : Piece::Black;
    }

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

int main() {
    Game game;
    game.play();
    return 0;
}

这个示例代码实现了一个简单的控制台五子棋游戏。游戏使用Piece枚举类型表示棋盘上的棋子状态,Game类封装了游戏的逻辑。play()方法开始游戏循环,每次循环,玩家输入行和列来下棋,程序检查输入是否合法,如果合法则在棋盘上落子,并检查是否有玩家胜出,如果有则结束游戏循环。drawBoard()方法用于绘制棋盘,isValidMove()方法用于检查落子是否合法,makeMove()方法用于在棋盘上落子,isWinningMove()方法用于检查是否有玩家胜出,checkHorizontal()checkVertical()checkDiagonal()分别用于检查水平、垂直和对角线方向上的连续五个棋子是否为同一种颜色,switchPlayer()方法用于切换当前玩家。在main()函数中创建一个Game对象并调用play()方法开始游戏

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

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

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