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
|
#include "menu.h"
#include <stdlib.h>
#include <ncurses.h>
#include "color.h"
#include "game/entry.h"
#include "utilities.h"
#include "maze_generator.h"
#include "file.h"
#define MO_LOAD_FROM_FILE 1
#define MO_SAVE_TO_FILE 2
#define MO_GENERATE 3
#define MO_START 4
#define MO_QUIT 5
#define MO_INVALID_OPTION 6
int choose_menu_option()
{
attron(COLOR_PAIR(CI_DEFAULT));
clear();
printw("1. Load from file.\n");
printw("2. Save to file.\n");
printw("3. Random maze.\n");
printw("4. Start the game.\n");
printw("q. Quit.\n");
printw("Choose:\n");
curs_set(1);
echo();
refresh();
//char buffer[8];
//wgetnstr(stdscr, buffer, 8);
switch (getch())
{
case '1':
return MO_LOAD_FROM_FILE;
break;
case '2':
return MO_SAVE_TO_FILE;
break;
case '3':
return MO_GENERATE;
break;
case '4':
return MO_START;
break;
case 'q':
return MO_QUIT;
break;
}
return MO_INVALID_OPTION;
}
int get_file_path(char *path)
{
attron(COLOR_PAIR(CI_DEFAULT));
clear();
printw("Enter the path of file:\n");
curs_set(1);
echo();
wgetnstr(stdscr, path, 256);
return *path == 0 ? -1 : 1;
}
int save_file_path(char *path)
{
return get_file_path(path);
}
int open_file_path(char *path)
{
return get_file_path(path);
}
void get_size(int *width, int *height)
{
attron(COLOR_PAIR(CI_DEFAULT));
while (1)
{
clear();
printw("Enter a size:\n");
curs_set(1);
echo();
char buffer[16];
wgetnstr(stdscr, buffer, 16);
char *left, *right;
if (find_lr_ints(buffer, &left, &right))
{
char *end;
*width = strtol(left, &end, 10);
printw("\nwidth=%d\n", *width);
if (*end == '%')
{
*width = ((double)*width / 100) * getmaxx(stdscr);
if (!is_valid_maze_size(*width))
(*width)--;
}
*height = strtol(right, &end, 10);
printw("\nheight=%d\n", *height);
if (*end == '%')
{
*height = ((double)*height / 100) * getmaxy(stdscr);
if (!is_valid_maze_size(*height))
(*height)--;
}
printw("\nwidth=%d\nheight=%d\n", *width, *height);
}
if (is_valid_maze_size(*width) && is_valid_maze_size(*height))
return;
printw("\nWrong size number!\n");
while (getch() != 10)
;
}
}
void main_menu_loop(struct app *app)
{
while (1)
{
switch (choose_menu_option())
{
case MO_LOAD_FROM_FILE:
{
char path[256];
if (open_file_path(path))
{
app->maze = maze_from_file(path);
if (app->maze == NULL)
{
printw("\nFile is not exsist!\n");
while (getch() != 10)
;
}
}
}
break;
case MO_SAVE_TO_FILE:
{
char path[256];
if (save_file_path(path))
{
maze_to_file(path, app->maze);
}
}
break;
case MO_GENERATE:
{
app->maze = malloc(sizeof(struct maze));
get_size(&(app->maze->width), &(app->maze->height));
//maze->width = 41;
//maze->height = 21;
app->maze->map = malloc(sizeof_2d(app->maze->width, app->maze->height, sizeof(int)));
init2d((void **)app->maze->map, app->maze->width, app->maze->height, sizeof(int));
generate_maze(app->maze);
}
break;
case MO_START:
{
if (app->maze == NULL)
{
printw("\nThere is no maze!\n");
while (getch() != 10)
;
}
else if (game(app) == GR_QUIT)
{
return;
}
}
break;
case MO_QUIT:
{
return;
}
case MO_INVALID_OPTION:
{
printw("\nError!\n");
while (getch() != 10)
;
}
break;
}
//TODO: Handle.
}
}
|