1.项目主要功能
(1)构造迷宫
(2)通过prim和dfs两种算法生成路径
2.项目总体框架
1.后端基于springboot进行开发
2.前端使用html结合jquery进行gui的展示
3.接口设计
前端接口
![](https://i-blog.csdnimg.cn/blog_migrate/1140ddebac25025ed12f207708b6b505.png)
后端接口
![](https://i-blog.csdnimg.cn/blog_migrate/9ddc4f5be51053308806c3492a1aa78e.png)
下面列举几个重要接口的代码:
(1) MazeFactory
创建maze的工厂类
![](https://i-blog.csdnimg.cn/blog_migrate/190e8acc845cbdb11406229a8bce9250.png)
(2) Gui
![](https://i-blog.csdnimg.cn/blog_migrate/9c3a852e103771b77461af423e110c67.png)
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
![](https://i-blog.csdnimg.cn/blog_migrate/07f33bec0e9fcfba6fe2834faf916e3e.png)