-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.c
83 lines (68 loc) · 1.76 KB
/
tetris.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
#include <stdio.h>
#include "tetris.h"
char ruudukko[20][10] = {
"@@@@@@@@@@", /* Rivi 0: ylin rivi. */
" x x ", /* Rivi 1: toiseksi ylin rivi. */
" xx xxx ", /* ...ja niin edelleen... */
"xxx xxxxxx",
"@@@@@@@@@@", /* Rivi 4: täynnä. */
"@@@@@@@@@@", /* Rivi 5: täynnä. */
"xxxxxx xxx",
"xxxxxxx xx",
"x xxxxxxxx",
"xxx xxx xx",
"xxxxxx xxx",
"x xx xxx x",
"xxxxxxxxx ",
"xxx xxx xx",
"xxxxx xx x",
" xxxxxxxxx",
"xxx xxx xx",
"x xxxx xxx",
"xxxxx xxxx", /* Rivi 18: toiseksi alin rivi. */
"@@@@@@@@@@"}; /* Rivi 19: alin rivi. */
int main(void) {
int i = 0;
paivita(ruudukko); /* Poistetaan tyhjät rivit. */
/* Tulostetaa ruudukko. */
for(i = 0; i < 20; ++i) {
printf("[%02d] %.10s\n", i,ruudukko[i]); /* Tulostetaan 1 rivi eli 10 merkkiä. */
}
return 0;
}
/* Removes full rows and then moves upper rows down */
void paivita(char r[20][10]) {
int ri = 19;
int collapse = 0; /* how many rows downwords */
int ri_ = 0; /* used in last step */
for(;ri>0; ri--) {
/* check if row full */
int ci = 0; /* start from left */
int isFull = 1; /* bool */
for(;ci<10; ci++) {
if(r[ri][ci] == ' ') {
isFull = 0;
break;
}
}
/* */
if(isFull==1) {
collapse++;
/*printf("row idx %d is full\n", ri);
*/
} else {
/* Collapse this entire row down X rows */
int cic = 0; /* column index */
for(;cic<10; cic++) {
r[ri+collapse][cic] = r[ri][cic];
}
}
}
/* then make top X rows empty (X=amount of collapsed rows) */
for(;ri_<=collapse; ri_++) {
int ci = 0; /* start from left */
for(;ci<10; ci++) {
r[ri_][ci] = ' ';
}
}
}