【发布时间】:2012-11-02 06:38:14
【问题描述】:
我目前正在编写一个检查程序,它使用 alpha-beta 修剪和启发式来玩游戏。我试图在评估板时打印出板,但我遇到了让它工作的问题。我的老师给了我们运行程序所需的所有代码,减去了评估函数和 alpha-beta 修剪函数。打印棋盘的方法与寻找棋盘游戏最佳走法的方法不同。
下面是搜索函数类头的部分(它不包括所有方法调用)和我们要打印板子的 evalBoard 方法
double evalBoard1(State *state) {
int x, y;
double rval = 0.0;
double rval2 = 0.0;
evals++;
for (x = 0; x < 8; x++)
for (y = 0; y < 8; y++) {
if (x % 2 != y % 2 && !empty(state->board[y][x])) {
if (king(state->board[y][x])) { /* King */
if (((state->board[y][x] & White) && !player1)
|| (!(state->board[y][x] & White) && player1))
rval += 2.0;
else
rval2 += 2.0;
} else if (piece(state->board[y][x])) { /* Piece */
if (((state->board[y][x] & White) && !player1)
|| (!(state->board[y][x] & White) && player1))
rval += 1.0;
else
rval2 += 1.0;
}
}
}
state->PrintBoard(); //should print the board
fprintf(stderr,"Value = %g\n",rval); //prints the evaluation of that board
if(rval <= 0.0) return -10000.0;
if(rval2 <= 0.0) return 10000.0;
return rval - rval2;
}
#ifndef COMPUTER_H
#define COMPUTER_H
#define Empty 0x00
#define Piece 0x20
#define King 0x60
#define Red 0x00
#define White 0x80
#define number(x) ((x)&0x1f)
#define empty(x) ((((x)>>5)&0x03)==0?1:0)
#define piece(x) ((((x)>>5)&0x03)==1?1:0)
#define king(x) ((((x)>>5)&0x03)==3?1:0)
#define color(x) ((((x)>>7)&1)+1)
#define Clear 0x1f
typedef struct{
int player;
char board[8][8];
char movelist[48][12];
int numLegalMoves;
}State;
//all method calls occur after here
#endif
下面是 checkers.h 文件(不包括 PrintBoard() 之外的所有其他方法调用)和 checkers.c 文件(仅包括 printBoard 方法和 Struct square 调用)
struct Square square[16][16];
void PrintBoard() {
int board[8][8];
int x,y;
char ch = 127;
for(y=0; y<8; y++)
{
for(x=0; x<8; x++)
{
if(x%2 != y%2) {
if(square[y][x].state) {
if(square[y][x].col)
{
if(square[y][x].state == King) board[y][x] = 'B';
else board[y][x] = 'b';
}
else
{
if(square[y][x].state == King) board[y][x] = 'A';
else board[y][x] = 'a';
}
} else board[y][x] = ' ';
} else board[y][x] = ch;
printf("%c",board[y][x]);
}
printf("\n");
}
}
#ifndef CHECKERS_H
#define CHECKERS_H
#define Empty 0
#define Piece 1
#define King 2
#define HUMAN 1
#define COMPUTER 2
struct Square {
Widget widget;
int val;
int state;
int col;
int hilite;
};
void PrintBoard();
#endif
我试图只调用 state->PrintBoard() 但程序无法识别该调用。我还尝试将 Square 结构添加到计算机头文件中的 State 结构,但这也产生了错误。我还在 computer.c 文件中创建了一个新的 PrintBoard 方法,但它不知道每个方块中的颜色状态。任何帮助将不胜感激,如果需要,我可以发布更多代码。
【问题讨论】:
-
为什么您认为 PrintBoard 与状态相关联?这是 C 还是 C++?我觉得你很困惑。 C 中没有类或方法之类的东西。
-
我们的老师给了我们运行这个程序的所有代码,除了实际的搜索功能,但要求我们使用不同类的 PrintBoard() 方法打印出每个评估状态的板。我只是在试图弄清楚如何将该方法用于另一个需要访问 Square 结构的类时遇到问题
-
一种肮脏的方法是在任何类之外定义 PrintBoard() 函数,并在您需要的每个类中使用它。