#define GRIDSIZE 15 #define judge_black 0 #define judge_white 1 #define grid_blank 0 #define grid_black 1 #define grid_white -1 using namespace std; int currBotColor; // 本方所执子颜色(1为黑,-1为白,棋盘状态亦同) int gridInfo[GRIDSIZE][GRIDSIZE] = { 0 }; // 先x后y,记录棋盘状态int main() { int x0, y0, x1, y1; // 分析自己收到的输入和自己过往的输出,并恢复棋盘状态 int turnID; cin >> turnID; currBotColor = grid_white; // 先假设自己是白方 for (int i = 0; i < turnID; i++) { // 根据这些输入输出逐渐恢复状态到当前回合 cin >> x0 >> y0 >> x1 >> y1; if (x0 == -1) currBotColor = grid_black; // 第一回合收到坐标是-1, -1,说明我是黑方 if (x0 >= 0) ProcStep(x0, y0, x1, y1, -currBotColor, false); // 模拟对方落子 if (i < turnID - 1) { cin >> x0 >> y0 >> x1 >> y1; if (x0 >= 0) ProcStep(x0, y0, x1, y1, currBotColor, false); // 模拟己方落子 } } int X1,Y1,X2,Y2; bool selfFirstBlack = (turnID == 1 && currBotColor == grid_black);//本方是黑方先手 /****在上方填充你的代码,决策结果(本方将落子的位置)存X1、Y1、X2、Y2中****/ // 决策结束,向平台输出决策结果 cout << X1 << ' ' << Y1 << ' ' << X2<< ' '<< Y2<< endl; return 0; }补充函数,实现六子棋下两步棋,每次堵住对方不让它赢即可

