请把该六子棋代码优化,考虑对方眠五活五眠四活四的情况: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 == 5) { score += 100000; } else 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; } } 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 += 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; } } } // 判断是否能够同时堵住对方的四连和我方的三连或二连 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; }
时间: 2024-03-07 18:52:34 浏览: 54
java计算四舍五入,float转int,string转int做计算
首先,可以将判断对方的五连、四连和三连的代码抽取出来,形成一个公共的函数,避免重复代码。这个函数可以返回对应的分值,而不是直接修改score。类似的,可以将判断我方的五连、四连和三连的代码抽取出来,形成一个公共的函数。
其次,可以将判断是否能够延续三连的代码抽取出来,形成一个公共的函数,避免重复代码。这个函数可以返回对应的分值,而不是直接修改score。
最后,可以将判断是否能够同时堵住对方的四连和我方的三连或二连的代码抽取出来,形成一个公共的函数,避免重复代码。这个函数可以返回对应的分值,而不是直接修改score。
下面是优化后的代码:
int getScore(int cnt) {
if(cnt == 5) {
return 100000;
}
else if(cnt == 4) {
return 1000;
}
else if(cnt == 3) {
return 200;
}
else if(cnt == 2) {
return 20;
}
else if(cnt == 1) {
return 5;
}
else {
return 1;
}
}
int getExtendScore(int cnt, int color) {
if(cnt == 3) {
return 50;
}
else if(cnt == 2 && color == currBotColor) {
return 10;
}
else {
return 0;
}
}
int getDefendScore(int oppoCnt, int myCnt) {
if(oppoCnt == 4 && (myCnt == 3 || myCnt == 2)) {
return 1000;
}
else {
return 0;
}
}
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++;
}
score += getScore(cnt);
score += getExtendScore(cnt, gridInfo[bx][by]);
}
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++;
}
score += getScore(cnt);
score += getExtendScore(cnt, gridInfo[bx][by]);
}
for(int i=0; i<8; i++) {
int oppoCnt = 0, myCnt = 0, bx = x+dx[i], by = y+dy[i];
while(inMap(bx, by) && gridInfo[bx][by] == oppoColor) {
bx += dx[i];
by += dy[i];
oppoCnt++;
}
bx += dx[i];
by += dy[i];
while(inMap(bx, by) && gridInfo[bx][by] == myColor) {
bx += dx[i];
by += dy[i];
myCnt++;
}
score += getDefendScore(oppoCnt, myCnt);
}
return score;
}
阅读全文