C语言手把手教你实现贪吃蛇AI(上)
本文实例为大家分享了手把手教你实现贪吃蛇AI的具体步骤,供大家参考,具体内容如下 1. 目标 编写一个贪吃蛇AI,也就是自动绕过障碍,去寻找最优路径吃食物。 2. 问题分析 为了达到这一目的,其实很容易,总共只需要两步,第一步抓一条蛇,第二步给蛇装一个脑子。具体来说就是,首先我们需要有一条普通的贪吃蛇,也就是我们常玩儿的,手动控制去吃食物的贪吃蛇;然后给这条蛇加入AI,也就是通过算法控制,告诉蛇怎么最方便的绕开障碍去吃食物。为了讲清楚这个问题,文章将分为三部分:上,写一个贪吃蛇程序;中,算法基础(需要运用到什么算法);下,运用算法基础中的算法编写一个贪吃蛇AI。 在动手写贪吃蛇之前,我们需要想清楚以下几个问题,就非常容易了: 3. 运行效果 4. 源代码 总共由三个文件组成gluttonous.h,source.c & main.cpp。由于这个贪吃蛇是用于后面加AI,所以并没有加入一些错误检测,比如是否撞到边界,是否撞到蛇身等。 4.1 gluttonous.h #ifndef SNAKE_H_ #define SNAKE_H_ #include<stdio.h> #include<Windows.h> //SetConsoleCursorPosition,sleep函数的头函数 #include<time.h> //time()的头函数 #include<malloc.h> //malloc()的头函数 #define N 32 //地图大小 #define snake_mark '#'//表示蛇身 #define food_mark '$' #define sleeptime 500 /*表示蛇身坐标的结构体*/ typedef struct SNAKE{ int x; //行坐标 int y; //列坐标 struct SNAKE* next; }snake_body,*psnake; extern psnake food; typedef enum Direction{ U,D,L,R} direction;//蛇头的朝向 extern direction snake_direction; void set_cursor_position(int x,int y); void initial_map(); psnake initial_snake(); void create_food(psnake snake,psnake food); void printe_map(psnake snake,psnake food); int is_food(psnake snake_head,psnake food); int is_boundary(psnake snake_head,psnake food); int is_snakebody(psnake snake_head,psnake food); psnake snake_move(psnake sanke,psnake food); void control_snake(); #endif 4.2 source.cpp #include"gluttonous.h" void set_cursor_position(int x,int y) { COORD coord = { x,y };//x表示列,y表示行。 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); } /*初始化后的地图为 N列 N/2行*/ /*游戏的空间为2至N+1列,1至N/2行*/ void initial_map() { int i = 0; //打印上下边框(每个■占用一行两列) for (i = 0; i<N/2+2; i++) { set_cursor_position(2*i,0); printf("■"); set_cursor_position(2*i,N/2+1); printf("■"); } for (i = 0; i<N/2+2; i++) //打印左右边框 { set_cursor_position(0,i); printf("■"); set_cursor_position(N+2,i); printf("■"); } } /*初始化蛇身*/ /*蛇身初始化坐标为(5,8),(4,8),(3,8) */ psnake initial_snake() { int i=5;//列 int j = N / 4;//行 psnake snake = NULL,tsnake = NULL,temp = NULL; snake = (psnake)malloc(sizeof(snake_body)); (snake)->x = i; (snake)->y = j; (snake)->next = NULL; tsnake = snake; for (i = 4; i >2; i--) { temp = (psnake)malloc(sizeof(snake_body)); (temp)->x = i; (temp)->y = j; (temp)->next = NULL; (tsnake)->next = (temp); (tsnake) = (tsnake)->next; } return snake; } void create_food(psnake snake,psnake food) { static int i=1; psnake head = snake; srand((unsigned)time(NULL)); food->x = rand() % N + 2; food->y = rand() % (N/2) + 1; //检查食物是否和蛇身重回 while (head) { if (head->x == food->x && head->y == food->y) { free(food); food = NULL; create_food(snake,food); } else { head = head->next; } } } void printe_map(psnake snake,psnake food) { psnake temp=snake; while (temp) { set_cursor_position(temp->x,temp->y); printf("%c",snake_mark); temp = temp->next; } if (food) set_cursor_position(food->x,food->y ); printf("%c",food_mark); set_cursor_position(0,N/2+2); } //判断是否吃到食物,吃到食物返回 1,否则返回 0; int is_food(psnake snake_head,psnake food) { if (snake_head->x == food->x && snake_head->y == food->y) return 1; return 0; } //判断是否撞到墙,撞到墙返回 1,否则返回 0; int is_boundary(psnake snake_head) { if (snake_head->y <= 0 || snake_head->y >= N / 2 + 1 || snake_head->x <= 1 || snake_head->x >= N + 1) return 1; return 0; } //判断是否撞到自己,撞到自己返回 1,否则返回 0; int is_snakebody(psnake snake_head) { psnake temp=snake_head->next; while (temp) { if (snake_head->x == temp->x && snake_head->y == temp->y) return 1; else temp = temp->next; } return 0; } //将蛇身移动到合适的位置,并打印出来 psnake snake_move(psnake snake,psnake food) { psnake snake_head = (psnake)malloc(sizeof(snake_body)); if (snake_direction == U) { snake_head->y = snake->y-1; snake_head->x = snake->x; snake_head->next = snake; } else if (snake_direction == D) { snake_head->y = snake->y + 1; snake_head->x = snake->x; snake_head->next = snake; } else if (snake_direction == L) { snake_head->y = snake->y; snake_head->x = snake->x - 1; snake_head->next = snake; } else if (snake_direction == R) { snake_head->y = snake->y; snake_head->x = snake->x + 1; snake_head->next = snake; } if (is_food(snake_head,food))//如果是食物 { create_food(snake_head,food); printe_map(snake_head,food); } else if (is_boundary(snake_head) == 0 && is_snakebody(snake_head) == 0)//不是食物,不是边界,也不是蛇身 { psnake temp = snake_head; while (temp->next->next)//寻找蛇尾 { temp = temp->next; } set_cursor_position(temp->next->x,temp->next->y); printf(" ");//把蛇尾用空格消掉 free(temp->next);//释放蛇尾的内存空间 temp->next = NULL;//将temp的next置成NULL printe_map(snake_head,food); } else { free(snake_head); snake_head = NULL; } return snake_head; } void control_snake() { if (GetAsyncKeyState(VK_UP) && snake_direction != D) { snake_direction = U; } else if (GetAsyncKeyState(VK_DOWN) && snake_direction != U) { snake_direction = D; } else if (GetAsyncKeyState(VK_LEFT) && snake_direction != R) { snake_direction = L; } else if (GetAsyncKeyState(VK_RIGHT) && snake_direction != L) { snake_direction = R; } } 4.3 main.cpp #include"gluttonous.h" direction snake_direction; psnake food; int main(void) { psnake snake; initial_map(); snake=initial_snake(); food = (psnake)malloc(sizeof(snake_body)); food->next = NULL; create_food(snake,food); printe_map(snake,food); snake_direction = R; while (1) { Sleep(sleeptime); control_snake(); snake=snake_move(snake,food); } return 0; } 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。 您可能感兴趣的文章:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |