Example #1
0
    // Check if two vertices are legally connected via edges
    // Note: returns true if there is an enemy knight on v2
    // (Helpful for displacing knights)
    // Note: returns false if there are any other pieces on v2
    // Make sure to reset vertices before using this method
    public bool areConnectedVertices(Vertex v1, Vertex v2, Enums.Color color)
    {
        // If the vertices are the same, they are connected
        if (Object.ReferenceEquals(v1, v2))
        {
            return(true);
        }

        v1.setVisited();
        foreach (Edge e in v1.getNeighbouringEdges())
        {
            GamePiece edgePiece = e.getOccupyingPiece();

            // Make sure the edge has a correctly colored piece
            if (Object.ReferenceEquals(edgePiece, null))
            {
                continue;
            }
            else if (edgePiece.getColor() != color)
            {
                continue;
            }
            else if (edgePiece.getPieceType() != Enums.PieceType.ROAD)
            {
                continue;
            }

            // Check the opposite vertex along the edge
            Vertex opposite;
            if (Object.ReferenceEquals(e.getLeftVertex(), v1))
            {
                opposite = e.getRightVertex();
            }
            else
            {
                opposite = e.getLeftVertex();
            }

            // If the opposite vertex has an enemy piece, further vertices are not connected
            GamePiece intersection = opposite.getOccupyingPiece();
            if (!Object.ReferenceEquals(intersection, null))
            {
                if (intersection.getColor() != color)
                {
                    if (Object.ReferenceEquals(opposite, v2))
                    {
                        if (intersection.getPieceType() != Enums.PieceType.KNIGHT)
                        {
                            opposite.setVisited();
                            continue;
                        }
                    }
                    else
                    {
                        opposite.setVisited();
                        continue;
                    }
                }
            }

            // If the opposite vertex is the target vertex, return true
            if (Object.ReferenceEquals(opposite, v2))
            {
                return(true);
            }

            // Check the opposite vertex if it hasn't been visited
            if (opposite.getVisited() == 0)
            {
                if (areConnectedVertices(opposite, v2, color))
                {
                    return(true);
                }
            }
        }
        return(false);
    }
Example #2
0
		//------------------TOPOLOGICAL-SORTING-----------------------------------

		public void depthFirstTS(Vertex v, List<Vertex> list) {

			Edge edge;
			v.setVisited(true);

			foreach (Vertex neighbor in v.getNeighbors()) {
				edge = findEdge(v, neighbor);
				if( !neighbor.isVisited() && (edge != null) )
					depthFirstTS(neighbor, list);
			}

			list.Add(v);
		}
Example #3
0
        /// <summary>
        /// Performs Dijkstra's routine on a weighted graph to determine the cheapest path from start vertex to a goal vertex.
        /// </summary>
        /// <param name="graph">
        /// The graph object to be traversed
        /// </param>
        /// <param name="startName">
        /// Name of the starting vertex in the path
        /// </param>
        /// <param name="goalName">
        /// Name of the ending vertex in the path
        /// </param>
        /// <returns>
        /// String List of the vertices that make up the cheapest path from the starting vertex (inclusive) to the
        /// ending vertex (inclusive) based on weight associated with the edges between the graphs vertices
        /// </returns>
        public static List <String> shortestPath(Graph graph, String startName, String goalName)
        {
            Queue <Vertex> queue = new Queue <Vertex>();
            List <String>  path  = new List <String>();

            Vertex start;

            graph.getVertices().TryGetValue(startName, out start);
            Vertex goal;

            graph.getVertices().TryGetValue(goalName, out goal);

            // if start and goal are the same, return one
            if (start.equals(goal))
            {
                path.Add(startName);
                return(path);
            }

            // Initialize all nodes' cost to infinity
            foreach (Vertex v in graph.getVertices().Values)
            {
                v.setCostFromStart(int.MaxValue);
                v.setVisited(false);
                v.setCameFrom(null);
            }

            // Enqueue the start
            queue.Enqueue(start);
            start.setCostFromStart(0);

            while (queue.Count != 0)
            {
                Vertex curr = queue.Dequeue();

                if (curr.equals(goal))
                {
                    break;
                }
                curr.setVisited(true);

                //get all of current's neighbors
                foreach (Edge e in curr.getEdges())
                {
                    Vertex neighbor = e.getOtherVertex();
                    if (neighbor.getVisited() == false)
                    {
                        LinkedList <Edge> edges = curr.getEdges();

                        foreach (Edge E in edges)
                        {
                            if (E.getOtherVertex().equals((neighbor)))
                            {
                                // if neighbor's cost from start is higher than current cost + edge weight,
                                // enqueue and neighbor becomes current
                                if (neighbor.getCostFromStart() > (curr.getCostFromStart() + E.getWeight()))
                                {
                                    queue.Enqueue(neighbor);
                                    neighbor.setCameFrom(curr);
                                    neighbor.setCostFromStart(curr.getCostFromStart() + E.getWeight());
                                }
                            }
                        }
                    }
                }
            }

            if (goal.getCameFrom() == null)
            {
                return(path);
            }

            //get the data from the cameFrom and add it to the list to be returned
            for (Vertex v = goal; v != null; v = v.getCameFrom())
            {
                path.Add(v.getName());
            }

            //If no path was found, return an empty list
            if (!path.Contains(goalName) && !path.Contains(startName))
            {
                path.Clear();
                return(path);
            }
            else
            {
                return(path);
            }
        }