c语言贪吃蛇自动寻路代码,开个帖子分享一个带有自动寻路的贪吃蛇,快点进来盖楼...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

int getStat(int x, int y) { // 获取坐标的状态

int i;

for (i = 0; i < LIST; i++) {

if (lists[i][0] != EMPTY && lists[i][1] == x && lists[i][2] == y) {

return lists[i][0];

}

}

return 0;

}

int getDIRECTION(int x, int y) { // 获取坐标的方向

int i;

for (i = 0; i < LIST; i++) {

if (lists[i][0] != EMPTY && lists[i][1] == x && lists[i][2] == y) {

return lists[i][6];

}

}

return 0;

}

void changeStat(int stat, int x, int y) { // 修改坐标的状态

int i;

for (i = 0; i < LIST; i++) {

if (lists[i][0] != EMPTY && lists[i][1] == x && lists[i][2] == y) {

lists[i][0] = stat;

return;

}

}

}

void gotoSmallest() { // 选择 F 最小的坐标继续搜索

int i, f = (X + Y + curr_g), s_x = 0, s_y = 0, s_g = 0, a = 0;

changeStat(CLOSED, curr_x, curr_y);

for (i = LIST; i >= 0; i--) {

if (lists[i][0] == OPENED) {

if (f > lists[i][5]) {

f = lists[i][5];

s_x = lists[i][1];

s_y = lists[i][2];

s_g = lists[i][3];

}

a = 1;

}

}

if (a == 0) { // 找不到任何 opened 的坐标了

AS=1;

}

curr_x = s_x;

curr_y = s_y;

curr_g = s_g;

}

void addWay(int wayto) { // 将最终路径添加到结果数组

int i;

for (i = 0; i < LIST; i++) {

if (way[i] == NONE) {

way[i] = wayto;

break;

}

}

}

