Skip to content

Commit f0deaef

Browse files
committed
Graphs part 1.
1 parent d109203 commit f0deaef

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

game/main.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,78 @@ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON A
1515
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1616
*/
1717

18+
#include <vector>
19+
#include <assert.h>
20+
21+
using namespace std;
22+
23+
class CGraph
24+
{
25+
public:
26+
class CEdge
27+
{
28+
public:
29+
int first;
30+
int second;
31+
};
32+
33+
class CNode
34+
{
35+
public:
36+
vector<int> edges;
37+
};
38+
39+
void AddNode()
40+
{
41+
nodes.push_back(CNode());
42+
}
43+
44+
void AddEdge(int a, int b)
45+
{
46+
edges.push_back(CEdge());
47+
edges.back().first = a;
48+
edges.back().second = b;
49+
50+
nodes[a].edges.push_back(edges.size()-1);
51+
nodes[b].edges.push_back(edges.size()-1);
52+
}
53+
54+
CNode* GetNode(int i)
55+
{
56+
return &nodes[i];
57+
}
58+
59+
CEdge* GetEdge(int i)
60+
{
61+
return &edges[i];
62+
}
63+
64+
protected:
65+
vector<CNode> nodes;
66+
vector<CEdge> edges;
67+
};
68+
69+
int main()
70+
{
71+
CGraph g;
72+
g.AddNode();
73+
g.AddNode();
74+
g.AddNode();
75+
76+
g.AddEdge(0, 1);
77+
g.AddEdge(1, 2);
78+
g.AddEdge(0, 2);
79+
80+
printf("Node 0 has %d edges.\n", g.GetNode(0)->edges.size());
81+
82+
for (int i = 0; i < g.GetNode(0)->edges.size(); i++)
83+
{
84+
int edge = g.GetNode(0)->edges[i];
85+
printf("Node 0 has an edge that goes between nodes %d and %d\n", g.GetEdge(edge)->first, g.GetEdge(edge)->second);
86+
}
87+
}
88+
89+
#if 0
1890
#include "game.h"
1991

2092
int main(int argc, char* argv[])
@@ -33,3 +105,4 @@ int main(int argc, char* argv[])
33105

34106
return 0;
35107
}
108+
#endif

0 commit comments

Comments
 (0)