-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.c
145 lines (124 loc) · 3.38 KB
/
grid.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
#include <stdio.h>
#include <ncurses.h>
#include "gamedefinitions.h"
#include "color_management.h"
// Assumes grid is a ROWxCOL char-array
void initgrid(char grid[ROW][COL])
{
for (int y = 0; y < ROW; y++) {
for (int x = 0; x < COL; x++) {
grid[y][x] = EMPTYCHAR;
}
}
}
void print_block(WINDOW *gridwin, char c)
{
if (has_colors() == TRUE)
{
char colorcode;
switch (c)
{
case 'I':
colorcode = 'C';
break;
case 'J':
colorcode = 'B';
break;
case 'L':
colorcode = 'O';
break;
case 'O':
colorcode = 'Y';
break;
case 'S':
colorcode = 'G';
break;
case 'T':
colorcode = 'M';
break;
case 'Z':
colorcode = 'R';
break;
case EMPTYCHAR:
colorcode = 'N';
break;
}
block_color_control(gridwin, colorcode, 1);
waddch(gridwin, ' ');
waddch(gridwin, ' ');
block_color_control(gridwin, colorcode, 0);
}
else
{
waddch(gridwin, c);
waddch(gridwin, c);
}
}
// Print the grid and return to top-left corner of it.
void printgrid(WINDOW *gridwin, char _2darray[ROW][COL])
{
char element;
for (int y = 2; y < ROW; y++) {
for (int x = 0; x < COL; x++) {
element = _2darray[y][x];
print_block(gridwin, element);
}
}
wmove(gridwin, 0, 0);
}
void draw_gridborder(int gridy, int gridx)
{
int bordery = gridy - 1;
int borderx = gridx - 2;
attron(A_REVERSE);
// Top border
mvwhline(stdscr, bordery, borderx, ' ', (COL+2)*2);
// Left border
mvwvline(stdscr, bordery+1, borderx, ' ', ROW-1);
mvwvline(stdscr, bordery+1, borderx+1, ' ', ROW-1);
// Right border
mvwvline(stdscr, bordery+1, (borderx+COL+1)*2, ' ', ROW-1);
mvwvline(stdscr, bordery+1, (borderx+COL+1)*2+1, ' ', ROW-1);
// Bottom border
mvwhline(stdscr, bordery+ROW-1, borderx, ' ', (COL+2)*2);
attroff(A_REVERSE);
}
// Test if there are filled lines, lines that should got cleared.
// If it doesn't, then just skip. But if there are, clear those lines
// and descend all the blocks above.
void test_clear_line(char grid[ROW][COL])
{
int x, y, k, line_filled;
for (y = ROW-1; y > -1; y--)
{
// Looking for available lines.
line_filled = 1;
for (x = 0; x < COL; x++)
{
if (grid[y][x] == EMPTYCHAR)
{
line_filled = 0;
break;
}
}
// If there is a line that's suitable for cleaning...
if (line_filled)
{
// Deletes every character on that line...
for (x = 0; x < COL; x++)
grid[y][x] = EMPTYCHAR;
// Then descends every block above by one.
for (k = y-1; k > -1; k--)
{
for (x = 0; x < COL; x++)
{
grid[k+1][x] = grid[k][x];
grid[k][x] = EMPTYCHAR;
}
}
// Because all the grid got moved down by a block,
// We also move down y.
y = y + 1;
}
}
}