在DEV c++5.11下单步调试没有问题,直接运行就内存错误,怎么解决?是一个写了一半的贪吃蛇小游戏。
#include<cstdio>#include<cstdlib>#include<assert.h>#definebud4typedefstructfood{intx;...
#include<cstdio>
#include<cstdlib>
#include<assert.h>
#define bud 4
typedef struct food
{
int x;
int y;
}food;
typedef struct snake
{
int x;
int y;
int lenth;
struct snake *next;
}snake;
void creat_food(food* &now,int a[][20]);
void creat_snake(snake *&head,int a[][20]);
void initmap(int a[][20])
{
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
a[i][j]=0;
}
}
}
void creatmap_food_snake(snake*&head,food*&now,int a[][20])
{
initmap(a);
assert(a[10][10]==0);
creat_snake(head,a);
creat_food(now,a);
for(int i=0;i<bud;i++)
{
for(int j=0;j<bud;j++)
{
if(i==0||j==0||i==bud-1||j==bud-1)
printf("=");
else if(a[i][j]==1)
printf("#");
else if(a[i][j]==3)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
void creat_food(food* &now,int a[][20])
{
int x,y;
do
{
x=rand()%20;
y=rand()%20;
}while(a[x][y]);
now->x=x;
now->y=y;
a[x][y]=3;
}
void creat_snake(snake *&head,int a[][20])
{
snake*temp=head;
//printf("%d",temp->x);
while(temp)
{
a[temp->x][temp->y]=1;
temp=temp->next;
}
}
void initsnake(snake *&head,int a[][20])
{
head= new snake;
head->x=5;
head->y=5;
head->next=NULL;
}
void free_all(snake*head)
{
snake *temp=head;
while(temp)
{
head=head->next;
free(temp);
temp=head;
}
}
int main(void)
{
int a[20][20];
snake *head;
food *now;
initsnake(head,a);
assert(head->x>=0);
//you xi yunxing
//while(1)
//{
creatmap_food_snake(head,now,a);
//creatsnake();
//creatfood();
//receive_command();
//judgement();//if nextsnake
//system("CLS");
//}
free_all(head);
} 展开
#include<cstdlib>
#include<assert.h>
#define bud 4
typedef struct food
{
int x;
int y;
}food;
typedef struct snake
{
int x;
int y;
int lenth;
struct snake *next;
}snake;
void creat_food(food* &now,int a[][20]);
void creat_snake(snake *&head,int a[][20]);
void initmap(int a[][20])
{
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
a[i][j]=0;
}
}
}
void creatmap_food_snake(snake*&head,food*&now,int a[][20])
{
initmap(a);
assert(a[10][10]==0);
creat_snake(head,a);
creat_food(now,a);
for(int i=0;i<bud;i++)
{
for(int j=0;j<bud;j++)
{
if(i==0||j==0||i==bud-1||j==bud-1)
printf("=");
else if(a[i][j]==1)
printf("#");
else if(a[i][j]==3)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
void creat_food(food* &now,int a[][20])
{
int x,y;
do
{
x=rand()%20;
y=rand()%20;
}while(a[x][y]);
now->x=x;
now->y=y;
a[x][y]=3;
}
void creat_snake(snake *&head,int a[][20])
{
snake*temp=head;
//printf("%d",temp->x);
while(temp)
{
a[temp->x][temp->y]=1;
temp=temp->next;
}
}
void initsnake(snake *&head,int a[][20])
{
head= new snake;
head->x=5;
head->y=5;
head->next=NULL;
}
void free_all(snake*head)
{
snake *temp=head;
while(temp)
{
head=head->next;
free(temp);
temp=head;
}
}
int main(void)
{
int a[20][20];
snake *head;
food *now;
initsnake(head,a);
assert(head->x>=0);
//you xi yunxing
//while(1)
//{
creatmap_food_snake(head,now,a);
//creatsnake();
//creatfood();
//receive_command();
//judgement();//if nextsnake
//system("CLS");
//}
free_all(head);
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询