文字バージョンのヘビの設計とアルゴリズムを貪る
18491 ワード
ヘビを貪るとお金を食べて体が長くなり、壁にぶつかったり、自分が死んだりしてmainの中にIF not die setfood move eat END IF gameoverがあるか、setfoodの中にWHILE food isn’t exsit IF somewhere is not snake or border change this place to¥END IF END WHILEそしてmove get direction case W:head up and other part follow case A:head up and other part follow case S: head up and other part follow case D: head up and other part follow
eat food not exsit grow
eat food not exsit grow
#include
#include
#include
#include
#define SNAKE_MAX_LENGTH 20
#define SNAKE_HEAD 'H'
#define SNAKE_BODY 'X'
#define BLANK_CELL ' '
#define WALL_CELL '*'
#define SNAKE_FOOD '$'
char map[12][12] =
{ "************",
"* *",
"* *",
"* * *",
"* *",
"* * *",
"* *",
"* *",
"* *",
"* * *",
"* *",
"************"
};
int sy[SNAKE_MAX_LENGTH] = { 1,2,3,4,5 };
int sx[SNAKE_MAX_LENGTH] = { 1,1,1,1,1 };
int snakelen = 5;
int foodnum = 0;
int fx, fy;
void initial(void)
{
int i,j;
for(i = 0; i < snakelen; i ++)
{
map[sx[i]][sy[i]] = ' ';
}
}
void printmap(void)
{
system("cls");
map[sx[snakelen - 1]][sy[snakelen - 1]] = 'H';
int i, j;
for(i = 0; i < snakelen - 1; i ++)
{
map[sx[i]][sy[i]] = 'X';
}
for(i = 0; i < 12; i ++)
{
for(j = 0; j < 12; j ++)
{
printf("%c", map[i][j]);
}
printf("
");
}
initial();
}
void snakemove(char ch)
{
int i;
printf("
");
for(i = 0; i < snakelen - 1; i ++)
{
sx[i] = sx[i + 1];
sy[i] = sy[i + 1];
}
switch(ch)
{
case 'W':
sx[snakelen - 1] = sx[snakelen - 1] - 1;break;
case 'A':
sy[snakelen - 1] = sy[snakelen - 1] - 1;break;
case 'S':
sx[snakelen - 1] = sx[snakelen - 1] + 1;break;
case 'D':
sy[snakelen - 1] = sy[snakelen - 1] + 1;
}
int j;
printmap();
}
int gameover(void)
{
int i, j;
if(sy[snakelen - 1] == 0 || sy[snakelen - 1] == 11 || sx[snakelen - 1] == 0 || sx[snakelen - 1] ==11)
{
return 0;
}
for(j = 0; j < snakelen - 1; j ++)
{
if(sx[snakelen - 1] == sx[j] && sy[snakelen - 1] == sy[j])return 0;
}
return 1;
}
void makefood(void)
{
int i;
for(i = 0; i < snakelen; i ++)
{
srand((int)time(0));
fx = (int)(rand()%10) + 1;
fy = (int)(rand()%10) + 1;
if(fx == sx[i] && fy == sy[i])
continue;
}
map[fx][fy] = '$';
foodnum = 1;
}
void grow(void)
{
snakelen += 1;
int i;
for(i = snakelen - 1; i > 0; i --)
{
sx[i] = sx[i - 1];
sy[i] = sy[i - 1];
}
}
void eat(void)
{
if(sx[snakelen - 1] == fx && sy[snakelen - 1] == fy)
{
foodnum = 0;
grow();
}
}
int JDZ(int x)//jueduizhi
{
return x >= 0 ? x : -x;
}
int findmin(const int x, const int y, const int z, const int a)
{
int temp1 = x < y ? x : y;
int temp2 = z < a ? z : a;
int temp3 = temp1 < temp2 ? temp1 : temp2;
if(temp3 == x)return 0;
else if(temp3 == y)return 1;
else if(temp3 == z)return 2;
else if(temp3 == a)return 3;
}
char wheretogo(void)
{
while(1)
{
char moveable[5];
moveable[0] = 'W';
moveable[1] = 'A';
moveable[2] = 'S';
moveable[3] = 'D';
moveable[4] = '\0';
int dist[4] = {0,0,0,0};
dist[0] = JDZ(sx[snakelen - 1] - 1 - fx) + JDZ(sy[snakelen - 1] - fy);
if(map[sx[snakelen - 1] - 1][sy[snakelen - 1]] == 'X' || map[sx[snakelen - 1] - 1][sy[snakelen - 1]] == '*')
dist[0] = 9999;
dist[1] = JDZ(sx[snakelen - 1] - fx) + JDZ(sy[snakelen - 1] - 1 - fy);
if(map[sx[snakelen - 1]][sy[snakelen - 1] - 1] == 'X' || map[sx[snakelen - 1]][sy[snakelen - 1] - 1] == '*')
dist[0] = 9999;
dist[2] = JDZ(sx[snakelen - 1] + 1 - fx) + JDZ(sy[snakelen - 1] - fy);
if(map[sx[snakelen - 1] + 1][sy[snakelen - 1]] == 'X' || map[sx[snakelen - 1] + 1][sy[snakelen - 1]] == '*')
dist[0] = 9999;
dist[3] = JDZ(sx[snakelen - 1] - fx) + JDZ(sy[snakelen - 1] + 1 - fy);
if(map[sx[snakelen - 1]][sy[snakelen - 1] + 1] == 'X' || map[sx[snakelen - 1]][sy[snakelen - 1] + 1] == '*')
dist[0] = 9999;
char ch = moveable[findmin(dist[0], dist[1], dist[2], dist[3])];
if(foodnum == 0)
{
srand((int)time(0));
int a = (int)(rand()%10) + 1;
int b = (int)(rand()%10) + 1;
int c = (int)(rand()%10) + 1;
int d = (int)(rand()%10) + 1;
ch = moveable[findmin(a,b,c,d)];
}
if(ch == 'W')
{
if(sx[snakelen - 1] - 1 == sx[snakelen - 2] && sy[snakelen - 1] == sy[snakelen - 2])
continue;
else return ch;
}
else if(ch == 'A')
{
if(sx[snakelen - 1] == sx[snakelen - 2] && sy[snakelen - 1] - 1 == sy[snakelen - 2])
continue;
else return ch;
}
else if(ch == 'S')
{
if(sx[snakelen - 1] + 1 == sx[snakelen - 2] && sy[snakelen - 1] == sy[snakelen - 2])
continue;
else return ch;
}
else if(ch == 'D')
{
if(sx[snakelen - 1] == sx[snakelen - 2] && sy[snakelen - 1] + 1 == sy[snakelen - 2])
continue;
else return ch;
}
return ch ;
}
}
int main()
{
printmap();
while(gameover()) //shiyixia tanhao
{
if(foodnum == 0)makefood();
char ch = wheretogo();
snakemove(ch);
eat();
}
printf("U LOSE HHH");
}