时间: 2024-03-04 15:50:25 浏览: 16
以下是实现六子棋下两步棋,每次堵住对方不让它赢的代码: // 判断是否有一方已经胜利 int judgeWin(int color) { for (int i = 0; i < GRIDSIZE; i++) { for (int j = 0; j < GRIDSIZE; j++) { if (gridInfo[i][j] == color) { // 判断横向是否连成六子 if (j + 5 < GRIDSIZE && gridInfo[i][j + 1] == color && gridInfo[i][j + 2] == color && gridInfo[i][j + 3] == color && gridInfo[i][j + 4] == color && gridInfo[i][j + 5] == color) { return true; } // 判断纵向是否连成六子 if (i + 5 < GRIDSIZE && gridInfo[i + 1][j] == color && gridInfo[i + 2][j] == color && gridInfo[i + 3][j] == color && gridInfo[i + 4][j] == color && gridInfo[i + 5][j] == color) { return true; } // 判断左上到右下是否连成六子 if (i + 5 < GRIDSIZE && j + 5 < GRIDSIZE && gridInfo[i + 1][j + 1] == color && gridInfo[i + 2][j + 2] == color && gridInfo[i + 3][j + 3] == color && gridInfo[i + 4][j + 4] == color && gridInfo[i + 5][j + 5] == color) { return true; } // 判断左下到右上是否连成六子 if (i + 5 < GRIDSIZE && j - 5 >= 0 && gridInfo[i + 1][j - 1] == color && gridInfo[i + 2][j - 2] == color && gridInfo[i + 3][j - 3] == color && gridInfo[i + 4][j - 4] == color && gridInfo[i + 5][j - 5] == color) { return true; } } } } return false; } // 生成所有可能的落子位置 vector<pair<int, int> > generate_next_moves() { vector<pair<int, int> > next_moves; for (int i = 0; i < GRIDSIZE; i++) { for (int j = 0; j < GRIDSIZE; j++) { if (gridInfo[i][j] == grid_blank) { next_moves.push_back(make_pair(i, j)); } } } return next_moves; } // 模拟落子,并判断是否胜利 bool ProcStep(int x0, int y0, int x1, int y1, int color, bool real) { if (x0 < 0 || x0 >= GRIDSIZE || y0 < 0 || y0 >= GRIDSIZE || x1 < 0 || x1 >= GRIDSIZE || y1 < 0 || y1 >= GRIDSIZE) { return false; } if (gridInfo[x0][y0] != color || gridInfo[x1][y1] != grid_blank) { return false; } if (real) { gridInfo[x0][y0] = grid_blank; gridInfo[x1][y1] = color; } if (judgeWin(color)) { if (real) { gridInfo[x1][y1] = grid_blank; gridInfo[x0][y0] = color; } return true; } if (real) { gridInfo[x1][y1] = grid_blank; gridInfo[x0][y0] = color; } return false; } // alpha-beta剪枝算法 int alpha_beta(int depth, int alpha, int beta, int color) { if (depth == 0) { return evaluate(currBotColor); } int best_score = INT_MIN; vector<pair<int, int> > next_moves = generate_next_moves(); for (auto& next_move : next_moves) { int x0 = next_move.first; int y0 = next_move.second; if (ProcStep(x0, y0, x0, y0, -color, false)) { continue; } for (auto& next_move2 : next_moves) { int x1 = next_move2.first; int y1 = next_move2.second; if (ProcStep(x0, y0, x1, y1, color, false)) { continue; } int score = -alpha_beta(depth - 1, -beta, -alpha, -color); if (score > best_score) { best_score = score; if (best_score > alpha) { alpha = best_score; } if (best_score >= beta) { ProcStep(x1, y1, x0, y0, color, false); ProcStep(x0, y0, x0, y0, -color, false); return best_score; } } ProcStep(x1, y1, x0, y0, color, false); } ProcStep(x0, y0, x0, y0, -color, false); } return best_score; } // 评估当前局面的得分 int evaluate(int color) { int score = 0; for (int i = 0; i < GRIDSIZE; i++) { for (int j = 0; j < GRIDSIZE; j++) { if (gridInfo[i][j] != grid_blank) { // 判断横向是否连成五子 if (j + 4 < GRIDSIZE && gridInfo[i][j + 1] == gridInfo[i][j] && gridInfo[i][j + 2] == gridInfo[i][j] && gridInfo[i][j + 3] == gridInfo[i][j] && gridInfo[i][j + 4] == gridInfo[i][j]) { if (gridInfo[i][j] == color) { score += 1000; } else { score -= 1000; } } // 判断纵向是否连成五子 if (i + 4 < GRIDSIZE && gridInfo[i + 1][j] == gridInfo[i][j] && gridInfo[i + 2][j] == gridInfo[i][j] && gridInfo[i + 3][j] == gridInfo[i][j] && gridInfo[i + 4][j] == gridInfo[i][j]) { if (gridInfo[i][j] == color) { score += 1000; } else { score -= 1000; } } // 判断左上到右下是否连成五子 if (i + 4 < GRIDSIZE && j + 4 < GRIDSIZE && gridInfo[i + 1][j + 1] == gridInfo[i][j] && gridInfo[i + 2][j + 2] == gridInfo[i][j] && gridInfo[i + 3][j + 3] == gridInfo[i][j] && gridInfo[i + 4][j + 4] == gridInfo[i][j]) { if (gridInfo[i][j] == color) { score += 1000; } else { score -= 1000; } } // 判断左下到右上是否连成五子 if (i + 4 < GRIDSIZE && j - 4 >= 0 && gridInfo[i + 1][j - 1] == gridInfo[i][j] && gridInfo[i + 2][j - 2] == gridInfo[i][j] && gridInfo[i + 3][j - 3] == gridInfo[i][j] && gridInfo[i + 4][j - 4] == gridInfo[i][j]) { if (gridInfo[i][j] == color) { score += 1000; } else { score -= 1000; } } } } } return score; } // 实现六子棋下两步棋,每次堵住对方不让它赢 void decide() { int x1 = -1; int y1 = -1; int x2 = -1; int y2 = -1; // 模拟对方落子 for (auto& next_move : generate_next_moves()) { int x = next_move.first; int y = next_move.second; if (ProcStep(x, y, x, y, -currBotColor, true)) { if (judgeWin(-currBotColor)) { ProcStep(x, y, x, y, -currBotColor, false); continue; } // 模拟己方落子 for (auto& next_move2 : generate_next_moves()) { int x2 = next_move2.first; int y2 = next_move2.second; if (ProcStep(x, y, x2, y2, currBotColor, true)) { if (!judgeWin(-currBotColor)) { ProcStep(x, y, x2, y2, currBotColor, false); cout << x << ' ' << y << ' ' << x2 << ' ' << y2 << endl; return; } ProcStep(x, y, x2, y2, currBotColor, false); } } ProcStep(x, y, x, y, -currBotColor, false); } } // 如果没有找到合适的位置,就随机落子 while (true) { int x = rand() % GRIDSIZE; int y = rand() % GRIDSIZE; if (gridInfo[x][y] == grid_blank) { cout << x << ' ' << y << ' ' << x << ' ' << y << endl; return; } } } int main() { int turnID; cin >> turnID; currBotColor = grid_white; for (int i = 0; i < turnID; i++) { int x0, y0, x1, y1; cin >> x0 >> y0 >> x1 >> y1; if (x0 == -1) currBotColor = grid_black; if (x0 >= 0) ProcStep(x0, y0, x1, y1, -currBotColor, true); if (i < turnID - 1) { cin >> x0 >> y0 >> x1 >> y1; if (x0 >= 0) ProcStep(x0, y0, x1, y1, currBotColor, true); } } decide(); return 0; }

相关推荐

最新推荐

recommend-type

C# #define条件编译详解

主要介绍了C# #define条件编译,告诉大家#define是用来做什么?如何使用#define,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

详解C语言中的#define宏定义命令用法

有的时候为了程序的通用性,可以使用#define预处理宏定义命令,它的具体作用就是方便程序段的定义和修改,下面就来详解C语言中的#define宏定义命令用法.
recommend-type

预编译#define_#ifdef_#endif用法

最近在看Linux底层代码,发现好多代码里有#define #ifdef #endif,找了个介绍详细的文章,供大家参考!
recommend-type

浅谈#ifndef,#define,#endif的作用和用法

下面小编就为大家带来一篇浅谈#ifndef,#define,#endif的作用和用法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

#define用法集锦.doc

The #define Directive  You can use the #define directive to give a meaningful name to a constant in your program. The two forms of the syntax are:  Syntax  #define identifier token-stringopt  #...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。