-1

所以一切正常,直到第四步。然后它重置板并将 X 放在第一个框中。它出什么问题了?另外,我希望我能被引导到答案,而不仅仅是给出答案,而是为了学习。我还尝试通过切割一个部分并检查它的作用来在没有任何帮助的情况下找到答案。(不知道如何使用调试器,因为它总是给出,文件名没有调试信息)。

#include <stdio.h>
#include <stdlib.h>
int clear(void);
int displayboard(void);
int initialize(void);
int startgame(void);
int checkwin(char);
int canwin(void);
int getmove(void);
int makemove(void);
int canblock(void);
char board[3][3];
int main()
{   
    int menuchoice;
    while(1)
    {
        printf("            |\\  /| | |    /\\   |\\  |\n");
        printf("            | \\/ | | |   /--\\  | \\ |\n");
        printf("            |    | | |_ /    \\ |  \\|\n");
        printf("              Tic-tac-toe Version 1 \n\n");
        printf("                  [Main Menu]\n");
        printf("                 1 - Start game\n");
        printf("                 2 - Exit game\n");
        scanf("%d",&menuchoice);
        if(menuchoice==1)
        {
            clear();
            startgame();
        }
        else if(menuchoice==2)
        {
            exit(EXIT_SUCCESS);
        }
    }
}
/* Prints newlines to refresh the page */
int clear(void)
{
    int i;
    for(i=0;i<25;i++)
    {
        printf("\n");
    }
    return 0;
}
/* Makes the board blank */
int initialize(void)
{
    int numrow,numcol;
    numrow=0;
    numcol=0;
    while(numrow<3)
    {   
        while(numcol<3)
        {   
            board[numrow][numcol]=' ';
            numcol++;
        }
        numcol=0;
        numrow++;
    }
    return 0;
}
/* Prints the board */
int displayboard(void)
{
    printf("                 1    2    3\n\n\n");
    printf("              1   %c | %c | %c \n",board[0][0],board[0][1],board[0][2]);
    printf("                ------------- \n");
    printf("              2   %c | %c | %c  \n",board[1][0],board[1][1],board[1][2]);
    printf("                ------------- \n");
    printf("              3   %c | %c | %c  \n",board[2][0],board[2][1],board[2][2]);
    return 0;
}
/* Function that controls the game*/
int startgame(void)
{

    int movecount;
    while(1)
    {
        movecount=0;
        initialize();   
        displayboard();
        board[1][1]='X';
        clear();
        displayboard();
        getmove();
        makemove();
        movecount=3;
        while(1)
        {
            clear();
            displayboard();
            getmove();
            /* I believe the problem starts here */
            if(checkwin('O')==1)
            {
                printf("\n\t\t\tYou WIN!!!!");
                /*just to test*/
                exit(EXIT_SUCCESS);
            }
            movecount++;
            canwin(); /* Checks if it can make winning move */
            if(canblock()==0) /*if a move to block has not been made*/
            {
                makemove();
                movecount++;
            }
            else if(canblock()==1) /*if a move to block has been made*/
            {
                movecount++;
            }
            if(checkwin('X')==1)
            {
                printf("\n\t\t\tYou LOSE!!!");
                /* just to test */
                exit(EXIT_SUCCESS);
            }
            if(movecount==9)
            {
                printf("                Its a DRAW!!!");
                /*Just to test */
                exit(EXIT_SUCCESS);
            }
        }
    }
}
/* Function to check win */
int checkwin(char ltr)
{
    int rownumber,colnumber;
    for(rownumber=0;rownumber<3;rownumber++)
    {
        if(board[rownumber][0]==ltr && board[rownumber][1]==ltr && board[rownumber][2]==ltr)
        {
            return 1;
        }
    }
    for(colnumber=0;colnumber<3;colnumber++)
    {
        if(board[0][colnumber]==ltr && board[1][colnumber]==ltr && board[2][colnumber]==1)
        {
            return 1;
        }
    }
    if(board[0][0]==ltr && board[1][1]==ltr && board[2][2]==ltr)
    {
        return 1;
    }
    else if(board[0][2]==ltr && board[1][1]==ltr && board[2][0]==ltr)
    {
        return 1;
    }
    else
        return 0;
}
/* Function to get move */
int getmove(void)
{
    int colnumber,rownumber;
    printf("\n\t\t\tWhat row do you want to put your move?\n");
    scanf("%d",&rownumber);
    printf("\n\t\t\tWhat column do you want to put your move?\n");
    scanf("%d",&colnumber);
    board[rownumber-1][colnumber-1]='O';
    return 0;
}
/* Function to makemove */
int makemove(void)
{
    if(board[0][0]==' ')
    {
        board[0][0]='X';
        return 0;
    }
    else if(board[0][2]==' ')
    {
        board[0][2]='X';
        return 0;
    }
    else if(board[2][0]==' ')
    {
        board[2][0]='X';
        return 0;
    }
    else if(board[2][2]==' ')
    {
        board[2][2]='X';
        return 0;
    }
    else if(board[0][1]==' ')
    {
        board[0][1]=='X';
        return 0;
    }
    else if(board[1][0]==' ')
    {
        board[1][0]='X';
        return 0;
    }
    else if(board[1][2]==' ')
    {
        board[1][2]='X';
        return 0;
    }
    else if(board[2][1]==' ')
    {
        board[2][1]='X';
        return 0;
    }
}
/* Function to make winning move */
int canwin(void)
{
    int rownumber,colnumber;
    for(rownumber=0;rownumber<3;rownumber++)
    {
        for(colnumber=0;colnumber<3;colnumber++)
        {
            board[rownumber][colnumber]='X';
            if(checkwin('X')==1)
            {
                return 1;
            }
            board[rownumber][colnumber]=' ';
        }
    }
    return 0;
}
/* Function to block winning move */
int canblock(void)
{
    int rownumber,colnumber;
    for(rownumber=0;rownumber<3;rownumber++)
    {
        for(colnumber=0;colnumber<3;colnumber++)
        {
            board[rownumber][colnumber]='O';
            if(checkwin('O')==1)
            {
                board[rownumber][colnumber]='X';
                return 1;
            }
            else if(checkwin('O')==0)
            {
                board[rownumber][colnumber]=' ';
            }
        }
    }
    return 0;
}
4

1 回答 1

0

我认为您的canwin()功能存在问题,据我了解,您希望 AI 测试它是否可以做出成功的举动。但是看功能

int canwin(void)
{
    int rownumber,colnumber;
    for(rownumber=0;rownumber<3;rownumber++)
    {
        for(colnumber=0;colnumber<3;colnumber++)
        {
            board[rownumber][colnumber]='X'; // <--- Problem here
            if(checkwin('X')==1)
            {
                return 1;
            }
            board[rownumber][colnumber]=' ';
        }
    }
    return 0;
}

你实际上是用 覆盖你的棋盘的当前位置'X',而不检查那里是否已经有一个符号!因此,您实际上是在用'X'es 覆盖您的棋盘,检查 X-player 是否赢了,如果没有“重置”到空白空间' '

您可以通过在写入之前插入支票来解决此问题,如下所示:

if (board[rownumber][colnumber] == ' ')
{
    board[rownumber][colnumber]='X';
    ...
于 2012-04-14T12:24:21.880 回答