Commit f16d0f12 authored by Lucas Delcros's avatar Lucas Delcros
Browse files

Added javadoc

parent cd34735b
...@@ -13,6 +13,11 @@ public class RoomGraph { ...@@ -13,6 +13,11 @@ public class RoomGraph {
private HashMap<RoomBuilder, RoomNode> rooms; private HashMap<RoomBuilder, RoomNode> rooms;
private boolean isBuilded = false; private boolean isBuilded = false;
/**
*
* @param start starting room
* @param rooms all the rooms
*/
public RoomGraph(RoomBuilder start, ArrayList<RoomBuilder> rooms) { public RoomGraph(RoomBuilder start, ArrayList<RoomBuilder> rooms) {
this.rooms = new HashMap<RoomBuilder, RoomGraph.RoomNode>(); this.rooms = new HashMap<RoomBuilder, RoomGraph.RoomNode>();
for (RoomBuilder room : rooms) { for (RoomBuilder room : rooms) {
...@@ -21,15 +26,31 @@ public class RoomGraph { ...@@ -21,15 +26,31 @@ public class RoomGraph {
this.start = findRoom(start); this.start = findRoom(start);
if (this.start == null) throw new IllegalArgumentException("Start is not is room list !"); if (this.start == null) throw new IllegalArgumentException("Start is not is room list !");
} }
/**
* Finds a room Node according to a RoomBuilder
* @param room
* @return
*/
private RoomNode findRoom(RoomBuilder room){ private RoomNode findRoom(RoomBuilder room){
return rooms.get(room); return rooms.get(room);
} }
/**
* Add room a son of room father
* @param room
* @param fatherNode
*/
private void addRoomTo(RoomNode room, RoomNode fatherNode){ private void addRoomTo(RoomNode room, RoomNode fatherNode){
if(fatherNode.voisins == null) fatherNode.voisins = new ArrayList<RoomGraph.RoomNode>(); if(fatherNode.voisins == null) fatherNode.voisins = new ArrayList<RoomGraph.RoomNode>();
if (!fatherNode.voisins.contains(room)) fatherNode.voisins.add(room); if (!fatherNode.voisins.contains(room)) fatherNode.voisins.add(room);
} }
/**
* Links room1 and room2 in graph
* @param room1
* @param room2
*/
public void linkRooms(RoomBuilder room1, RoomBuilder room2){ public void linkRooms(RoomBuilder room1, RoomBuilder room2){
RoomNode fatherNode = findRoom(room1); RoomNode fatherNode = findRoom(room1);
RoomNode roomNode = findRoom(room2); RoomNode roomNode = findRoom(room2);
...@@ -38,11 +59,23 @@ public class RoomGraph { ...@@ -38,11 +59,23 @@ public class RoomGraph {
addRoomTo(roomNode, fatherNode); addRoomTo(roomNode, fatherNode);
addRoomTo(fatherNode, roomNode); addRoomTo(fatherNode, roomNode);
} }
/**
* Checks if room1 and room2 are linked
* @param room1
* @param room2
* @return
*/
public boolean areLinked(RoomBuilder room1, RoomBuilder room2){ public boolean areLinked(RoomBuilder room1, RoomBuilder room2){
RoomNode n = findRoom(room1), n2 = findRoom(room2); RoomNode n = findRoom(room1), n2 = findRoom(room2);
if(n.voisins == null) return false; if(n.voisins == null) return false;
return n.voisins.contains(n2); return n.voisins.contains(n2);
} }
/**
*
* @return a list containing all the rooms that are not reachable from the starting room
*/
public ArrayList<RoomBuilder> getNonReachableRooms(){ public ArrayList<RoomBuilder> getNonReachableRooms(){
ArrayList<RoomBuilder> res = new ArrayList<RoomBuilder>(); ArrayList<RoomBuilder> res = new ArrayList<RoomBuilder>();
for(RoomBuilder room : rooms.keySet()){ for(RoomBuilder room : rooms.keySet()){
...@@ -50,9 +83,12 @@ public class RoomGraph { ...@@ -50,9 +83,12 @@ public class RoomGraph {
} }
return res; return res;
} }
public void removeRoom(RoomBuilder room){
rooms.remove(room); /**
} * Inner function for buildLeft function
* @param top
* @param l
*/
private void buildLength_inner(RoomNode top, int l){ private void buildLength_inner(RoomNode top, int l){
if (top.ltostart > -1 && top.ltostart < l) return; if (top.ltostart > -1 && top.ltostart < l) return;
top.ltostart = l; top.ltostart = l;
...@@ -61,10 +97,20 @@ public class RoomGraph { ...@@ -61,10 +97,20 @@ public class RoomGraph {
if(voisin.ltostart == -1) buildLength_inner(voisin, l+1); if(voisin.ltostart == -1) buildLength_inner(voisin, l+1);
} }
} }
/**
* Builds the length of all the rooms in the graph
* The length of a room is it's distance (in rooms) from the starting room
*/
private void buildLength(){ private void buildLength(){
buildLength_inner(start, 0); buildLength_inner(start, 0);
isBuilded = true; isBuilded = true;
} }
/**
*
* @return The farthest room in the graph for the starting room
*/
public RoomBuilder getMostFarNode(){ public RoomBuilder getMostFarNode(){
if(!isBuilded)buildLength(); if(!isBuilded)buildLength();
RoomNode max = start; RoomNode max = start;
...@@ -74,21 +120,43 @@ public class RoomGraph { ...@@ -74,21 +120,43 @@ public class RoomGraph {
} }
return max.room; return max.room;
} }
/**
* Checks that a room is reachable
* @param room
* @return
*/
public boolean roomIsReachable(RoomBuilder room){ public boolean roomIsReachable(RoomBuilder room){
if(!isBuilded) buildLength(); if(!isBuilded) buildLength();
return rooms.get(room).ltostart != -1; return rooms.get(room).ltostart != -1;
} }
/**
* Checks that all the rooms are reachable
* @return
*/
public boolean everyRoomIsReachable(){ public boolean everyRoomIsReachable(){
for(RoomBuilder room : rooms.keySet()){ for(RoomBuilder room : rooms.keySet()){
if(!roomIsReachable(room)) return false; if(!roomIsReachable(room)) return false;
} }
return true; return true;
} }
/**
* Check whether a room is a dead-end in the graph
* @param room
* @return
*/
private boolean isDeadEnd(RoomBuilder room){ private boolean isDeadEnd(RoomBuilder room){
RoomNode node = rooms.get(room); RoomNode node = rooms.get(room);
if (node.voisins == null) return true; if (node.voisins == null) return true;
return node.voisins.size() == 1; return node.voisins.size() == 1;
} }
/**
*
* @return All list of all the dead-ends in the graph
*/
public ArrayList<RoomBuilder> getDeadEnds(){ public ArrayList<RoomBuilder> getDeadEnds(){
ArrayList<RoomBuilder> deadEnds = new ArrayList<RoomBuilder>(); ArrayList<RoomBuilder> deadEnds = new ArrayList<RoomBuilder>();
for(RoomBuilder room : rooms.keySet()){ for(RoomBuilder room : rooms.keySet()){
...@@ -96,7 +164,11 @@ public class RoomGraph { ...@@ -96,7 +164,11 @@ public class RoomGraph {
} }
return deadEnds; return deadEnds;
} }
class RoomNode { class RoomNode {
/**
* Inner class of a Node in the Graph of Rooms
*/
private ArrayList<RoomNode> voisins; private ArrayList<RoomNode> voisins;
private RoomBuilder room; private RoomBuilder room;
private int ltostart = -1; private int ltostart = -1;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment