• gzr2017
    了解作者
  • QT
    开发工具
  • 10KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 5
    下载次数
  • 2017-11-04 15:50
    上传日期
这是一个使用qt开发的五子棋游戏,带有人机对战的ai功能
GoBang.zip
  • GoBang
  • GoBang.pro.user
    23.7KB
  • myscene.cpp
    2.8KB
  • myscene.h
    718B
  • GoBang.pro
    1.1KB
  • mainwindow.cpp
    391B
  • core.h
    1.7KB
  • playview.cpp
    536B
  • mainwindow.h
    317B
  • main.cpp
    189B
  • core.cpp
    8KB
  • playview.ui
    1.6KB
  • mainwindow.ui
    630B
  • playview.h
    489B
内容介绍
#include "core.h" Core::Core() : Chessboard(new int *[15 + 1]), bestAIX(-1), bestAIY(-1) { for (int i = 0; i <= 15; ++i) Chessboard[i] = new int[15 + 1]{0}; Chessboard[7][7] = -1; } Core::~Core() { for (int i = 0; i <= 15; ++i) delete[] Chessboard[i]; delete[] Chessboard; } bool Core::win(int X, int Y) { int tempX(X); int tempY(Y); int Dir[4][2][2]{{{-1, 0}, {1, 0}}, {{0, -1}, {0, 1}}, {{-1, -1}, {1, 1}}, {{1, -1}, {-1, 1}}}; for (int i = 0; i < 4; ++i) { int Count = 1; for (int j = 0; j < 2; ++j) { bool Flag = true; while (Flag) { tempX = tempX + Dir[i][j][0]; tempY = tempY + Dir[i][j][1]; if (0 <= tempX && 0 <= tempY && tempX <= 15 && tempY <= 15) { //如果没有越界的话 if (Chessboard[tempX][tempY] == Chessboard[X][Y] && Chessboard[tempX][tempY] != 0 && Chessboard[tempX][tempY] != -2) { Count++; if (Count == 5) return true; } else { Flag = false; } } else //如果越界的话 Flag = false; } tempX = X; tempY = Y; } } return false; } int Core::get(int X, int Y) { return Chessboard[X][Y]; } void Core::divide(int i, int j, vector<Location> &Scare1, vector<Location> &Scare2, vector<Location> &Scare3) { int temp = cal_value(i, j); if (temp < 1000) Scare1.push_back(Location(i, j)); else if (1000 <= temp && temp < 10000) Scare2.push_back(Location(i, j)); else if (10000 <= temp) Scare3.push_back(Location(i, j)); Chessboard[i][j] = -2; } void Core::cal_range(bool IsAI, vector<Location> &Range) { //将可以走的地方加入到vector中去 //1~100 vector<Location> Scare1; //1000~10000 vector<Location> Scare2; //10000~ vector<Location> Scare3; //首先将所有的值都清空 for (int i = 0; i <= 15; ++i) { for (int j = 0; j <= 15; ++j) { if (Chessboard[i][j] == -2) Chessboard[i][j] = 0; } } int Qi; if (IsAI) Qi = -1; else if (!IsAI) Qi = 1; int Dir[4][2][2]{{{-1, 0}, {1, 0}}, {{0, -1}, {0, 1}}, {{-1, -1}, {1, 1}}, {{1, -1}, {-1, 1}}}; for (int i = 0; i <= 15; ++i) { for (int j = 0; j <= 15; ++j) { if (Chessboard[i][j] == 1 || Chessboard[i][j] == -1) { //如果这个点有棋子的话,判断这个棋子的上下左右棋子加入到行列中去 for (int k = 0; k < 4; ++k) { for (int l = 0; l < 2; ++l) { int tempX = i + Dir[k][l][0]; int tempY = j + Dir[k][l][1]; if (0 <= tempX && 0 <= tempY && tempX <= 15 && tempY <= 15) { if (Chessboard[tempX][tempY] == 0) { Chessboard[tempX][tempY] = Qi; divide(tempX, tempY, Scare1, Scare2, Scare3); } } } } } } } if (IsAI) { Range.insert(Range.end(), Scare1.begin(), Scare1.end()); Range.insert(Range.end(), Scare2.begin(), Scare2.end()); Range.insert(Range.end(), Scare3.begin(), Scare3.end()); } else if (!IsAI) { Range.insert(Range.end(), Scare3.begin(), Scare3.end()); Range.insert(Range.end(), Scare2.begin(), Scare2.end()); Range.insert(Range.end(), Scare1.begin(), Scare1.end()); } } void Core::ai_do(int &tempAIX, int &tempAIY, int Deep) { max(Deep, -10000000, 10000000); tempAIX = bestAIX; tempAIY = bestAIY; } int Core::min(int Deep, int Alpha, int Beta) { int Best = 10000000; vector<Location> Range; cal_range(false, Range); while (!Range.empty()) { if (Chessboard[Range.back().X][Range.back().Y] != -1 && Chessboard[Range.back().X][Range.back().Y] != 1) { Chessboard[Range.back().X][Range.back().Y] = 1; int V = max(Deep - 1, Alpha, Best < Beta ? Best : Beta); Chessboard[Range.back().X][Range.back().Y] = 0; if (V < Best) Best = V; if (V <= Alpha) return V; Range.pop_back(); } } return Best; } int Core::max(int Deep, int Alpha, int Beta) { if (Deep <= 0) { int Best = -10000000; vector<Location> Range; cal_range(true, Range); for (int i = 0; i < Range.size(); ++i) { Chessboard[Range[i].X][Range[i].Y] = -1; int temp = evalute(); if (temp > Best) Best = temp; Chessboard[Range[i].X][Range[i].Y] = 0; } return Best; } int Best = -10000000; vector<Location> Range; cal_range(true, Range); while (!Range.empty()) { if (Chessboard[Range.back().X][Range.back().Y] != -1 && Chessboard[Range.back().X][Range.back().Y] != 1) { Chessboard[Range.back().X][Range.back().Y] = -1; //假装下棋 int V = min(Deep - 1, Best > Alpha ? Best : Alpha, Beta); Chessboard[Range.back().X][Range.back().Y] = 0; if (V > Best) { if (Deep == 4) { bestAIX = Range.back().X; bestAIY = Range.back().Y; } Best = V; } if (V >= Beta) { if (Deep == 4) Chessboard[bestAIX][bestAIY] = -1; return V; } Range.pop_back(); } } if (Deep == 4) Chessboard[bestAIX][bestAIY] = -1; return Best; } void Core::human_do(int tempHumanX, int tempHumanY) { //人类落子 if (Chessboard[tempHumanX][tempHumanY] == 0 || Chessboard[tempHumanX][tempHumanY] == -2) Chessboard[tempHumanX][tempHumanY] = 1; } int Core::cal_value(int X, int Y) { int SumScore = 0; int tempX(X); int tempY(Y); int Dir[4][2][2]{{{-1, 0}, {1, 0}}, {{0, -1}, {0, 1}}, {{-1, -1}, {1, 1}}, {{1, -1}, {-1, 1}}}; for (int i = 0; i < 4; ++i) { int Count = 1; int Yoko = 2; for (int j = 0; j < 2; ++j) { bool Flag = true; while (Flag) { tempX = tempX + Dir[i][j][0]; tempY = tempY + Dir[i][j][1]; if (tempX != -1 && tempX != 16 && tempY != -1 && tempY != 16) { //如果没有越界的话 if (Chessboard[tempX][tempY] == Chessboard[X][Y]) { Count++; } else { if (Chessboard[tempX][tempY] == -Chessboard[X][Y]) //如果这个地方它有别的棋子 Yoko--; Flag = false; } } else { //如果这个棋子到达了边界 Yoko--; Flag = false; } } tempX = X; tempY = Y; } SumScore += rank_score(Count, Yoko); } return SumScore; } int Core::rank_score(int Count, int Yoko) { if (Count >= 5) return 100000; else { int Score; if (Count == 4) { Score = 10000; } else if (Count == 3) { Score = 1000; } else if (Count == 2) { Score = 100; } else if (Count == 1) { Score = 10; } if (Yoko == 1) { Score /= 10; } else if (Yoko == 0) { Score /= 100; } return Score; } } int Core::evalute() { int Human(0), AI(0); for (int i = 0; i <= 15; ++i) { for (int j = 0; j <= 15; ++j) { if (Chessboard[i][j] == 1) //评估每个棋子的价值 Human +=
评论
    相关推荐
    • 五子棋.zip
      Written in C + +Gobang
    • Gobang.zip
      五子棋人人与人机对战,用java实现。界面清晰。提供模式选择
    • 五子棋(GoBang) VC++ 源码
      纯手工打造五子棋Windows平台人机版,共享完整源码,方便大家交流与学习!如有问题,欢迎留言或私信交流!
    • GoBang 五子棋java源程序
      自己学习java做的五子棋源代码,运行没问题,有兴趣的下载玩下。
    • Gobang 五子棋 c++课程设计
      纯c++的五子棋程序,多文件!惊喜不容错过!
    • gobang:Java五子棋项目
      gobang Java五子棋项目
    • Gobang五子棋VC编程
      五子棋,VC++6.0编程,里面有自己写得步骤说明,非常详细。欢迎交流,wln。
    • 单机五子棋 gobang
      用MFC练习做了个五子棋 还在测试完善 先发个上来 改天把源码一并送上
    • 五子棋GoBang)VC++源码
      纯手工打造五子棋Windows 人机版,共享完整源码及详细注释,方便大家交流与学习!如大家有问题欢迎留言!
    • 五子棋 gobang
      之前发了一个五子棋,只有单机版功能,这个加上了网络联机对战。发上来露个脸先。用MFC,基于VC6实现的。还有不少Bug,算法还需修正。隔天奉上源码和文档。