-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.c
98 lines (90 loc) · 2.36 KB
/
render.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
#include <render.h>
#include <cbreak.h>
#include <sideEffects.h>
#include <sys/select.h>
void exitRetur(const int tmp){
offcbreak();
oncursor();
printf("\n");
exit(tmp != 0 ? 1 : 0);
}
void beginGame(const size_t len){
offcursor();
repeat(" ", len / 2 - 4);
printf("\e[1;36mGame 2048\e[0m\n");
}
void printBar(const int max, const int score, const size_t len){
tableRows("\u250C", "\u2500", "\u2510\n", len);
printf("\u2502\e[38;5;208;1mScore:%*d\e[0m\u2502\n", len * 4 + len - 7, score);
printf("\u2502\e[38;5;208;1mMax number:%*d\e[0m\u2502\n", len * 4 + len - 12, max);
tableRows("\u2514", "\u2500", "\u2518\n", len);
}
void printBarEnd(const int max, const int score, const size_t len){
tableRows("\u250C", "\u2500", "\u2510\n", len);
printf("\u2502\e[38;5;208;1mScore:%*d\e[0m\u2502\n", len * 4 + len - 7, score);
printf("\u2502");
max == 2048 ? wing(len * 4 + len - 2) : gameOver(len * 4 + len - 2);
printf("\u2502\n");
printf("\u2502\e[38;5;208;1mMax number:%*d\e[0m\u2502\n", len * 4 + len - 12, max);
tableRows("\u2514", "\u2500", "\u2518\n", len);
}
void printField(int** data, const size_t len){
tableRows("\u250C", "\u252C", "\u2510\n", len);
for (size_t i = 0; i < len; i++){
for (size_t j = 0; j < len; j++){
printf("\u2502");
if (data[i][j])
printf("\x1b[1;38;5;%sm%4d\x1b[0m", color(data[i][j]), data[i][j]);
else
repeat(" ", 4);
}
printf("\u2502\n");
if (i + 1 != len)
tableRows("\u251C", "\u253C", "\u2524\n", len);
}
tableRows("\u2514", "\u2534", "\u2518", len);
}
char exitOrRestart(){
char locations = 0;
while (1){
button(locations);
char symbol = input();
if (upperrcase(symbol) == 'A')
locations = (--locations % 2 + 2) % 2;
else if (upperrcase(symbol) == 'D')
locations = (++locations % 2 + 2) % 2;
else if (symbol == '\n')
return locations;
printf(CLEAR);
printf("\n");
}
}
char input(){
fflush(stdout);
oncbreak();
char symbol;
read(0, &symbol, 1);
if (symbol == 27){
fd_set x1, x2;
FD_ZERO(&x1);
FD_SET(0, &x1);
FD_ZERO(&x2);
struct timeval tm = {0, 5};
if (select(1, &x1, &x2, &x2, &tm)){
read(0, &symbol, 1);
if (symbol == 91){
read(0, &symbol, 1);
if (symbol == 'A')
symbol = 'W';
else if (symbol == 'B')
symbol = 'S';
else if (symbol == 'C')
symbol = 'D';
else if (symbol == 'D')
symbol = 'A';
}
}
}
offcbreak();
return symbol;
}