21xrx.com
2024-12-23 00:36:20 Monday
登录
文章检索 我的文章 写文章
C++编写的五子棋游戏代码
2023-06-24 04:27:23 深夜i     --     --
C++ 五子棋 游戏代码 编写

五子棋是一种非常经典的棋类游戏,它又名“连五”,是双方玩家通过不断下棋,先在棋盘上连成五个棋子的一方获胜。在这篇文章中,我们将分享一下使用C++编写的五子棋游戏代码。

首先,我们需要定义一个棋盘类,来表示五子棋游戏中的棋盘。这个类应该包含以下成员变量:

- 棋盘的宽度和高度

- 每个位置上的棋子状态

同时,这个类还要包含一些成员函数来执行下棋、判断胜负、以及生成棋盘等功能。这些函数应该包括:

- 下棋功能函数:每次玩家下棋时,将对应位置的棋子状态设置为玩家所属的颜色,并检查是否有一方胜利。

- 判断胜负函数:在每次下棋后,检查是否有一方已经连成五个棋子,如果有则游戏结束,胜负已分。

- 生成棋盘函数:将棋盘中每一个位置上的棋子状态打印输出。

下面是实现这些功能的C++代码:


#include <iostream>

using namespace std;

class ChessBoard {

  public:

    int width;

    int height;

    int** board; // Pointer to two-dimensional array

    ChessBoard(int w, int h) { // Constructor

      width = w;

      height = h;

      board = new int*[width];

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

        board[i] = new int[height];

      }

      reset_board(); // Initialize chessboard

    }

    ~ChessBoard() { // Destructor

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

        delete[] board[i];

      }

      delete[] board;

    }

    void reset_board() { // Rest function

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

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

          board[i][j] = 0;

        }

      }

    }

    bool is_win(int row, int col, int player) { // Winning judgment function

      int count = 1;

      // Vertical judgment

      for (int i = row + 1; i < height && board[i][col] == player; i++, count++);

      for (int i = row - 1; i >= 0 && board[i][col] == player; i--, count++);

      if (count >= 5) return true;

      // Horizontal judgment

      count = 1;

      for (int j = col + 1; j < width && board[row][j] == player; j++, count++);

      for (int j = col - 1; j >= 0 && board[row][j] == player; j--, count++);

      if (count >= 5) return true;

      // Diagonal judgment (\ direction)

      count = 1;

      for (int i = row + 1, j = col + 1; i < height && j < width && board[i][j] == player; i++, j++, count++);

      for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && board[i][j] == player; i--, j--, count++);

      if (count >= 5) return true;

      // Diagonal judgment (/ direction)

      count = 1;

      for (int i = row - 1, j = col + 1; i >= 0 && j < width && board[i][j] == player; i--, j++, count++);

      for (int i = row + 1, j = col - 1; i < height && j >= 0 && board[i][j] == player; i++, j--, count++);

      if (count >= 5) return true;

      return false;

    }

    bool put_chess(int row, int col, int player) { // Place chess function with player id as input argument

      if (board[row][col] != 0) return false; // The position has been occupied

      board[row][col] = player;

      return true;

    }

    void print_board() { // Output the current game board

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

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

          switch (board[i][j]) {

            case 0:

              cout << ".";

              break;

            case 1:

              cout << "X";

              break;

            case 2:

              cout << "O";

              break;

          }

          cout << " ";

        }

        cout << endl;

      }

    }

};

// Main function

int main() {

  ChessBoard board(15, 15); // Create a 15*15 board

  

  bool win = false;

  int player = 1; // The first player is set to 1

  while (!win) {

    board.print_board();

    int row, col;

    // Player's turn

    cout << "Player " << player << "'s turn:" << endl;

    cout << "Row: ";

    cin >> row;

    cout << "Col: ";

    cin >> col;

    if (board.put_chess(row, col, player)) { // Place chess

      // Check if the player wins

      if (board.is_win(row, col, player)) {

        cout << "Player " << player << " wins!" << endl;

        win = true;

      }

      player = player == 1 ? 2 : 1; // Switch player

    } else {

      cout << "This position has been occupied. Please place the chess again." << endl;

    }

  }

  return 0;

}

在上面的代码中,我们通过一个`ChessBoard`类来表示整个游戏的棋盘,并定义了该类的一些成员函数来实现下棋、判断胜利和生成棋盘等功能。在`main()`函数中,我们创建了一个15*15的棋盘,并设定了两个玩家,轮流下棋直到一方获胜。

总的来说,使用C++编写五子棋游戏代码相对来说并不难,只要熟悉C++的基本语法和相关库函数就可以轻松实现。当然,如果想要进一步精进自己的代码,还需要多加练习和实践。

  
  

评论区

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