A*算法迷宫游戏

1.项目主要功能

(1)构造迷宫

(2)通过prim和dfs两种算法生成路径

2.项目总体框架

1.后端基于springboot进行开发

2.前端使用html结合jquery进行gui的展示

3.接口设计

前端接口

后端接口

下面列举几个重要接口的代码:

(1) MazeFactory

创建maze的工厂类

(2) Gui

AStarMazeRunner

先前我们是用dfs算法生成的路径,不符合实习要求,这里我们改用A*算法

private static void findPath(int[][] map, int x1, int y1, int x2, int y2) {

List<Position> openList = new ArrayList<AStarAlgorithm.Position>();

List<Position> closeList = new ArrayList<AStarAlgorithm.Position>();

boolean findFlag = false;

Position termPos = null;

// 起始点

Position startPos = new Position(x1, y1, calcH(x1, y1, x2, y2));

openList.add(startPos);

do {

// 通过在开启列表中找到F值最小的点作为当前点

Position currentPos = openList.get(0);

for (int i = 0; i < openList.size(); i++) {

if (currentPos.F > openList.get(i).F) {

currentPos = openList.get(i);

}

}

// 将找到的当前点放到关闭列表中,并从开启列表中删除

closeList.add(currentPos);

openList.remove(currentPos);

//遍历当前点对应的8个相邻点

for (int i = 0; i < DIREC.length; i++) {

int tmpX = currentPos.row + DIREC[i][0];

int tmpY = currentPos.col + DIREC[i][1];

if (tmpX < 0 || tmpX >= map.length || tmpY < 0 || tmpY >= map[0].length) {

continue;

}

//创建对应的点

Position tmpPos = new Position(tmpX, tmpY, calcH(tmpX, tmpY, x2, y2), currentPos);

//map中对应的格子中的值为1(障碍), 或对应的点已经在关闭列表中

if (map[tmpX][tmpY] == 1 || closeList.contains(tmpPos)) {

continue;

}

//如果不在开启列表中,则加入到开启列表

if (!openList.contains(tmpPos)) {

openList.add(tmpPos);

} else {

// 如果已经存在开启列表中,则用G值考察新的路径是否更好,如果该路径更好,则把父节点改成当前格并从新计算FGH

Position prePos = null;

for (Position pos : openList) {

if (pos.row == tmpX && pos.col == tmpY) {

prePos = pos;

break;

}

}

if (tmpPos.G < prePos.G) {

prePos.setFaPos(currentPos);

}

}

}

// 判断终点是否在开启列表中

for (Position tpos : openList) {

if (tpos.row == x2 && tpos.col == y2) {

termPos = tpos;

findFlag = true;

break;

}

}

} while(openList.size() != 0);

if(!findFlag) {

System.out.println("no valid path!");

return;

}

Stack<String> resStack = new Stack<String>();

String pattern = "(%d, %d)";

if (termPos != null) {

resStack.push(String.format(pattern, termPos.row, termPos.col));

while(termPos.fa != null) {

termPos = termPos.fa;

resStack.push(String.format(pattern, termPos.row, termPos.col));

}

}

StringBuilder sb = new StringBuilder();

while (!resStack.empty()) {

sb.append(resStack.pop());

if (!resStack.empty()) {

sb.append(" -> ");

}

}

System.out.println(sb.toString());

}

/**

* 计算某个格子的H值

* @param x

* @param y

* @param tx 终点的x值

* @param ty 终点的y值

* @return

*/

private static int calcH(int x, int y, int tx, int ty) {

int diff = Math.abs(x - tx) + Math.abs(y - ty);

return diff * 10;

}

static class Position {

public int F;

public int G;

public int H;

public Position fa;

public int row;

public int col;

public Position() {

}

public Position(int row, int col, int H) {

this(row, col, H, null);

}

public Position(int row, int col, int H, Position pos) {

this.H = H;

this.row = row;

this.col = col;

this.fa = pos;

this.G = calcG();

this.F = G + H;

}

/**

* 计算某个点到起始点的代价G

* @return

*/

private int calcG() {

if (fa == null) return 0;

if (fa.row != this.row && fa.col != this.col) {

return 14 + fa.G;

}

return 10 + fa.G;

}

public void setFaPos(Position pos) {

this.fa = pos;

this.G = calcG();

this.F = G + H;

}

@Override

public boolean equals(Object obj) {

if (obj == null) {

return false;

}

if (!(obj instanceof Position)) {

return false;

}

Position pos = (Position) obj;

return this.row == pos.row && this.col == pos.col;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + row;

result = prime * result + col;

return result;

}

}

4.用户界面设计

Html+Css+JavaScript+Jquery

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于A*算法的迷宫小游戏开发,可以让玩家面对迷宫的挑战,通过智慧和策略找到迷宫的出口。 首先,我们需要设计一个迷宫地图。可以采用多种方式生成迷宫地图,如随机生成、手动设计或者使用迷宫生成算法。迷宫地图由起点、终点以及迷宫墙壁组成。 接下来,我们使用A*算法来寻找最佳路径。A*算法是一种启发式搜索算法,通过估计每个节点到目标点的距离来决定搜索方向。在实现A*算法时,需要定义一个启发函数来评估节点的价值,以便选择最优的路径。在该游戏中,可以使用曼哈顿距离或欧几里得距离作为启发函数。 当玩家开始游戏后,可以使用方向键或鼠标来控制角色移动。同时,在游戏界面上显示迷宫地图和玩家的当前位置。 在实现A*算法时,需要考虑一些特殊情况。比如,如何处理墙壁、如何处理无法到达的位置等。可以采用合适的数据结构,如优先队列或堆栈,来实现算法的搜索和路径的存储。 最后,为了增加游戏的趣味性和挑战性,可以在迷宫中添加一些道具或陷阱,用来干扰玩家的寻路过程。比如,道具可以提供额外的移动能力,而陷阱则会减慢玩家的速度。 通过以上方法,基于A*算法的迷宫小游戏可以提供给玩家一个有趣而挑战的寻路体验。同时,这个游戏也可以帮助玩家锻炼逻辑思维和空间认知能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值