breadth first search help

I was wondering if someone could help me figure out how to do incorporate breadth first search into my dungon of doom here. I have a file that reads in and makes all the vertices and such but I don't know how to make a breadth first search function that will work on it properly. Can someone please guide me on how to do this?

What I have written so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#include <iostream>
#include <string>
#include <fstream>
#include "dungeonGraph.h"
using namespace std;


int main()
{
	dungeonGraph G;

	ifstream myfile;
	//myfile.open ("Map.txt");
	myfile.open ("map.txt");

	string x, y;
	int num;
	bool var = false;

	getline(myfile, x);

	while (myfile && var == false)
	{
		getline(myfile, x);

		if (x == " ")
		{
			//getline(myfile, x);
			var = true;
		}
		else
		{
			G.addVertex(x);
		}
	}

	while (!myfile.eof())
	{
		myfile>> x;
		if ( x == "Doors")
		{
			myfile >> y;
			myfile >> y;
		}
		else
		{
			myfile>>num;
			G.addWeightedEdge(y, x, num);
		}
	}

	myfile.close();

	//G.addWeightedEdge("foyer","helipad", 4);
	//G.addWeightedEdge("helipad","snakepit", 2);
	//G.addWeightedEdge("snakepit","foyer", 10);
	//G.addWeightedEdge("helipad","foyer", 4);
	//G.addWeightedEdge("foyer", "snakepit", 2);
	//G.addWeightedEdge("snakepit", "helipad", 10);

	//where you start your journey
	G.movePlayer("foyer");

	cout << "If at anytime you want to find the shortest path to a location" << endl << "just type in 'cheater'!!!!"<< endl;

	string destination;
	while(true)
	{
		//tell the player where they are
		cout << "You are in the " << G.getPlayerLocation() << ". " << G.getTime() << " hours have passed since you entered." << endl;

		//tell the player where they can go
		cout << "There are doors to " << G.getExits() << endl;

		//get desired destination from player, try to move them their
		cout << "Where would you like to go?" << endl;
		cin >> destination;
		if( G.tryToTravelTo(destination) )
			cout << "You begin walking to your destination..." << endl;
		else if (destination == "cheater")
		{
			cout<<"cheater"<<endl;
		}
		else
			cout << "You RUN INTO A WALL!!!" << endl;

		cout << endl;
	}







}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
#include <list>
using namespace std;

class weightedGraph
{
protected:
	//because this is c++....
	class vertex;
	class edge;

	class vertex
	{
	public:
		//in general, could be anything
		string data;
		list<edge*> adjacencyList;
		bool visited;
		vertex * prev;

		vertex(string a)
		{
			data = a;
			visited = false;
			prev = NULL;
		}
	};

	class edge
	{
	public:
		vertex* start;
		vertex* end;
		double weight;

		edge(vertex* s, vertex* e, double w)
		{
			start=s;
			end=e;
			weight=w;
		}
	};


	list<vertex*> vertexList;
	
	vertex* findVertex(string a)
	{
		for each( vertex* v in vertexList )
		{
			if( v->data == a )
				return v;
		}
		return NULL;
	}

	//return true if there is an edge from u to v
	//, false otherwise
	bool existsEdge(vertex* u, vertex* v)
	{
		for each(edge* x in u->adjacencyList)
		{
			if( v == x->end )
				return true;
		}
		return false;
	}

public:
	weightedGraph()
	{
	}

	//add vertex with data a to graph
	void addVertex(string a)
	{
		vertexList.push_back( new vertex(a) );
	}

	//add edge going from a to b in the graph
	void addWeightedEdge(string a, string b, double w)
	{
		vertex* vertA = findVertex(a);
		vertex* vertB = findVertex(b);

		if( vertA != NULL && vertB != NULL )
			vertA->adjacencyList.push_back( new edge(vertA,vertB,w) );
	}

	//add an edge with weight 1
	void addEdge(string a, string b)
	{
		addWeightedEdge(a,b,1);
	}

	//return edge (pointer) from a to b,
	//return NULL if no edge exists
	edge* getEdge(vertex* a, vertex* b)
	{
		for each(edge* e in a->adjacencyList)
		{
			if( e->end == b )
				return e;
		}
		return NULL;
	}

};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "weightedGraph.h"

class dungeonGraph : public weightedGraph
{
private:
	//location of player in dungeon graph
	vertex* playerVertex;

	//time of day
	double time;

public:
	dungeonGraph()
	{
		playerVertex = NULL;
		time = 0;
	}

	//return time of day
	double getTime()
	{
		return time;
	}

	//teleport player to destination
	void movePlayer(string destination)
	{
		playerVertex = findVertex(destination);
	}

	//return lcoation of player
	string getPlayerLocation()
	{
		if( playerVertex != NULL )
			return playerVertex->data;
		else
			return "Whoa!  We are nowhere....";
	}

	//return string listing exits from
	//player location
	string getExits()
	{
		string output = "";
		for each(edge* e in playerVertex->adjacencyList)
		{
			output+= e->end->data + ",";
		}
		return output;
	}

	bool tryToTravelTo(string dest)
	{
		vertex* vertd = findVertex(dest);
		edge* e = getEdge(playerVertex,vertd);

		if( e != NULL )
		{
			time += e->weight;
			movePlayer(dest);
			return true;
		}
		else
			return false;
	}
};
Topic archived. No new replies allowed.