blib/lib/Term/Maze.pm

filter: Statementstmt 93.8% 91 / 97 Branchbran 83.3% 35 / 42 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 92.8% 13 / 14 Podpod 100% 3 / 3 totaltotal 90.0% 145 / 161 CC 26 SCAR 19.9
CC
26
Cov
90%
_sizes
12.0
_play
10.2
run
6.0
CRAP 7.3
↑ Summary blib/lib/aze.pm »
1 package Term::Maze;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Term::ReadKey ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 our $VERSION = '0.06';
9  
10 3 require XSLoader;
11 3 XSLoader::load('Term::Maze', $VERSION);
12  
13 3 our $READER = \&Term::ReadKey::ReadKey;
14 3 our $READMODE = \&Term::ReadKey::ReadMode;
15 3 our $MODE;
16  
17 14 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
18 14 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 19 ▶sub _sizes {
Subroutine _sizes: called CC=12 SCAR=24.9
44 19 my (@argv) = @_;
45 19 my ($width, $height) = (40, 20);
46 19 ▶ if (grep { !/^\d+$/ } @argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not /^\d+$/;} @argv) { } 12 7 19
47 12 my %opt;
48 12 my $ok = eval {
49 12 ▶ local $SIG{__WARN__} = sub { print STDERR "maze: $_[0]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
50 12 Getopt::Long::GetOptionsFromArray(\@argv, \%opt, 'width=i', 'height=i', 'help');
51 };
52 12 ▶ if (!$ok) { print STDERR _usage(); return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 3 9 12
53 9 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 6 3 9
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 2 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
72 return [
73 2 { name => 'width', type => 'int', min => 5, max => 120, help => 'cells across' },
74 { name => 'height', type => 'int', min => 5, max => 60, help => 'cells down' },
75 ];
76 }
77  
78 19 ▶sub run {
Subroutine run: called CC=6 SCAR=18.0
Pod documented
79 19 my ($class, @argv) = @_;
80 19 my @size = _sizes(@argv);
81 19 ▶ return 2 unless @size;
Branch: 1 branch
BranchTrueFalseTotal
unless @size 8 11 19
82 11 ▶ return 0 if @size == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @size == 1 6 5 11
83 5 my ($width, $height) = @size;
84  
85 5 local @SIG{qw(INT TERM HUP)};
86 5 for my $sig (qw(INT TERM HUP)) {
87 0 ▶ $SIG{$sig} = sub { _mode(0); $SIG{$sig} = 'DEFAULT'; kill $sig, $$ };
Subroutine __ANON__: not called CC=1 SCAR=3.5
88 }
89  
90 5 _mode("cbreak");
91 5 my $won = eval { _play($width, $height) };
92 5 my $died = $@;
93 5 _mode(0);
94 5 ▶ die $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 1 4 5
95 4 ▶ print "You win!\n" if $won;
Branch: 1 branch
BranchTrueFalseTotal
if $won 1 3 4
96 4 return 0;
97 }
98  
99 5 ▶sub _play {
Subroutine _play: called CC=9 SCAR=23.2
100 5 my ($width, $height) = @_;
101 5 my $maze = Term::Maze->new($width, $height);
102 5 $maze->refresh();
103 5 ▶ while (!$maze->at_exit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($maze->at_exit) 5 1 6
104 5 my $key = $READER->(0);
105 4 ▶ next unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 4 4
106 4 ▶ return 0 if $key eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" 2 2 4
107 2 ▶ if ($key eq "\e") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "\e") 2 0 2
108 2 my $next = $READER->(-1);
109 2 ▶ return 0 unless defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 1 1 2
110 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
111 1 next;
112 }
113 0 ▶ if ($key =~ m/^[wasdhjkl]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^[wasdhjkl]$/) 0 0 0
114 0 $maze->move_player($key);
115 }
116 0 $maze->refresh();
117 }
118 1 return 1;
119 }
120 5 ▶sub refresh {
Subroutine refresh: called CC=2 SCAR=6.9
Pod documented
121 5 my ($self) = @_;
122 5 print "\e[H\e[2J";
123 5 my $rows = $self->get_maze_with_player();
124 5 print "$_\n" for @$rows;
125 5 print "Use WASD or HJKL to move. Reach the exit to win.\n";
126 }
127  
128 3 1;
129  
130 __END__
↑ Summary blib/lib/aze.pm »