使用二叉搜索树的Tic-Tac-Toe(C)

使用二叉搜索树的Tic-Tac-Toe(C),c,pointers,struct,C,Pointers,Struct,我已经在C中使用二进制搜索树实现了Tic-Tac-Toe。但不幸的是,它没有运行 .exe文件停止工作 另一方面,代码成功编译。 如果有人能指出错误,我们将不胜感激 代码如下: #include<stdio.h> #include<stdlib.h> int x,win=0,gcursor=1,count=0,j=10; char ch; struct node { struct node *left; int cell; char data;

我已经在C中使用二进制搜索树实现了Tic-Tac-Toe。但不幸的是,它没有运行

.exe文件停止工作

另一方面,代码成功编译。 如果有人能指出错误,我们将不胜感激

代码如下:

#include<stdio.h>
#include<stdlib.h>

int x,win=0,gcursor=1,count=0,j=10;
char ch;

struct node
{
    struct node *left;
    int cell;
    char data;
    struct node *right;
};

void insert(struct node **sr, int num)
{
    if(*sr==NULL)
    {
        *sr = (struct node*)calloc(1,sizeof(struct node));
        (*sr)->left = NULL;
        (*sr)->cell = num;
        (*sr)->data = ' ';
        (*sr)->right = NULL;
    }

    else
    {
        if(num<(*sr)->cell)
        insert(&((*sr)->left),num);
        else
        insert(&((*sr)->right),num);
    }
}

void inorder(struct node *t,int pos)
{
    if(t!=NULL)
    {
        inorder(t->left,pos);
        if(t->cell==pos)
        {
        ch = t->data;
        return; 
        }
            inorder(t->right,pos);
    }
}

void display(struct node *t,int cursor)
{
    char arr[10],i;
    system("cls");
    for(i=1;i<=9;i++)
    {
        inorder(t,i);
        arr[i] = ch;
    }
    printf("\n\nTIC TAC TOE");
    if(cursor==1)
    printf("\n-------------\n| %c.| %c | %c |\n-------------\n| %c | %c | %c |\n-------------\n| %c | %c | %c |\n-------------\n",arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8],arr[9]);
    else if(cursor==2)
    printf("\n-------------\n| %c | %c.| %c |\n-------------\n| %c | %c | %c |\n-------------\n| %c | %c | %c |\n-------------\n",arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8],arr[9]);
    else if(cursor==3)
    printf("\n-------------\n| %c | %c | %c.|\n-------------\n| %c | %c | %c |\n-------------\n| %c | %c | %c |\n-------------\n",arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8],arr[9]);
    else if(cursor==4)
    printf("\n-------------\n| %c | %c | %c |\n-------------\n| %c.| %c | %c |\n-------------\n| %c | %c | %c |\n-------------\n",arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8],arr[9]);
    else if(cursor==5)
    printf("\n-------------\n| %c | %c | %c |\n-------------\n| %c | %c.| %c |\n-------------\n| %c | %c | %c |\n-------------\n",arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8],arr[9]);
    else if(cursor==6)
    printf("\n-------------\n| %c | %c | %c |\n-------------\n| %c | %c | %c.|\n-------------\n| %c | %c | %c |\n-------------\n",arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8],arr[9]);
    else if(cursor==7)
    printf("\n-------------\n| %c | %c | %c |\n-------------\n| %c | %c | %c |\n-------------\n| %c.| %c | %c |\n-------------\n",arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8],arr[9]);
    else if(cursor==8)
    printf("\n-------------\n| %c | %c | %c |\n-------------\n| %c | %c | %c |\n-------------\n| %c | %c.| %c |\n-------------\n",arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8],arr[9]);
    else if(cursor==9)
    printf("\n-------------\n| %c | %c | %c |\n-------------\n| %c | %c | %c |\n-------------\n| %c | %c | %c.|\n-------------\n",arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8],arr[9]);
    else
    printf("\n-------------\n| %c | %c | %c |\n-------------\n| %c | %c | %c |\n-------------\n| %c | %c | %c |\n-------------\n",arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8],arr[9]);
}


void winner(struct node *t)
{
    int arr[][3] = {{1,2,3},{4,5,6},{7,8,9},{1,5,9},{3,5,7},{1,4,7},{2,5,8},{3,6,9}};
    int i;
    char x1,y1,z1;
    win = 0;
    for(i=0;i<=7;i++)
    {
        inorder(t,arr[i][0]); x1 = ch;
        inorder(t,arr[i][1]); y1 = ch;
        inorder(t,arr[i][2]); z1 = ch;
        if(x1==y1 && y1==z1 && x1!=' ' && y1!=' ' && z1!=' ')
        { win = 1;break;}
    }
}

