// 如果没有找到合适的位置,就随机落子 while (true) { int x = rand() % GRIDSIZE; int y = rand() % GRIDSIZE; int x2 = rand() % GRIDSIZE; int y2 = rand() % GRIDSIZE; if (gridInfo[x][y] == grid_blank&&gridInfo[x2][y2] == grid_blank) { cout << x << ' ' << y << ' ' << x2 << ' ' << y2 << endl; return; } } }修改代码,随机生成两个挨着的棋子
时间: 2024-03-04 18:49:20 浏览: 15
好的,我来帮你修改代码。以下是修改后的代码:
```c++
while (true) {
int x = rand() % (GRIDSIZE - 1); // 限制 x 和 y 的范围,避免越界
int y = rand() % GRIDSIZE;
if (gridInfo[x][y] == grid_blank && gridInfo[x+1][y] == grid_blank) { // 判断两个位置是否为空
cout << x << ' ' << y << ' ' << x+1 << ' ' << y << endl; // 输出两个位置的坐标
return;
}
}
```
修改后的代码中,我们限制了生成的第一个棋子的位置,使其 x 坐标不能超过 GRIDSIZE-2,y 坐标不能超过 GRIDSIZE-1。然后判断这个位置和它右边一个位置是否都为空,如果是,则输出两个位置的坐标;否则继续随机生成位置,直到找到符合条件的位置为止。
希望这个修改后的代码能满足你的需求。如果还有什么不明白的地方,可以继续提问。
相关问题
优化该六子棋计算当前落子位置得分的函数,使其包含的情况更加丰富,下棋时不仅能堵对方,还能兼顾我方,使我方连成六子int calcScore(int x, int y) { // 给位置赋值,越往中间的分越高 int center = GRIDSIZE / 2;//棋盘中心点的位置 int score = max(abs(x-center), abs(y-center));//abs函数 对整数取绝对值 计算当前位置与棋盘中心点的距离 // 如果当前位置已经有棋子,则返回0分 if (gridInfo[x][y] != grid_blank) { return 0; } // 检测周围是否有对方的四连或五连,然后进行堵截 int oppoColor = -currBotColor; for(int i=0; i<8; i++) {//在上下左右以及斜方向分别寻找 int cnt = 0, bx = x+dx[i], by = y+dy[i]; while(inMap(bx, by) && gridInfo[bx][by] == oppoColor) { bx += dx[i]; by += dy[i]; cnt++; } if(cnt == 4) score += 500; else if(cnt == 3) score += 100; else if(cnt == 2) score += 20; else if(cnt == 1) score += 5; else if(cnt == 0) score += 1; } return score; }
为了使得计算当前落子位置得分的函数更加丰富,可以考虑增加以下情况:
1. 判断是否有我方的四连或五连,给予高分,以便下一步落子时能够延续或完成连珠。
2. 判断当前位置是否能够成为我方的三连,给予一定分数,以便下一步落子时能够延续三连。
3. 判断当前位置是否能够成为我方的二连,给予一定分数,以便下一步落子时能够延续二连。
4. 判断当前位置是否能够同时堵住对方的四连和我方的三连,给予较高分数,以达到兼顾我方和堵住对方的目的。
5. 判断当前位置是否能够同时堵住对方的四连和我方的二连,给予一定分数,以达到兼顾我方和堵住对方的目的。
修改后的代码如下:
int calcScore(int x, int y) {
int center = GRIDSIZE / 2;
int score = max(abs(x-center), abs(y-center));
if (gridInfo[x][y] != grid_blank) {
return 0;
}
int oppoColor = -currBotColor;
int myColor = currBotColor;
// 检测周围是否有对方的四连或五连,然后进行堵截
for(int i=0; i<8; i++) {
int cnt = 0, bx = x+dx[i], by = y+dy[i];
while(inMap(bx, by) && gridInfo[bx][by] == oppoColor) {
bx += dx[i];
by += dy[i];
cnt++;
}
if(cnt == 4) {
score += 500;
}
else if(cnt == 3) {
score += 100;
// 判断是否能够延续三连
int nx = bx+dx[i], ny = by+dy[i];
if(inMap(nx, ny) && gridInfo[nx][ny] == grid_blank) {
score += 50;
}
}
else if(cnt == 2) {
score += 20;
// 判断是否能够延续二连
int nx = bx+dx[i], ny = by+dy[i];
if(inMap(nx, ny) && gridInfo[nx][ny] == myColor) {
score += 10;
}
}
else if(cnt == 1) {
score += 5;
}
else if(cnt == 0) {
score += 1;
}
}
// 判断是否能够形成我方的四连或五连
for(int i=0; i<8; i++) {
int cnt = 0, bx = x+dx[i], by = y+dy[i];
while(inMap(bx, by) && gridInfo[bx][by] == myColor) {
bx += dx[i];
by += dy[i];
cnt++;
}
if(cnt == 4) {
score += 1000;
}
else if(cnt == 3) {
score += 200;
// 判断是否能够延续三连
int nx = bx+dx[i], ny = by+dy[i];
if(inMap(nx, ny) && gridInfo[nx][ny] == grid_blank) {
score += 50;
}
}
}
// 判断是否能够同时堵住对方的四连和我方的三连或二连
for(int i=0; i<8; i++) {
int cnt1 = 0, cnt2 = 0, bx = x+dx[i], by = y+dy[i];
while(inMap(bx, by) && gridInfo[bx][by] == oppoColor) {
bx += dx[i];
by += dy[i];
cnt1++;
}
bx += dx[i];
by += dy[i];
while(inMap(bx, by) && gridInfo[bx][by] == myColor) {
bx += dx[i];
by += dy[i];
cnt2++;
}
if(cnt1 == 4 && (cnt2 == 3 || cnt2 == 2)) {
score += 1000;
}
}
return score;
}
#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; }补充函数,实现六子棋下两步棋,每次堵住对方不让它赢即可
以下是实现六子棋下两步棋,每次堵住对方不让它赢的代码:
// 判断是否有一方已经胜利
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;
}
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)