西北工业大学计算机大作业(C++)

为大家编写了一段五子棋游戏的建议代码!

以下是一个简单的C++控制台版五子棋游戏的示例。该示例包含基本的游戏逻辑,包括棋盘的显示、玩家轮流下棋、胜利条件检查等。

#include <iostream>
#include <vector>
#include <limits>

const int BOARD_SIZE = 15;

enum class Player { EMPTY, PLAYER1, PLAYER2 };

class GomokuGame {
private:
    std::vector<std::vector<Player>> board;

public:
    GomokuGame() : board(BOARD_SIZE, std::vector<Player>(BOARD_SIZE, Player::EMPTY)) {}

    void printBoard() const {
        for (int i = 0; i < BOARD_SIZE; ++i) {
            for (int j = 0; j < BOARD_SIZE; ++j) {
                char symbol = ' ';
                switch (board[i][j]) {
                    case Player::EMPTY:
                        symbol = '.';
                        break;
                    case Player::PLAYER1:
                        symbol = 'X';
                        break;
                    case Player::PLAYER2:
                        symbol = 'O';
                        break;
                }
                std::cout << symbol << ' ';
            }
            std::cout << std::endl;
        }
    }

    bool isMoveValid(int row, int col) const {
        return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == Player::EMPTY;
    }

    bool makeMove(int row, int col, Player player) {
        if (isMoveValid(row, col)) {
            board[row][col] = player;
            return true;
        }
        return false;
    }

    bool checkWin(int row, int col, Player player) const {
        // Check horizontally
        int count = 1;
        for (int i = 1; i < 5; ++i) {
            if (col - i >= 0 && board[row][col - i] == player) {
                ++count;
            } else {
                break;
            }
        }
        for (int i = 1; i < 5; ++i) {
            if (col + i < BOARD_SIZE && board[row][col + i] == player) {
                ++count;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return true;
        }

        // Check vertically
        count = 1;
        for (int i = 1; i < 5; ++i) {
            if (row - i >= 0 && board[row - i][col] == player) {
                ++count;
            } else {
                break;
            }
        }
        for (int i = 1; i < 5; ++i) {
            if (row + i < BOARD_SIZE && board[row + i][col] == player) {
                ++count;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return true;
        }

        // Check diagonally (top-left to bottom-right)
        count = 1;
        for (int i = 1; i < 5; ++i) {
            if (row - i >= 0 && col - i >= 0 && board[row - i][col - i] == player) {
                ++count;
            } else {
                break;
            }
        }
        for (int i = 1; i < 5; ++i) {
            if (row + i < BOARD_SIZE && col + i < BOARD_SIZE && board[row + i][col + i] == player) {
                ++count;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return true;
        }

        // Check diagonally (bottom-left to top-right)
        count = 1;
        for (int i = 1; i < 5; ++i) {
            if (row + i < BOARD_SIZE && col - i >= 0 && board[row + i][col - i] == player) {
                ++count;
            } else {
                break;
            }
        }
        for (int i = 1; i < 5; ++i) {
            if (row - i >= 0 && col + i < BOARD_SIZE && board[row - i][col + i] == player) {
                ++count;
            } else {
                break;
            }
        }
        return count >= 5;
    }

    bool isBoardFull() const {
        for (int i = 0; i < BOARD_SIZE; ++i) {
            for (int j = 0; j < BOARD_SIZE; ++j) {
                if (board[i][j] == Player::EMPTY) {
                    return false;
                }
            }
        }
        return true;
    }
};

int main() {
    GomokuGame game;
    Player currentPlayer = Player::PLAYER1;

    while (true) {
        game.printBoard();

        int row, col;
        std::cout << "Player " << (currentPlayer == Player::PLAYER1 ? "X" : "O") << "'s turn. Enter row and column (e.g., 1 2): ";
        if (!(std::cin >> row >> col) || !game.isMoveValid(row - 1, col - 1)) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid move. Try again." << std::endl;
            continue;
        }

        if (!game.makeMove(row - 1, col - 1, currentPlayer)) {
            std::cout << "Invalid move. The cell is already occupied. Try again." << std::endl;
            continue;
        }

        if (game.checkWin(row - 1, col - 1, currentPlayer)) {
            game.printBoard();
            std::cout << "Player " << (currentPlayer == Player::PLAYER1 ? "X" : "O") << " wins!" << std::endl;
            break;
        }

        if (game.isBoardFull()) {
            game.printBoard();
            std::cout << "It's a draw!" << std::endl;
            break;
        }

        currentPlayer = (currentPlayer == Player::PLAYER1) ? Player::PLAYER2 : Player::PLAYER1;
    }

    return 0;
}

这是一个简单的控制台版五子棋游戏。玩家轮流输入行和列来下棋,游戏会检查是否有玩家胜利或者平局。请注意,这只是一个基本示例,你可以根据需要进行扩展和改进。

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值