-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
163 lines (136 loc) · 3.77 KB
/
Graph.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "Graph.h"
using namespace std;
Graph::Graph(){
current = nullptr;
source = nullptr;
statevar = 0;
};
Graph::~Graph(){
};
//return the location of pointer tracking current story location/state
shared_ptr<Node> Graph::getCurrent (){
if (current == nullptr){
current = source;
}
return current;
}
//update location of current location, finds adjacent edges and chooses the one matching user input (based on edge weight), int weight as input
//and each answer will correspond to a certain weight
void Graph::setCurrent (int weight){
//if current hasn't been set yet
if (current == nullptr){
current = source;
}
//pointer to edge
shared_ptr<Edge> e;
//pointer to node
shared_ptr<Node> next;
//get adjacent edges of current node
set<shared_ptr<Edge> > adedge = getAdjacentEdges (current);
for (set<shared_ptr<Edge> >::iterator it= adedge.begin(); it != adedge.end(); it++){
e = *it;
if (e->getStart() == current && e->getWeight() == weight){
current->predecessor = current;
next = e->getEnd();
current = next;
}
}
}
//get adjacent edges of node
set<shared_ptr<Edge> > Graph::getAdjacentEdges (shared_ptr<Node> node){
//initializing return set
set<shared_ptr<Edge> > ret;
//iterating over edges stored in edges vector
for (vector<shared_ptr<Edge> >::iterator it = edges.begin(); it != edges.end(); it++) {
shared_ptr<Edge> edge = *it;
//if the edge's start node == search node, add it to the set of
//adjacent edges
if (edge->getStart() == node) {
ret.insert(edge);
}
}
return ret;
}
//set start of story node
void Graph::setSource (shared_ptr<Node> sourcenode){
source = sourcenode;
}
//get start of story node
shared_ptr<Node> Graph::getSource(){
return source;
}
//add storyNode
void Graph::addNode (shared_ptr<Node> n){
nodes.push_back (n);
}
vector<shared_ptr<Node> > Graph::getNodes() {
return nodes;
}
vector <shared_ptr<Edge> > Graph::getEdges (){
return edges;
}
//add edge
void Graph::addEdge (shared_ptr<Edge> e){
edges.push_back (e);
}
int Graph::choice (string userc, shared_ptr<Node> node){
while (userc == "q" || userc == "Q" || userc == "a" || userc == "A" || userc == "b" || userc == "B"){
if (userc == "q" || userc == "Q"){
return 9;
}
else {
if (userc == "a" || userc == "A"){
return node ->achoice;
}
if (userc == "b" || userc == "B"){
return node ->bchoice;
}
}
}
cout << "Error: please choose a or b, or press q to quit."<< endl << endl;
string x;
cin >> x;
return choice (x, node);
}
void Graph::clearCurrent(){
current = nullptr;
}
void Graph::playGame(shared_ptr<Node> node){
//checking to see if it's an end or quite node
if (node->edges == 0){
node->printFormatText();
cout << "Thank you for playing.";
return;
}
//print node's text
node->printFormatText();
string userchoice;
cin >> userchoice;
//checking to see if this node effects statevar
if (node->statechange == true){
updateState (node->changevar);
}
//check to see if this node is impacted by statevar
if (node->isimpact == true){
setCurrent (statevar);
playGame (current);
}
//otherwise proceed as usual based on userchoice
else {
int weight = choice (userchoice, node);
setCurrent (weight);
playGame(current);
}
}
void Graph::quitNode (shared_ptr<Node> quit){
for (int i=0; i<nodes.size(); i++){
if (nodes[i]->edges !=0){
shared_ptr<Edge> edge (new Edge (nodes[i], quit));
edge->setWeight(9);
edges.push_back(edge);
}
}
}
void Graph::updateState (int var){
statevar = var;
}