第十四周问题总结

#include "stdio.h"
#include "windows.h"
#include "time.h"

char env[200][200];
int x=99,y=99,head[2]={99,85},tail[2]={99,80};

void showstart ()
{
printf("*****************************");
printf("* *");
printf("* snake *");
printf("* press 9 to qiut !!! *");
printf("*****************************");
Sleep(1000);
System("cls");
}


void init()

{
int i,j;
for(i=0;i<200;i++)
for(j=0;j<200;j++)
{
if(i==0||i==199||j==0||j==199)
env[i][j]='#';
if(i==99&&j==99)
env[x][y]='@';
if(i=99&&(j>=80&&j<=85))
env[i][j]='*';

}

}

void show ()

{
int i,j;
for(i=0;i<200;i++)
for(j=0;j<200;j++)
{
printf("%c",env[i][j]);
}
}

void move (int der)

{
switch (der)
{
case 8:
{
if(env[head[0]-1][head[1]]=='*');
else{
if((head[0]-1)==x&&head[1]==y)
{
head[0]--;
env[head[0]][head[1]]='*';

}
head[0]--;
env[head[0]][head[1]]='*';
env[tail[0]][tail[1]]=' ';
if(env[tail[0]-1][tail[1]]=='*')
tail[0]--;
if(env[tail[0]][tail[1]-1]=='*')
tail[1]--;
if(env[tail[0]+1][tail[1]]=='*')
tail[0]++;
if(env[tail[0]][tail[1]+1]=='*')
tail[1]++;
}


}
case 2:
{

if((head[0]+1)==x&&head[1]==y)
{
head[0]++;
env[head[0]][head[1]]='*';
}
head[0]++;
env[head[0]][head[1]]='*';
env[tail[0]][tail[1]]=' ';
if(env[tail[0]-1][tail[1]]=='*')
tail[0]--;
if(env[tail[0]][tail[1]-1]=='*')
tail[1]--;
if(env[tail[0]+1][tail[1]]=='*')
tail[0]++;
if(env[tail[0]][tail[1]+1]=='*')
tail[1]++;

}
case 4:
{
if(env[head[0]][head[1]-1]=='*');
else{
if(head[0]==x&&(head[1]-1)==y)
{
head[1]--;
env[head[0]][head[1]]='*';
}
head[1]--;
env[head[0]][head[1]]='*';
env[tail[0]][tail[1]]=' ';
if(env[tail[0]-1][tail[1]]=='*')
tail[0]--;
if(env[tail[0]][tail[1]-1]=='*')
tail[1]--;
if(env[tail[0]+1][tail[1]]=='*')
tail[0]++;
if(env[tail[0]][tail[1]+1]=='*')
tail[1]++;
}

}
case 6:
{

if(head[0]==x&&(head[1]+1)==y)
{
head[1]++;
env[head[0]][head[1]]='*';
}
head[1]++;
env[head[0]][head[1]]='*';
env[tail[0]][tail[1]]=' ';
if(env[tail[0]-1][tail[1]]=='*')
tail[0]--;
if(env[tail[0]][tail[1]-1]=='*')
tail[1]--;
if(env[tail[0]+1][tail[1]]=='*')
tail[0]++;
if(env[tail[0]][tail[1]+1]=='*')
tail[1]++;


}
default:
{
if(env[head[0]-1][head[1]]=='*')
{
head[0]++;
if(head[0]==x&&head[1]==y)
env[head[0]][head[1]]='*';
head[0]++;
env[head[0]][head[1]]='*';
}
if(env[head[0]+1][head[1]]=='*')
{
head[0]--;
if(head[0]==x&&head[1]==y)
env[head[0]][head[1]]='*';
head[0]--;
env[head[0]][head[1]]='*';
}
if(env[head[0]][head[1]-1]=='*')
{
head[1]++;
if(head[0]==x&&head[1]==y)
env[head[0]][head[1]]='*';
head[1]++;
env[head[0]][head[1]]='*';
}
if(env[head[0]][head[1]+1]=='*')
{
head[1]--;
if(head[0]==x&&head[1]==y)
env[head[0]][head[1]]='*';
head[1]--;
env[head[0]][head[1]]='*';
}


env[tail[0]][tail[1]]=' ';
if(env[tail[0]-1][tail[1]]=='*')
tail[0]--;
if(env[tail[0]][tail[1]-1]=='*')
tail[1]--;
if(env[tail[0]+1][tail[1]]=='*')
tail[0]++;
if(env[tail[0]][tail[1]+1]=='*')
tail[1]++;


}
}
}

void main()
{
int der=0,sig=1;
init();
showstart ();

while (sig)
{
scanf("%d",&der);
if(der!=8&&der!=2&&der!=4&&der!=6)
der=0;
if(der==9)
sig=0;
move(der);
show();

}


}

原文地址:https://www.cnblogs.com/besti145306/p/6212439.html