求解答问什么我的程序加了蛇身撞自己死亡的程序后运行死机了
# include <curses.h>
# include <stdlib.h>
# include <pthread.h>
struct snake
{
int hang;
int lie;
struct snake *next;
};
struct snake *head = NULL;
struct snake *tail = NULL;
int key = KEY_RIGHT;
int com;
struct snake food;
void updatefood()
{
int x = rand()%20;
int y = rand()%20;
food.hang = x;
food.lie = y;
}
int judgesnakes(int i, int j)
{
struct snake *p = head;
while(p != NULL)
{
if(p->hang == i && p->lie == j)
{
return 1;
}
else
{
p = p->next;
}
}
return 0;
}
void deleteheath()
{
struct snake *p = head;
head = head->next;
free(p);
}
void creathead()
{
while(head != NULL)
{
struct snake *p = head;
head = head->next;
free(p);
}
updatefood();
head = (struct snake*)malloc(sizeof(struct snake));
head->hang = 2;
head->lie = 2;
head->next = NULL;
tail = head;
}
void creatnewhealth1()
{
struct snake *new = (struct snake*)malloc(sizeof(struct snake));
tail->next = new;
switch(key)
{
case KEY_UP:
new->hang = tail->hang - 1;
new->lie = tail->lie;
new->next = NULL;
tail = new;
com = 1;
break;
case KEY_DOWN:
new->hang = tail->hang + 1;
new->lie = tail->lie;
new->next = NULL;
tail = new;
com = -1;
break;
case KEY_LEFT:
new->hang = tail->hang;
new->lie = tail->lie - 1;
new->next = NULL;
tail = new;
com = 2;
break;
case KEY_RIGHT:
new->hang = tail->hang;
new->lie = tail->lie + 1;
new->next = NULL;
tail = new;
com = -2;
break;
}
}
int snakefood(int a, int b)
{
if(food.hang == a && food.lie == b)
return 1;
else
return 0;
}
int judgerefresh()
{
struct snake *p = head;
if(tail->lie == 20 || tail->lie == 0 || tail->hang < 0 || tail->hang == 20)
{
return 1;
}
else
{
while(p->next != NULL)
{
if(p->hang == tail->hang && p->lie == tail->lie)
return 1;
else
p == p->next;
}
}
if(p->next == NULL)
{
return 0;
}
}
void creatnewhealth2()
{
if(judgerefresh())
{
key = KEY_RIGHT;
creathead();
creatnewhealth1();
creatnewhealth1();
}
else if(snakefood(tail->hang, tail->lie))
{
creatnewhealth1();
updatefood();
}
else
{
deleteheath();
creatnewhealth1();
}
}
void gamechart()
{
move(0, 0);
int hang;
int lie;
for(hang = 0; hang<=20; ++hang)
{
if(hang == 0 || hang == 20)
{
for(lie = 0; lie<20; ++lie)
{
printw("--");
}
printw("\n");
}
else
{
for(lie = 0; lie<=20; ++lie)
{
if(lie == 0 || lie == 20)
{
printw("|");
}
else if(judgesnakes(hang, lie))
{
printw("[]");
}
else if(snakefood(hang, lie))
{
printw("##");
}
else
{
printw(" ");
}
}
printw("\n");
}
}
}
void* startchart()
{
while(1)
{
creatnewhealth2();
gamechart();
refresh();
usleep(100000);
}
}
void gamecurses()
{
initscr();
keypad(stdscr,1);
}
void* gamekey()
{
int t;
int con;
while(1)
{
t = getch();
switch(t)
{
case KEY_UP:
con = 1;
break;
case KEY_DOWN:
con = -1;
break;
case KEY_LEFT:
con = 2;
break;
case KEY_RIGHT:
con = -2;
break;
}
if(abs(con) == abs(com))
{
t = key;
}
else
{
switch(t)
{
case KEY_UP:
key = KEY_UP;
break;
case KEY_DOWN:
key = KEY_DOWN;
break;
case KEY_LEFT:
key = KEY_LEFT;
break;
case KEY_RIGHT:
key = KEY_RIGHT;
break;
}
}
}
}
int main()
{
gamecurses();
creathead();
creatnewhealth1();
creatnewhealth1();
gamechart();
noecho();
pthread_t st1;
pthread_t st2;
pthread_create(&st1, NULL, gamekey, NULL);
pthread_create(&st2, NULL, startchart, NULL);
while(1);
endwin;
return 0;
}
运行效果
加了这个判定函数后就死机了,没加之前还能运行
int judgerefresh()
{
struct snake *p = head;
if(tail->lie == 20 || tail->lie == 0 || tail->hang < 0 || tail->hang == 20)
{
return 1;
}
else
{
while(p->next != NULL)
{
if(p->hang == tail->hang && p->lie == tail->lie)
return 1;
else
p == p->next;
}
}
if(p->next == NULL)
{
return 0;
}
}