当前位置:首页 C++ > 正文

C++写的五子棋游戏

作者:野牛程序员:2023-07-13 07:19:18 C++阅读 2774

五子棋是一种双人对弈的棋类游戏,目标是先在棋盘上形成连续的五个棋子,横、竖、斜线均可。

以下是五子棋的基本规则:

  1. 游戏棋盘:五子棋棋盘是一个15×15的方格网格。

  2. 对弈双方:游戏中有两名玩家,通常分为黑方和白方。黑方先手。

  3. 下棋操作:玩家轮流在空白的交叉点上落子,每次只能落一个棋子。

  4. 胜利条件:玩家要求在棋盘上连续形成五个自己的棋子,可以是横线、竖线或斜线。先达成这个条件的玩家获得胜利。

  5. 平局:如果棋盘填满而没有任何一方达成胜利条件,游戏宣布平局。

  6. 禁手规则(可选):为了平衡游戏,一些规则集可能会引入禁手规则。常见的禁手规则包括禁止长连、禁止双活三等。

总之,五子棋的目标是通过合理的下棋策略,在棋盘上形成连续的五个自己的棋子,同时阻止对方达成胜利条件。玩家需要运用战术和战略思考来赢得游戏。

以下是一个简单的五子棋游戏的示例代码,使用C++编写。这个示例使用了命令行界面,玩家可以通过输入坐标来下棋。你可以根据需要对其进行扩展和改进。

#include <iostream>
#include <vector>

const int BOARD_SIZE = 15;

enum class Player {
    None,
    Black,
    White
};

struct Move {
    int row;
    int col;
};

class Gomoku {
public:
    Gomoku() : currentPlayer(Player::Black), gameOver(false) {
        board.resize(BOARD_SIZE, std::vector<Player>(BOARD_SIZE, Player::None));
    }

    void play() {
        while (!gameOver) {
            drawBoard();
            std::cout << "Player " << getPlayerSymbol(currentPlayer) << " turn. Enter your move (row col): ";
            Move move;
            std::cin >> move.row >> move.col;
            if (isValidMove(move)) {
                makeMove(move);
                if (checkWin(move)) {
                    gameOver = true;
                    std::cout << "Player " << getPlayerSymbol(currentPlayer) << " wins!" << std::endl;
                } else if (isBoardFull()) {
                    gameOver = true;
                    std::cout << "It's a draw!" << std::endl;
                } else {
                    switchPlayer();
                }
            } else {
                std::cout << "Invalid move. Please try again." << std::endl;
            }
        }
    }

private:
    std::vector<std::vector<Player>> board;
    Player currentPlayer;
    bool gameOver;

    void drawBoard() {
        for (int row = 0; row < BOARD_SIZE; ++row) {
            for (int col = 0; col < BOARD_SIZE; ++col) {
                std::cout << getPlayerSymbol(board[row][col]) << " ";
            }
            std::cout << std::endl;
        }
    }

    std::string getPlayerSymbol(Player player) {
        switch (player) {
            case Player::Black:
                return "B";
            case Player::White:
                return "W";
            default:
                return ".";
        }
    }

    bool isValidMove(const Move& move) {
        if (move.row < 0 || move.row >= BOARD_SIZE || move.col < 0 || move.col >= BOARD_SIZE) {
            return false;
        }
        return board[move.row][move.col] == Player::None;
    }

    void makeMove(const Move& move) {
        board[move.row][move.col] = currentPlayer;
    }

    bool checkWin(const Move& move) {
        int row = move.row;
        int col = move.col;
        Player player = currentPlayer;

        // Check horizontal
        int count = 0;
        while (col >= 0 && board[row][col] == player) {
            ++count;
            --col;
        }
        col = move.col + 1;
        while (col < BOARD_SIZE && board[row][col] == player) {
            ++count;
            ++col;
        }
        if (count >= 5) {
            return true;
        }

        // Check vertical
        count = 0;
        while (row >= 0 && board[row][col] == player) {
            ++count;
            --row;
        }
        row = move.row + 1;
        while (row < BOARD_SIZE && board[row][col] == player) {
            ++count;
            ++row;
        }
        if (count >= 5) {
            return true;
        }

        // Check diagonal (top-left to bottom-right)
        count = 0;
        row = move.row;
        col = move.col;
        while (row >= 0 && col >= 0 && board[row][col] == player) {
            ++count;
            --row;
            --col;
        }
        row = move.row + 1;
        col = move.col + 1;
        while (row < BOARD_SIZE && col < BOARD_SIZE && board[row][col] == player) {
            ++count;
            ++row;
            ++col;
        }
        if (count >= 5) {
            return true;
        }

        // Check diagonal (top-right to bottom-left)
        count = 0;
        row = move.row;
        col = move.col;
        while (row >= 0 && col < BOARD_SIZE && board[row][col] == player) {
            ++count;
            --row;
            ++col;
        }
        row = move.row + 1;
        col = move.col - 1;
        while (row < BOARD_SIZE && col >= 0 && board[row][col] == player) {
            ++count;
            ++row;
            --col;
        }
        if (count >= 5) {
            return true;
        }

        return false;
    }

    bool isBoardFull() {
        for (int row = 0; row < BOARD_SIZE; ++row) {
            for (int col = 0; col < BOARD_SIZE; ++col) {
                if (board[row][col] == Player::None) {
                    return false;
                }
            }
        }
        return true;
    }

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

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


野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
相关推荐

最新推荐

热门点击