bin/merrills

filter: Statementstmt 97.2% 107 / 110 Branchbran 97.5% 80 / 82 Conditioncond 88.8% 24 / 27 MC/DCmcdc 81.2% 13 / 16 Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 95.1% 235 / 247 CC 4 SCAR 5.0
CC
4
Cov
96%
reason
3.2
__ANON__
2.0
usage
2.0
CRAP 1.6
« blib/lib/Game/Merrills/Error.pm ↑ Summary
1 #!/usr/bin/env perl
2 63 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 63 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 63 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 63 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 63 ▶use Game::Merrills;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 63 ▶use Game::Merrills::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 63 ▶use Game::Merrills::Terminal;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 63 my %opt = (
14 level => [],
15 side => 'white',
16 flying => 1,
17 ascii => 0,
18 );
19  
20 63 Getopt::Long::Configure(qw/no_ignore_case/);
21 63 ▶GetOptions(\%opt,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%opt, "level|l=s\@", "side|s=s", "hotseat", "bot-vs-bot", "position=s", "load=s", "replay=s", "seed=s", "flying!", "ascii|a", "colour|color!", "pick!", "version|v", "help|h") or usage(2) 62 1 63
22 'level|l=s@',
23 'side|s=s',
24 'hotseat',
25 'bot-vs-bot',
26 'position=s',
27 'load=s',
28 'replay=s',
29 'seed=s',
30 'flying!',
31 'ascii|a',
32 'colour|color!',
33 'pick!',
34 'version|v',
35 'help|h',
36 ) or usage(2);
37  
38 62 ▶usage(0) if $opt{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 3 59 62
39  
40 59 ▶if ($opt{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"version"}) 2 57 59
41 2 print "merrills $Game::Merrills::VERSION\n";
42 2 exit 0;
43 }
44  
45 57 ▶bad("there is nothing to do with '$ARGV[0]': options begin with --") if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 1 56 57
46  
47 56 my @levels = Game::Merrills::Bot->levels;
48 56 my %is_level = map { $_ => 1 } @levels;
49 56 my @level = @{ $opt{level} };
50 56 for my $level (@level) {
51 bad("--level takes a number from $levels[0] to $levels[-1], not '$level'")
52 32 ▶ unless $is_level{$level};
Branch: 1 branch
BranchTrueFalseTotal
unless $is_level{$level} 2 30 32
53 }
54 54 ▶bad('--level can be given twice at most, once a side') if @level > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @level > 2 1 53 54
55 bad('--level twice is for --bot-vs-bot, where each side has a bot')
56 53 ▶ if @level == 2 && !$opt{'bot-vs-bot'};
Branch: 1 branch
BranchTrueFalseTotal
if @level == 2 and not $opt{"bot-vs-bot"} 1 52 53
Condition100%3/3
@level == 2 and not $opt{"bot-vs-bot"}
!ll&&!rl&&r
5111
MC/DC100%2/2
@level == 2 and not $opt{"bot-vs-bot"}
@level == 2 not $opt{"bot-vs-bot"}
Truth table100%3/3
A and not(B)
A= @level == 2B= not $opt{"bot-vs-bot"}
ABresult
0X0
100
111
57 bad('--hotseat is two people and --bot-vs-bot is none: choose one')
58 52 ▶ if $opt{hotseat} && $opt{'bot-vs-bot'};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"hotseat"} and $opt{"bot-vs-bot"} 1 51 52
Condition100%3/3
$opt{"hotseat"} and $opt{"bot-vs-bot"}
!ll&&!rl&&r
32191
MC/DC100%2/2
$opt{"hotseat"} and $opt{"bot-vs-bot"}
$opt{"hotseat"} $opt{"bot-vs-bot"}
Truth table100%3/3
A and B
A= $opt{"hotseat"}B= $opt{"bot-vs-bot"}
ABresult
0X0
100
111
59 51 ▶bad('--hotseat has no bot for --level to set') if $opt{hotseat} && @level;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"hotseat"} and @level 1 50 51
Condition100%3/3
$opt{"hotseat"} and @level
!ll&&!rl&&r
32181
MC/DC100%2/2
$opt{"hotseat"} and @level
$opt{"hotseat"} @level
Truth table100%3/3
A and B
A= $opt{"hotseat"}B= @level
ABresult
0X0
100
111
60 bad("--side is white, black or random, not '$opt{side}'")
61 50 ▶ unless $opt{side} =~ m/^(?:white|black|random)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"side"} =~ /^(?:white|black|random)$/ 1 49 50
62 bad("--seed takes a whole number, not '$opt{seed}'")
63 49 ▶ if defined $opt{seed} && $opt{seed} !~ m/^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"seed"} and not $opt{"seed"} =~ /^[0-9]+$/ 1 48 49
Condition100%3/3
defined $opt{"seed"} and not $opt{"seed"} =~ /^[0-9]+$/
!ll&&!rl&&r
33151
MC/DC100%2/2
defined $opt{"seed"} and not $opt{"seed"} =~ /^[0-9]+$/
defined $opt{"seed"} not $opt{"seed"} =~ /^[0-9]+$/
Truth table100%3/3
A and not(B)
A= defined $opt{"seed"}B= not $opt{"seed"} =~ /^[0-9]+$/
ABresult
0X0
100
111
64  
65 48 my @sources = grep { defined $opt{$_} } qw/position load replay/;
66 48 ▶bad('--' . join(' and --', @sources) . ' each say where the game begins: give one')
Branch: 1 branch
BranchTrueFalseTotal
if @sources > 1 1 47 48
67 if @sources > 1;
68  
69 47 ▶my $seed = defined $opt{seed} ? $opt{seed} : (time ^ $$) % 1_000_000;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'seed'} ? : 15 32 47
70 47 ▶my $side = $opt{side} eq 'random' ? ($seed % 2 ? 'black' : 'white') : $opt{side};
Branch: 2 branches
BranchTrueFalseTotal
$opt{'side'} eq 'random' ? : 8 39 47
$seed % 2 ? : 4 4 8
71 47 ▶@level = (3) unless @level;
Branch: 1 branch
BranchTrueFalseTotal
unless @level 24 23 47
72 47 ▶@level = (@level, @level) if @level == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @level == 1 46 1 47
73  
74 47 my ($game, $record);
75 47 ▶if (defined $opt{position}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opt{'position'}) { } 6 41 47
elsif (defined(my $file = defined $opt{'load'} ? $opt{'load'} : $opt{'replay'})) { } 5 36 41
76 6 ▶ $game = eval { Game::Merrills->new(position => $opt{position}, flying => $opt{flying}) }
Branch: 1 branch
BranchTrueFalseTotal
$game = eval { do { "Game::Merrills"->new("position", $opt{"position"}, "flying", $opt{"flying"}) } } or bad('--position: ' . reason($@)) 5 1 6
77 or bad('--position: ' . reason($@));
78 }
79 ▶elsif (defined(my $file = defined $opt{load} ? $opt{load} : $opt{replay})) {
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'load'} ? : 3 38 41
80 5 ▶ open my $handle, '<', $file or bad("cannot read $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<", $file or bad("cannot read ${file}: $!") 3 2 5
81 3 $record = do { local $/; <$handle> };
82 3 close $handle;
83 3 ▶ $game = eval { Game::Merrills->from_text($record, flying => $opt{flying}) }
Branch: 1 branch
BranchTrueFalseTotal
$game = eval { do { "Game::Merrills"->from_text($record, "flying", $opt{"flying"}) } } or bad("$file is not a game that plays: " . reason($@)) 2 1 3
84 or bad("$file is not a game that plays: " . reason($@));
85 }
86 else {
87 36 $game = Game::Merrills->new(flying => $opt{flying});
88 }
89  
90 43 my ($human, $bot, $who);
91 43 ▶if (defined $opt{replay}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opt{'replay'}) { } 1 42 43
elsif ($opt{'hotseat'}) { } 18 24 42
elsif ($opt{'bot-vs-bot'}) { } 5 19 24
92 1 $human = 'both';
93 }
94 elsif ($opt{hotseat}) {
95 18 $human = 'both';
96 18 $who = 'White and Black: two people at one keyboard.';
97 }
98 elsif ($opt{'bot-vs-bot'}) {
99 5 $human = 'none';
100 5 $bot = {
101 white => Game::Merrills::Bot->new(level => $level[0], seed => $seed),
102 black => Game::Merrills::Bot->new(level => $level[1], seed => $seed + 1),
103 };
104 5 $who = "White: the bot at level $level[0]. Black: the bot at level $level[1].";
105 }
106 else {
107 19 $human = $side;
108 19 $bot = Game::Merrills::Bot->new(level => $level[0], seed => $seed);
109 19 ▶ $who = sprintf '%s: you. %s: the bot at level %d.',
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'white' ? : 14 5 19
110 ucfirst $side, $side eq 'white' ? 'Black' : 'White', $level[0];
111 }
112 43 ▶$who .= ' No flying.' if defined $who && !$opt{flying};
Branch: 1 branch
BranchTrueFalseTotal
if defined $who and not $opt{"flying"} 1 42 43
Condition100%3/3
defined $who and not $opt{"flying"}
!ll&&!rl&&r
1411
MC/DC100%2/2
defined $who and not $opt{"flying"}
defined $who not $opt{"flying"}
Truth table100%3/3
A and not(B)
A= defined $whoB= not $opt{"flying"}
ABresult
0X0
100
111
113  
114 my $terminal = Game::Merrills::Terminal->new(
115 game => $game,
116 human => $human,
117 ascii => $opt{ascii} ? 1 : 0,
118 (defined $bot ? (bot => $bot) : ()),
119 (exists $opt{colour} ? (colour => $opt{colour} ? 1 : 0) : ()),
120 43 ▶ (exists $opt{pick} ? (picking => $opt{pick} ? 1 : 0) : ()),
Branch: 6 branches
BranchTrueFalseTotal
$opt{'ascii'} ? : 42 1 43
defined $bot ? : 24 19 43
exists $opt{'colour'} ? : 42 1 43
$opt{'colour'} ? : 2 40 42
exists $opt{'pick'} ? : 2 41 43
$opt{'pick'} ? : 1 1 2
121 );
122  
123 43 for my $signal (qw/INT TERM HUP/) {
124 0 ▶ $SIG{$signal} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 $terminal->leave_raw;
126 0 $SIG{ $_[0] } = 'DEFAULT';
127 0 kill $_[0], $$;
128 129 };
129 }
130  
131 43 my $result;
132 43 ▶if (defined $opt{replay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{'replay'}) { } 1 42 43
133 1 $result = eval { $terminal->replay($record) };
134 }
135 else {
136 42 $terminal->say($who);
137 42 $result = eval { $terminal->start };
138 }
139 43 my $died = $@;
140 43 $terminal->leave_raw;
141 43 ▶die $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 0 43 43
142  
143 43 ▶exit 0 unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 35 8 43
144 8 ▶exit 0 if $result->is_draw || $human eq 'both' || $human eq 'none' || defined $opt{replay};
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_draw or $human eq "both" or $human eq "none" or defined $opt{"replay"} 6 2 8
Condition88%8/9
$result->is_draw or $human eq "both" or $human eq "none" or defined $opt{"replay"}
l!l&&r!l&&!r
602
$result->is_draw or $human eq "both" or $human eq "none"
l!l&&r!l&&!r
332
$result->is_draw or $human eq "both"
l!l&&r!l&&!r
215
MC/DC75%3/4
$result->is_draw or $human eq "both" or $human eq "none" or defined $opt{"replay"}
$result->is_draw $human eq "both" $human eq "none" defined $opt{"replay"}
Truth table80%4/5
A or B or C or D
A= $result->is_drawB= $human eq "both"C= $human eq "none"D= defined $opt{"replay"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
145 2 ▶exit($result->winner eq $human ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$result->winner eq $human ? : 1 1 2
146  
147 2 ▶sub reason {
Subroutine reason: called CC=3 SCAR=11.8
148 2 my ($why) = @_;
149 2 ▶ $why = 'could not start' unless defined $why && length $why;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $why and length $why 0 2 2
Condition33%1/3
defined $why and length $why
!ll&&!rl&&r
002
MC/DC0%0/2
defined $why and length $why
defined $why length $why
Truth table33%1/3
A and B
A= defined $whyB= length $why
ABresult
0X0
100
111
150 2 $why =~ s/ at \S+ line \d+\.?\s*\z//;
151 2 $why =~ s/\s+\z//;
152 2 return $why;
153 }
154  
155 14 ▶sub bad {
Subroutine bad: called CC=1 SCAR=0.0
156 14 my ($why) = @_;
157 14 print STDERR "merrills: $why\n\n";
158 14 usage(2);
159 }
160  
161 18 ▶sub usage {
Subroutine usage: called CC=2 SCAR=6.9
162 18 my ($status) = @_;
163 18 ▶ my $handle = $status ? \*STDERR : \*STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 15 3 18
164 18 my @levels = Game::Merrills::Bot->levels;
165 18 my $levels = "$levels[0] to $levels[-1]";
166 18 my $limit = Game::Merrills::NO_MILL_PLIES;
167  
168 18 print {$handle} <<"USAGE";
169 merrills - play Nine Men's Morris
170  
171 Usage: merrills [options]
172  
173 -l, --level N the bot's strength, $levels (default 3)
174 -s, --side SIDE the side you play: white, black or random (default white)
175 --hotseat two people at one keyboard, and no bot
176 --bot-vs-bot watch two bots; give --level twice for white and black
177 --position STR begin from a position, as the position command prints it
178 --load FILE carry on a game that the save command wrote
179 --replay FILE show such a game a move at a time
180 --seed N makes a game repeatable: the same seed, the same game
181 --no-flying a side down to three men still moves along the lines
182 -a, --ascii letters and dashes, for a terminal without the glyphs
183 --colour paint the board even off a terminal; --no-colour never
184 --no-pick type the moves out instead of choosing them with the keys
185 -v, --version print the version
186 -h, --help this
187  
188 White moves first. Place your nine men in turn, then move them along the
189 lines. Three in a row is a mill and takes a man: one in a mill is safe while
190 any stands outside one. Down to three men, you fly. Two men, or no move, and
191 you have lost. A position three times, or $limit moves without a mill, is a
192 draw.
193  
194 On a terminal a move is chosen with the arrow keys, a step at a time: the
195 man, where it goes, the man it takes. The board is drawn as the move would
196 leave it, so a mill is seen before it is closed. Needs Term::ReadKey; without
197 it, or off a terminal, moves are typed: d2 to place, d2-d3 to move, d2-d3xa1
198 to take. Type help in the game for the commands.
199  
200 The board marks what the last move did with shapes, so they survive
201 --no-colour and a file: (W) the man that moved, (.) where it came from, (x)
202 where a man was taken.
203  
204 Levels $levels[-2] and $levels[-1] look a good deal further and take noticeably longer a move.
205 A level's effort is counted in positions and never in seconds, so a busy
206 machine plays the same game as an idle one.
207  
208 Exit status is 0 if you won or drew, 1 if you lost, 2 for a bad option.
209 USAGE
210  
211 18 exit $status;
212 }
« blib/lib/Game/Merrills/Error.pm ↑ Summary