#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 11:50:25 浏览: 160
以下是实现六子棋下两步棋,每次堵住对方不让它赢的代码:
// 判断是否有一方已经胜利
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;
}
阅读全文