-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·128 lines (113 loc) · 3.64 KB
/
main.c
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
/*
* File: main.c
* Authors: Gabriel Graells, Arnau Blanco, Asfandyar Abbasi.
*
*
*
* This file contains the "main" function which calls all other major functions, the "hanoiprint" which
* prints the node thats is being passed and also clear memory function which frees the memory at the
* end of the program.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "main.h"
#include "menu.h"
#include "database.h"
#include "playgame.h"
void hanoi(int nd, int torg, int tdest, int taux, stack *list){
if (nd == 1){
movedisk(torg, tdest, list, list->disk-nd);
}
else{
hanoi(nd - 1, torg, taux, tdest, list);
movedisk(torg, tdest, list, list->disk-nd);
hanoi(nd - 1, taux, tdest, torg, list);
}
}
int main(int argc, char **argv){
/*DESIGN GAME PRITING*/
printf("HANOI GAME\n===========\n\n");
/*STACK INITIALISATION*/
stack list; //stack declaration
gstruct g; //struct top play game
g.disk=NDISKS;
init_matrix(&g); //initializing the pplay game matrix.
list.num = 0; //number of moves at the star will be zero.
list.disk=NDISKS;
strcpy(list.operation,"ap");
int printfile = command(argv,argc,&list);
/*REQUEST FOR THE NUMBER OF DISKS*/
node_t* newnode=encapsulateinfo(list.disk); //Initialise the matrix according to the number of disks
createFirstNode(newnode,&list); //create new node function
hanoi(list.disk,0,1,2,&list);
//checks if it has to create a file or just print the matrice on the screen.
if(printfile==1){
create_file(&list, argv, argc);
}
else{
for(int c=0; c<=list.num; c++){
int m;
node_t *cnode;
cnode= list.top;
for(m=list.num; m>c; m--){
cnode = cnode->prev;
}
hanoiprint(cnode, list.disk);
}
}
menu_directory(&list, &g);
clearmemory(&list);
//free(&list);
return(0);
}
void hanoiprint(node_t *newnode,int numdisks){
printf(LOGDISKS,newnode->move_num,newnode->depth,newnode->disk_moved,newnode->torg,newnode->tdest);
printf("%s",NEWLINE);
/*LOOP TO PRINT EACH LINE OF THE DRAWING OF THE GAME*/
for(int i=0; i<numdisks; i++){
for(int k=0; k<NTOWERS; k++){
int dashes = newnode->matrix[i][k]; //Declaration of the value in position k i in the matrix
int dots=numdisks-dashes;
/*PRINT DOT D-max TIMES*/
for(int j=0; j<dots; j++)
printf(".");
/*PRINT UNDERSCORE max TIMES*/
for(int j=0; j<dashes; j++)
printf("-");
/*PRINT VERTICAL BAR*/
printf("|");
/*PRINT UNDERSCORE max TIMES*/
for(int j=0; j<dashes; j++)
printf("-");
/*PRINT DOT D-max TIMES*/
for(int j=0; j<dots; j++)
printf(".");
//If it's not printing the last tower, then print a tabspace
if(k<NTOWERS-1){
printf("%s",TABSPACE);
}
else{
printf("\n");
}
}
}
printf("\n");
}
//frees the memory from each node and matrix.
void clearmemory (stack *list){
for(int c=0; c<=list->num; c++){
int m;
node_t *cnode;
cnode= list->top;
for(m=list->num; m>c; m--){
cnode = cnode->prev;
}
for(int c=0;c<list->disk;c++){
for (int k= 0; k<NTOWERS; k++){
free(*cnode->matrix[c][k]);
}
}
free(cnode);
}
}