我已經在這個論壇發佈了一個類似的問題,但是由於舊帖子有點長,我重寫了我的算法,我開始了這個新帖子。 舊的帖子可以找到here。TicTacToe的Minimax算法不能正常工作
所以我只是想實現一個極大極小算法我井字遊戲,但它被證明是相當困難,甚至試圖發現其中的錯誤天后,我找不到它。你可以在下面找到我的代碼。首先,我有幾個定義,typedef和聲明:
typedef signed char s8;
typedef unsigned char u8;
typedef s8 score;
#define STATE_00 getBoardState(0, 0)
#define STATE_01 getBoardState(0, 1)
#define STATE_02 getBoardState(0, 2)
#define STATE_10 getBoardState(1, 0)
#define STATE_11 getBoardState(1, 1)
#define STATE_12 getBoardState(1, 2)
#define STATE_20 getBoardState(2, 0)
#define STATE_21 getBoardState(2, 1)
#define STATE_22 getBoardState(2, 2)
typedef enum {
EPlayerX = 1,
EPlayerO
} EPlayer;
typedef enum {
EMinimizing = 0,
EMaximizing
} EMinMax;
static u8 g_boardState[3][3] = {
{0, 0, 0,},
{0, 0, 0,},
{0, 0, 0,},
};
這些都是其次的一些功能:
u8 getBoardState(u8 row, u8 column);
EPlayer isWon(void)
{
EPlayer winningBoards[8][3] = {
{STATE_00, STATE_01, STATE_02},
{STATE_10, STATE_11, STATE_12},
{STATE_20, STATE_21, STATE_22},
{STATE_00, STATE_10, STATE_20},
{STATE_01, STATE_11, STATE_21},
{STATE_02, STATE_12, STATE_22},
{STATE_00, STATE_11, STATE_22},
{STATE_20, STATE_11, STATE_02},
};
u8 i;
for(i=0; i<8; i++){
if((winningBoards[i][0] != 0) &&
(winningBoards[i][0] == winningBoards[i][1]) &&
(winningBoards[i][0] == winningBoards[i][2])){
return winningBoards[i][0];
}
}
return 0;
}
u8 getBoardState(u8 row, u8 column)
{
return g_boardState[row][column];
}
void setBoardState(u8 row, u8 column, u8 state)
{
g_boardState[row][column] = state;
}
u8 isDraw(void)
{
u8 i, j;
for(i=0; i<3; i++){
for(j=0; j<3; j++){
if(getBoardState(i, j) == 0){
return 0;
}
}
}
return 1;
}
void dumpTable(score table[3][3])
{
int i, j;
for(i=0; i<3; i++) {
printf("\n");
for(j=0; j<3; j++){
printf("%6i ", table[i][j]);
}
}
printf("\n");
}
EPlayer playerSwitch(EPlayer player)
{
if(player == EPlayerO) return EPlayerX;
if(player == EPlayerX) return EPlayerO;
return 0;
}
EMinMax modeSwitch(EMinMax mode)
{
if(mode == EMaximizing) return EMinimizing;
if(mode == EMinimizing) return EMaximizing;
return 0;
}
再有就是這裏所說的scoring
實際極小算法:
score scoring(EMinMax mode, EPlayer player, u8 depth)
{
score thisScore, tempScore;
if(mode == EMaximizing){
thisScore = -20;
if(isWon()) return 15-depth;
}
if(mode == EMinimizing){
thisScore = 20;
if(isWon()) return depth-15;
}
if(isDraw()){
return 0;
}
u8 i, j;
for(i=0; i<3; i++){
for(j=0; j<3; j++){
if(getBoardState(i, j) == 0){
setBoardState(i, j, player);
tempScore = scoring(modeSwitch(mode), playerSwitch(player), depth+1);
if((mode == EMaximizing) && (tempScore > thisScore)){
thisScore = tempScore;
}
if((mode == EMinimizing) && (tempScore < thisScore)){
thisScore = tempScore;
}
setBoardState(i, j, 0);
}
}
}
return thisScore;
}
最後一個函數打印在表格中的分數以及main
:
void printSocredBoards(EPlayer player)
{
score thisScore[3][3] = {
{STATE_00, STATE_01, STATE_02},
{STATE_10, STATE_11, STATE_12},
{STATE_20, STATE_21, STATE_22},
};
int i, j;
if((isWon() == 0) && (isDraw() == 0)){
for(i=0; i<3; i++){
for(j=0; j<3; j++){
if(getBoardState(i, j) == 0){
setBoardState(i, j, player);
thisScore[i][j] = scoring(EMaximizing, playerSwitch(player), 0);
setBoardState(i, j, 0);
}
}
}
}
dumpTable(thisScore);
}
int main(int argc, char **argv)
{
printSocredBoards(EPlayerO);
return 0;
}
據我所知,這個算法應該工作正常,但是它給了我一個荒謬的輸出:
7 7 7
7 0 7
7 7 7
我缺少什麼? 在此先感謝您的幫助。
getBoardState的定義是什麼? –
@ clarasoft它說下面幾行。 – Lithimlin
你在期待什麼? – 4386427