作为一个刚接触编程的小白,智能蛇设计对我的难度还是很大的,但想想看这好歹是我做的第一个AI,不过我写出的应该是个Artificial Idiot,看看大佬们写出的智能蛇只有羡慕的份
关于智能蛇的核心算法,应该是自动寻路的算法实现,我选择使用贪婪算法结合伪代码,决定蛇行走方向的伪代码大致如下
// Hx,Hy: 头的位置
// Fx,Fy:食物的位置
function whereGoNext(Hx,Hy,Fx,Fy) {
// 用数组movable[3]={“a”,”d”,”w”,”s”} 记录可走的方向
// 用数组distance[3]={0,0,0,0} 记录离食物的距离
// 分别计算蛇头周边四个位置到食物的距离。H头的位置,F食物位置
// 例如:假设输入”a” 则distance[0] = |Fx – (Hx-1)| + |Fy – Hy|
// 如果 Hx-1,Hy 位置不是Blank,则 distance[0] = 9999
// 选择distance中存最小距离的下标p,注意最小距离不能是9999
// 返回 movable[p]
}
接下来只需要将伪代码用C语言实现就OK了
char wheregonext(int snakeheadx, int snakeheady, int foodx, int foody) {
char ch;
char movable[4] = {'a','d','w','s'};
int distance[4] = {0,0,0,0};
if(map[snakeheadx][snakeheady - 1] == BLANK_CELL || map[snakeheadx][snakeheady - 1] == SNAKE_FOOD){
distance[0] = abs(foodx - snakeheadx) + abs(foody - (snakeheady - 1));
}
else distance[0] = 9999;
if(map[snakeheadx][snakeheady + 1] == BLANK_CELL || map[snakeheadx][snakeheady + 1] == SNAKE_FOOD){
distance[1] = abs(foodx - snakeheadx) + abs(foody - (snakeheady + 1));
}
else distance[1] = 9999;
if(map[snakeheadx - 1][snakeheady] == BLANK_CELL || map[snakeheadx - 1][snakeheady] == SNAKE_FOOD){
distance[2] = abs(foodx - (snakeheadx - 1)) + abs(foody - snakeheady);
}
else distance[2] = 9999;
if(map[snakeheadx + 1][snakeheady] == BLANK_CELL || map[snakeheadx + 1][snakeheady] == SNAKE_FOOD){
distance[3] = abs(foodx - (snakeheadx + 1)) + abs(foody - snakeheady);
}
else distance[3] = 9999;
int min = 9999;
int i;
for(i = 0; i < 4; i ++){
if(min > distance[i])
min = distance[i];
}
for (i = 0; i < 4; i ++) {
if(min == distance[i])
ch = movable[i];
}
return ch;
}
以上就是我写出的Wheregonext函数,将函数加在贪吃蛇的代码中在对主函数进行修改,就能写出一个能初步实现自动寻路效果的智能蛇
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<windows.h>
#define SNAKE_MAX_LENGTH 100
#define SNAKE_HEAD 'H'
#define SNAKE_BODY 'X'
#define BLANK_CELL ' '
#define SNAKE_FOOD '$'
#define WALL_CELL '*'
void printmap(void);
void snakeMove(char);
void putfood(void);
void output(char);
void gameover(void);
char wheregonext(int,int,int,int);
char map[12][26] =
{
"*************************",
"*XH *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"*************************"
};
int snakeheadx = 1;
int snakeheady = 2;
int snakebodyx[SNAKE_MAX_LENGTH] = {1};
int snakebodyy[SNAKE_MAX_LENGTH] = {1};
int snakelength = 2;
int foodx = 0, foody = 0;
int gamerunning = 1;
int score = 0;
int main(){
char control;
srand(time(NULL));
putfood();
printmap();
printf(" 得分 %d",score);
while(gamerunning){
Sleep(30);
//control = getch();//getch代替getchar/scanf可以不用回车
control = wheregonext(snakeheadx,snakeheady,foodx,foody);
output(control);
printmap();
printf(" 得分 %d",score);
}
putchar('\n');
printf("Game over\n");
return 0;
}
void printmap(void){
system("cls");
int i;
for(i = 0; i < 12; i ++){
printf("%s\n",map[i]);
}
}
void gameover(void){
printf("Game over\n");
gamerunning = 0;
}
void snakeMove(char control){
switch(control){
case 'a':
snakeheady --;
break;
case 's':
snakeheadx ++;
break;
case 'w':
snakeheadx --;
break;
case 'd':
snakeheady ++;
break;
default:
return;
}
}
void putfood(void) {
foodx = rand() % 10 + 1;
foody = rand() % 10 + 1;
while(map[foodx][foody] != BLANK_CELL){
foodx = rand() % 12 + 1;
foody = rand() % 26 + 1;
}
map[foodx][foody] = SNAKE_FOOD;
}
void output(char control) {
int i;
int presnakeheadx = snakeheadx;
int presnakeheady = snakeheady;
snakeMove(control);
if(map[snakeheadx][snakeheady] != BLANK_CELL && map[snakeheadx][snakeheady] != SNAKE_FOOD){
gameover();
}
else if (map[snakeheadx][snakeheady] == BLANK_CELL){
map[snakeheadx][snakeheady] = SNAKE_HEAD;
map[presnakeheadx][presnakeheady] = SNAKE_BODY;
for(i = 0; i < snakelength - 2; i ++){
map[snakebodyx[i]][snakebodyy[i]] = map[snakebodyx[i + 1]][snakebodyy[i + 1]];
}
map[snakebodyx[i]][snakebodyy[i]] = BLANK_CELL;
for (i = snakelength - 3; i >= 0; i--) {
snakebodyx[i + 1] = snakebodyx[i];
snakebodyy[i + 1] = snakebodyy[i];
}
snakebodyx[0] = presnakeheadx;
snakebodyy[0] = presnakeheady;
}
else if (map[snakeheadx][snakeheady] == SNAKE_FOOD){
map[snakeheadx][snakeheady] = SNAKE_HEAD;
i = 0;
map[presnakeheadx][presnakeheady] = map[snakebodyx[i]][snakebodyy[i]];
for (i; i < snakelength - 2; i++) {
map[snakebodyx[i]][snakebodyy[i]] = map[snakebodyx[i + 1]][snakebodyy[i + 1]];
}
map[snakebodyx[i]][snakebodyy[i]] = SNAKE_BODY;
snakelength ++;
for (i = snakelength - 3; i >= 0; i--) {
snakebodyx[i + 1] = snakebodyx[i];
snakebodyy[i + 1] = snakebodyy[i];
}
snakebodyx[0] = presnakeheadx;
snakebodyy[0] = presnakeheady;
score ++;
putfood();
}
}
char wheregonext(int snakeheadx, int snakeheady, int foodx, int foody) {
char ch;
char movable[4] = {'a','d','w','s'};
int distance[4] = {0,0,0,0};
if(map[snakeheadx][snakeheady - 1] == BLANK_CELL || map[snakeheadx][snakeheady - 1] == SNAKE_FOOD){
distance[0] = abs(foodx - snakeheadx) + abs(foody - (snakeheady - 1));
}
else distance[0] = 9999;
if(map[snakeheadx][snakeheady + 1] == BLANK_CELL || map[snakeheadx][snakeheady + 1] == SNAKE_FOOD){
distance[1] = abs(foodx - snakeheadx) + abs(foody - (snakeheady + 1));
}
else distance[1] = 9999;
if(map[snakeheadx - 1][snakeheady] == BLANK_CELL || map[snakeheadx - 1][snakeheady] == SNAKE_FOOD){
distance[2] = abs(foodx - (snakeheadx - 1)) + abs(foody - snakeheady);
}
else distance[2] = 9999;
if(map[snakeheadx + 1][snakeheady] == BLANK_CELL || map[snakeheadx + 1][snakeheady] == SNAKE_FOOD){
distance[3] = abs(foodx - (snakeheadx + 1)) + abs(foody - snakeheady);
}
else distance[3] = 9999;
int min = 9999;
int i;
for(i = 0; i < 4; i ++){
if(min > distance[i])
min = distance[i];
}
for (i = 0; i < 4; i ++) {
if(min == distance[i])
ch = movable[i];
}
return ch;
}
但这个蛇只能寻求最短的路径,不能在寻路过程中避开自己的身体,所以经常会自杀,我做了个计分界面,最多的一次只跑到了40分,以我目前的水平没办法对算法进行修改,希望在以后的学习中能实现更好的算法模式