-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.c
132 lines (119 loc) · 3.01 KB
/
field.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
#include "field.h"
#include "action.h"
#include <ncurses.h>
#include <stdlib.h>
char **create_game_field(int height, int width)
{
char **field;
int i;
field = (char **)malloc(sizeof(char *) * height);
if (!field)
return (NULL);
i = 0;
while (i < height)
{
field[i] = (char *)malloc(sizeof(char) * (width + 1));
if (!field[i])
{
while (i > 0)
{
i--;
free(field[i]);
}
free(field);
return (NULL);
}
i++;
}
return (field);
}
void fill_field_for_start(char **field, int height, int width)
{
int i_height;
int j_width;
i_height = 0;
while (i_height < height)
{
j_width = 0;
while (j_width < width)
{
if (i_height >= 0 && i_height <= 3 )
field[i_height][j_width] = ' ';
else if (j_width == 0 || j_width == 1 || j_width == width - 1 || j_width == width - 2)
field[i_height][j_width] = '|';
else if (i_height == height - 1 || i_height == height - 2)
field[i_height][j_width] = '-';
else
field[i_height][j_width] = ' ';
j_width++;
}
field[i_height][j_width] = '\0';
i_height++;
}
}
void put_row(char *row)
{
printw("%s", row);
}
void put_next_piece(char *row, int index)
{
int i;
i = 0;
while (i < 4)
{
printw("%c", row[i + (4 * index)]);
}
printw("|");
}
void put_field(char **field, int height, int points, char *next_piece, char *hold_piece)
{
int i;
i = 0;
while (i < height)
{
if (i >= 4 && i <= 17)
{
if (i < 11)
{
if (i == 4)
printw(" NEXT ");
else if (i == 5 || i == 10)
printw(" -------");
else
printw(" | %c%c%c%c ", next_piece[((i - 6) * 4)], next_piece[((i - 6) * 4) + 1], next_piece[((i - 6) * 4) + 2], next_piece[((i - 6) * 4) + 3]);
}
else
{
if (i == 11)
printw(" HOLD");
else if (i == 12 || i == 17)
printw(" -------");
else
{
if (hold_piece == NULL)
printw(" | ");
else
printw(" | %c%c%c%c ", hold_piece[((i - 13) * 4)], hold_piece[((i - 13) * 4) + 1], hold_piece[((i - 13) * 4) + 2], hold_piece[((i - 13) * 4) + 3]);
}
}
}
else
printw(" ");
put_row(field[i]);
printw("\n");
i++;
}
printw("Points: %d", points);
refresh();
}
void free_field(char **field, int height)
{
int i;
i = 0;
while (i < height)
{
free(field[i]);
i++;
}
free(field);
}