forked from kmrodgers/GraphTheoryProgram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHypercubeGraphLogical.cc
461 lines (430 loc) · 14.4 KB
/
HypercubeGraphLogical.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// HypercubeGraphLogical.cc
#include <list>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <fstream>
//#include <boolean>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#include "Token.h"
#include "Node.h"
#include "HypercubeGraphLogical.h"
#include "RandomEngine.h"
/*
This is the .cpp file for the HypercubeGraphLogical object.
*/
//Desc: Constructor for the HypercubeGraphLogical object. Constructs a HypercubeGraphLogical of Nodes,
// and creates edges between them.
//Input: int i for the number of Nodes
HypercubeGraphLogical::HypercubeGraphLogical(int i, int j, bool b, int k, int size)
{
gameNumber = i;
edgeWeight = j;
totalGames = k;
numberOfNodes = (int)pow(2, size);
dimension = size;
watch = b;
nodeNameCount = 0;
for (int l = 1; l <= numberOfNodes; l++)
{
createInitialNodes(l);
nodeNameCount++;
}
createInitialEdges();
std::ofstream master_data;
// print();
game();
}
//analysis(numGames);
//Desc: Destructor for the HypercubeGraphLogical object. Deletes the nodeList
// and frees up memory.
HypercubeGraphLogical::~HypercubeGraphLogical()
{
while(!nodeList.empty()) {
delete nodeList.front();
nodeList.pop_front();
}
nodeList.clear();
//player1.clear();
//player2.clear();
}
//Desc: Creates the initial edges
void HypercubeGraphLogical::createInitialEdges()
{
int creationMatrix [numberOfNodes][numberOfNodes];
int dim3size = (int)pow(2,3);
int dim4size = (int)pow(2,4);
int dim5size = (int)pow(2,5);
int dim6size = (int)pow(2,6);
int dim7size = (int)pow(2,7);
int dim8size = (int)pow(2,8);
int dim9size = (int)pow(2,9);
int dim3 [dim3size][dim3size];
int dim4 [dim4size][dim4size];
int dim5 [dim5size][dim5size];
int dim6 [dim6size][dim6size];
int dim7 [dim7size][dim7size];
int dim8 [dim8size][dim8size];
int dim9 [dim9size][dim9size];
if (dimension <= 3)
{
for (int i = 0; i < numberOfNodes; i++)
{
for (int j = 0; j < numberOfNodes; j++)
{
if (i%2 == 0)
{
if (j%2 == 0)
creationMatrix[i][j] = 0;
else
creationMatrix[i][j] = 1;
}
else
{
if (j%2 == 0)
creationMatrix[i][j] = 1;
else
creationMatrix[i][j] = 0;
}
}
}
}
else //creates the hypercube above dimension 4
{
for (int i = 0; i < (int)pow(2, 3); i++) //creates dim3 matrix
{
for (int j = 0; j < (int)pow(2, 3); j++)
{
if (i%2 == 0)
{
if (j%2 == 0)
dim3[i][j] = 0;
else
dim3[i][j] = 1;
}
else
{
if (j%2 == 0)
dim3[i][j] = 1;
else
dim3[i][j] = 0;
}
}
}
for (int i = 0; i < (int)pow(2, 3); i++) //copies dim3 to dim4 top-left quadrant
{
for (int j = 0; j < (int)pow(2, 3); j++)
{
dim4[i][j] = dim3[i][j];
}
}
for (int i = 0; i < (int)pow(2, 4) / 2; i++) //copies dim3 to bottom-right quadrant
{
for (int j = 0; j < (int)pow(2, 4) / 2; j++)
{
dim4[i + ((int)pow(2, 4) / 2)][i + ((int)pow(2, 4) / 2)] = dim3[i][j];
}
}
for (int i = 0; i < numberOfNodes/2; i++) //covers the top-left quadrant
{
for (int j = 0; j < numberOfNodes/2; j++)
{
if (i%2 == 0)
{
if (j%2 == 0)
creationMatrix[i][j] = 0;
else
creationMatrix[i][j] = 1;
}
else
{
if (j%2 == 0)
creationMatrix[i][j] = 1;
else
creationMatrix[i][j] = 0;
}
}
}
for (int i = numberOfNodes/2; i < numberOfNodes; i++) //covers the bottom right quadrant
{
for (int j = numberOfNodes/2; j < numberOfNodes; j++)
{
if (i%2 == 0)
{
if (j%2 == 0)
creationMatrix[i][j] = 0;
else
creationMatrix[i][j] = 1;
}
else
{
if (j%2 == 0)
creationMatrix[i][j] = 1;
else
creationMatrix[i][j] = 0;
}
}
}
int row = 0;
int col = 0;
for (int i = 0; i < numberOfNodes/2; i++) //covers the top right quadrant
{
for (int j = numberOfNodes/2; j < numberOfNodes; j++)
{
creationMatrix[i][j] = 0; //fills array with zeroes
}
}
row = 0;
col = numberOfNodes/2;
while (col != numberOfNodes)
{
creationMatrix[row][col] = 1;
row++;
col++;
}
for (int i = numberOfNodes/2; i < numberOfNodes; i++) //covers the bottom left
{
for (int j = 0; j < numberOfNodes/2; j++)
{
creationMatrix[i][j] = 0; //fills array with zeroes
}
}
row = numberOfNodes/2;
col = 0;
while (col != numberOfNodes/2)
{
creationMatrix[row][col] = 1;
row++;
col++;
}
}
for (int i = 0; i < numberOfNodes; i++)
{
for (int j = 0; j < numberOfNodes; j++)
std::cout << creationMatrix[i][j] << " ";
std::cout << std::endl;
}
/* std::list<Node*>::iterator iter1 = nodeList.begin();
std::list<Node*>::iterator iter2 = nodeList.begin();
for (int i = 0; i < numberOfNodes; i++)
for (int j = 0; j < numberOfNodes; j++)
{
if (creationMatrix[i][j] == 1)
{
std::advance(iter1, i);
std::advance(iter2, j);
(*iter1)->setEdge(*iter2, edgeWeight);
(*iter2)->setEdge(*iter1, edgeWeight);
iter1 = nodeList.begin();
iter2 = nodeList.begin();
}
}
*/}
//Desc: Creates the initial Nodes
//Input: int i, which is the name of the Node
void HypercubeGraphLogical::createInitialNodes(int i)
{
Node *n = new Node(i);
nodeList.push_back(n);
}
void HypercubeGraphLogical::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 HypercubeGraphLogical::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 HypercubeGraphLogical::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 HypercubeGraphLogical::getRandomNumber(int size)
{
RandomEngine *rand = RandomEngine::instance();
double r = rand->getRandom();
int randNum = (int)(r * size);
return randNum;
}
int HypercubeGraphLogical::getnumberOfNodes()
{
return numberOfNodes;
}
//This is the method that plays the game
//The master data is sent to the file /output_data/mater_data.txt
void HypercubeGraphLogical::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;
}
int HypercubeGraphLogical::getRandomEdge(int size)
{
RandomEngine *rand = RandomEngine::instance();
double r = rand->getRandom();
int randNum = (int)((r * size)+1);
return randNum;
}
void HypercubeGraphLogical::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();
}
}
/*
To do: global variables of arrays, array size? That way, can pass pointers and array size to loops ((int)power(2, x)).
Hash table to say "I want array 3 and it returns pointer to array"
*/