【发布时间】:2017-10-25 16:50:30
【问题描述】:
我尝试在我的四人连线游戏中实现极小极大算法。 我已经完成了评估函数和算法函数的一半。
我只是找不到“最后一个”问题的解决方案。这是我的功能:
void minimax(field f){
int i;
field c;
convert_1D_to_2D(f, c);
for(i=0;i<COLS;i++) {
if(can_throw(c, i) == 0) {
throw(f, i);
convert_1D_to_2D(f, c);
if((is_winner(c) == 0) && (is_board_full(f) == 0)) { //no winner, board not full
minimax(f);
}
else if(is_winner(c) == 1) { //there is a winner
evaluate_turn(f);
//compare evaluation
undo_turn(f);
}
else if(is_winner(c) == 0 && (is_board_full(f) == 1)) { //no winner, board full
evaluate_turn(f);
//compare evaluation
undo_turn(f);
}
}
}
该字段是一个包含 f[COLS*ROWS+1] 的数组,其中 f[0] 是深度,其他元素保存在哪些列中被抛出。 “c”-board 代表“图形”板,0 代表免费,1 代表玩家 1,2 代表玩家 2。
static int evaluate_turn(field f) {
field c;
convert_1D_to_2D(f, c);
if (((f[0] % 2) == 1) && (current_player == 1) && (is_winner(c) == 1) ) { //player 1 won, max for him || +1
return 1;
}
else if (((f[0] % 2) == 2) && (current_player == 2) && (is_winner(c) == 1) ) { //player 2 won, max for him || +1
return 1;
}
if (((f[0] % 2) == 1) && (current_player == 2) && (is_winner(c) == 1) ) { //player 2 won, counting for 1 || -1
return -1;
}
else if (((f[0] % 2) == 2) && (current_player == 1) && (is_winner(c) == 1) ) { //player 1 won, counting for 2 || -1
return -1;
}
else if ((is_board_full(f) == 1) && (is_winner(c) == 0)) { //draw || 0
return 0;
}
所以我的问题是,我想不出一个干净的解决方案来比较评估自下而上。我真的认为,我不需要引入新的数据结构(它会变得太大)。就像解决方案就在我面前,但我无法抓住它。
是否可以仅比较递归“返回”的评估?如果是,怎么做?
或者我真的需要引入一些新的更复杂的东西吗?或者也许我完全错过了什么?
谢谢!
【问题讨论】: