-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTowerOfHanoi.txt
426 lines (347 loc) · 8.18 KB
/
TowerOfHanoi.txt
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
//Header files
#include <stdlib.h>
#include <GL/glut.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
GLfloat baseVertices[] = {100, -20, -300, 1260, -20, -300,
1260, 0, 300, 100, 0, -300, 100,
-20, 300, 1260, -20, 300, 1260,
0, 300, 100, 0, 300};
GLfloat baseColors[] = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
1.0, 1.0, 0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 1.0, 1.0, 0.0, 1.0,
1.0, 1.0, 1.0, 0.0, 1.0, 1.0};
GLubyte cubeIndices[]={0,3,2,1,
2,3,7,6,
0,4,7,3,
1,2,6,5,
4,5,6,7,
0,1,5,4};
GLfloat WIDTH = 1360;
GLfloat HEIGHT = 768;
GLint NUM_DISKS;
GLboolean motion = GL_FALSE;
GLfloat xangle = 0, yangle = 0;
GLfloat xlangle = 0, ylangle = 0;
#define other(i,j) (6-(i+j))
#define DISK_HEIGHT 35
#define CONE NUM_DISKS+1
#define HANOI_SOLVE 0
#define HANOI_QUIT 1
GLfloat lightTwoColor[] = {1.0, 0.0, 1, 1.0};
GLfloat lightZeroColor[] = {.3, .3, .3, .3};
GLfloat diskColor[] = {1.0, 0.0, 0.0};
GLfloat poleColor[] = {0.5, 0.5, 1.5};
//Structures for stack
typedef struct stack_node
{
int size;
struct stack_node *next;
} stack_node;
typedef struct stack
{
struct stack_node *head;
int depth;
} stack;
stack poles[4];
int push(int which, int size)
{
stack_node *next = new(stack_node);
if (!next)
{
fprintf(stderr, "out of memory!\n");
exit(-1);
}
next->size = size;
next->next = poles[which].head;
poles[which].head = next;
poles[which].depth++;
}
int pop(int which)
{
int retval = poles[which].head->size;
stack_node *temp = poles[which].head;
poles[which].head = poles[which].head->next;
poles[which].depth--;
free(temp);
return retval;
}
typedef struct move_node
{
int t, f;
struct move_node *next;
struct move_node *prev;
} move_node;
typedef struct move_stack
{
int depth;
struct move_node *head, *tail;
} move_stack;
move_stack moves;
//Initialize towers and rings
void init(void)
{
glClearColor(0.15, 0.15, 0.15, 0.15);
glShadeModel(GL_SMOOTH);
int i;
for (i = 0; i < 4; i++)
{
poles[i].head = NULL;
poles[i].depth = 0;
}
moves.head = NULL;
moves.tail = NULL;
moves.depth = 0;
for (i = 1; i <= NUM_DISKS; i++)
{
glNewList(i, GL_COMPILE);
{
glutSolidTorus(DISK_HEIGHT / 2, 12 * i, 15, 15); //glutSolidTorus(innerRadius, outerRadius, nsides, rings)
}
glEndList();
}
glNewList(CONE, GL_COMPILE);
{
glutSolidCone(15, (NUM_DISKS + 1) * DISK_HEIGHT, 20, 20); //glutSolidCone(base, height, slices, stacks);
}
glEndList();
}
void mpop(void)
{
move_node *temp = moves.head;
moves.head = moves.head->next;
free(temp);
moves.depth--;
}
void mpush(int t, int f)
{
move_node *new1 = new(move_node);
new1->t = t;
new1->f = f;
new1->next = NULL;
new1->prev = moves.tail;
if (moves.tail)
moves.tail->next = new1;
moves.tail = new1;
if (!moves.head)
moves.head = moves.tail;
moves.depth++;
}
void update(void)
{
while(motion == GL_TRUE && motion++) //To solve it at once without mouse clicks, comment this while loop
{
glutPostRedisplay();
}
}
void DrawPost(GLfloat xcenter)
{
glPushMatrix();
{
glTranslatef(xcenter, 0, 0);
glRotatef(90, -1, 0, 0);
glCallList(CONE);
}
glPopMatrix();
}
void DrawPosts(void)
{
glColor3fv(poleColor);
glLineWidth(10);
glMaterialfv(GL_FRONT, GL_DIFFUSE, poleColor);
DrawPost((int)(WIDTH / 4));
DrawPost((int)(2 * WIDTH / 4));
DrawPost((int)(3 * WIDTH / 4));
}
void DrawDisk(GLfloat xcenter, GLfloat ycenter, GLfloat size)
{
glPushMatrix();
{
glTranslatef(xcenter, ycenter, 0);
glRotatef(90, 1, 0, 0);
glCallList(size);
}
glPopMatrix();
}
void DrawDisks(void)
{
int i;
stack_node *temp;
int xcenter, ycenter;
glColor3fv(diskColor);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diskColor);
for (i = 1; i <= 3; i++)
{
xcenter = i * WIDTH / 4;
for (temp = poles[i].head, ycenter = DISK_HEIGHT * poles[i].depth - DISK_HEIGHT / 2; temp; temp = temp->next, ycenter -= DISK_HEIGHT) {
DrawDisk(xcenter, ycenter, temp->size);
}
}
}
#define MOVE(t,f) mpush((t),(f))
static void mov(int n, int f, int t)
{
int o;
if (n == 1)
{
MOVE(t, f);
printf("\nDisk moves From Peg: %d -> Peg: %d",f,t);
return;
}
o = other(f, t);
mov(n - 1, f, o);
mov(1, f, t);
mov(n - 1, o, t);
}
void Drawcube(){
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);
glColor3fv(baseColors);
glMaterialfv(GL_FRONT, GL_DIFFUSE, baseColors);
}
void draw(void)
{
int t, f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
{
glTranslatef(WIDTH / 2, HEIGHT / 2, 0);
glRotatef(xangle, 0, 1, 0);
glRotatef(yangle, 1, 0, 0);
glTranslatef(-WIDTH / 2, -HEIGHT / 2, 0);
DrawPosts();
DrawDisks();
Drawcube();
}
glPopMatrix();
if (motion && moves.depth)
{
t = moves.head->t;
f = moves.head->f;
push(t, pop(f));
mpop();
}
glutSwapBuffers();
}
void hanoi_menu(int value)
{
switch (value)
{
case HANOI_SOLVE: motion = !motion;
if(motion)
{
glutIdleFunc(update);
}
else
{
glutIdleFunc(NULL);
}
break;
case HANOI_QUIT: exit(0);
break;
}
glutPostRedisplay();
}
int oldx, oldy;
GLboolean leftb = GL_FALSE, middleb = GL_FALSE;
void hanoi_mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
oldx = x;
oldy = y;
if (state == GLUT_DOWN)
leftb = GL_TRUE;
else
leftb = GL_FALSE;
}
if (button == GLUT_MIDDLE_BUTTON)
{
oldx = x;
oldy = y;
if (state == GLUT_DOWN)
middleb = GL_TRUE;
else
middleb = GL_FALSE;
}
}
void hanoi_visibility(int state)
{
if (state == GLUT_VISIBLE && motion)
{
glutIdleFunc(update);
}
else
{
glutIdleFunc(NULL);
}
}
void hanoi_motion(int x, int y)
{
if (leftb)
{
xangle -= (x - oldx);
yangle -= (y - oldy);
}
if (middleb)
{
xlangle -= (x - oldx);
ylangle -= (y - oldy);
}
oldx = x;
oldy = y;
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
int i;
printf("Enter the number of Disks: ");
scanf("%d",& NUM_DISKS);
double DISK_MOVES = pow(2.0,NUM_DISKS)-1;
printf("\nTotal number of Disk movements for %d disks : %.0lf\n",NUM_DISKS, DISK_MOVES);
if(NUM_DISKS>0)
{
glutInit(&argc, argv);
glutInitWindowSize(1000,600);
glutInitWindowPosition(100,100);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Tower of Hanoi");
glutDisplayFunc(draw);
glViewport(0, 0, (int)WIDTH, (int)HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, -70.0, HEIGHT, -10000, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0, 0, 0, 0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT2, GL_DIFFUSE, lightTwoColor);
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 10);
glEnable(GL_LIGHT2);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glutMouseFunc(hanoi_mouse);
glutMotionFunc(hanoi_motion);
glutVisibilityFunc(hanoi_visibility);
glutCreateMenu(hanoi_menu);
glutAddMenuEntry("Solve", HANOI_SOLVE);
glutAddMenuEntry("Quit", HANOI_QUIT);
glutAttachMenu(GLUT_RIGHT_BUTTON);
init();
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, baseVertices);
glColorPointer(3,GL_FLOAT, 0, baseColors);
for (i = 0; i < NUM_DISKS; i++)
push(1, NUM_DISKS - i);
mov(NUM_DISKS, 1, 3);
glutMainLoop();
return 0;
}
else
{
exit(0);
}
}