java 贪吃蛇ai_C语言手把手教你实现贪吃蛇AI(上)

本文实例为大家分享了手把手教你实现贪吃蛇AI的具体步骤,供大家参考,具体内容如下

1. 目标

编写一个贪吃蛇AI,也就是自动绕过障碍,去寻找最优路径吃食物。

2. 问题分析

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

在动手写贪吃蛇之前,我们需要想清楚以下几个问题,就非常容易了:

a. 蛇身。由于蛇在吃食物的过程中会不断的长大,所以很适合用单链表表示,并且吃食物的过程就是用头插法插入元素的过程

b. 食物。食物直接用随机生成函数,随机生成食物,但是需要检查,所生成的食物的位置不可以和蛇身重合

c. 显示。我们需要实时的显示出蛇身的移动,但事实上,我们不用每次都打印整个蛇身,因为蛇身每走一步,仅仅是蛇头和蛇尾的位置移动一格,其他的地方都没有变化,所以只需要打印一个新的蛇头,并把蛇尾的位置抹掉,那么视觉效果就是蛇身先前移动了一格,这个过程中,我们需要用到SetConsoleCursorPosition(),将光标移到到指定的位置(比如蛇尾),完成相应的操作(比如打印空格抹掉蛇尾)

d.控制。我们需要用键盘来控制蛇身的移动,这个程序中是利用上下左右方向键来实现的,这里需要用到GetAsyncKeyState(),来实时监测按键的状态

3. 运行效果

fc7ba85e31fe13b9f4a9e957fc540e0f.png

4. 源代码

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

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

另外还有一点容易混淆的是,我们通常都是用(x,y)坐标表示第x行,第y列,但是在SetConsoleCursorPosition(x,y)中,表示把光标移动到第y行,第x列

4.1 gluttonous.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 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

{

set_cursor_position(2*i, 0);

printf("■");

set_cursor_position(2*i, N/2+1);

printf("■");

}

for (i = 0; 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;

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值