-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.c
executable file
·191 lines (178 loc) · 5.88 KB
/
menu.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
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
/*
* File: menu.c
* Authors: Gabriel Graells, Arnau Blanco, Asfandyar Abbasi.
*
* This file contains all the funtions relating to the menu and selecting the options. It also
* contains the command function which compares the commands by the user with different values.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "menu.h"
#include "playgame.h"
#include "main.h"
void play_game(gstruct *ll, stack *l)
{int end=1;
/*COUNTER INSIDE A LOOP*/
while(end_game(ll) != 1&&end!=0)
{
end=playgame_directory(ll,l);
}
}
/*FUNCTION THAT EXECUTES THE hanoiplus COMMAND*/
int command(char **argv, int argc, stack *list){
int option = 0;
int i = 0;
char str[MAX_FIELD];
while(i < argc){
if(argv[i][0] == '-' && argv [i][1] == 'd' && argv[i+1]){
list->disk = atoi(argv[i+1]);
strcat(str,argv[i+1]);
}
if(argv[i][0] == '-' && argv [i][1] == 'f' && argv[i+1]){
strcpy(list->fname,argv[i+1]);
option = 1;
strcat(str,argv[i+1]);
}
if(argv[i][0] == '-' && argv [i][1] == 'o' && argv[i+1]){
strlcpy(list->operation,argv[i+1],MAX_FIELD);
strcat(str,argv[i+1]);
}
i++;
}
return option;
}
// Creating a file to save the matrix
void create_file(stack *list, char **argv, int argc){
FILE *f; //Declare the file
char path[MAX_FIELD]; //Declaration of the path of the file
sprintf(path,"%s%s",list->fname,".txt"); //path = name.txt (THIS IS NOT WORKING)
//If new_file is true, then create a new file or overwrite it. Otherwise, append the content at the end of it.
if(strcmp(list->operation,"w")==0){
f = fopen(path,"w"); //Open file with given name
}else if(strcmp(list->operation,"ap")==0){
f = fopen(path,"a"); //Append file with given name
}
//Check if the file has been opened correctly
if(f==NULL){
printf("Error while creating the file.");
exit(0);
}
//Printing stuff on he file.
fprintf(f,"%s%s",LINE,NEWLINE);
fprintf(f,"Command line entered:");
for(int c = 0; c < argc; c++){
fprintf(f,"%s ",argv[c]);
}
//printing the rest of file header.
fprintf(f,"%s",NEWLINE);
fprintf(f,"Number of Towers: %d\n",NTOWERS);
fprintf(f,"Number of Discs: %d\n",list->disk);
fprintf(f,"Output filename: %s\n",list->fname);
fprintf(f,"Total number of moves: %d\n",list->num);
fprintf(f,"File operation: %s\n\n",list->operation);
//Calculating and printing time.
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ("Current local time and date: %s", asctime (timeinfo) );
fprintf (f, "INIT TIME: %s", asctime (timeinfo) );
fprintf(f,"%s%s",LINE,NEWLINE);
//Printing all the nodes on the file by going to the last node and starting printing from there by calling the printfile function.
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;
}
file_print(list,f,cnode);
}
//file close
fclose(f);
}
void file_print(stack *list, FILE *f, node_t *cnode){
/*LOOP THAT WILL PRINT ALL THE MOVES IN THE FILE*/
int dots, bars=0;
//hanoiprint(list->top,list->disks);
for(int i=0; i<list->disk; i++){
for(int k=0; k<NTOWERS; k++){
bars = cnode->matrix[i][k]; //Number to bars to print same as the size of disk.
dots=list->disk-bars; //Calculating number of dots to print
/*PRINT DOT number of disks-mat TIMES*/
for(int j=0; j<dots; j++)
fprintf(f,"%s",".");
/*PRINT UNDERSCORE mat TIMES*/
for(int j=0; j<bars; j++)
fprintf(f,"%s","-");
/*PRINT VERTICAL BAR*/
fprintf(f,"%s","|");
/*PRINT UNDERSCORE mat TIMES*/
for(int j=0; j<bars; j++)
fprintf(f,"%s","-");
/*PRINT DOT umber of disks-mat TIMES*/
for(int j=0; j<dots; j++)
fprintf(f,"%s",".");
//If it's not printing the last tower, then print a tabspace
if(k<NTOWERS){
fprintf(f,"%s","\t");
}else{
fprintf(f,"%s","\n");
}
}
fprintf(f,"%s","\n");
}
fprintf(f,"%s","\n");
}
int display_menu()
{
int option;
printf("\nMenu Options.\n"
"1.Show Hanoi solution. Type 1.\n"
"2.Play Hanoi. Type 2.\n"
"0.Exit. Type 0.\n\n"
"Option: ");
scanf("%d", &option);
return option;
}
void menu_directory(stack *l, gstruct *g)
{
int option;
option = display_menu();
while (option != 0)
{
switch(option)
{
case OPTION_1:
show_game(l);
break;
case OPTION_2:
play_game(g,l);
break;
default:
printf("\nInvalid option.\n");
break;
}
option = display_menu();
}
}
/*FUNCTION THAT SHOWS A MOVE ON-SCREEN*/
void show_game(stack *l){
int move; //Declare move which stores the input of the user
node_t *current_node = l->top; //Create a node that points to the last node
printf("%sType the number of the move you want to see: ",NEWLINE);
scanf("%d",&move); //Scan user's input
//If the user's input is not valid, then print a warning message
if(move >= 0 && move <= l->num){
//Loop to jump from one node to another
for(int m=l->num; m>move; m--){
current_node = current_node->prev;
}
hanoiprint(current_node,l->disk); //Print the move
}else{
printf("This move does not exist.%s",NEWLINE);
}
}