-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetris
executable file
·177 lines (155 loc) · 3.81 KB
/
Tetris
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
#!/usr/local/bin/perl
#u# http://rosettacode.org/wiki/Tetris
#t# interactive
#c# 2021-02-24 <RC
#p# OK
# Very basic tetris. Use arrow keys for left and right (or 'h' and 'l'), up arrow for rotate (or 'r'),
# and down arrow for instant drop (or 'j').
use strict;
use warnings;
use Curses;
use Term::ReadKey;
use Time::HiRes qw( time );
use IO::Select;
my $delay = 1;
my $width = 12;
my $oneshort = $width - 1;
my $g3 = qr/(..{$oneshort})/s;
my $g4 = qr/(.{$oneshort})/s;
my $below = qr/....{$oneshort}/s;
my $height = 20;
my $well = ( '|' . ' ' x $width . "|\n" ) x $height . '-' x($width + 2) . "\n";
my $piece;
my $nexttime = time + $delay;
my $sel = IO::Select->new( *STDIN );
sub transpose
{
local $_ = $well;
$well = '';
$well .= "\n" while s/^./ $well .= $&; ''/gem;
}
sub place
{
substr $well, $width / 2 - 1 + ($width + 3) * $_, 4, shift for 0 .. 3;
}
my %shape =
(
I => [' O ', ' O ', ' O ', ' O '],
J => [' ', ' ', 'OOO ', ' O '],
L => [' ', ' ', ' OOO', ' O '],
O => [' ', ' ', ' OO ', ' OO '],
S => [' ', ' ', ' OO ', 'OO '],
T => [' ', ' ', ' O ', 'OOO '],
Z => [' ', ' ', ' OO ', ' OO'],
);
sub add
{
if( $well =~ /^(. *.\n){4}/ )
{
place $shape{$piece}->@*;
$piece = (keys %shape)[rand keys %shape];
}
else
{
die "end of game\n";
}
}
sub rotate
{
s/ ${g3}OO $g3 OO/ O$1 OO$2 O / or # Z
s/ O$g3 OO$g3 O / $1OO $2 OO/ or
s/ $g3 OO${g3}OO / O $1 OO$2 O/ or # S
s/ O $g3 OO$g3 O/ $1 OO$2OO / or
s/ ${g3}OOO${g3}O /OO $1 O $2 O / or # L
s/OO $g3 O $g3 O / $1 O$2OOO/ or
s/ $g3 O${g3}OOO/ O $1 O $2 OO/ or
s/ O $g3 O $g3 OO/ $1OOO$2O / or
s/ ${g3}OOO$g3 O/ O $1 O $2OO / or # J
s/ O $g3 O ${g3}OO / $1O $2OOO/ or
s/ ${g3}O ${g3}OOO/ OO$1 O $2 O / or
s/ OO$g3 O $g3 O / $1OOO$2 O/ or
s/ $g3 O ${g3}OOO/ O $1 OO$2 O / or # T
s/ O $g3 OO$g3 O / $1OOO$2 O / or
s/ ${g3}OOO$g3 O / O $1OO $2 O / or
s/ O ${g3}OO $g3 O / $1 O $2OOO/ or
s/ $g4 $g4 ${g4}OOOO/ O $1 O $2 O $3 O / or # I
s/ O $g4 O $g4 O $g4 O / $1 $2 $3OOOO/ or
s/O ${g4}O ${g4}O ${g4}O / $1 $2 $3OOOO/ or
s/ O$g4 O$g4 O$g4 O/ $1 $2 $3OOOO/
for $well;
}
sub step
{
if( $well =~ s/(?<=\|)#{$width}(?=\|)/ '=' x $width /e ) # full row?
{
transpose();
$well =~ s/(.*)=/ $1/g; # remove full row
transpose();
}
elsif( $well !~ /O/ ) # any O ?
{
add();
}
elsif( not down() ) # can't move down
{
$well =~ tr/O/#/; # convert to #
}
}
sub down
{
$well !~ /O/ || $well =~ /O$below[#-]/ and return 0;
transpose();
$well =~ s/(O+) / $1/g;
transpose();
return 1;
}
sub drop { 1 while down() }
sub right { $well =~ /O[#|]/ or $well =~ s/(O+) / $1/g }
sub left { $well =~ /[#|]O/ or $well =~ s/ (O+)/$1 /g }
sub draw
{
addstr( 2, 0, ($well . "\n\n") =~ s/^/' ' x 20 /gmer);
my $row = 4;
addstr( $row++, 10, $_ ) for @{ $shape{$piece} };
addstr( 22, 0, ' ' );
refresh;
};
sub eventloop
{
while( 1 )
{
my $time = time;
my $delta = $nexttime - $time;
if( $delta <= 0 )
{
step();
$nexttime = time + $delay;
}
else
{
draw();
for ( $sel->can_read( $delta ) )
{
sysread *STDIN, $_, 1024;
for ( /\e(?:\[M...|[O\[][0-9;]*[A-~])|./gs ) # keep esc seq together
{
/^(?:q|\e)\z/i ? die "quit\n" :
/^(?:h|\e\[D)\z/ ? left() :
/^(?:l|\e\[C)\z/ ? right() :
/^(?:r|\e\[A)\z/ ? rotate() :
/^(?:[j ]|\e\[B)\z/ ? drop() :
0;
}
}
}
}
}
$piece = (keys %shape)[rand keys %shape];
initscr();
clear;
ReadMode 'cbreak';
eval { eventloop() };
my $errormsg = $@;
ReadMode 'restore';
endwin();
print $errormsg;