在运行M Tim Jones'人工智能:系统方法的最佳优先搜索时,我们被要求确定下一步如何生成以及解决方案的原因采摘。找出最佳优先搜索的评估函数
为了做到这一点,两个代码段绊倒我们:
#define checkPiece(board, y)((board & (1 << (15-y))) ? 1 : 0)
#define MAX_TESTS 14
#define MAX_VECTOR 4
typedef struct {
unsigned char len;
unsigned char vector[MAX_VECTOR];
} test_t;
和
const test_t tests[MAX_TESTS]={
{ 4, { 0, 4, 8, 12 } },
{ 4, { 1, 5, 9, 13 } },
{ 4, { 2, 6, 10, 14 } },
{ 4, { 3, 7, 11, 15 } },
{ 2, { 8, 13 } },
{ 3, { 4, 9, 14 } },
{ 4, { 0, 5, 10, 15 } },
{ 3, { 1, 6, 11 } },
{ 2, { 2, 7 } },
{ 2, { 1, 4 } },
{ 3, { 2, 5, 8 } },
{ 4, { 3, 6, 9, 12 } },
{ 3, { 7, 10, 13 } },
{ 2, { 11, 14 } }
};
这两个都在EvaluateBoard功能
void evaluateBoard(node_t *node_p) {
int test, i, check;
int cost = 0;
for (test = 0 ; test < MAX_TESTS ; test++) {
check = 0;
for (i = 0 ; i < tests[test].len ; i++) {
check += checkPiece(node_p->board, tests[test].vector[i]);
}
if (check > 1) cost+= (check-1);
}
node_p->g = cost;
printf(" evaluateBoard %04x = (h %d, g %d)\n",
node_p->board, node_p->h, node_p->g);
return;
}
下一步的行动使用由Cost int确定,并且是checkPiece结果的结果。在这种情况下,电路板== 1288,但是如何从test_t tests
中选择y值?
另外,什么是结构test_t tests
?我们从未在C代码中见过类似的东西。它是一种多维数组的形式吗?
顺便说一下,当我在游戏行业工作时,我对这个东西的工作非常熟悉。虽然,我还没有读过你正在使用的书。最好的第一个搜索是一个非常容易理解的算法。如果您有任何问题,如果您的书不适合您,我可以帮助您。 –
谢谢,这个解释帮了很大忙。 – Jason