21xrx.com
2024-11-10 00:53:18 Sunday
登录
文章检索 我的文章 写文章
C++五子棋人机对战代码
2023-06-29 07:57:39 深夜i     --     --
C++ 五子棋 人机对战 代码

五子棋是一种非常经典的棋类游戏。在网络上,很多玩家会选择人机对战的方式来进行游戏。C++语言作为一种非常高效的编程语言,也可以用来开发五子棋人机对战的代码。

以下是一份C++五子棋人机对战代码,供感兴趣的读者学习与参考:


#include <iostream>

#include <stdlib.h>

#include <string.h>

using namespace std;

const int SIZE = 15; // 表示棋盘大小为15*15

const int WIN_NUM = 5; // 表示一条线上连续的五个棋子获胜

// 定义棋盘

enum ChessMan

  BLACK;

struct ChessBoard {

  ChessMan point[SIZE][SIZE];

};

// 定义位置

struct Point {

  int x;

  int y;

  Point(int x = 0, int y = 0): x(x), y(y) {}

};

// 定义游戏状态

enum GameStatus

  BLACK_WIN;

// 初始化棋盘

void InitBoard(ChessBoard& board) {

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

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

      board.point[i][j] = EMPTY;

    }

  }

}

// 打印棋盘

void PrintBoard(const ChessBoard& board) {

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

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

      switch (board.point[i][j])

        case EMPTY:

          cout << ".";

          break;

        case BLACK:

          cout << "@";

          break;

        case WHITE:

          cout << "O";

          break;

        default:

          break;

      

    }

    cout << endl;

  }

}

// 人类下棋

void HumanMove(ChessBoard& board) {

  int x, y;

  cout << "请输入您要下棋的坐标(格式为x y):";

  cin >> x >> y;

  while (board.point[x][y] != EMPTY) 请重新输入坐标:";

    cin >> x >> y;

  

  board.point[x][y] = BLACK;

}

// 电脑下棋

void ComputerMove(ChessBoard& board) {

  Point result;

  int k = 0;

  int max = 0;

  int score[SIZE][SIZE] = { 0 };

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

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

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

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

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

            if (m == 0 && n == 0)

              continue;

            

            int row = i + m;

            int col = j + n;

            while (row >= 0 && row < SIZE && col >= 0 && col < SIZE && board.point[row][col] == WHITE) {

              k++;

              row += m;

              col += n;

            }

            if (row >= 0 && row < SIZE && col >= 0 && col < SIZE && board.point[row][col] == EMPTY) {

              score[i][j] += k;

            }

            if (k > max)

              max = k;

              result.x = i;

              result.y = j;

            

            k = 0;

          }

        }

      }

    }

  }

  board.point[result.x][result.y] = WHITE;

}

// 判断是否有一方胜利

GameStatus Judge(ChessBoard& board) {

  int count = 0;

  // 判断行是否连续五子

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

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

      if (board.point[i][j] == BLACK) {

        count++;

      } else

        count = 0;

      

      if (count == WIN_NUM)

        return BLACK_WIN;

      

    }

    count = 0;

  }

  // 判断列是否连续五子

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

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

      if (board.point[i][j] == BLACK) {

        count++;

      } else

        count = 0;

      

      if (count == WIN_NUM)

        return BLACK_WIN;

      

    }

    count = 0;

  }

  // 判断左上-右下是否连续五子

  for (int i = 0; i <= SIZE - WIN_NUM; i++) {

    for (int j = 0; j <= SIZE - WIN_NUM; j++) {

      for (int k = 0; k < WIN_NUM; k++) {

        if (board.point[i + k][j + k] == BLACK) {

          count++;

        } else

          count = 0;

          break;

        

      }

      if (count == WIN_NUM)

        return BLACK_WIN;

      

      count = 0;

    }

  }

  // 判断右上-左下是否连续五子

  for (int i = 0; i <= SIZE - WIN_NUM; i++) {

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

      for (int k = 0; k < WIN_NUM; k++) {

        if (board.point[i + k][j - k] == BLACK) {

          count++;

        } else

          count = 0;

          break;

        

      }

      if (count == WIN_NUM)

        return BLACK_WIN;

      

      count = 0;

    }

  }

  // 判断行是否连续五子

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

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

      if (board.point[i][j] == WHITE) {

        count++;

      } else

        count = 0;

      

      if (count == WIN_NUM)

        return WHITE_WIN;

      

    }

    count = 0;

  }

  // 判断列是否连续五子

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

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

      if (board.point[i][j] == WHITE) {

        count++;

      } else

        count = 0;

      

      if (count == WIN_NUM)

        return WHITE_WIN;

      

    }

    count = 0;

  }

  // 判断左上-右下是否连续五子

  for (int i = 0; i <= SIZE - WIN_NUM; i++) {

    for (int j = 0; j <= SIZE - WIN_NUM; j++) {

      for (int k = 0; k < WIN_NUM; k++) {

        if (board.point[i + k][j + k] == WHITE) {

          count++;

        } else

          count = 0;

          break;

        

      }

      if (count == WIN_NUM)

        return WHITE_WIN;

      

      count = 0;

    }

  }

  // 判断右上-左下是否连续五子

  for (int i = 0; i <= SIZE - WIN_NUM; i++) {

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

      for (int k = 0; k < WIN_NUM; k++) {

        if (board.point[i + k][j - k] == WHITE) {

          count++;

        } else

          count = 0;

          break;

        

      }

      if (count == WIN_NUM)

        return WHITE_WIN;

      

      count = 0;

    }

  }

  // 判断是否平局

  bool draw = true;

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

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

      if (board.point[i][j] == EMPTY)

        draw = false;

        break;

      

    }

    if (!draw)

      break;

    

  }

  if (draw)

    return DRAW;

  

  return PLAYING;

}

// 主函数

int main() {

  ChessBoard board;

  InitBoard(board);

  PrintBoard(board);

  GameStatus status = PLAYING;

  while (status == PLAYING) {

    HumanMove(board);

    PrintBoard(board);

    status = Judge(board);

    if (status != PLAYING)

      break;

    

    ComputerMove(board);

    PrintBoard(board);

    status = Judge(board);

  }

  if (status == BLACK_WIN)

    cout << "您赢了!" << endl;

   else if (status == WHITE_WIN)

    cout << "电脑赢了!" << endl;

   else

    cout << "平局!" << endl;

  

  return 0;

}

这份代码实现了一个五子棋人机对战的简单版本。当程序运行后,它会先打印出一个初始的棋盘,然后人类玩家会以黑色棋子开始下棋,接着电脑会以白色棋子回应,双方轮流下棋,直到出现一方获胜或者平局,最后程序会显示结果并退出。

这份代码实现了五子棋的基本规则,但是也有很多可以改进的地方。比如,电脑下棋的算法还有优化的空间,比如使用更智能的遍历算法。另外,该程序还没有实现图形界面,如果想要让用户能够通过更直观的视觉效果来体验游戏,就需要更深入地了解C++的图形界面编程。

  
  

评论区

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