line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Games::2048::Util; |
2
|
4
|
|
|
4
|
|
99
|
use 5.012; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
135
|
|
3
|
4
|
|
|
4
|
|
16
|
use strictures; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
19
|
|
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
2918
|
use if $^O eq "MSWin32", "Win32::Console::ANSI"; |
|
4
|
|
|
|
|
29
|
|
|
4
|
|
|
|
|
19
|
|
6
|
4
|
|
|
4
|
|
2396
|
use Term::ReadKey; |
|
4
|
|
|
|
|
15783
|
|
|
4
|
|
|
|
|
393
|
|
7
|
4
|
|
|
4
|
|
2402
|
use Time::HiRes; |
|
4
|
|
|
|
|
5739
|
|
|
4
|
|
|
|
|
20
|
|
8
|
|
|
|
|
|
|
|
9
|
4
|
|
|
4
|
|
8165
|
eval 'END { ReadMode "normal" }'; |
10
|
|
|
|
|
|
|
ReadMode "cbreak"; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# manual and automatic window size updating |
13
|
|
|
|
|
|
|
my $_window_size; |
14
|
|
|
|
|
|
|
my $_window_size_is_automatic = eval { $SIG{WINCH} = \&update_window_size; 1 }; |
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
|
|
|
|
|
|
push @keys, $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
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
return shift @keys; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub poll_key { |
44
|
0
|
|
|
0
|
0
|
|
while (1) { |
45
|
0
|
|
|
|
|
|
my $key = read_key; |
46
|
0
|
0
|
|
|
|
|
return $key if defined $key; |
47
|
0
|
|
|
|
|
|
Time::HiRes::sleep(0.1); |
48
|
|
|
|
|
|
|
} |
49
|
0
|
|
|
|
|
|
return; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub key_vector { |
53
|
0
|
|
|
0
|
0
|
|
my ($key) = @_; |
54
|
0
|
|
|
|
|
|
state $vectors = [ [0, -1], [0, 1], [1, 0], [-1, 0] ]; |
55
|
0
|
|
|
|
|
|
state $keys = [ map "\e[$_", "A".."D" ]; |
56
|
0
|
|
|
|
|
|
for (0..3) { |
57
|
0
|
0
|
|
|
|
|
return $vectors->[$_] if $key eq $keys->[$_]; |
58
|
|
|
|
|
|
|
} |
59
|
0
|
|
|
|
|
|
return; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub frame_delay { |
63
|
0
|
|
|
0
|
0
|
|
state $time; |
64
|
|
|
|
|
|
|
|
65
|
0
|
0
|
|
|
|
|
if (@_ < 1) { |
66
|
0
|
|
|
|
|
|
$time = Time::HiRes::time; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else { |
69
|
0
|
|
|
|
|
|
my ($frame_time) = @_; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
my $new_time = Time::HiRes::time; |
72
|
0
|
|
|
|
|
|
my $delta_time = $new_time - $time; |
73
|
0
|
|
|
|
|
|
my $delay = $frame_time - $delta_time; |
74
|
0
|
|
|
|
|
|
$time = $new_time; |
75
|
0
|
0
|
|
|
|
|
if ($delay > 0) { |
76
|
0
|
|
|
|
|
|
Time::HiRes::sleep($delay); |
77
|
0
|
|
|
|
|
|
$time += $delay; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub update_window_size { |
83
|
0
|
|
|
0
|
0
|
|
($_window_size) = eval { GetTerminalSize *STDOUT }; |
|
0
|
|
|
|
|
|
|
84
|
0
|
|
0
|
|
|
|
$_window_size //= 80; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub window_size { |
88
|
0
|
|
|
0
|
0
|
|
$_window_size; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub window_size_is_automatic { |
92
|
0
|
|
|
0
|
0
|
|
$_window_size_is_automatic; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub maybe { |
96
|
0
|
0
|
|
0
|
0
|
|
if (@_ == 2) { return @_ if defined $_[1] } |
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
elsif (@_ == 1) { return @_ if defined $_[0] } |
98
|
0
|
|
|
|
|
|
return; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1; |