-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.pas
154 lines (141 loc) · 4.56 KB
/
tetris.pas
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
unit Tetris;
interface
const BLine=1;
BHorseLeft=2;
BHorseRight=3;
BBricksLeft=4;
BBricksRight=5;
BTank=6;
BSquare=7;
mw=9;
mh=25;
mwx=10;
mhx=26;
type
TField = array [0..mwx,0..mhx] of 0..1;
TBlock = array [0..4,0..4] of 0..1;
TScore = array [1..4] of 0..1;
TTetris =
object
field:TField;
block:TBlock;
shape:Integer;
angle:Integer;
color:Integer;
procedure Init;
procedure Define(sh,a:Integer);
function Scan(x,y:integer):boolean;
procedure Update(x,y:integer);
function Score(var scores:TScore):boolean;
end;
implementation
procedure TTetris.Init;
var i,j:integer;
begin
for i:=1 to mw do begin
for j:=1 to mh do begin
field[i,j]:=0;
end;
field[i,mhx]:=1;
end;
for i:=0 to mhx do begin
field[0,i]:=1;
field[mwx,i]:=1;
end;
end;
procedure TTetris.Define;
var i,j:integer;
begin
shape:=sh; angle:=a;
for i:=1 to 4 do for j:=1 to 4 do block[i,j]:=0;
case shape of
1:case angle of
1:begin block[2,1]:=1; block[2,2]:=1; block[2,3]:=1; block[2,4]:=1; end;
2:begin block[1,2]:=1; block[2,2]:=1; block[3,2]:=1; block[4,2]:=1; end;
3:begin block[2,1]:=1; block[2,2]:=1; block[2,3]:=1; block[2,4]:=1; end;
4:begin block[1,2]:=1; block[2,2]:=1; block[3,2]:=1; block[4,2]:=1; end;
end;
2:case angle of
1:begin block[1,1]:=1; block[2,1]:=1; block[2,2]:=1; block[2,3]:=1; end;
2:begin block[1,2]:=1; block[2,2]:=1; block[3,2]:=1; block[3,1]:=1; end;
3:begin block[2,1]:=1; block[2,2]:=1; block[2,3]:=1; block[3,3]:=1; end;
4:begin block[1,2]:=1; block[2,2]:=1; block[3,2]:=1; block[1,3]:=1; end;
end;
3:case angle of
1:begin block[2,1]:=1; block[2,2]:=1; block[2,3]:=1; block[3,1]:=1; end;
2:begin block[1,2]:=1; block[2,2]:=1; block[3,2]:=1; block[3,3]:=1; end;
3:begin block[2,1]:=1; block[2,2]:=1; block[2,3]:=1; block[1,3]:=1; end;
4:begin block[1,2]:=1; block[2,2]:=1; block[3,2]:=1; block[1,1]:=1; end;
end;
4:case angle of
1:begin block[2,1]:=1; block[2,2]:=1; block[1,1]:=1; block[3,2]:=1; end;
2:begin block[3,1]:=1; block[2,2]:=1; block[3,2]:=1; block[2,3]:=1; end;
3:begin block[2,1]:=1; block[2,2]:=1; block[1,1]:=1; block[3,2]:=1; end;
4:begin block[3,1]:=1; block[2,2]:=1; block[3,2]:=1; block[2,3]:=1; end;
end;
5:case angle of
1:begin block[2,1]:=1; block[2,2]:=1; block[3,1]:=1; block[1,2]:=1; end;
2:begin block[2,1]:=1; block[2,2]:=1; block[3,2]:=1; block[3,3]:=1; end;
3:begin block[2,1]:=1; block[2,2]:=1; block[3,1]:=1; block[1,2]:=1; end;
4:begin block[2,1]:=1; block[2,2]:=1; block[3,2]:=1; block[3,3]:=1; end;
end;
6:case angle of
1:begin block[2,1]:=1; block[2,2]:=1; block[1,2]:=1; block[3,2]:=1; end;
2:begin block[2,1]:=1; block[2,2]:=1; block[3,2]:=1; block[2,3]:=1; end;
3:begin block[1,2]:=1; block[2,2]:=1; block[2,3]:=1; block[3,2]:=1; end;
4:begin block[1,2]:=1; block[2,2]:=1; block[2,1]:=1; block[2,3]:=1; end;
end;
7:case angle of
1:begin block[2,1]:=1; block[2,2]:=1; block[1,1]:=1; block[1,2]:=1; end;
2:begin block[2,1]:=1; block[2,2]:=1; block[1,1]:=1; block[1,2]:=1; end;
3:begin block[2,1]:=1; block[2,2]:=1; block[1,1]:=1; block[1,2]:=1; end;
4:begin block[2,1]:=1; block[2,2]:=1; block[1,1]:=1; block[1,2]:=1; end;
end;
end;
end;
function TTetris.Scan;
var i,j:integer; ok:boolean;
begin
Scan:=false; j:=1; i:=1; ok:=false;
while not ok do begin
if (block[i,j]=1) and (field[i+x,j+y]=1) then begin
Scan:=true; ok:=true;
end;
inc(j); if j=5 then begin j:=1; inc(i); end;
if i=5 then ok:=true;
end;
end;
procedure TTetris.Update;
var i,j:integer;
begin
for i:=1 to 4 do for j:=1 to 4 do
if (block[i,j]=1) and (i+x<mwx) and (j+y<mhx) then
field[i+x,j+y]:=1;
end;
function TTetris.Score;
const nilscore:TScore = (0,0,0,0);
var i,j,count:integer;
full:boolean;
procedure del(y:integer);
var i,j:integer;
begin
for i:=1 to 9 do
for j:=y downto 2 do begin
field[i,j]:=field[i,j-1];
field[i,j-1]:=0;
end;
end;
begin
scores:=nilscore; count:=0; score:=false;
for j:=1 to mh do begin
full:=true;
for i:=1 to mw do
if field[i,j]=0 then full:=false;
if full then begin del(j); inc(count); end;
end;
if count>0 then begin
scores[count]:=1;
score:=true;
end;
end;
end.