21xrx.com
2025-04-02 01:56:49 Wednesday
文章检索 我的文章 写文章
C++五子棋代码
2023-06-30 18:53:41 深夜i     87     0
C++ 五子棋 代码

五子棋是一种非常流行的棋类游戏,有很多人喜欢玩。如果你也喜欢玩五子棋,并且想尝试用 C++ 来写一个五子棋程序,那么就看看下面的代码吧。

首先,我们需要定义一个棋盘的数组,这个数组的大小可以根据需要来确定。

const int BOARD_SIZE = 15; // 棋盘大小
char board[BOARD_SIZE][BOARD_SIZE]; // 棋盘数组

然后,我们需要写一个函数来初始化棋盘,将每个位置设置为空。

void initBoard() {
 for (int i = 0; i < BOARD_SIZE; i++) {
  for (int j = 0; j < BOARD_SIZE; j++) {
   board[i][j] = '+';
  }
 }
}

接下来,我们需要写一个函数来打印棋盘,让玩家可以看到当前的游戏状态。

void printBoard() {
 for (int i = 0; i < BOARD_SIZE; i++) {
  for (int j = 0; j < BOARD_SIZE; j++) {
   cout << board[i][j] << " ";
  }
  cout << endl;
 }
}

接下来,我们需要哟写一个函数来判断当前位置是否可以落子,以及判断游戏是否已经结束。

bool isValidMove(int x, int y) {
 if (board[x][y] != '+')
  return false; // 如果当前位置不为空
 // 如果当前位置为空,则可以落子,继续判断是否有五个相同连续的棋子
 int count = 0;
 // 判断横向是否有五连棋子
 for (int i = x - 4; i <= x + 4; i++) {
  if (i < 0 || i >= BOARD_SIZE)
   continue;
  
  if (board[i][y] == board[x][y]) {
   count++;
   if (count == 5)
    return true;
   
  } else
   count = 0;
  
 }
 count = 0;
 // 判断竖向是否有五连棋子
 for (int j = y - 4; j <= y + 4; j++) {
  if (j < 0 || j >= BOARD_SIZE)
   continue;
  
  if (board[x][j] == board[x][y]) {
   count++;
   if (count == 5)
    return true;
   
  } else
   count = 0;
  
 }
 count = 0;
 // 判断左上到右下的斜线是否有五连棋子
 for (int i = x - 4, j = y - 4; i <= x + 4 && j <= y + 4; i++, j++) {
  if (i < 0 || i >= BOARD_SIZE || j < 0 || j >= BOARD_SIZE)
   continue;
  
  if (board[i][j] == board[x][y]) {
   count++;
   if (count == 5)
    return true;
   
  } else
   count = 0;
  
 }
 count = 0;
 // 判断左下到右上的斜线是否有五连棋子
 for (int i = x - 4, j = y + 4; i <= x + 4 && j >= y - 4; i++, j--) {
  if (i < 0 || i >= BOARD_SIZE || j < 0 || j >= BOARD_SIZE)
   continue;
  
  if (board[i][j] == board[x][y]) {
   count++;
   if (count == 5)
    return true;
   
  } else
   count = 0;
  
 }
 return false;
}

最后,我们需要写一个函数来让玩家下棋。

void playGame() {
 int x, y;
 while (true) { // 游戏循环
  cout << "请下子(x y):" << endl;
  cin >> x >> y;
  if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE)
   cout << "坐标不合法
  if (isValidMove(x, y)) {
   board[x][y] = 'O';
   printBoard();
   cout << "恭喜你获胜了!" << endl;
   break; // 如果玩家已经取得胜利,则结束游戏
  }
  // 如果玩家没有获胜,则电脑进行下子
  int rx = rand() % BOARD_SIZE;
  int ry = rand() % BOARD_SIZE;
  while (!isValidMove(rx, ry)) {
   rx = rand() % BOARD_SIZE;
   ry = rand() % BOARD_SIZE;
  }
  board[rx][ry] = 'X';
  printBoard();
  if (isValidMove(rx, ry)) 则结束游戏
  
 }
}

将上述函数整合起来,得到我们的五子棋代码。

#include <iostream>
#include <cstdlib>
using namespace std;
const int BOARD_SIZE = 15; // 棋盘大小
char board[BOARD_SIZE][BOARD_SIZE]; // 棋盘数组
void initBoard() {
 for (int i = 0; i < BOARD_SIZE; i++) {
  for (int j = 0; j < BOARD_SIZE; j++) {
   board[i][j] = '+';
  }
 }
}
void printBoard() {
 for (int i = 0; i < BOARD_SIZE; i++) {
  for (int j = 0; j < BOARD_SIZE; j++) {
   cout << board[i][j] << " ";
  }
  cout << endl;
 }
}
bool isValidMove(int x, int y) {
 if (board[x][y] != '+')
  return false; // 如果当前位置不为空
 // 如果当前位置为空,则可以落子,继续判断是否有五个相同连续的棋子
 int count = 0;
 // 判断横向是否有五连棋子
 for (int i = x - 4; i <= x + 4; i++) {
  if (i < 0 || i >= BOARD_SIZE)
   continue;
  
  if (board[i][y] == board[x][y]) {
   count++;
   if (count == 5)
    return true;
   
  } else
   count = 0;
  
 }
 count = 0;
 // 判断竖向是否有五连棋子
 for (int j = y - 4; j <= y + 4; j++) {
  if (j < 0 || j >= BOARD_SIZE)
   continue;
  
  if (board[x][j] == board[x][y]) {
   count++;
   if (count == 5)
    return true;
   
  } else
   count = 0;
  
 }
 count = 0;
 // 判断左上到右下的斜线是否有五连棋子
 for (int i = x - 4, j = y - 4; i <= x + 4 && j <= y + 4; i++, j++) {
  if (i < 0 || i >= BOARD_SIZE || j < 0 || j >= BOARD_SIZE)
   continue;
  
  if (board[i][j] == board[x][y]) {
   count++;
   if (count == 5)
    return true;
   
  } else
   count = 0;
  
 }
 count = 0;
 // 判断左下到右上的斜线是否有五连棋子
 for (int i = x - 4, j = y + 4; i <= x + 4 && j >= y - 4; i++, j--) {
  if (i < 0 || i >= BOARD_SIZE || j < 0 || j >= BOARD_SIZE)
   continue;
  
  if (board[i][j] == board[x][y]) {
   count++;
   if (count == 5)
    return true;
   
  } else
   count = 0;
  
 }
 return false;
}
void playGame() {
 int x, y;
 while (true) { // 游戏循环
  cout << "请下子(x y):" << endl;
  cin >> x >> y;
  if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE)
   cout << "坐标不合法
  if (isValidMove(x, y)) {
   board[x][y] = 'O';
   printBoard();
   cout << "恭喜你获胜了!" << endl;
   break; // 如果玩家已经取得胜利,则结束游戏
  }
  // 如果玩家没有获胜,则电脑进行下子
  int rx = rand() % BOARD_SIZE;
  int ry = rand() % BOARD_SIZE;
  while (!isValidMove(rx, ry)) {
   rx = rand() % BOARD_SIZE;
   ry = rand() % BOARD_SIZE;
  }
  board[rx][ry] = 'X';
  printBoard();
  if (isValidMove(rx, ry)) 则结束游戏
  
 }
}
int main() {
 initBoard(); // 初始化棋盘
 printBoard(); // 输出棋盘
 playGame(); // 开始游戏
 return 0;
}

  
  

评论区