主题:[讨论]讨论一下五子棋的估价函数吧
今天写了个五子棋的程序(穷举版),智商超弱.最后不知到是我逻辑混乱了还是输出有问题,有点bug,就不放出全部代码来了. 只讨论估价函数吧,下面是我写的value(),方法是横竖斜4个方向扫描4次棋盘,遇到活n就把总值加n^3,活n就是连着的n个相同且没有被堵的子数.但没有考虑间隔的情况和胜利等等...
/////////////////////////////////////////
enum {MAN = -1, BOT = 1};
int board[20][20];
// no special meaning :(
inline void quote (int &totv, int &n, int &last, int &i, int &j)
{
n += board[i][j];
if (board[i][j] != last) {
last = board[i][j];
totv += n*n*n;
n = last;
}
}
int value ()
{
int totv = 0, last, n;
int i, j, row, col;
// col scan
for (i = 0; i < 19; ++i) {
n = 0;
last = board[i][0];
for (j = 1; j < 20; ++j)
quote (totv, n, last, i, j);
}
// row scan
for (j = 0; j < 19; ++j) {
n = 0;
last = board[0][j];
for (i = 1; i < 19; ++i)
quote (totv, n, last, i, j);
}
// left-up to right-down slanting scan
for (col = 0; col < 17; ++col) {
n = 0;
last = board[0][col];
for (i = 1, j = col + 1; i < 20 && j < 20; ++i, ++j)
quote (totv, n, last, i, j);
}
for (row = 1; row < 17; ++row) {
n = 0;
last = board[row][0];
for (i = row + 1, j = 1; i < 20 && j < 20; ++i, ++j)
quote (totv, n, last, i, j);
}
// left-down to right-up slanting scan
for (col = 0; col < 17; ++col) {
n = 0;
last = board[18][col];
for (i = 17, j = col + 1; i >= 0 && j < 20; --i, ++j)
quote (totv, n, last, i, j);
}
for (row = 2; row < 18; ++row) {
n = 0;
last = board[row][0];
for (i = row - 1, j = 1; i >= 0 && j < 20; --i, ++j)
quote (totv, n, last, i, j);
}
return totv;
}
/////////////////////////////////////////
enum {MAN = -1, BOT = 1};
int board[20][20];
// no special meaning :(
inline void quote (int &totv, int &n, int &last, int &i, int &j)
{
n += board[i][j];
if (board[i][j] != last) {
last = board[i][j];
totv += n*n*n;
n = last;
}
}
int value ()
{
int totv = 0, last, n;
int i, j, row, col;
// col scan
for (i = 0; i < 19; ++i) {
n = 0;
last = board[i][0];
for (j = 1; j < 20; ++j)
quote (totv, n, last, i, j);
}
// row scan
for (j = 0; j < 19; ++j) {
n = 0;
last = board[0][j];
for (i = 1; i < 19; ++i)
quote (totv, n, last, i, j);
}
// left-up to right-down slanting scan
for (col = 0; col < 17; ++col) {
n = 0;
last = board[0][col];
for (i = 1, j = col + 1; i < 20 && j < 20; ++i, ++j)
quote (totv, n, last, i, j);
}
for (row = 1; row < 17; ++row) {
n = 0;
last = board[row][0];
for (i = row + 1, j = 1; i < 20 && j < 20; ++i, ++j)
quote (totv, n, last, i, j);
}
// left-down to right-up slanting scan
for (col = 0; col < 17; ++col) {
n = 0;
last = board[18][col];
for (i = 17, j = col + 1; i >= 0 && j < 20; --i, ++j)
quote (totv, n, last, i, j);
}
for (row = 2; row < 18; ++row) {
n = 0;
last = board[row][0];
for (i = row - 1, j = 1; i >= 0 && j < 20; --i, ++j)
quote (totv, n, last, i, j);
}
return totv;
}