void find_g(int a) {

int i,j;

for(i=0;i

for(j=0;j<7;j++){

lists[i][j]=0;

}

way[i] = 0;

}

tar_x = Gx;

tar_y = Gy;

if(a==1){

cat_x = Q1->x;

cat_y = Q1->y;

}

if(a==2){

cat_x = Q2->x;

cat_y = Q2->y;

}

curr_x = cat_x;

curr_y = cat_y;

curr_g = 0;

addStat(CLOSED, curr_x, curr_y, 0); // 猫咪起始位置,close

for (;;) {

if (getStat(curr_x - 1, curr_y) == EMPTY && (m[curr_x - 1][curr_y] == GND || m[curr_x - 1][curr_y] == FISH)) { // 向北搜索

addStat(OPENED, curr_x - 1, curr_y, UP);

}

if (getStat(curr_x + 1, curr_y) == EMPTY && (m[curr_x + 1][curr_y] == GND || m[curr_x + 1][curr_y] == FISH)) { // 向南搜索

addStat(OPENED, curr_x + 1, curr_y, DOWN);

}

if (getStat(curr_x, curr_y - 1) == EMPTY && (m[curr_x][curr_y - 1] == GND || m[curr_x][curr_y - 1] == FISH)) { // 向西搜索

addStat(OPENED, curr_x, curr_y - 1, LEFT);

}

if (getStat(curr_x, curr_y + 1) == EMPTY && (m[curr_x][curr_y + 1] == GND || m[curr_x][curr_y + 1] == FISH)) { // 向东搜索

addStat(OPENED, curr_x, curr_y + 1, RIGHT);

}

gotoSmallest(); // 移动

if (getStat(tar_x, tar_y) == OPENED||AS==1) { // 鱼在 opened 区域

break;

}

}

//开始回推

int back_x = tar_x, back_y = tar_y;

int direct = 0;

for (;;) {

direct = getDIRECTION(back_x, back_y); // 获取方向

addWay(direct); // 回推过程写入结果数组

if (direct == UP) back_x++;

else if (direct == DOWN) back_x--;

else if (direct == LEFT) back_y++;

else if (direct == RIGHT) back_y--;

else if (direct == NONE) break;

}

//Draw

int draw_x = cat_x, draw_y = cat_y;

for (i = LIST; i >= 0; i--) { // 结果数组倒序输出

if (way[i] != NONE) {

if (draw_x != cat_x || draw_y != cat_y) {

m[draw_x][draw_y] = way[i] + GO_OFFSET; // 向地图添加箭头信息

}

if (way[i] == UP) {

draw_x--;

} else if (way[i] == DOWN) {

draw_x++;

} else if (way[i] == LEFT) {

draw_y--;

} else if (way[i] == RIGHT) {

draw_y++;

}

}

}

if(a==1){

if((m[Q1->x][Q1->y-1]>=11&&m[Q1->x][Q1->y-1]<=14)||m[Q1->x][Q1->y-1]==A4){fx1=1;}

if((m[Q1->x][Q1->y+1]>=11&&m[Q1->x][Q1->y+1]<=14)||m[Q1->x][Q1->y+1]==A4){fx1=2;}

if((m[Q1->x-1][Q1->y]>=11&&m[Q1->x-1][Q1->y]<=14)||m[Q1->x-1][Q1->y]==A4){fx1=3;}

if((m[Q1->x+1][Q1->y]>=11&&m[Q1->x+1][Q1->y]<=14)||m[Q1->x+1][Q1->y]==A4){fx1=4;}

}

if(a==2){

if((m[Q2->x][Q2->y-1]>=11&&m[Q2->x][Q2->y-1]<=14)||m[Q2->x][Q2->y-1]==A4){fx2=1;}

if((m[Q2->x][Q2->y+1]>=11&&m[Q2->x][Q2->y+1]<=14)||m[Q2->x][Q2->y+1]==A4){fx2=2;}

if((m[Q2->x-1][Q2->y]>=11&&m[Q2->x-1][Q2->y]<=14)||m[Q2->x-1][Q2->y]==A4){fx2=3;}

if((m[Q2->x+1][Q2->y]>=11&&m[Q2->x+1][Q2->y]<=14)||m[Q2->x+1][Q2->y]==A4){fx2=4;}

}

for(i=0;i

for(j=0;j

if(m[i][j]>=11&&m[i][j]<=14){

m[i][j]=0;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你写一个贪吃蛇自动寻路的Python代码。以下是代码: ```python import random # 定义方向常量 UP = 1 DOWN = 2 LEFT = 3 RIGHT = 4 # 定义节点类 class Node(object): def __init__(self, x, y): self.x = x self.y = y self.f = 0 self.g = 0 self.h = 0 self.parent = None # 计算f值 def count_f(self): self.f = self.g + self.h # 定义A*寻路算法函数 def A_star(start, end, obstacles): # 初始化open列表和closed列表 open_list = [] closed_list = [] # 把起点加入open列表 open_list.append(start) while len(open_list) > 0: # 找到f值最小的节点 cur_node = open_list[0] for node in open_list: if node.f < cur_node.f: cur_node = node # 把当前节点从open列表中删除,加入到closed列表中 open_list.remove(cur_node) closed_list.append(cur_node) # 如果当前节点为终点,返回路径 if cur_node.x == end.x and cur_node.y == end.y: path = [] node = cur_node while node.parent is not None: path.append((node.x, node.y)) node = node.parent return path[::-1] # 遍历当前节点周围的节点 for i, j in [(0, -1), (0, 1), (-1, 0), (1, 0)]: x = cur_node.x + i y = cur_node.y + j # 如果节点在障碍物中或超出边界,则跳过 if (x, y) in obstacles or x < 0 or x >= 20 or y < 0 or y >= 20: continue # 如果节点已经在closed列表中,则跳过 if any(node.x == x and node.y == y for node in closed_list): continue # 如果该节点不在open列表中,则加入open列表 if not any(node.x == x and node.y == y for node in open_list): new_node = Node(x, y) new_node.g = cur_node.g + 1 new_node.h = (abs(end.x - x) + abs(end.y - y)) * 10 new_node.count_f() new_node.parent = cur_node open_list.append(new_node) # 如果该节点已经在open列表中,则更新其父节点 else: for node in open_list: if node.x == x and node.y == y: if cur_node.g + 1 < node.g: node.g = cur_node.g + 1 node.count_f() node.parent = cur_node break return None # 定义贪吃蛇自动寻路函数 def auto_move(snake, food, obstacles): start = Node(snake[-1][0], snake[-1][1]) end = Node(food[0], food[1]) path = A_star(start, end, obstacles) if path: # 如果路径存在,则返回下一步的方向 next_step = (path[0][0] - start.x, path[0][1] - start.y) if next_step == (-1, 0): return UP elif next_step == (1, 0): return DOWN elif next_step == (0, -1): return LEFT else: return RIGHT # 如果路径不存在,则随机移动 return random.choice([UP, DOWN, LEFT, RIGHT]) ``` 这段代码可以自动寻找贪吃蛇的下一步移动方向,如果存在路径,则返回最优路径的第一步;如果不存在路径,则随机移动。你可以把这段代码放在你的程序中进行调用,帮助贪吃蛇更快地吃到食物。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值