-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
92 lines (87 loc) · 2.24 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
#include "utils.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void print_help(char *exe_name)
{
fprintf(stderr, "%s [-f FILE -r NUM -h]\n", exe_name);
}
int
main(int argc, char *argv[])
{
char *text = NULL;
int opt;
while ((opt = getopt(argc, argv, "f:r:h"))!= -1)
switch (opt)
{
case 'f':
get_input_from_file(&text, optarg);
break;
case 'r':
get_random_text(&text, (size_t)atoi(optarg));
break;
case 'h':
print_help(argv[0]);
return 0;
case '?':
return 1;
}
if (! text) get_default_text(&text);
if (! text)
{
fprintf(stderr, "error: could not load input text\n");
return 1;
}
init_display_updates();
Game_Win *game_window = create_game_win();
if (!game_window)
{
fprintf(stderr, "error: game window creation failed\n");
free_text(text);
return 1;
}
Game_Status *status = create_game_status();
if (!status)
{
fprintf(stderr, "error: game status creation failed\n");
delete_game_win(game_window);
free_text(text);
return 1;
}
Stats *measures = create_stats();
if (!measures)
{
fprintf(stderr, "error: game statistics creation failed\n");
delete_game_win(game_window);
delete_game_status(status);
free_text(text);
return 1;
}
pre_start_state_config(status, measures, text);
display_status_connect(game_window, status, measures);
update_game_display(game_window, status);
int first_iter = 1;
while (1)
{
get_user_input(game_window, status);
if (first_iter)
{
start_game(status, measures);
first_iter = 0;
}
update_game_state(status, measures);
update_game_display(game_window, status);
if (end_of_game(status, measures))
{
update_game_display(game_window, status);
getchar();
break;
}
}
delete_game_win(game_window);
delete_stats(measures);
delete_game_status(status);
free_text(text);
endwin();
return 0;
}