package gameDesign; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import db.SqlHelper; import entity.UserInfo; import java.io.BufferedReader; import java.io.StringReader; import java.util.HashMap; import java.util.HashSet; import java.util.List; public class MapConstructor extends ElementConstructor{ int counter = 1; int[][] matrix; int locationNum; HashMap id2j = new HashMap(); JSONArray locations = null; SqlHelper helper = null; public MapConstructor(SqlHelper helper) { super(); this.helper = helper; } public void constructGreph(JSONArray locations) { JSONObject jobj = null; int locationId, index; //ArrayList> matrix= new ArrayList>(); //ArrayList vec1d = new ArrayList(); locationNum = locations.size(); for (int i = 0; i < locationNum; i++) { jobj = locations.getJSONObject(i); jobj.put("index", i); locationId = (Integer) jobj.get("id"); id2j.put(locationId, jobj); } //邻接矩阵初始化 matrix = new int[locationNum][locationNum]; for (int i = 0; i < locationNum; i++) { for (int j = 0; j < locationNum; j++) { matrix[i][j] = Integer.MAX_VALUE; } } for (int i = 0; i < locationNum; i++) { List neighbours = JSONArray.parseArray(locations.getJSONObject(i).getJSONArray("neighbours").toJSONString(), Integer.class); for (int neighbourId : neighbours) { JSONObject neighbourObject = id2j.get(neighbourId); index = (Integer) neighbourObject.get("index"); matrix[i][index] = 1; matrix[index][i] = 1; } } } public void construct(JSONObject jsonObject){ JSONArray locations = jsonObject.getJSONArray("locations"); this.locations = locations; //通过邻接表构造邻接矩阵 constructGreph(locations); //用弗洛伊德算法求最短路径 Floyd floyd = new Floyd(); floyd.findShortestPath(matrix, locationNum); floyd.printPath(matrix, locationNum); generateWantToThere(floyd.pathMatirx, floyd.preTable, jsonObject); generateWhereCanIgo(floyd.pathMatirx, floyd.preTable, jsonObject); generateWhereIam(jsonObject); generateHowToThere(floyd.pathMatirx, floyd.preTable, jsonObject); // } /** * 生成‘我在哪里’所需要的配置文件 */ public void generateWhereIam(JSONObject jsonObject){ String zainaKeywords = (String)jsonObject.get("zaina"); configText += "zaina=" + zainaKeywords + "| " + "|match|=>|你在{site-2}|nextStep=yaozuo|status=waiting|configId=" + counter + "\n"; counter++; } /** * 生成‘怎么去哪里’所需要的配置文件 */ public void generateHowToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject){ String placeA, placeB, descriptionB, placeKeywords, text, pathNode; String zenmequKeywords = (String)jsonObject.get("zenmequ"); for(int i = 0;i < locationNum;i++){ for(int j = 0;j < locationNum;j++){ placeA = (String)locations.getJSONObject(i).get("name"); placeB = (String)locations.getJSONObject(j).get("name"); placeKeywords = zenmequKeywords.replace("地点", placeB); if(i == j){ configText += "zenmequ=" + placeKeywords + "|site=" + placeA + "|match|=>|" + "你已经在" + placeB + "了|nextStep=yaozuo|status=waiting|configId=" + counter + "\n"; counter++; continue; }; if(pathMatirx[i][j] == 1){ configText += "zenmequ=" + placeKeywords + "|site=" + placeA + "|match|=>|" + placeB + "就在附近哦|nextStep=yaozuo|status=waiting|configId=" + counter + "\n"; counter++; }else if(pathMatirx[i][j] > 1 && pathMatirx[i][j] < Integer.MAX_VALUE){ int k = preTable[i][j]; pathNode = ""; while (k != j) { pathNode += (String)locations.getJSONObject(k).get("name"); k = preTable[k][j]; } configText += "zenmequ=" + placeKeywords + "|site=" + placeA + "|match|=>|经过" + pathNode + "就可以到达" + placeB + "|nextStep=yaozuo|status=waiting|configId=" + counter + "\n"; counter++; } } } } /** * 生成‘我能去哪里’所需要的配置文件 */ public void generateWhereCanIgo(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject){ String nengqunaKeywords = (String)jsonObject.get("nengquna"); String placeA, placeB, pathNode; for(int i = 0;i < locationNum;i++){ placeA = (String)locations.getJSONObject(i).get("name"); pathNode = ""; for(int j = 0;j < locationNum;j++){ if(pathMatirx[i][j] == 1) { placeB = (String) locations.getJSONObject(j).get("name"); pathNode += "、" + placeB; } } configText += "nengquna=" + nengqunaKeywords + "|site=" + placeA + "|match|=>|你可以去" + pathNode + "|nextStep=yaozuo|status=waiting|configId=" + counter + "\n"; counter++; } } /** * 生成‘我要去那里’所需要的配置文件 */ public void generateWantToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject){ String placeA, placeB, descriptionB, placeKeywords, text, pathNode; String qunaKeywords = (String)jsonObject.get("quna"); for(int i = 0;i < locationNum;i++){ for(int j = 0;j < locationNum;j++){ placeA = (String)locations.getJSONObject(i).get("name"); placeB = (String)locations.getJSONObject(j).get("name"); descriptionB = (String)locations.getJSONObject(j).get("description"); placeKeywords = qunaKeywords.replace("地点", placeB); if(i == j){ configText += "quna=" + placeKeywords + "|site=" + placeA + "|match|=>|" + "你已经在" + placeB + "了|nextStep=yaozuo|status=waiting|configId=" + counter + "\n"; counter++; continue; }; if(pathMatirx[i][j] == 1){ configText += "quna=" + placeKeywords + "|site=" + placeA + "|match,updateKnowledge-<-site:" + placeB + "|=>|" + descriptionB + "|nextStep=yaozuo|status=waiting|configId=" + counter + "\n"; counter++; }else if(pathMatirx[i][j] > 1 && pathMatirx[i][j] < Integer.MAX_VALUE){ int k = preTable[i][j]; pathNode = ""; while (k != j) { pathNode += (String)locations.getJSONObject(k).get("name"); k = preTable[k][j]; } configText += "quna=" + placeKeywords + "|site=" + placeA + "|match|=>|从" + placeA + "不能直接到达" + placeB + ", 需要经过" + pathNode + "才能到达" + placeB + "|nextStep=yaozuo|status=waiting|configId=" + counter + "\n"; counter++; } } } } }