forked from kmrodgers/GraphTheoryProgram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompleteGraphRandom.cc
276 lines (256 loc) · 8.63 KB
/
CompleteGraphRandom.cc
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* Author: Kyle Rodgers
* Date: 6/12/2014
*/
#include <list>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "Token.h"
#include "Node.h"
#include "CompleteGraphRandom.h"
#include "RandomEngine.h"
// This is the .cc file for the CompleteGraphRandom object.
/* Constructor for the CompleteGraphRandom. Takes three ints: i, numNodes, weight,
* and a bool b and assigns initial values.
*/
CompleteGraphRandom::CompleteGraphRandom(int i, int numNodes, int weight, bool b, int k)
{
edgeWeight = weight;
gameNumber = i;
totalGames = k;
watch = b;
nodeNameCount = 0;
createInitialNodes(numNodes);
createInitialEdges();
game();
}
// Destructor for the CompleteGraphRandom object
CompleteGraphRandom::~CompleteGraphRandom()
{
while(!nodeList.empty()) {
delete nodeList.front();
nodeList.pop_front();
}
nodeList.clear();
//player1.clear();
//player2.clear();
}
// Creates the initial edges
void CompleteGraphRandom::createInitialEdges()
{
for (std::list<Node*>::iterator iter1 = nodeList.begin(); iter1 != nodeList.end(); iter1++)
{
for (std::list<Node*>::iterator iter2 = nodeList.begin(); iter2 != nodeList.end(); iter2++)
{
if ((iter1) != (iter2))
{
if (edgeWeight == 0)
(*iter1)->setEdge(*iter2, getRandomNumber(10));
else
(*iter1)->setEdge(*iter2, edgeWeight);
}
}
}
}
//Desc: Creates the initial Nodes
//Input: int i, which is the name of the Node
void CompleteGraphRandom::createInitialNodes(int nodes)
{
for (int i = 1; i <= nodes; i++)
{
Node *n = new Node(i);
nodeList.push_back(n);
}
}
void CompleteGraphRandom::removeEdge()
{
int n1 = 0;
int n2 = 0;
std::cout << "Please enter the nodes you would like to remove the edge between (x y): ";
std::cin >> n1;
std::cin >> n2;
std::list<Node*>::iterator iter1;
std::list<Node*>::iterator iter2;
for (iter1 = nodeList.begin(); iter1 != nodeList.end(); iter1++)
{
if ((*iter1)->getName() == n1)
{
for (iter2 = nodeList.begin(); iter2 != nodeList.end(); iter2++)
{
if ((*iter2)->getName() == n2)
{
(*iter1)->destroyEdge(*iter2);
(*iter2)->destroyEdge(*iter1);
}
}
}
}
}
//Desc: Prints out the current version of the program the user is running
void CompleteGraphRandom::printVersion()
{
std::cout << std::endl;
std::cout << "Version: 0.2 Beta" << std::endl;
std::cout << "Author: Kyle Rodgers" << std::endl;
std::cout << "Date of Last Revision: 5/4/14 23:30" << std::endl;
}
void CompleteGraphRandom::print()
{
std::cout << "This surface contains the following nodes and edges: " << std::endl;
std::list<Node*>::iterator iter;
for (iter = nodeList.begin(); iter != nodeList.end(); iter++)
{
(*iter)->printEdges();
}
}
int CompleteGraphRandom::getRandomNumber(int size)
{
RandomEngine *rand = RandomEngine::instance();
double r = rand->getRandom();
int randNum = (int)(r * size);
return randNum;
}
int CompleteGraphRandom::getRandomEdge(int size)
{
RandomEngine *rand = RandomEngine::instance();
double r = rand->getRandom();
int randNum = (int)((r * size)+1);
return randNum;
}
void CompleteGraphRandom::rotateBar()
{
barCount++;
char barspin[4] = {'\\', '|', '/', '-'};
int whichOne;
whichOne = barCount % 4;
if (whichOne == 3)
{
std::cout << '\r' << barspin[whichOne] << " Please wait while the games are played. Game: " << gameNumber << " " << (int)(((double)gameNumber/totalGames)*100.0) << "% complete";
}
else
{
std::cout << '\r' << barspin[whichOne] << " Please wait while the games are played. Game: " << gameNumber << " " << (int)(((double)gameNumber/totalGames)*100.0) << "% complete";
}
std::cout.flush();
return;
}
void CompleteGraphRandom::game()
{
/* std::cout << edgeWeight << std::endl;
for (std::list<Node*>::iterator iter = nodeList.begin(); iter != nodeList.end(); iter++)
std::cout << (*iter)->getDegree() << std::endl;
*/ std::ofstream master_data;
//master_data.open("logs/log.txt");
//std::string moves;
// std::string gameName = std::to_string(i) + ".txt";
std::string master_data_path = "output_data/master_data.txt";
// master_data.open(path+gameName);
master_data.open(master_data_path.c_str(), std::ios_base::app);
std::list<Node*>::iterator iter1 = nodeList.begin();
Node* nextMoveNode;
Token* t = new Token(*iter1);
int nextRandomWeight = 0;
int edgeSize = 0;
int nextMove = 0;
if (watch == true)
{
std::cout << "Game " << gameNumber << std::endl;
std::cout << std::endl;
master_data << (t->getTokenLocation())->getName() << "-";
while (true)
{
if (t->getCurrentPlayerTurn() == 2)
{
nextMove = getRandomNumber(t->getTokenLocation()->getEdgeListSize());
nextMoveNode = t->getTokenLocation()->getNodeAtElement(nextMove);
}
else
nextMoveNode = t->getTokenLocation()->getMinimalDegreeNode(); // sets node at based in this number
edgeSize = t->getTokenLocation()->getWeight(nextMoveNode);
nextRandomWeight = getRandomEdge(edgeSize);
nextRandomWeight = 1;
if (nextRandomWeight == 0)
{
t->getTokenLocation()->destroyEdge(nextMoveNode);
nextMoveNode->destroyEdge(t->getTokenLocation());
}
else if (nextRandomWeight > 0)
{
master_data << nextRandomWeight << "-";
(t->getTokenLocation())->setEdge(nextMoveNode, nextMoveNode->getWeight(t->getTokenLocation()) - nextRandomWeight);
(nextMoveNode)->setEdge(t->getTokenLocation(), t->getTokenLocation()->getWeight(nextMoveNode) - nextRandomWeight);
std::cout << "Player " << t->getCurrentPlayerTurn() << " removed edge " << (t->getTokenLocation())->getName() << " - " << nextRandomWeight << " - " << (nextMoveNode)->getName() << std::endl;
//master_data << "Player " << t->getCurrentPlayerTurn() << " removed edge " << (t->getTokenLocation())->getName() << " - " << (nextMoveNode)->getName() << std::endl;
t->setPlayerTurn();
t->setTokenLocation(nextMoveNode);
master_data << (t->getTokenLocation())->getName() << "-";
}
if (t->getTokenLocation()->getEdgeListSize() == 0)
{
//moves += std::to_string((t->getTokenLocation())->getName());
t->setPlayerTurn();
std::cout << std::endl;
//master_data << "Player " << t->getCurrentPlayerTurn() << " wins!\n";
master_data << t->getCurrentPlayerTurn() << std::endl;
std::cout << "Player " << t->getCurrentPlayerTurn() << " wins!" << std::endl;
std::cout << std::endl;
delete t;
break;
}
}
}
else
{
master_data << (t->getTokenLocation())->getName() << "-";
while (true)
{
rotateBar();
for (std::list<Node*>::iterator iter = nodeList.begin(); iter != nodeList.end(); iter++)
(*iter)->setDegree();
// for (std::list<Node*>::iterator iter = nodeList.begin(); iter != nodeList.end(); iter++)
// std::cout << "Node: " << (*iter)->getName() << " Degree: " << (*iter)->getDegree() << std::endl;
if (t->getCurrentPlayerTurn() == 2)
{
nextMove = getRandomNumber(t->getTokenLocation()->getEdgeListSize());
nextMoveNode = t->getTokenLocation()->getNodeAtElement(nextMove);
}
else
nextMoveNode = t->getTokenLocation()->getMinimalDegreeNode();
edgeSize = t->getTokenLocation()->getWeight(nextMoveNode);
nextRandomWeight = getRandomEdge(edgeSize);
if (nextRandomWeight > 0)
{
master_data << nextRandomWeight << "-";
(t->getTokenLocation())->removeEdgeWeight(nextMoveNode, nextRandomWeight);
(nextMoveNode)->removeEdgeWeight(t->getTokenLocation(), nextRandomWeight);
t->setPlayerTurn();
t->setTokenLocation(nextMoveNode);
master_data << (t->getTokenLocation())->getName() << "-";
}
/* else if (nextRandomWeight == 0)
{
t->getTokenLocation()->destroyEdge(nextMoveNode);
nextMoveNode->destroyEdge(t->getTokenLocation());
}
*/ if (t->getTokenLocation()->getEdgeListSize() == 0)
{
//moves += std::to_string((t->getTokenLocation())->getName());
t->setPlayerTurn();
// std::cout << std::endl;
//master_data << "Player " << t->getCurrentPlayerTurn() << " wins!\n";
master_data << t->getCurrentPlayerTurn() << std::endl;
// std::cout << "Player " << t->getCurrentPlayerTurn() << " wins!" << std::endl;
// std::cout << std::endl;
delete t;
break;
}
}
master_data.close();
}
}