-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
282 lines (259 loc) · 5.88 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <windows.h>
#include <unistd.h>
#include <time.h>
/*GRID CONFIG:
0 empty space
1 floating block
2 landded block
*/
// TODO:
// delete row after tetris = DONE
// game_end detection = DONE
// score count = DONE
// moving pieces right and left
// pieces rotation
// frontend
// GLOBAL VARIABLES
#define ROWS 5 /*20 */
#define COLS 10
int grid[ROWS][COLS] = {0};
int score = 0;
int multiplier = 1;
bool landded = true;
bool collision = false;
bool game_end = false;
int o_block[2][2] = {{1, 1}, {1, 1}};
int i_block[4][1] = {{1}, {1}, {1}, {1}};
int s_block[2][3] = {{0, 1, 1}, {1, 1, 0}};
int z_block[2][3] = {{1, 1, 0}, {0, 1, 1}};
int l_block[3][2] = {{1, 0}, {1, 0}, {1, 1}};
int j_block[3][2] = {{0, 1}, {0, 1}, {1, 1}};
int t_block[2][3] = {{1, 1, 1}, {0, 1, 0}};
/*bool game_end()
{
for (int j = 0; j < COLS; ++j)
{
if (grid[ROWS - 1][j] == 2)
{
return true;
}
}
return false;
}*/
void calculate_points()
{
if (score >= 1000)
{
multiplier = 1.25;
}
else if (score >= 2500)
{
multiplier = 1.25;
}
else if (score >= 5000)
{
multiplier = 1.5;
}
else
{
multiplier = 1.5;
}
score += 100 * multiplier;
printf("+100 points\n");
}
void tetris_check()
{
bool tetris = true;
for (int j = 0; j < COLS; ++j)
{
if (grid[ROWS - 1][j] == 0)
{
tetris = false;
}
}
if (tetris == true)
{
printf("TETRIS\n");
calculate_points();
for (int i = ROWS - 1; i > -1; i--)
{
for (int j = COLS - 1; j > -1; j--)
{
if (grid[i][j] == 2)
{
grid[i + 1][j] = grid[i][j];
grid[i][j] = 0;
}
}
}
}
}
void update_grid()
{
sleep(1);
for (int i = 0; i < ROWS; ++i)
{
for (int j = 0; j < COLS; ++j)
{
// printf("%d ", grid[i][j]);
if (grid[i][j] == 0)
{
printf("%c ", 35);
}
else
{
printf("%c ", 254);
}
}
printf("\n");
}
printf("SCORE: %d \n", score);
}
void turn_all_ones_into_twos()
{
for (int i = ROWS - 1; i > -1; i--)
{
for (int j = COLS - 1; j > -1; j--)
{
if (grid[i][j] == 1)
{
grid[i + 1][j] = 2;
grid[i][j] = 0;
}
}
}
}
bool check_collision()
{
// collision check goes here
for (int i = ROWS - 1; i > -1; i--)
{
for (int j = COLS - 1; j > -1; j--)
{
// stop if you touch a landed block stop if you are at the end of grid
if ((i != ROWS - 1 && grid[i][j] == 1 && grid[i + 1][j] == 2) || (grid[i][j] == 1 && i == ROWS - 1))
{
// problem 2 : landing the blocks without footing
grid[i][j] = 2;
turn_all_ones_into_twos();
landded = true;
collision = true;
return collision;
}
}
}
collision = false;
return false;
}
void if_block_drop()
{
for (int i = ROWS - 1; i > -1; i--)
{
for (int j = COLS - 1; j > -1; j--)
{
if (grid[i][j] == 1 && check_collision() == false /* && grid[i + 1][j] != 2 && grid[i + 1][j] != ROWS - 1*/)
{
grid[i + 1][j] = grid[i][j];
grid[i][j] = 0;
}
// treba nadodati neki else.
}
}
}
// idea: hidden polja kao assistencija za init
void add_block(int *block, int row_num, int col_num)
{
//*((arr+i*n) + j)
for (int i = row_num - 1; i > -1; i--)
{
if_block_drop();
for (int j = col_num - 1; j > -1; j--)
{
if (grid[0][(COLS / 2) - 1 + j] == 2)
{
game_end = true;
return;
}
else
{
// grid[0][4 + j] = *((block + i * col_num) + j);
grid[0][(COLS / 2) - 1 + j] = *((block + i * col_num) + j);
}
}
/////////print updated grid
update_grid();
}
}
void run()
{
// if statements koji podesavaju usmjerenje i brzinu spustanja
srand(time(0));
if (landded == true)
{
landded = false;
int value = rand() % (6 - 0 + 1) + 0;
// printf("%d\n", value);
switch (value)
{
case 0:
add_block((int *)o_block, 2, 2);
break;
case 1:
add_block((int *)i_block, 4, 1);
break;
case 2:
add_block((int *)s_block, 2, 3);
break;
case 3:
add_block((int *)z_block, 2, 3);
break;
case 4:
add_block((int *)l_block, 3, 2);
break;
case 5:
add_block((int *)j_block, 3, 2);
break;
case 6:
add_block((int *)t_block, 2, 3);
break;
default:
break;
}
}
while (collision == false)
{
if_block_drop();
tetris_check();
update_grid();
}
}
void start_game()
{
while (game_end == false) // podesiti condition da se zaustavi kada je predena granica
// dodati function za uvijet check_game_end
{
run();
}
// igra se zaustavila. kraj
}
int main()
{
// displaying the layout grid
printf("======= TETRIS=======\n");
update_grid();
/*
// tetris test zadnji red je pun
for (int i = ROWS - 1; i > ROWS - 2; i--)
{
for (int j = COLS - 1; j > -1; j--)
{
grid[i][j] = 2;
}
}
*/
start_game();
printf("%s", "game endded");
}