1.游戏中难免遇到世界寻路问题,关于世界寻路其实相对地图A*寻路简单的多,代码实现没有考虑效率(原因地图数量很少,此实现不会出现太高消耗),基本实现了地图点点相连查找。
2.没有实现最优路径查找,需要的朋友自行添加,原理简单,挑选所有可行方案,做最终筛选,筛选条件就是你要的所谓最优路径条件,包括避开某些地图,或是最短路径等等,这些不赘述。
3.代码已经验证可行,纯粹分享给各位,不喜欢的朋友直接右上角叉掉浏览器即可。
4.欢迎交流,最近开始把笔记上的文章提到博客,主要是为了巩固知识,分享知识。
5.本人非大牛。
地图描述
package com.findway;
import java.util.ArrayList;
/**
*
* @author Jachon
*
*/
public class MapInfo {
int mapId;
String mapName;
ArrayList<MapInfo> exit;
boolean isSearched = false;
public MapInfo(){
exit = new ArrayList<MapInfo>();
}
public int getMapId() {
return mapId;
}
public void setMapId(int mapId) {
this.mapId = mapId;
}
public String getMapName() {
return mapName;
}
public void setMapName(String mapName) {
this.mapName = mapName;
}
public void addExit(MapInfo map){
if(map == null){
return;
}
exit.add(map);
}
public boolean isSearched() {
return isSearched;
}
public void setSearched(boolean isSearched) {
this.isSearched = isSearched;
}
}
管理查询
package com.findway;
import java.util.ArrayList;
public class FindWay {
ArrayList<MapInfo> maplist;
public FindWay(){
maplist = new ArrayList<MapInfo>();
}
public void creadMap(){
addMapInfo("景阳冈", 1000);
addMapInfo("牛牛家", 1017);
addMapInfo("和珅家", 1015);
addMapInfo("白石桥", 1016);
addMapInfo("后海", 1014);
addMapInfo("国贸新村", 1010);
addMapInfo("故宫", 1013);
addMapInfo("天安门", 1011);
addMapInfo("英雄纪念碑", 1012);
addMapInfo("牛家新村", 1008);
addMapInfo("土村林", 1005);
addMapInfo("漠北", 1006);
addMapInfo("干家北", 1009);
addMapInfo("北岗村", 1001);
addMapInfo("百家村", 1007);
addMapInfo("田家村", 1002);
addMapInfo("泗水镇", 1003);
addMapInfo("葛家村", 1004);
linkMap(1017, 1016);
linkMap(1016, 1017, 1015);
linkMap(1015, 1016, 1014);
linkMap(1014, 1015, 1013);
linkMap(1013, 1014, 1011);
linkMap(1011, 1013, 1010, 1012);
linkMap(1012, 1011);
linkMap(1010, 1011, 1009);
linkMap(1008, 1005, 1009);
linkMap(1005, 1008, 1006, 1000);
linkMap(1006, 1005, 1007);
linkMap(1009, 1008, 1002, 1010);
linkMap(1000, 1005, 1001, 1002);
linkMap(1001, 1000, 1004);
linkMap(1007, 1006, 1004);
linkMap(1002, 1000, 1003);
linkMap(1003, 1002, 1004);
linkMap(1004, 1003, 1001, 1007);
}
public void linkMap(int id, int... exit){
MapInfo map = getMap(id);
if(map == null){
return;
}
for(int i : exit){
MapInfo em = getMap(i);
if(em != null){
map.addExit(em);
}
}
}
public void addMapInfo(String name, int id){
MapInfo info = getMap(id);
if(info == null){
info = new MapInfo();
info.setMapName(name);
info.setMapId(id);
maplist.add(info);
}
}
public MapInfo getMap(int id){
for(MapInfo info : maplist){
if(info != null && info.getMapId() == id){
return info;
}
}
return null;
}
public void checkMap(){
for(MapInfo info : maplist){
System.out.println(info.getMapName() + " " + info.getMapId());
}
}
public void findTheWay(int start, int end){
if(start == end){
System.out.println("简单巡查起始地图和末端地图相同");
return;
}
if(getMap(start) == null || getMap(end) == null){
System.out.println("巡查地图不能为不存在地图");
return;
}
changeMapState();
ArrayList<MapInfo> lin = new ArrayList<MapInfo>();
ArrayList<MapInfo> all = new ArrayList<MapInfo>();
checkExit(getMap(start), start, end, start, lin, all);
for(MapInfo info : all){
System.out.println(info.mapId + " " + info.mapName + "--->");
}
}
public void changeMapState(){
for(MapInfo info : maplist){
if(info != null){
info.setSearched(false);
}
}
}
public void checkExit(MapInfo current, int start, int end, int parent, ArrayList<MapInfo> lin, ArrayList<MapInfo> all){
if(current == null || current.isSearched){
return;
}
current.isSearched = true;
if(current.mapId == end){
ArrayList<MapInfo> tm = new ArrayList<MapInfo>();
tm.addAll(lin);
tm.add(current);
all.addAll(tm);
}else{
lin.add(current);
for(MapInfo m : current.exit){
if(m.mapId == end){
ArrayList<MapInfo> tm = new ArrayList<MapInfo>();
tm.addAll(lin);
tm.add(m);
all.addAll(tm);
return;
}
}
for(MapInfo m : current.exit){
if(m.mapId == current.mapId || m.mapId == start || m.mapId == parent)
continue;
checkExit(m, start, end, current.mapId, lin, all);
}
removeLastList(lin);
}
}
public void removeLastList(ArrayList<MapInfo> e){
if(e.isEmpty()){
return;
}
e.remove(e.size() - 1);
}
public static void main(String arg0[]){
FindWay way = new FindWay();
way.creadMap();
way.checkMap();
way.findTheWay(1000, 1017);
}
}
手绘地图