line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Games::2048::Input; |
2
|
4
|
|
|
4
|
|
88
|
use 5.012; |
|
4
|
|
|
|
|
15
|
|
|
4
|
|
|
|
|
153
|
|
3
|
4
|
|
|
4
|
|
132
|
use strictures; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
29
|
|
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
6733
|
use if $^O eq "MSWin32", "Win32::Console::ANSI"; |
|
4
|
|
|
|
|
38
|
|
|
4
|
|
|
|
|
23
|
|
6
|
4
|
|
|
4
|
|
4369
|
use Term::ReadKey; |
|
4
|
|
|
|
|
39802
|
|
|
4
|
|
|
|
|
454
|
|
7
|
4
|
|
|
4
|
|
340
|
use Time::HiRes; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
42
|
|
8
|
|
|
|
|
|
|
|
9
|
4
|
|
|
4
|
|
11007
|
eval 'END { ReadMode "normal" }'; |
10
|
|
|
|
|
|
|
ReadMode "cbreak"; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# manual and automatic window size updating |
13
|
|
|
|
|
|
|
my $_window_size; |
14
|
|
|
|
|
|
|
eval { $SIG{WINCH} = \&update_window_size }; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub read_key { |
17
|
0
|
|
|
0
|
0
|
|
state @keys; |
18
|
|
|
|
|
|
|
|
19
|
0
|
0
|
|
|
|
|
if (@keys) { |
20
|
0
|
|
|
|
|
|
return shift @keys; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
my $char; |
24
|
0
|
|
|
|
|
|
my $packet = ''; |
25
|
0
|
|
|
|
|
|
while (defined($char = ReadKey -1)) { |
26
|
0
|
|
|
|
|
|
$packet .= $char; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
while ($packet =~ m( |
30
|
|
|
|
|
|
|
\G( |
31
|
|
|
|
|
|
|
\e \[ # CSI |
32
|
|
|
|
|
|
|
[\x30-\x3f]* # Parameter Bytes |
33
|
|
|
|
|
|
|
[\x20-\x2f]* # Intermediate Bytes |
34
|
|
|
|
|
|
|
[\x40-\x7e] # Final Byte |
35
|
|
|
|
|
|
|
| |
36
|
|
|
|
|
|
|
. # Otherwise just any character |
37
|
|
|
|
|
|
|
) |
38
|
|
|
|
|
|
|
)gsx) { |
39
|
0
|
|
|
|
|
|
push @keys, $1; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
return shift @keys; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub poll_key { |
46
|
0
|
|
|
0
|
0
|
|
while (1) { |
47
|
0
|
|
|
|
|
|
my $key = read_key; |
48
|
0
|
0
|
|
|
|
|
return $key if defined $key; |
49
|
0
|
|
|
|
|
|
Time::HiRes::sleep(Games::2048::FRAME_TIME); |
50
|
|
|
|
|
|
|
} |
51
|
0
|
|
|
|
|
|
return; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub key_vector { |
55
|
0
|
|
|
0
|
0
|
|
my ($key) = @_; |
56
|
0
|
|
|
|
|
|
state $vectors = [ [0, -1], [0, 1], [1, 0], [-1, 0] ]; |
57
|
0
|
|
|
|
|
|
state $keys = [ map "\e[$_", "A".."D" ]; |
58
|
0
|
|
|
|
|
|
for (0..3) { |
59
|
0
|
0
|
|
|
|
|
return $vectors->[$_] if $key eq $keys->[$_]; |
60
|
|
|
|
|
|
|
} |
61
|
0
|
|
|
|
|
|
return; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub update_window_size { |
65
|
0
|
|
|
0
|
0
|
|
($_window_size) = eval { GetTerminalSize *STDOUT }; |
|
0
|
|
|
|
|
|
|
66
|
0
|
|
0
|
|
|
|
$_window_size //= 80; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub window_size { |
70
|
0
|
|
|
0
|
0
|
|
$_window_size; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |