就尽量不要太混乱的改了一下
#ifdef _WIN32
#define P Sleep
#include <windows.h>
#else
#define P sleep
#endif
#define W 79
#define H 23
char M[H][W+1];
typedef struct n{int x,y;struct n*n;}n;
n*S=0,b;
int c,K;
w(x,y) // new node
{
n*t=malloc(sizeof(n));
return t->n=0,t->x=x,t->y=y,t;
}
a(n**h) // snake add node
{
n*t=w((*h)->x,(*h)->y);
t->n=*h,*h=t,c++,b.x=rand()%W,b.y=rand()%H;
}
f(n**h) // free snake nodes
{
(*h)->n&&(f(&(*h)->n),0),free(*h),*h=0;
}
m(n*t) // snake move
{
t->n&&(m(t->n),t->n->x=t->x,t->n->y=t->y);
}
k(n**r) // check snake status
{
n*h=*r,*t=h->n;
for(;t;t=t->n)if(!(t->x-h->x||t->y-h->y))return 0;
h->x-b.x||h->y-b.y||a(r);
return 1;
}
i() // game init
{
S&&f(&S);
S=w(W/2,H/2),a(&S),a(&S),a(&S),c=0,b.n=0,K='a';
}
p() // game print scene
{
int i;
n*t;
system("cls"),printf("%d\n",c),memset(M,' ',sizeof(M));
for(t=S;t;t=t->n)M[t->y][t->x]=48;
M[b.y][b.x]='X';
for(i=0;i<H;i++)M[i][W]=0,puts(M[i]);
}
s() // game step
{
return S&&(m(S),K=='w'&&(S->y--||(S->y+=H))||K=='a'&&(S->x--||(S->x+=W))||
K=='s'&&(++S->y-H||(S->y=0))||K=='d'&&(++S->x-W||(S->x=0)),1)?k(&S):0;
}
int main()
{
do
{
for(i();s();)p(),P(c?600/(c+1)+100:500),kbhit()&&(K=getch());
printf("Game Over! You got %d point(S).\nContinue?(Y/N)\n",c),K=getch();
}while(K=='Y'||K=='y');
f(&S);
return 0;
}
改动了红色部分...
[此贴子已经被作者于2005-1-24 0:46:42编辑过]