21xrx.com
2024-11-22 03:59:36 Friday
登录
文章检索 我的文章 写文章
C++实现五子棋游戏的代码
2023-07-14 15:52:05 深夜i     --     --
C++ 五子棋 游戏 代码 实现
col

五子棋是一种非常受众多玩家喜爱的棋类游戏,它的规则简单,容易学习,但是玩起来却趣味盎然,让人乐此不疲。今天,我们来介绍一下如何使用C++语言来实现五子棋游戏的代码。

首先,我们需要定义一个棋盘类,来表示棋盘上的状态。这个类包括一个二维数组,用来存储棋盘上每个点的状态,以及一些方法来进行判断落子是否合法和判断胜负等操作。


class ChessBoard {

public:

  static const int BOARD_SIZE = 15;

  enum class BoardState

    EMPTY = 0;

  ChessBoard() {

    // 初始化棋盘上的每个点都为空

    memset(board_, (int)BoardState::EMPTY, sizeof(board_));

  }

  BoardState GetPiece(int row, int col) const {

    // 返回坐标为(row, col)的点的状态

    return board_[row][col];

  }

  void SetPiece(int row, int col, BoardState state) {

    // 设置坐标为(row, col)的点的状态为state

    board_[row][col] = state;

  }

  bool IsValidMove(int row, int col, BoardState state) const {

    // 判断落子(row, col)是否合法

    if (row < 0 || row > BOARD_SIZE - 1 || col < 0 || col > BOARD_SIZE - 1)

      // 落子位置越界

      return false;

    

    if (board_[row][col] != BoardState::EMPTY)

      // 落子位置已有棋子

      return false;

    

    // 判断落子后是否能形成更高级别的连子

    if (!IsConnected(row, col, state))

      return false;

    

    return true;

  }

  bool IsFull() const {

    // 判断棋盘是否已经下满

    for (int i = 0; i < BOARD_SIZE; ++i) {

      for (int j = 0; j < BOARD_SIZE; ++j) {

        if (board_[i][j] == BoardState::EMPTY)

          return false;

        

      }

    }

    return true;

  }

  bool IsWin(int row, int col, BoardState state) const {

    // 判断落子后是否形成了五子连珠

    return IsHorizontalConnected(row, col, state)

      || IsVerticalConnected(row, col, state)

      || IsDiagonalConnected(row, col, state);

  }

private:

  bool IsSameState(int row, int col, int d_row, int d_col, BoardState state) const {

    // 判断坐标为(row, col)的棋子和坐标为(d_row, d_col)的棋子是否为同一状态

    if (d_row < 0 || d_row > BOARD_SIZE - 1 || d_col < 0 || d_col > BOARD_SIZE - 1)

      // 棋子位置越界

      return false;

    

    if (board_[d_row][d_col] != state)

      // 棋子状态不同

      return false;

    

    if (row == d_row && col == d_col)

      // 同一位置的棋子

      return false;

    

    return true;

  }

  bool IsConnected(int row, int col, BoardState state) const {

    // 判断落子后是否能形成更高级别的连子

    for (int i = -1; i <= 1; ++i) {

      for (int j = -1; j <= 1; ++j) {

        int d_row = row + i;

        int d_col = col + j;

        if (IsSameState(row, col, d_row, d_col, state)) {

          if (IsSameState(row, col, d_row + i, d_col + j, state)) {

            if (IsSameState(row, col, d_row + i * 2, d_col + j * 2, state))

              return true;

            

          }

        }

      }

    }

    return false;

  }

  bool IsHorizontalConnected(int row, int col, BoardState state) const {

    // 判断水平方向是否有连子

    int count = 1;

    for (int i = col - 1; i >= 0; --i) {

      if (board_[row][i] == state) {

        ++count;

      }

      else

        break;

      

    }

    for (int i = col + 1; i < BOARD_SIZE; ++i) {

      if (board_[row][i] == state) {

        ++count;

      }

      else

        break;

      

    }

    return count >= 5;

  }

  bool IsVerticalConnected(int row, int col, BoardState state) const {

    // 判断竖直方向是否有连子

    int count = 1;

    for (int i = row - 1; i >= 0; --i) {

      if (board_[i][col] == state) {

        ++count;

      }

      else

        break;

      

    }

    for (int i = row + 1; i < BOARD_SIZE; ++i) {

      if (board_[i][col] == state) {

        ++count;

      }

      else

        break;

      

    }

    return count >= 5;

  }

