21xrx.com
2024-12-22 21:32:28 Sunday
登录
文章检索 我的文章 写文章
C++五子棋游戏的源代码
2023-06-25 07:09:46 深夜i     --     --
C++ 五子棋 游戏 源代码

五子棋是一种智力游戏,也是程序员们最喜欢挑战的游戏之一。C++是一种高级编程语言,用于开发各种应用程序。在这篇文章中,我们将学习如何使用C++编写五子棋游戏的源代码。

五子棋游戏通常由两个玩家玩,一位是黑子,另一位是白子。游戏板通常是一个 15x15 的网格,玩家轮流放置棋子。当一个玩家的五个棋子相连时,游戏就会结束。该玩家被视为胜者。

下面是五子棋游戏的C++源代码:

#include

using namespace std;

// 定义棋盘的大小

#define BOARD_SIZE 15

// 定义棋子的类型

#define EMPTY 0

#define BLACK 1

#define WHITE 2

// 存储棋盘上各个位置的棋子类型

int board[BOARD_SIZE][BOARD_SIZE];

// 初始化棋盘

void init_board() {

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

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

      board[i][j] = EMPTY;

    }

  }

}

// 打印棋盘

void print_board() {

  cout << " ";

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

    cout << i+1 << " ";

  }

  cout << endl;

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

    cout << i+1 << " ";

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

      switch (board[i][j])

        case EMPTY: cout << ". "; break;

        case BLACK: cout << "X "; break;

        case WHITE: cout << "O "; break;

        default: break;

    }

    cout << endl;

  }

}

// 判断某一个位置是否在棋盘内

bool is_valid_pos(int row, int col)

  return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;

// 判断是否有一方胜出

int who_win() {

  // 横向寻找

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

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

      if (board[i][j] != EMPTY && board[i][j] == board[i][j+1] && board[i][j] == board[i][j+2] && board[i][j] == board[i][j+3] && board[i][j] == board[i][j+4]) {

        return board[i][j];

      }

    }

  }

  // 纵向寻找

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

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

      if (board[i][j] != EMPTY && board[i][j] == board[i+1][j] && board[i][j] == board[i+2][j] && board[i][j] == board[i+3][j] && board[i][j] == board[i+4][j]) {

        return board[i][j];

      }

    }

  }

  // 左上到右下寻找

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

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

      if (board[i][j] != EMPTY && board[i][j] == board[i+1][j+1] && board[i][j] == board[i+2][j+2] && board[i][j] == board[i+3][j+3] && board[i][j] == board[i+4][j+4]) {

        return board[i][j];

      }

    }

  }

  // 右上到左下寻找

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

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

      if (board[i][j] != EMPTY && board[i][j] == board[i+1][j-1] && board[i][j] == board[i+2][j-2] && board[i][j] == board[i+3][j-3] && board[i][j] == board[i+4][j-4]) {

        return board[i][j];

      }

    }

  }

  // 没有人胜出

  return EMPTY;

}

// 下棋

void make_move(int row, int col, int chess) {

  board[row][col] = chess;

}

// 回退一步

void undo_move(int row, int col) {

  board[row][col] = EMPTY;

}

// 人机对弈

void man_machine_game() {

  int chess, row, col;

  init_board();

  print_board();

  // 选择黑子或白子

  cout << "请选择你要执的棋子(1:黑子,2:白子):";

  cin >> chess;

  // 人先行

  if (chess == BLACK) {

    while (true) {

      // 人下棋

      cout << "请输入要下的棋子坐标(行 列):";

      cin >> row >> col;

      row--; col--;

      if (is_valid_pos(row, col) && board[row][col] == EMPTY) {

        make_move(row, col, chess);

        print_board();

        if (who_win() == chess)

          cout << "恭喜你

        chess = 3 - chess; // 切换到机器方

      } else

        cout << "输入坐标不合法

      // 机器下棋

      cout << "请稍等,机器正在思考..." << endl;

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

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

          if (board[i][j] == EMPTY) {

            make_move(i, j, chess);

            if (who_win() == chess) {

              cout << "机器:(" << i+1 << "," << j+1 << ")" << endl;

              print_board();

              cout << "很遗憾,你输了!" << endl;

              return;

            }

            undo_move(i, j);

          }

        }

      }

      while (true) {

        row = rand() % BOARD_SIZE;

        col = rand() % BOARD_SIZE;

        if (board[row][col] == EMPTY) {

          make_move(row, col, chess);

          cout << "机器:(" << row+1 << "," << col+1 << ")" << endl;

          print_board();

          if (who_win() == chess) 你输了!" << endl;

            return;

          chess = 3 - chess; // 切换到人方

          break;

        }

      }

    }

  } else {

    while (true) {

      // 人下棋

      cout << "请稍等,机器正在思考..." << endl;

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

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

          if (board[i][j] == EMPTY) {

            make_move(i, j, chess);

            if (who_win() == chess) {

              cout << "机器:(" << i+1 << "," << j+1 << ")" << endl;

              print_board();

              cout << "很遗憾,你输了!" << endl;

              return;

            }

            undo_move(i, j);

          }

        }

      }

      while (true) {

        cout << "请输入要下的棋子坐标(行 列):";

        cin >> row >> col;

        row--; col--;

        if (is_valid_pos(row, col) && board[row][col] == EMPTY) {

          make_move(row, col, chess);

          cout << "你:(" << row+1 << "," << col+1 << ")" << endl;

          print_board();

          if (who_win() == chess) 你赢了!" << endl;

            return;

          chess = 3 - chess; // 切换到机器方

          break;

        } else 请重新输入!" << endl;

      }

      // 机器下棋

      cout << "请稍等,机器正在思考..." << endl;

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

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

          if (board[i][j] == EMPTY) {

            make_move(i, j, chess);

            if (who_win() == chess) {

              cout << "机器:(" << i+1 << "," << j+1 << ")" << endl;

              print_board();

              cout << "很遗憾,你输了!" << endl;

              return;

            }

            undo_move(i, j);

          }

        }

      }

      while (true) {

        row = rand() % BOARD_SIZE;

        col = rand() % BOARD_SIZE;

        if (board[row][col] == EMPTY) {

          make_move(row, col, chess);

          cout << "机器:(" << row+1 << "," << col+1 << ")" << endl;

          print_board();

          if (who_win() == chess) 你输了!" << endl;

            return;

          chess = 3 - chess; // 切换到人方

          break;

        }

      }

    }

  }

}

// 主函数

int main() {

  man_machine_game(); // 人机对弈

  return 0;

}

以上代码是人机对弈模式的源代码,如果你需要写其他的代码模式,请根据自己的需求进行改写。

  
  

评论区

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