-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
145 lines (134 loc) · 3.46 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dskrypny <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/07/21 09:57:26 by dskrypny #+# #+# */
/* Updated: 2018/07/22 23:06:05 by dskrypny ### ########.fr */
/* */
/* ************************************************************************** */
#include "header.h"
static int end_game(int numbers[4][4], t_result *res)
{
short i;
short j;
short count;
short max;
count = 0;
max = 0;
i = -1;
while (++i < 4)
{
j = -1;
while (++j < 4)
{
if (numbers[i][j] != 0)
count += check_number(numbers, i, j);
if (numbers[i][j] > max)
max = numbers[i][j];
}
}
if (count == 16)
return (3);
if (max == WIN_VALUE && !res->won)
return (2);
return (0);
}
static int hook_keys(int key, t_result *res, int numbers[4][4],
int reserve[4][4])
{
int result;
short end;
result = 1;
if (key == KEY_UP)
result = move_up(numbers, res);
else if (key == KEY_DOWN)
result = move_down(numbers, res);
else if (key == KEY_LEFT)
result = move_left(numbers, res);
else if (key == KEY_RIGHT)
result = move_right(numbers, res);
else if (key == 117)
copy_numbers(reserve, numbers);
else
return (1);
if (result && key != 117)
add_number(numbers);
print_numbers(res->win[0], numbers);
if ((end = end_game(numbers, res)))
return (end);
return (1);
}
static int print_info(short c, t_result *res)
{
int key;
mvwprintw(res->win[1], 1, 1, "%55s", "");
if (c == 1)
mvwprintw(res->win[1], 1, 3, "Goodbye, press any key to exit");
else if (c == 3)
mvwprintw(res->win[1], 1, 3,
"You lost! Press R to replay or ESC to exit");
else if (c == 2)
{
mvwprintw(res->win[1], 1, 3,
"Well done! Press R to replay or any key to continue");
res->won = 1;
}
wrefresh(res->win[1]);
keypad(res->win[1], 1);
key = wgetch(res->win[1]);
if ((c == 1) || (c == 2 && key != 114))
return ((c == 1) ? 27 : key);
while (key != 114 && key != 27)
key = wgetch(res->win[1]);
(key == 114) ? create_numbers(res) : NULL;
return (key);
}
static void doing(t_result res)
{
int reserve[4][4];
int key;
short c;
c = 1;
res.won = 0;
res.result = 0;
create_numbers(&res);
print_result(&res);
keypad(res.win[0], 1);
while ((key = wgetch(res.win[0])) != 27)
{
draw_map(res.win[0]);
update_map(res.win[1], res.win[2]);
print_numbers(res.win[0], res.numbers);
print_result(&res);
if ((c = hook_keys(key, &res, res.numbers, reserve)) != 1)
key = print_info(c, &res);
if (key == 27)
break ;
}
print_info(1, &res);
}
int main(void)
{
t_result res;
short c;
if (WIN_VALUE < 4 || WIN_VALUE > 16384 || !is_power2(WIN_VALUE))
{
ft_printf("%{red}s\n", "Invalid number for win!");
return (1);
}
res.name = read_name();
initscr();
cbreak();
noecho();
c = -1;
while (++c < 3)
res.win[c] = NULL;
init_window(&res.win[0], &res.win[1], &res.win[2]);
draw_map(res.win[0]);
doing(res);
endwin();
return (0);
}