实现tic-tac-toe的minimax算法

时间:2012-07-30 15:05:58

标签: c++ algorithm minimax

我正在尝试为tic-tac-toe游戏实现minimax算法,其中两个玩家都是人类,每次计算机使用minimax算法建议最佳移动。但它并不是每次都给出正确的建议。例如:它没有为以下场景提供正确的建议: 球员X:1 球员O:2 球员X:5。  这是我的代码:

#include <stdio.h>
#include <algorithm>  
#include <string>
using namespace std;

#define inf 1<<20
int posmax, posmin;
char board[15];

void print_board()
{
    int i;
    for (i = 1; i <= 9; i++)
    {   
        printf("%c ",board[i]);
        if (i % 3 == 0)
            printf("\n");
    }
    printf("\n");
}

int check_win(char board[])
{
    if ((board[1] == 'X' && board[2] == 'X' && board[3] == 'X') ||
        (board[4] == 'X' && board[5] == 'X' && board[6] == 'X') ||
        (board[7] == 'X' && board[8] == 'X' && board[9] == 'X') ||
        (board[1] == 'X' && board[4] == 'X' && board[7] == 'X') ||
        (board[2] == 'X' && board[5] == 'X' && board[8] == 'X') ||
        (board[3] == 'X' && board[6] == 'X' && board[9] == 'X') ||
        (board[1] == 'X' && board[5] == 'X' && board[9] == 'X') ||
        (board[3] == 'X' && board[5] == 'X' && board[7] == 'X'))
    {
        return 1;
    }
    else if((board[1] == 'O' && board[2] == 'O' && board[3] == 'O') ||
            (board[4] == 'O' && board[5] == 'O' && board[6] == 'O') ||
            (board[7] == 'O' && board[8] == 'O' && board[9] == 'O') ||
            (board[1] == 'O' && board[4] == 'O' && board[7] == 'O') ||
            (board[2] == 'O' && board[5] == 'O' && board[8] == 'O') ||
            (board[3] == 'O' && board[6] == 'O' && board[9] == 'O') ||
            (board[1] == 'O' && board[5] == 'O' && board[9] == 'O') ||
            (board[3] == 'O' && board[5] == 'O' && board[7] == 'O'))
    {
        return -1;
    }
    else return 0;
}

int check_draw(char board[])
{
    if ((check_win(board) == 0) && (board[1] != '_') && (board[2] != '_') &&
        (board[3] != '_') && (board[4] != '_') && (board[5] != '_') &&
        (board[6] != '_') && (board[7] != '_') && (board[8] != '_') &&
        (board[9] != '_'))
    {
        return 1;
    }
    else return 0;
}

int minimax(int player, char board[], int n)
{
    int i, res, j;

    int max = -inf;
    int min = inf;

    int chk = check_win(board);
    if (chk == 1)
        return 1;
    else if (chk == (-1))
        return -1;
    else if (chk = check_draw(board))
        return 0;

    for (i = 1; i <= 9; i++)
    {
        if(board[i] == '_')
        {
            if(player == 2)  
            {
                board[i] = 'O';
                res = minimax(1, board, n + 1);

                board[i] = '_';
                if(res < min)
                {
                    min = res;
                    if (n == 0)
                        posmin = i;
                }
            }
            else if (player == 1)
            {
                board[i] = 'X';
                res = minimax(2, board, n + 1);
                board[i] = '_';
                if (res > max)
                {
                    max = res;
                    if (n == 0)
                        posmax = i;
                }
            }
        }
    }

    if (player == 1)
        return max;
    else return min;    
}


// 1 is X, 2 is O
int main()
{
    int i, j, input, opt;

    for(i = 1; i <= 9; i++)
        board[i] = '_';

    printf("Board:\n");
    print_board();

    for(i = 1; i <= 9; i++)
    {
        if (i % 2 == 0)
            printf("Player O give input:\n");
        else 
            printf("Player X give input:\n");

        scanf("%d", &input);
        if (i % 2 != 0)
            board[input] = 'X';
        else
            board[input] = 'O';

        printf("Board:\n");
        print_board();

        int chk = check_win(board);
        if (chk == 1)
        {
            printf("Player X wins!\n");
            break;
        }
        else if (chk == -1)
        {
            printf("Player O wins!\n");
            break;
        }
        else if ((chk == 0) && (i != 9))
        {
            posmax = -1;
            posmin = -1;
            if(i % 2 == 0)
            {
                opt = minimax(1, board, 0);
                printf("Optimal move for player X is %d\n", posmax);
            }
            else 
            {
            opt = minimax(2, board, 0);
            printf("Optimal move for player O is %d\n", posmin);
            }
        }
        else 
            printf("The game is tied!\n");
    }
    return 0;
}

4 个答案:

答案 0 :(得分:2)

在我看来,你的程序没有给出错误的建议。如果两名球员都打出最佳状态,Minimax会计算一次移动的得分。你的情况下的得分可以是+1,-1和0,因此如果是游戏,例如不可避免地会丢失,它在失去的深度上没有区别。鉴于以下游戏状态

X O _
X _ _
_ _ _

并且玩家X的最佳游戏,玩家O移动的位置无关紧要(在任何一种情况下他都输了):

  • 在O播放7之后,X播放5,O播放6,X播放8 - > X胜利
  • 在O播放3之后,X播放7 - > X胜利

玩家X获胜。移动7给出与移动3和所有其他可玩移动相同的分数。如果您想让算法给出此示例的移动建议7,则必须将游戏深度包含在评估函数中。您可以通过将函数的返回值更改为以下内容来执行此操作:

int chk = check_win(board);
if (chk == 1)
    return (10 - n);
else if (chk == (-1))
    return -(10 - n);
else if (chk = check_draw(board))
    return 0;

答案 1 :(得分:0)

除非我正在读取main()错误,否则你只需要填充8个正方形,然后再宣布它为平局。它可能不是你正在寻找的 错误,但它是一个开始。

答案 2 :(得分:0)

我认为这(虽然编码效率低)是正确的。如果没有,请给出你认为程序错误的移动顺序。

它没有给出最短的移动顺序,这可能是你所追求的。然后,你应该重构它以返回给出最短移动序列(如果获胜)或最长移动序列(当丢失时)的移动。

答案 3 :(得分:0)

替换 printf("Optimal move for player X is %d %d\n", posmax); printf("Optimal move for player X is %d\n", posmax);

printf("Optimal move for player O is %d %d\n", posmin); printf("Optimal move for player O is %d\n", posmin);

其他一切似乎都是正确的,尽管它并不总能打出最快的胜利(如果存在胜利)。