Class AreaGraph

java.lang.Object
ch.epfl.cs107.play.areagame.AreaGraph

public class AreaGraph extends Object
AreaGraph is a specific kind of graph apply to Area. The graph is composed of AreaNodes which are defined by their position in the graph (DiscreteCoordinates) and the existence of directed edge between them (from) and their four neighbors (to). Nodes are stored into a Map. Note: DiscreteCoordinate are serializable reimplementing hashCode() and equals() making the keys dependant only from the DiscreteCoordinate x and y values and not from the object itself.
  • Constructor Details

    • AreaGraph

      public AreaGraph()
      Default AreaGraph Constructor
  • Method Details

    • addNode

      public void addNode(DiscreteCoordinates c, boolean left, boolean up, boolean right, boolean down)
      Add if absent a new Node into the graph. Create a new Node and put it in the nodes map at given coordinates key. Note: DiscreteCoordinate are serializable reimplementing hashCode() and equals() making the keys dependant only from the DiscreteCoordinate x and y values and not from the object itself.
      Parameters:
      c - (DiscreteCoordinate): Position in the graph of the node to add, used as key for the map, not null
      left - (boolean): indicate if directed edge to the left direction exists
      up - (boolean): indicate if directed edge to the up direction exists
      right - (boolean): indicate if directed edge to the right direction exists
      down - (boolean): indicate if directed edge to the down direction exists
    • getNodes

    • nodeExists

      public boolean nodeExists(DiscreteCoordinates coordinates)
      Return if a node exists in the graph
      Parameters:
      coordinates - (DiscreteCoordinates): may be null
      Returns:
      (boolean): true if the given node exists in the graph
    • setSignal

      public void setSignal(DiscreteCoordinates coordinates, Logic signal)
      Activates/desactivates a node using a logic signal
      Parameters:
      coordinates - (DiscreteCoordinates): the nodes coordinates
      signal - (Logic) : the Logic value assigned to the node
    • keySet

      public List<DiscreteCoordinates> keySet()
      Returns:
      (List of DiscreteCoordinates): the positions of all the nodes in the graph
    • shortestPath

      public Queue<Orientation> shortestPath(DiscreteCoordinates from, DiscreteCoordinates to)
      Compute the shortest path in this AreaGraph from given DiscreteCoordinate to given DiscreteCoordinates
      Parameters:
      from - (DiscreteCoordinates): source node of the desired path, not null
      to - (DiscreteCoordinates): sink node of the desired path, not null
      Returns:
      (Iterator of Orientation): return an iterator containing the shortest path from source to sink, or null if the path does not exists !