您的位置:首页 > 其它

贪吃蛇设计与算法

2017-12-26 01:09 288 查看
我觉得在这个游戏中,最难的就是move函数了,打得很艰难,不过最后终于在改了很多次后成功打出来了。如下:

void move(char a)
{
int j1 = sx[0], j2 = sy[0];
if (foodnum == 0){
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
while (map[foodx][foody] != ' '){
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
}
map[foodx][foody] = '$';
foodnum = 1;
}
switch (a){
case 'A':case 'a':
sy[0]--;
break;
case 'D':case 'd':
sy[0]++;
break;
case 'W':case 'w':
sx[0]--;
break;
case 'S':case 's':
sx[0]++;
break;
}
if (map[sx[0]][sy[0]] != ' '&&map[sx[0]][sy[0]] != '$'){
zhixing = 0;
return;
}
if (map[sx[0]][sy[0]] == ' '){
map[sx[len - 1]][sy[len - 1]] = ' ';
for (i = len - 2; i >= 1; i--){
sx[i + 1] = sx[i];
sy[i + 1] = sy[i];
}
sx[1] = j1, sy[1] = j2;
map[sx[0]][sy[0]] = 'H';
for (i = 1; i <= len - 1; i++){
map[sx[i]][sy[i]] = 'X';
}
}
if (map[sx[0]][sy[0]] == '$'){
foodnum = 0;
len++;
for (i = len - 2; i >= 1; i--){
sx[i + 1] = sx[i], sy[i + 1] = sy[i];
}
sx[1] = j1, sy[1] = j2;
map[sx[0]][sy[0]] = 'H';
for (i = 1; i <= len - 1; i++){
map[sx[i]][sy[i]] = 'X';
}
}
}


不过我的和别人的不太一样,我是把食物的代码也放进了move函数中。

下面是我的wheregonext函数(超低配智能):

char wheregonext(void)
{
int heng, shu;
heng = abs(sy[0] - foody);
shu = abs(sx[0] - foodx);
if (heng >= shu){
if (sy[0] - foody > 0){
return 'a';
}
else{
return 'd';
}
}
if (heng < shu){
if (sx[0] - foodx > 0){
return 'w';
}
else{
return 's';
}
}
}


在写的时候可以用自顶向下的方法,先写好总控代码,再分别写各个函数,下面是全部代码:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<Windows.h>
char map[12][13] = {
"************",
"*XXXXH *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"
};
int sx[100] = { 1, 1, 1, 1, 1 }, sy[100] = { 5, 4, 3, 2, 1 };
int foodnum = 0, zhixing = 1, len = 5;
int i, foodx, foody;
void move(char a);
char wheregonext(void);
int main(void)
{
char c;
srand(time(NULL));
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
while (map[foodx][foody] != ' '){
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
}
map[foodx][foody] = '$';
foodnum = 1;
for (i = 0; i <= 11; i++){
printf("%s\n", map[i]);
}
while (zhixing){
c = wheregonext();
move(c);
system("cls");
Sleep(100);
for (i = 0; i <= 11 && zhixing == 1; i++){
printf("%s\n", map[i]);
}
}
printf("Game over!\n");
return 0;
}
void move(char a) { int j1 = sx[0], j2 = sy[0]; if (foodnum == 0){ foodx = rand() % 10 + 1; foody = rand() % 10 + 1; while (map[foodx][foody] != ' '){ foodx = rand() % 10 + 1; foody = rand() % 10 + 1; } map[foodx][foody] = '$'; foodnum = 1; } switch (a){ case 'A':case 'a': sy[0]--; break; case 'D':case 'd': sy[0]++; break; case 'W':case 'w': sx[0]--; break; case 'S':case 's': sx[0]++; break; } if (map[sx[0]][sy[0]] != ' '&&map[sx[0]][sy[0]] != '$'){ zhixing = 0; return; } if (map[sx[0]][sy[0]] == ' '){ map[sx[len - 1]][sy[len - 1]] = ' '; for (i = len - 2; i >= 1; i--){ sx[i + 1] = sx[i]; sy[i + 1] = sy[i]; } sx[1] = j1, sy[1] = j2; map[sx[0]][sy[0]] = 'H'; for (i = 1; i <= len - 1; i++){ map[sx[i]][sy[i]] = 'X'; } } if (map[sx[0]][sy[0]] == '$'){ foodnum = 0; len++; for (i = len - 2; i >= 1; i--){ sx[i + 1] = sx[i], sy[i + 1] = sy[i]; } sx[1] = j1, sy[1] = j2; map[sx[0]][sy[0]] = 'H'; for (i = 1; i <= len - 1; i++){ map[sx[i]][sy[i]] = 'X'; } } }
char wheregonext(void) { int heng, shu; heng = abs(sy[0] - foody); shu = abs(sx[0] - foodx); if (heng >= shu){ if (sy[0] - foody > 0){ return 'a'; } else{ return 'd'; } } if (heng < shu){ if (sx[0] - foodx > 0){ return 'w'; } else{ return 's'; } } }


下面是在运行的程序:



欢迎大家指正。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: