-
Notifications
You must be signed in to change notification settings - Fork 0
/
numbers.c
97 lines (86 loc) · 2.31 KB
/
numbers.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* numbers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dskrypny <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/07/21 11:15:43 by dskrypny #+# #+# */
/* Updated: 2018/07/22 21:27:25 by dskrypny ### ########.fr */
/* */
/* ************************************************************************** */
#include "header.h"
void add_number(int mas[4][4])
{
short num_pos;
srand(time(NULL));
num_pos = rand() % 16;
while (mas[num_pos / 4][num_pos % 4] != 0)
num_pos = rand() % 16;
mas[num_pos / 4][num_pos % 4] = (rand() % 8 == 4) ? 4 : 2;
}
void create_numbers(t_result *res)
{
short i;
short j;
i = -1;
while (++i < 4)
{
j = -1;
while (++j < 4)
res->numbers[i][j] = 0;
}
add_number(res->numbers);
add_number(res->numbers);
res->won = 0;
res->result = 0;
print_numbers(res->win[0], res->numbers);
print_result(res);
}
void print_numbers(WINDOW *win, int mas[4][4])
{
short i;
short j;
int y;
int x;
i = -1;
getmaxyx(stdscr, y, x);
while (++i < 4)
{
j = -1;
while (++j < 4)
(mas[i][j]) ?
mvwprintw(win, ((y - 5) / 4) * j + y / 8, ((x - CHAMPS_WIDTH)
/ 4) * i + x / 8 - 5, " %4d", mas[i][j]) :
mvwprintw(win, ((y - 5) / 4) * j + y / 8, ((x - CHAMPS_WIDTH)
/ 4) * i + x / 8 - 5, "%5s", "");
}
mvwprintw(win, 0, 0, "%s", "");
wrefresh(win);
}
short check_number(int mas[4][4], short x, short y)
{
short res;
res = 1;
if (x > 0 && mas[x - 1][y] == mas[x][y])
res = 0;
if (x < 3 && mas[x + 1][y] == mas[x][y])
res = 0;
if (y < 3 && mas[x][y + 1] == mas[x][y])
res = 0;
if (y > 0 && mas[x][y - 1] == mas[x][y])
res = 0;
return (res);
}
void copy_numbers(int src[4][4], int dst[4][4])
{
short i;
short j;
i = -1;
while (++i < 4)
{
j = -1;
while (++j < 4)
dst[i][j] = src[i][j];
}
}