void check(struct node *t,int pos,char c,char c2)
{   

        if(t!=NULL)
    {
        check(t->left,pos,c,c2);
        if(t->cell==pos && t->data==' ' && c==c2)
        {
        t->data = c;
        count = 1;
        return; 
        }
        check(t->right,pos,c,c2);
    }
}


void human_being(struct node *t, char human)
{
    char c;
    count = 0;
    while(count<1)
    {
        if(gcursor==10)
        gcursor = 1;

        display(t,gcursor);
        scanf("%c",&c);

        if(c=='\n')
        gcursor++;

        else
        check(t,gcursor,c,human);
        j++;
    }
}

void computer(struct node *t, char comp)
{
    int random,k;
    char c;
    count = 0;
    while(count<1)
    {
        k = rand()%j++;
        if(k!=0)
        {
        random = rand()%k;
        gcursor = random;
        }
        check(t,gcursor,comp,comp); 
    }
}


void main()
{
    char human,comp;
    struct node *t;
    int i,arr[] = {5,3,4,2,1,7,6,8,9};
    for(i=0;i<9;i++)
    insert(&t,arr[i]);
    printf("\n\n------------------------------RULES-----------------------------\n1. Press ENTER to move the cursor '.' around the squares.\n2. Press X or O as per your choice and then again press enter.\n3. X or O should be in capital.");
    printf("\n\n\n------------------------CONFIGURATION-------------------------\n1. Enter O and press enter if you want to start first.\n2. Enter X and press enter if you want computer to start first.\nEnter your choice :- ");
    scanf("%c",&human);
    if(human=='O')
    comp = 'X';
    else if(human=='X')
    comp = 'O';



                if(human=='O')
                for(i=1;i<=9;i++)
                {
                    if(i%2!=0)
                    {
                    human_being(t,human);
                    winner(t);
                    if(win==1)
                    {display(t,0); printf("\nGame Over....Winner Is Human Being."); break; }
                    }
                    else
                    {
                    computer(t,comp);
                    winner(t);
                    if(win==1)
                    { display(t,0); printf("\nGame Over....Winner Is Computer."); break; }
                    }
                }

                else if(human=='X')
                for(i=1;i<=9;i++)
                {
                    if(i%2==0)
                    {
                    human_being(t,human);
                    winner(t);
                    if(win==1)
                    { display(t,0); printf("\nGame Over....Winner Is Human Being."); break; }
                    }
                    else
                    {
                    computer(t,comp);
                    winner(t);
                    if(win==1)
                    { display(t,0); printf("\nGame Over....Winner Is Computer."); break; }
                    }
                }

                if(win==0)
                {display(t,0); printf("\nGame Over....Match Draw.");
    }
getch();
}
#包括
#包括
int x,win=0,gcursor=1,count=0,j=10;
char ch;
结构节点
{
结构节点*左;
int细胞;
字符数据;
结构节点*右;
};
空插入(结构节点**sr,int num)
{
如果(*sr==NULL)
{
*sr=(结构节点*)calloc(1,sizeof(结构节点));
(*sr)->左=空;
(*sr)->cell=num;
(*sr)->数据='';
(*sr)->right=NULL;
}
其他的
{
if(numcell)
插入(&((*sr)->左),数值;
其他的
插入(&((*sr)->右),num;
}
}
无效索引顺序(结构节点*t,整数位置)
{
如果(t!=NULL)
{
顺序(t->左,位置);
如果(t->cell==pos)
{
ch=t->data;
返回;
}
顺序(t->右,位置);
}
}
无效显示(结构节点*t,int光标)
{
char-arr[10],i;
系统(“cls”);
对于(i=1;icell==pos&&t->data==''&&c==c2)
{
t->data=c;
计数=1;
返回;
}
检查(t->右侧,位置,c,c2);
}
}
无效人类(结构节点*t,字符人类)
{
字符c;
计数=0;

而(count由于访问冲突-访问未分配的内存地址,
问题是在
insert
中,您验证
*sr
不是
NULL
,并且在第一次运行时它确实不是
NULL
——它等于
t
,在
main
中声明

它没有初始化,所以它的值无效-它是
0xCCCC
,可能不是一个有效地址。

步骤1:打开调试器并逐步执行。我们需要查看堆积如山的代码,因此您需要缩小到有实际问题的部分。它在代码的哪一点中断?在批准时插入打印为了找出答案,请稍晚。我运行@NeoNote后,注意:您正在使用
scanf(“%c”,…);
不止一次:后续使用将读取上一次输入缓冲区中留下的换行符。如果您依赖于用户键入有效输入,则会出错。您需要添加空格,例如
scanf(“%c”,…);
请参见始终严格检查用户输入。请参见