  bool IsDiagonalConnected(int row, int col, BoardState state) const {

    // 判断对角线方向是否有连子

    int count = 1;

    for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; --i, --j) {

      if (board_[i][j] == state) {

        ++count;

      }

      else

        break;

      

    }

    for (int i = row + 1, j = col + 1; i < BOARD_SIZE && j < BOARD_SIZE; ++i, ++j) {

      if (board_[i][j] == state) {

        ++count;

      }

      else

        break;

      

    }

    if (count >= 5)

      return true;

    

    count = 1;

    for (int i = row - 1, j = col + 1; i >= 0 && j < BOARD_SIZE; --i, ++j) {

      if (board_[i][j] == state) {

        ++count;

      }

      else

        break;

      

    }

    for (int i = row + 1, j = col - 1; i < BOARD_SIZE && j >= 0; ++i, --j) {

      if (board_[i][j] == state) {

        ++count;

      }

      else

        break;

      

    }

    return count >= 5;

  }

  BoardState board_[BOARD_SIZE][BOARD_SIZE];

};

接下来,我们需要定义一个游戏类,来控制游戏流程。这个类包括一个棋盘对象和一些方法来控制落子、切换玩家、打印棋盘等操作。


class GomokuGame {

public:

  GomokuGame()

    player_ = ChessBoard::BoardState::BLACK;

  

  ChessBoard& GetChessBoard()

    return board_;

  

  void SwitchPlayer() {

    if (player_ == ChessBoard::BoardState::BLACK)

      player_ = ChessBoard::BoardState::WHITE;

    

    else

      player_ = ChessBoard::BoardState::BLACK;

    

  }

  ChessBoard::BoardState GetPlayer() const

    return player_;

  

  void PlayOneMove(int row, int col) {

    // 在坐标为(row, col)的点落子

    board_.SetPiece(row, col, player_);

  }

  bool IsGameOver(int row, int col) const {

    // 判断游戏是否结束

    if (board_.IsWin(row, col, player_))

      return true;

    

    if (board_.IsFull())

      return true;

    

    return false;

  }

  void PrintBoard() const {

    // 打印棋盘

    std::cout << " ";

    for (int i = 0; i < ChessBoard::BOARD_SIZE; ++i) {

      std::cout << char('a' + i) << ' ';

    }

    std::cout << '\n';

    for (int i = 0; i < ChessBoard::BOARD_SIZE; ++i) {

      std::cout << i + 1 << ' ';

      for (int j = 0; j < ChessBoard::BOARD_SIZE; ++j) {

        char c = '.';

        if (board_.GetPiece(i, j) == ChessBoard::BoardState::BLACK)

          c = 'X';

        

        else if (board_.GetPiece(i, j) == ChessBoard::BoardState::WHITE)

          c = 'O';

        

        std::cout << c << ' ';

      }

      std::cout << '\n';

    }

    std::cout << std::endl;

  }

private:

  ChessBoard board_;

  ChessBoard::BoardState player_;

};

最后,我们来使用这些类来实现一个完整的五子棋游戏。


int main()

{

  GomokuGame game;

  while (true) {

    game.PrintBoard();

    std::cout << "Player " << (game.GetPlayer() == ChessBoard::BoardState::BLACK ? "X" : "O")

      << "'s turn:\n";

    std::string move;

    std::cin >> move;

    if (move == "exit")

      break;

    

    if (move.size() == 2) {

      int row = move[1] - '1';

      int col = move[0] - 'a';

      if (game.GetChessBoard().IsValidMove(row, col, game.GetPlayer())) {

        game.PlayOneMove(row, col);

        if (game.IsGameOver(row, col)) {

          game.PrintBoard();

          std::cout << "Player " << (game.GetPlayer() == ChessBoard::BoardState::BLACK ? "X" : "O")

            << " wins!\n";

          break;

        }

        game.SwitchPlayer();

      }

      else

        std::cout << "Invalid move!" << std::endl;

      

    }

    else

      std::cout << "Invalid input!" << std::endl;

    

  }

  return 0;

}

现在,我们就可以愉快地玩起这个五子棋游戏了。只需要在命令行输入落子的坐标,就可以和电脑或者其他玩家对战了。当你落下五个连续的棋子时,就赢得了比赛的胜利。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复