手把手教你实现贪吃蛇AI的具体步骤,也就是自动绕过障碍

本文的例子分享了具体步骤,教你如何实现Snake AI,供大家参考。具体内容如下

1. 目标

写一个Snake AI,即自动绕过障碍物找到吃食物的最佳路径。

2.问题分析

为了达到这个目的,其实很容易,总共只需要两步,第一步是抓蛇,第二步是给蛇装脑。具体来说贪吃蛇c语言流程图,首先我们需要有一条普通的贪吃蛇,也就是我们经常玩、手动控制吃东西的贪吃蛇;然后给这条蛇加上AI,也就是通过算法控制,告诉蛇最方便的绕过障碍吃食物的方法。为了弄清这个问题,文章将分为三个部分:第一贪吃蛇c语言流程图,写一个snake程序;中,算法基础(需要用到什么算法);二、利用算法基础中的算法,写一个蛇AI。

在写蛇之前,我们需要思考以下几个问题,很容易:

一个。蛇身。由于蛇在吃食物的过程中会不断长大,所以用单链表来表示是非常合适的,而吃食物的过程就是通过头部插入来插入元素的过程

b.食物。食物直接使用随机生成函数随机生成食物,但需要检查,生成食物的位置不能与蛇身重叠

c。展示。我们需要实时显示蛇身的运动,但其实我们不需要每次都打印整个蛇身,因为蛇身每走一步,只有蛇身的头尾位置蛇移动一格,其他位置不变。所以我们只需要打印一个新的蛇头并擦除蛇尾的位置,那么视觉效果就是蛇身之前已经移动了一个空间。在这个过程中,我们需要使用 SetConsoleCursorPosition() 将光标移动到指定位置(比如蛇尾),完成相应的操作(比如打印一个空格来擦除蛇尾)

d。控制。我们需要使用键盘来控制蛇身的移动。在这个程序中,我们使用上、下、左、右和箭头键来实现这一点。这里我们需要使用GetAsyncKeyState()来实时监控keys的状态

3.运行效果

4.源码

它总共由三个文件组成:gluttonous.h、source.c 和 main.cpp。由于这条贪吃蛇是后面加AI的,所以没有加一些错误检测,比如是否撞到边界,是否撞到蛇身等。

需要注意的是,这个程序使用了一个特殊字符(’■’)来表示游戏空间的边界。 VS2013可以正常编译,但是codeblock会乱码。

另外一个混淆点是我们通常用(x,y)坐标来表示第x行第y列,但是在SetConsoleCursorPosition(x,y)中,它的意思是把光标移动到第y行第x列

4.1 贪吃.h

#ifndef SNAKE_H_ 
#define SNAKE_H_ 
#include 
#include //SetConsoleCursorPosition, sleep函数的头函数 
#include //time()的头函数 
#include  //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 源.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; ix = 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; 
}

以上就是本文的全部内容。希望对大家的学习有所帮助,也希望大家多多支持Script Home。

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发

请登录后发表评论