quark 发表于 2023-3-4 16:18:10

Mr.屎壳螂写的贪食蛇snakemove()函数有疑问,急,拜托,谢谢

如题,求助各位大佬,也求助@Mr.屎壳螂,拜托,谢谢啦。
下列snakemove()函数中,加深、斜体、下划线的while的条件部分,为什么不是
                         while(q->next!=NULL)
                        {
                              Pos(q->x,q->y);
                              printf("■");
                              q=q->next;      
                        }
而是               while(q->next->next!=NULL)
                        {
                              Pos(q->x,q->y);
                              printf("■");
                              q=q->next;      
                        }
求解惑,拜托了,谢谢。。。
void snakemove()//蛇前进,上U,下D,左L,右R
{
      cantcrosswall();
      snake *nexthead;
      nexthead=(snake*)malloc(sizeof(snake));
      if(status==U)
      {
                nexthead->x=head->x;
                nexthead->y=head->y-1;
                if(nexthead->x==food->x && nexthead->y==food->y)//如果下一个有食物//
                {
                        nexthead->next=head;
                        head=nexthead;
                        q=head;
                        while(q!=NULL)
                        {
                              Pos(q->x,q->y);
                              printf("■");
                              q=q->next;
                        }
                        score=score+add;
                        createfood();
                }
                else                                             //如果没有食物//
                {
                        nexthead->next=head;
                        head=nexthead;
                        q=head;
                        while(q->next->next!=NULL)
                        {
                              Pos(q->x,q->y);
                              printf("■");
                              q=q->next;      
                        }
                        Pos(q->next->x,q->next->y);
                        printf("");
                        free(q->next);
                        q->next=NULL;
                }
      }
      if(status==D)
      {
                nexthead->x=head->x;
                nexthead->y=head->y+1;
                if(nexthead->x==food->x && nexthead->y==food->y)//有食物
                {
                        nexthead->next=head;
                        head=nexthead;
                        q=head;
                        while(q!=NULL)
                        {
                              Pos(q->x,q->y);
                              printf("■");
                              q=q->next;
                        }
                        score=score+add;
                        createfood();
                }
                else                               //没有食物
                {
                        nexthead->next=head;
                        head=nexthead;
                        q=head;
                        while(q->next->next!=NULL)
                        {
                              Pos(q->x,q->y);
                              printf("■");
                              q=q->next;      
                        }
                        Pos(q->next->x,q->next->y);
                        printf("");
                        free(q->next);
                        q->next=NULL;
                }
      }
      if(status==L)
      {
                nexthead->x=head->x-2;
                nexthead->y=head->y;
                if(nexthead->x==food->x && nexthead->y==food->y)//有食物
                {
                        nexthead->next=head;
                        head=nexthead;
                        q=head;
                        while(q!=NULL)
                        {
                              Pos(q->x,q->y);
                              printf("■");
                              q=q->next;
                        }
                        score=score+add;
                        createfood();
                }
                else                              //没有食物
                {
                        nexthead->next=head;
                        head=nexthead;
                        q=head;
                        while(q->next->next!=NULL)
                        {
                              Pos(q->x,q->y);
                              printf("■");
                              q=q->next;      
                        }
                        Pos(q->next->x,q->next->y);
                        printf("");
                        free(q->next);
                        q->next=NULL;
                }
      }
      if(status==R)
      {
                nexthead->x=head->x+2;
                nexthead->y=head->y;
                if(nexthead->x==food->x && nexthead->y==food->y)//有食物
                {
                        nexthead->next=head;
                        head=nexthead;
                        q=head;
                        while(q!=NULL)
                        {
                              Pos(q->x,q->y);
                              printf("■");
                              q=q->next;
                        }
                        score=score+add;
                        createfood();
                }
                else                                       //没有食物
                {
                        nexthead->next=head;
                        head=nexthead;
                        q=head;
                        while(q->next->next!=NULL)
                        {
                              Pos(q->x,q->y);
                              printf("■");
                              q=q->next;      
                        }
                        Pos(q->next->x,q->next->y);
                        printf("");
                        free(q->next);
                        q->next=NULL;
                }
      }
      if(biteself()==1)       //判断是否会咬到自己
      {
                endgamestatus=2;
                endgame();
      }
}

quark 发表于 2023-3-7 19:58:20

自顶

jhq999 发表于 2023-3-8 08:18:28

本帖最后由 jhq999 于 2023-3-8 08:19 编辑

我以为

                        nexthead->next=head;
                        head=nexthead;
                        q=head;
                        while(q->next->next!=NULL)
                        {
                              Pos(q->x,q->y);
                              printf("■");
                              q=q->next;      
                        }
                        Pos(q->next->x,q->next->y);
                        printf("");
                        free(q->next);////////如果用q->next条件,这里就应该释放q。释放q以后
                        q->next=NULL;/////////它前面的节点的next怎么赋值为NULL?

页: [1]
查看完整版本: Mr.屎壳螂写的贪食蛇snakemove()函数有疑问,急,拜托,谢谢