blib/lib/Term/Maze.pm

filter: Statementstmt 93.7% 90 / 96 Branchbran 83.3% 35 / 42 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 92.3% 12 / 13 Podpod 100% 2 / 2 totaltotal 89.8% 142 / 158 CC 26 SCAR 20.2
CC
26
Cov
90%
_sizes
12.0
_play
10.2
run
6.0
CRAP 7.5
↑ Summary blib/lib/aze.pm »
1 package Term::Maze;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Term::ReadKey ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our $VERSION = '0.05';
9  
10 2 require XSLoader;
11 2 XSLoader::load('Term::Maze', $VERSION);
12  
13 2 our $READER = \&Term::ReadKey::ReadKey;
14 2 our $READMODE = \&Term::ReadKey::ReadMode;
15 2 our $MODE;
16  
17 8 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
18 8 return <<'USAGE';
19 maze - mazes in the terminal
20  
21 usage: maze [options]
22 maze WIDTH HEIGHT
23  
24 --width N columns, at least 2 (default 40)
25 --height N rows, at least 2 (default 20)
26 --help this
27  
28 exit status:
29 0 you reached the exit, or you quit
30 2 a bad option
31  
32 Move with WASD or HJKL. Reach the exit to win; q or escape quits.
33 USAGE
34 }
35  
36 10 ▶sub _mode {
Subroutine _mode: called CC=1 SCAR=0.0
37 10 my ($mode) = @_;
38 10 $MODE = $mode;
39 10 $READMODE->($mode);
40 10 return;
41 }
42  
43 13 ▶sub _sizes {
Subroutine _sizes: called CC=12 SCAR=24.9
44 13 my (@argv) = @_;
45 13 my ($width, $height) = (40, 20);
46 13 ▶ if (grep { !/^\d+$/ } @argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not /^\d+$/;} @argv) { } 6 7 13
47 6 my %opt;
48 6 my $ok = eval {
49 6 ▶ local $SIG{__WARN__} = sub { print STDERR "maze: $_[0]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
50 6 Getopt::Long::GetOptionsFromArray(\@argv, \%opt, 'width=i', 'height=i', 'help');
51 };
52 6 ▶ if (!$ok) { print STDERR _usage(); return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 4 6
53 4 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 1 3 4
54 3 ▶ if (@argv) { print STDERR "maze: not an option: $argv[0]\n", _usage(); return }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 1 2 3
55 2 ▶ $width = $opt{width} if defined $opt{width};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"width"} 2 0 2
56 2 ▶ $height = $opt{height} if defined $opt{height};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"height"} 1 1 2
57 }
58 else {
59 7 ▶ if (@argv > 2) { print STDERR "maze: too many sizes: @argv\n", _usage(); return }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv > 2) 1 6 7
60 6 ▶ $width = $argv[0] if defined $argv[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $argv[0] 6 0 6
61 6 ▶ $height = $argv[1] if defined $argv[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $argv[1] 6 0 6
62 }
63 8 for ([width => $width], [height => $height]) {
64 14 ▶ next if $_->[1] >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $_->[1] >= 2 11 3 14
65 3 print STDERR "maze: --$_->[0] must be at least 2, not $_->[1]\n", _usage();
66 3 return;
67 }
68 5 return ($width, $height);
69 }
70  
71 13 ▶sub run {
Subroutine run: called CC=6 SCAR=18.0
Pod documented
72 13 my ($class, @argv) = @_;
73 13 my @size = _sizes(@argv);
74 13 ▶ return 2 unless @size;
Branch: 1 branch
BranchTrueFalseTotal
unless @size 7 6 13
75 6 ▶ return 0 if @size == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @size == 1 1 5 6
76 5 my ($width, $height) = @size;
77  
78 5 local @SIG{qw(INT TERM HUP)};
79 5 for my $sig (qw(INT TERM HUP)) {
80 15 ▶ $SIG{$sig} = sub { _mode(0); $SIG{$sig} = 'DEFAULT'; kill $sig, $$ };
Subroutine __ANON__: not called CC=1 SCAR=3.5
81 }
82  
83 5 _mode("cbreak");
84 5 my $won = eval { _play($width, $height) };
85 5 my $died = $@;
86 5 _mode(0);
87 5 ▶ die $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 1 4 5
88 4 ▶ print "You win!\n" if $won;
Branch: 1 branch
BranchTrueFalseTotal
if $won 1 3 4
89 4 return 0;
90 }
91  
92 5 ▶sub _play {
Subroutine _play: called CC=9 SCAR=23.2
93 5 my ($width, $height) = @_;
94 5 my $maze = Term::Maze->new($width, $height);
95 5 $maze->refresh();
96 5 ▶ while (!$maze->at_exit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($maze->at_exit) 5 1 6
97 5 my $key = $READER->(0);
98 4 ▶ next unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 4 4
99 4 ▶ return 0 if $key eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" 2 2 4
100 2 ▶ if ($key eq "\e") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "\e") 2 0 2
101 2 my $next = $READER->(-1);
102 2 ▶ return 0 unless defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 1 1 2
103 1 ▶ $next = $READER->(-1) while defined $next && $next !~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
defined $next and not $next =~ /[A-Za-z~]/ and do { $next = &$READER(-1) } 1 1 2
Condition66%2/3
defined $next and not $next =~ /[A-Za-z~]/
!ll&&!rl&&r
011
MC/DC50%1/2
defined $next and not $next =~ /[A-Za-z~]/
defined $next not $next =~ /[A-Za-z~]/
Truth table66%2/3
A and not(B)
A= defined $nextB= not $next =~ /[A-Za-z~]/
ABresult
0X0
100
111
104 1 next;
105 }
106 0 ▶ if ($key =~ m/^[wasdhjkl]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^[wasdhjkl]$/) 0 0 0
107 0 $maze->move_player($key);
108 }
109 0 $maze->refresh();
110 }
111 1 return 1;
112 }
113 5 ▶sub refresh {
Subroutine refresh: called CC=2 SCAR=6.9
Pod documented
114 5 my ($self) = @_;
115 5 print "\e[H\e[2J";
116 5 my $rows = $self->get_maze_with_player();
117 5 print "$_\n" for @$rows;
118 5 print "Use WASD or HJKL to move. Reach the exit to win.\n";
119 }
120  
121 2 1;
122  
123 __END__
↑ Summary blib/lib/aze.pm »