/
main.cpp
314 lines (287 loc) · 8.8 KB
/
main.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
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
#include "Include.h"
//this indicates the source input and output file for the matrix
char *fileName, *fileOut;
//a pointer to the visualization object to draw scenes
Visualization *v;
//these are pointers to appropriate matrix to be instantiated
payoffMatrix *pom; payoffMatrixN *pomn;
//keep track of whether the game is two person or more
bool nPerson = false;
//keep track of whether the game is a zero sum game
bool zeroSum = true;
//these are to keep track of the mouse motion
bool leftButton = false;
//this function is used to display OpenGL stuff
void display(void) {
//begin display process
v->beginDisplay();
/*For 2D Initialization*/
if(!nPerson)
{
//generate lines and draw
LineCollection lc = v->buildLines(*pom);
v->drawLines(lc);
//set color
v->color(1, 1, 1);
//generate points and draw
PointCollection<double> pc = v->buildPoints(*pom);
v->drawPoints(pc);
//generate texts and draw
TextCollection tc = v->buildTexts(*pom);
v->drawTexts(tc);
}
else
{
int nX = pomn->getNumX();
int nY = pomn->getNumY();
int nZ = pomn->getNumZ();
int numPoints = nX * nY * nZ;
int numLines = 3*nX*nY*nZ - nX*nY - nY*nZ - nZ*nX;
v->drawPoints(v->buildPoints(*pomn), numPoints);
v->drawLines(v->buildLines(*pomn), numLines+3);
v->drawTexts(v->buildTexts(*pomn), numPoints);
v->drawArrows(*pomn);
}
//end display process
v->endDisplay();
}
void init(void) {
v->init();
}
void my_keyboard(unsigned char key, int x, int y)
{
//when user pressed escape, quit
if (key == 27){
exit(0);
}
if(nPerson){
if(key == 'w')
{
v->TranslateZ(1);
}
if(key == 's')
{
v->TranslateZ(-1);
}
if(key == 'a')
{
v->TranslateX(1);
}
if(key == 'd')
{
v->TranslateX(-1);
}
}
v->Refresh();
}
//these keep track of where the mouse was a moment ago
//in order to implement mouse dragged
int prevX, prevY;
void mouse(int button, int state, int x, int y) {
//this takes care of the mouse action
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
leftButton = true;
prevX = x;
prevY = y;
}
if(state == GLUT_UP)
{
leftButton = false;
}
v->Refresh();
}
void mouse_dragged(int x, int y) {
//this takes care of the rotating scene for a mouse
v->RotateY(x-prevX);
v->RotateX(y-prevY);
prevX = x;
prevY = y;
v->Refresh();
}
//this is the menu for the GL scene
void myMenu(int value){
if(!nPerson)
{
if (value == 1){
v = new Envelope();
}
if (value == 2){
v = new Flow();
}
}
else
{
if (value == 1){
v = new Flow3D();
}
}
v->Refresh();
}
int main(int argc, char *argv[])
{
string s;
fileName = new char[50];
cout << "Enter file name: " ;
cin >> fileName;
s = fileName;
s = "Matrix Results For " + s;
fileOut = &s[0];
int matrixRows=-1,matrixCols=-1,matrixHeights=-1;
//set matrix number of rows and columns
//or set numX, numY, numZ for 3-person game if necessary
ifstream inFile(fileName);
//ifstream inFile(&s[0]);
if(!inFile.is_open())
{
cout << "File does not exist or is corrupted." << endl;
system("PAUSE");
exit(0);
}
ofstream outFile(fileOut);
if(!inFile.eof());
inFile >> matrixRows;
if(!inFile.eof());
inFile >> matrixCols;
if(!inFile.eof());
inFile >> matrixHeights;
inFile.close();
if(matrixRows == -1 || matrixCols == -1)
{
cout << "File does not exist or have invalid matrix information." << endl;
system("Pause");
return EXIT_SUCCESS;
}
nPerson = baseMatrix::checkNPersonGame(fileName, matrixRows, matrixCols);
zeroSum = baseMatrix::check2PersonZeroSum(fileName, matrixRows, matrixCols);
//initialize payoff matrix
if(!nPerson)
{
if(zeroSum)
{
payoffMatrix *Matrix = payoffMatrix::createGame(fileName, matrixRows, matrixCols);
pom = Matrix;
outFile << pom->getValue() << endl;
pom->writeSolutions(outFile);
}
else
{
pom = payoffMatrix::createGameNzs(fileName, matrixRows, matrixCols);
outFile << pom->getValueNzs() << endl;
for(int i=1; i<=2; i++)
{
payoffMatrix* pmzs = pom->getZeroSumSubGame(i);
outFile << "/////////////////////////////////////////////"<<endl;
if(i==2) outFile << "Rose's equalizing game: " << endl;
if(i==1) outFile << "Colin's equalizing game: " << endl;
outFile << pmzs->getValue() << endl;
pmzs->writeSolutions(outFile);
}
}
}
else
{
pomn = payoffMatrixN::createGame(fileName, matrixRows, matrixCols, matrixHeights);
outFile << pomn->getValue() << endl;
//if the game is zero sum n person game
if(pomn->IsZeroSum())
{
for(int i=1; i<=3; i++)
{
//switch it to 2 person zero sum
pom = pomn->get2PersonGame(i);
outFile << "///////////////////////////////////////////////" << endl;
if(i==1) outFile << "Rose's Game:" << endl;
if(i==2) outFile << "Colin's Game:" << endl;
if(i==3) outFile << "Larry's Game:" << endl;
if(pom!=(payoffMatrix*)NULL)
{
//now solve the 2 person zero sum game
outFile << pom->getValue() << endl;
pom->writeSolutions(outFile);
}
}
}
//case of non zero sum n person game
else
{
for(int i=1; i<=3; i++)
{
//first switch it to 2 person non zero sum game
//using the idea of coalition
pom = pomn->get2PersonGameNzs(i);
if(pom != (payoffMatrix*)NULL)
{
payoffMatrix* pmzs;
if(i==1) outFile << "Rose's prudential game: " << endl;
if(i==2) outFile << "Colin's prudential game: " << endl;
if(i==3) outFile << "Larry's prudential game: " << endl;
outFile << pom->getValueNzs() << endl;
for(int j=1; j<=2; j++)
{
if(i==1 && j==2) outFile << "Rose's equalizing game against Colin and Larry: " << endl;
if(i==1 && j==1) outFile << "Colin and Larry's equalizing game against Rose: " << endl;
if(i==2 && j==2) outFile << "Colin's equalizing game against Rose and Larry: " << endl;
if(i==2 && j==1) outFile << "Rose and Larry's equalizing game against Colin: " << endl;
if(i==3 && j==2) outFile << "Larry's equalizing game against Rose and Colin: " << endl;
if(i==3 && j==1) outFile << "Rose and Colin's equalizing game against Larry: " << endl;
//get the prudential strategy game solution
pmzs = pom->getZeroSumSubGame(j);
outFile << pmzs->getValue() << endl;
pmzs->writeSolutions(outFile);
}
}
}
}
}
/*---------------------------------------------------------------------------------*/
//initialize visualization object pointer to default to the flow diagram
if(!nPerson)
{
if(zeroSum)
v = new Flow();
else
v = new FlowNzs();
}
else
v = new Flow3D();
//OpenGL loop
glutInit(&argc, argv);
//initialize the proper rendering mode for either 2D or 3D
if(!nPerson)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
else
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(WIDTH,HEIGHT);
glutCreateWindow("Game Theory");
init();
glutDisplayFunc(display);
glutCreateMenu(myMenu);
//change the menu accordingly to whether a 2-person or n-person game is loaded
if(!nPerson)
{
if(zeroSum)
{
if(pom->Envelopable())
glutAddMenuEntry("Envelope", 1);
glutAddMenuEntry("Flow", 2);
}
}
else
{
glutAddMenuEntry("Flow3D", 1);
}
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutKeyboardFunc(my_keyboard);
//this is for 3D only
if(nPerson)
{
glutMotionFunc(mouse_dragged);
glutMouseFunc(mouse);
}
glutMainLoop();
outFile.close();
system("PAUSE");
return EXIT_SUCCESS;
}