Gin/Terminal.pm

filter: Statementstmt 84.2% 508 / 603 Branchbran 65.4% 229 / 350 Conditioncond 47.9% 69 / 144 MC/DCmcdc 25.0% 21 / 84 Subroutinesub 91.5% 65 / 71 Podpod 97.8% 45 / 46 totaltotal 72.1% 937 / 1298 CC 253 SCAR 41.2
CC
253
Cov
73%
run
600.0
human_move
77.1
pick_draw
53.4
CRAP 61.6
↑ Summary Gin/Bot.pm »
1 package Game::Gin::Terminal;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Game::Gin ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Game::Gin::Bot ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Game::Gin::Card qw(name_of long_name_of id_of deadwood_of rank_of suit_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Game::Gin::Deadwood qw(best deadwood KNOCK_AT);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Game::Gin::Search ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 our $VERSION = '0.03';
18  
19 2 our @RANK_TEXT = ('', 'A', 2 .. 9, '10', 'J', 'Q', 'K');
20  
21 2 our %SUIT = (
22 S => { wide => "\x{2660}", ascii => 'S', colour => '90' },
23 H => { wide => "\x{2665}", ascii => 'H', colour => '31' },
24 D => { wide => "\x{2666}", ascii => 'D', colour => '31' },
25 C => { wide => "\x{2663}", ascii => 'C', colour => '90' },
26 );
27  
28 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 CARD_WIDTH => 11,
30 FAN_WIDTH => 4,
31 2 };
32  
33 2 our %SEQUENCE = (
34 'A' => 'up',
35 'B' => 'down',
36 'C' => 'right',
37 'D' => 'left',
38 'H' => 'home',
39 'F' => 'end',
40 '1~' => 'home',
41 '4~' => 'end',
42 '5~' => 'page_up',
43 '6~' => 'page_down',
44 '7~' => 'home',
45 '8~' => 'end',
46 );
47  
48 2 our %CONTROL = (
49 "\r" => 'enter',
50 "\n" => 'enter',
51 "\t" => 'tab',
52 "\x7f" => 'backspace',
53 "\x08" => 'backspace',
54 "\x03" => 'interrupt',
55 "\x04" => 'eof',
56 );
57  
58 2 our %MARK_INK = (
59 drew => '1;32',
60 took => '1;36',
61 cursor => '1;7',
62 );
63  
64 2 our %FRAME = (
65 plain => {
66 wide => [ "\x{250C}", "\x{2510}", "\x{2514}", "\x{2518}",
67 "\x{2500}", "\x{2502}" ],
68 ascii => [ '+', '+', '+', '+', '-', '|' ],
69 },
70 fresh => {
71 wide => [ "\x{2554}", "\x{2557}", "\x{255A}", "\x{255D}",
72 "\x{2550}", "\x{2551}" ],
73 ascii => [ '#', '#', '#', '#', '=', '#' ],
74 },
75 cursor => {
76 wide => [ "\x{250F}", "\x{2513}", "\x{2517}", "\x{251B}",
77 "\x{2501}", "\x{2503}" ],
78 ascii => [ '>', '>', '>', '>', '=', '!' ],
79 },
80 );
81  
82 2 our %MARK_FRAME = (
83 drew => 'fresh',
84 took => 'fresh',
85 cursor => 'cursor',
86 );
87  
88 2 has game => (is => 'rw', isa => Any);
89 2 has out => (is => 'ro', isa => Any);
90 2 has in => (is => 'ro', isa => Any);
91 2 has mode => (is => 'ro', isa => Str);
92 2 has level => (is => 'ro', isa => Int);
93 2 has seat => (is => 'ro', isa => Str);
94 2 has ascii => (is => 'ro', isa => Any);
95 2 has colour => (is => 'ro', isa => Any);
96 2 has quit => (is => 'rw', isa => Any);
97 2 has raw => (is => 'rw', isa => Any);
98 2 has keysource => (is => 'rw', isa => Any);
99 2 has pending => (is => 'rw', isa => ArrayRef, default => []);
100 2 has marks => (is => 'rw', isa => HashRef, default => {});
101 2 has _painting => (is => 'rw', isa => Any);
102 2 has _picking => (is => 'rw', isa => Any, init_arg => 'picking');
103 2 has _staring => (is => 'rw', isa => Any, init_arg => 'interactive');
104  
105 2470 ▶sub _out { return $_[0]->out || \*STDOUT }
Subroutine _out: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]->out || \*STDOUT
l!l
24700
MC/DC0%0/1
$_[0]->out || \*STDOUT
$_[0]->out
Truth table50%1/2
A
A= $_[0]->out
Aresult
11
00
106 57 ▶sub _in { return $_[0]->in || \*STDIN }
Subroutine _in: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]->in || \*STDIN
l!l
570
MC/DC0%0/1
$_[0]->in || \*STDIN
$_[0]->in
Truth table50%1/2
A
A= $_[0]->in
Aresult
11
00
107 3440 ▶sub _mode { return $_[0]->mode || 'bot' }
Subroutine _mode: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]->mode || 'bot'
l!l
34400
MC/DC0%0/1
$_[0]->mode || 'bot'
$_[0]->mode
Truth table50%1/2
A
A= $_[0]->mode
Aresult
11
00
108 1118 ▶sub _level { return $_[0]->level || 2 }
Subroutine _level: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]->level || 2
l!l
11180
MC/DC0%0/1
$_[0]->level || 2
$_[0]->level
Truth table50%1/2
A
A= $_[0]->level
Aresult
11
00
109 110 ▶sub _seat { return $_[0]->seat || 'p1' }
Subroutine _seat: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]->seat || 'p1'
l!l
1100
MC/DC0%0/1
$_[0]->seat || 'p1'
$_[0]->seat
Truth table50%1/2
A
A= $_[0]->seat
Aresult
11
00
110 1724 ▶sub _ascii { return $_[0]->ascii ? 1 : 0 }
Subroutine _ascii: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->ascii ? : 1249 475 1724
111  
112 27 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
113 27 my ($self) = @_;
114 27 ▶ binmode $self->_out, ':encoding(UTF-8)' unless $self->_ascii;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_ascii 7 20 27
115  
116 27 my $previous = select $self->_out;
117 27 $| = 1;
118 27 select $previous;
119 27 return $self;
120 }
121  
122 4507 ▶sub _colour {
Subroutine _colour: called CC=6 SCAR=18.6
123 4507 my ($self) = @_;
124 4507 my $on = $self->_painting;
125 4507 ▶ return $on if defined $on;
Branch: 1 branch
BranchTrueFalseTotal
if defined $on 4485 22 4507
126 $on = defined $self->colour ? ($self->colour ? 1 : 0)
127 22 ▶ : ((eval { -t $self->_out } && !$ENV{NO_COLOR}) ? 1 : 0);
Branch: 3 branches
BranchTrueFalseTotal
defined $self->colour ? : 18 4 22
$self->colour ? : 0 18 18
eval { do { -t $self->_out } } && !$ENV{'NO_COLOR'} ? : 0 4 4
Condition33%1/3
eval { do { -t $self->_out } } and not $ENV{"NO_COLOR"}
!ll&&!rl&&r
400
MC/DC0%0/2
eval { do { -t $self->_out } } and not $ENV{"NO_COLOR"}
eval { do { -t $self->_out } } not $ENV{"NO_COLOR"}
Truth table33%1/3
A and not(B)
A= eval { do { -t $self->_out } }B= not $ENV{"NO_COLOR"}
ABresult
0X0
100
111
128 22 $self->_painting($on);
129 22 return $on;
130 }
131  
132 4507 ▶sub paint {
Subroutine paint: called CC=2 SCAR=8.1
Pod documented
133 4507 my ($self, $text, $code) = @_;
134 4507 ▶ return $text unless $self->_colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_colour 4507 0 4507
135 0 return "\e[${code}m" . $text . "\e[0m";
136 }
137  
138 2383 ▶sub say_to { my ($self, @what) = @_; my $fh = $self->_out; print {$fh} @what, "\n"; return }
Subroutine say_to: called CC=1 SCAR=0.0
Pod documented
139  
140 36 ▶sub interactive {
Subroutine interactive: called CC=4 SCAR=14.1
Pod documented
141 36 my ($self) = @_;
142 36 my $want = $self->_staring;
143 36 ▶ return $want ? 1 : 0 if defined $want;
Branch: 2 branches
BranchTrueFalseTotal
if defined $want 34 2 36
$want ? : 0 34 34
144 2 ▶ return (eval { -t $self->_out }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { -t $self->_out } } ? : 0 2 2
145 }
146  
147 60 ▶sub picking {
Subroutine picking: called CC=5 SCAR=16.1
Pod documented
148 60 my ($self, @set) = @_;
149 60 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 1 59 60
$set[0] ? : 0 1 1
150 60 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 52 8 60
$self->_picking ? : 45 7 52
151 8 return $self->keys_available;
152 }
153  
154 10 ▶sub keys_available {
Subroutine keys_available: called CC=4 SCAR=18.7
Pod documented
155 10 my ($self) = @_;
156 10 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 0 10 10
157 10 ▶ return 0 unless eval { -t $self->_in };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { -t $self->_in } } 10 0 10
158 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
159 }
160  
161 27 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=19.3
Pod documented
162 27 my ($self) = @_;
163 27 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 14 13 27
164  
165 13 ▶ if ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 12 1 13
166 12 $self->raw(1);
167 12 return $self;
168 }
169  
170 1 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 1 0 1
171 0 ▶ return undef unless eval { Term::ReadKey::ReadMode(3, $self->_in); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { Term::ReadKey::ReadMode(3, $self->_in); 1 } } 0 0 0
172  
173 0 $self->raw(1);
174 0 return $self;
175 }
176  
177 16 ▶sub leave_raw {
Subroutine leave_raw: called CC=3 SCAR=11.2
Pod documented
178 16 my ($self) = @_;
179 16 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 7 9 16
180 9 ▶ eval { Term::ReadKey::ReadMode(0, $self->_in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 9 9
181 9 $self->raw(0);
182 9 return $self;
183 }
184  
185 74 ▶sub read_char {
Subroutine read_char: called CC=6 SCAR=27.1
Pod documented
186 74 my ($self, $wait) = @_;
187  
188 74 my $pending = $self->pending;
189 74 ▶ return shift @$pending if @$pending;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending 2 72 74
190 72 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 72 0 72
191  
192 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->_in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
193 0 ▶ return $char if defined $char || $wait;
Branch: 1 branch
BranchTrueFalseTotal
if defined $char or $wait 0 0 0
Condition0%0/3
defined $char or $wait
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $char or $wait
defined $char $wait
Truth table0%0/3
A or B
A= defined $charB= $wait
ABresult
1X1
011
000
194  
195 0 select undef, undef, undef, 0.05;
196 0 return Term::ReadKey::ReadKey(-1, $self->_in);
197 }
198  
199 50 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
200 50 my ($self) = @_;
201 50 my $char = $self->read_char(1);
202 50 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 2 48 50
203 48 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Gin::Terminal::CONTROL{$char} 8 40 48
204 40 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 12 28 40
205 28 return $char;
206 }
207  
208 12 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=21.0
Pod documented
209 12 my ($self) = @_;
210  
211 12 my $opener = $self->read_char(0);
212 12 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 12 12
213  
214 12 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 2 10 12
Condition100%3/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
912
MC/DC100%2/2
$opener eq "[" or $opener eq "O"
$opener eq "[" $opener eq "O"
Truth table100%3/3
A or B
A= $opener eq "["B= $opener eq "O"
ABresult
1X1
011
000
215 2 unshift @{ $self->pending }, $opener;
216 2 return 'escape';
217 }
218  
219 10 my $tail = '';
220 10 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 12 0 12
221 12 my $char = $self->read_char(0);
222 12 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 12 12
223 12 $tail .= $char;
224 12 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 10 2 12
225 }
226  
227 10 ▶ return $SEQUENCE{$tail} || 'escape';
Condition50%1/2
$Game::Gin::Terminal::SEQUENCE{$tail} || 'escape'
l!l
100
MC/DC0%0/1
$Game::Gin::Terminal::SEQUENCE{$tail} || 'escape'
$Game::Gin::Terminal::SEQUENCE{$tail}
Truth table50%1/2
A
A= $Game::Gin::Terminal::SEQUENCE{$tail}
Aresult
11
00
228 }
229  
230 32 ▶sub clear {
Subroutine clear: called CC=2 SCAR=10.9
Pod documented
231 32 my ($self) = @_;
232 32 ▶ return $self unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 32 0 32
233 0 my $fh = $self->_out;
234 0 print {$fh} "\e[2J\e[3J\e[H";
235 0 return $self;
236 }
237  
238 15 ▶sub handover {
Subroutine handover: called CC=8 SCAR=21.4
Pod documented
239 15 my ($self, $seat) = @_;
240 15 ▶ return 1 unless $self->_mode eq 'hotseat';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_mode eq "hotseat" 11 4 15
241  
242 4 $self->clear;
243 4 ▶ $self->say_to('') for 1 .. ($self->interactive ? 2 : 30);
Branch: 1 branch
BranchTrueFalseTotal
$self->interactive ? : 0 4 4
244 4 $self->say_to("pass the keyboard to $seat, then press return");
245  
246 4 ▶ return defined $self->read_key ? 1 : 0
Branch: 2 branches
BranchTrueFalseTotal
if $self->picking and $self->enter_raw 3 1 4
defined $self->read_key ? : 3 0 3
Condition66%2/3
$self->picking and $self->enter_raw
!ll&&!rl&&r
103
MC/DC50%1/2
$self->picking and $self->enter_raw
$self->picking $self->enter_raw
Truth table66%2/3
A and B
A= $self->pickingB= $self->enter_raw
ABresult
0X0
100
111
247 if $self->picking && $self->enter_raw;
248  
249 1 ▶ return defined $self->ask('') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->ask('') ? : 0 1 1
250 }
251  
252 47 ▶sub ask {
Subroutine ask: called CC=2 SCAR=6.9
Pod documented
253 47 my ($self, $prompt) = @_;
254 47 my $fh = $self->_out;
255 47 print {$fh} $prompt;
256 47 my $in = $self->_in;
257 47 my $line = <$in>;
258 47 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 3 44 47
259 44 chomp $line;
260 44 return $line;
261 }
262  
263 828 ▶sub pretty {
Subroutine pretty: called CC=3 SCAR=11.0
Pod documented
264 828 my ($self, $id) = @_;
265 828 ▶ return '-' unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 828 828
266 828 my $suit = $SUIT{ suit_of($id) };
267 return $self->paint(
268 $RANK_TEXT[ rank_of($id) ] . $suit->{ $self->_ascii ? 'ascii' : 'wide' },
269 $suit->{colour}
270 828 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 460 368 828
271 }
272  
273 397 ▶sub _card_rows {
Subroutine _card_rows: called CC=5 SCAR=16.1
274 397 my ($self, $id, $mark) = @_;
275 397 my $rank = $RANK_TEXT[ rank_of($id) ];
276 397 my $suit = $SUIT{ suit_of($id) };
277 397 ▶ my $pip = $suit->{ $self->_ascii ? 'ascii' : 'wide' };
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 351 46 397
278 397 ▶ my $set = $FRAME{ ($mark && $MARK_FRAME{$mark}) || 'plain' };
Condition80%4/5
$mark && $Game::Gin::Terminal::MARK_FRAME{$mark} || 'plain'
l!l
38359
$mark and $Game::Gin::Terminal::MARK_FRAME{$mark}
!ll&&!rl&&r
359038
MC/DC50%1/2
$mark && $Game::Gin::Terminal::MARK_FRAME{$mark} || 'plain'
$mark $Game::Gin::Terminal::MARK_FRAME{$mark}
Truth table66%2/3
A and B
A= $markB= $Game::Gin::Terminal::MARK_FRAME{$mark}
ABresult
111
0X0
100
279 my ($tl, $tr, $bl, $br, $h, $v)
280 397 ▶ = @{ $set->{ $self->_ascii ? 'ascii' : 'wide' } };
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 351 46 397
281  
282 return ([
283 $tl . ($h x 9) . $tr,
284 $v . sprintf('%-9s', $rank) . $v,
285 $v . sprintf('%-9s', $pip) . $v,
286 $v . (' ' x 9) . $v,
287 $v . ' ' . $pip . ' ' . $v,
288 $v . (' ' x 9) . $v,
289 $v . sprintf('%9s', $pip) . $v,
290 $v . sprintf('%9s', $rank) . $v,
291 $bl . ($h x 9) . $br,
292 397 ], $suit->{colour});
293 }
294  
295 30 ▶sub card_art {
Subroutine card_art: called CC=3 SCAR=11.0
Pod documented
296 30 my ($self, $id, $mark) = @_;
297 30 my ($rows, $colour) = $self->_card_rows($id, $mark);
298 30 ▶ my $ink = $mark && $MARK_INK{$mark} ? $MARK_INK{$mark} : $colour;
Branch: 1 branch
BranchTrueFalseTotal
$mark && $Game::Gin::Terminal::MARK_INK{$mark} ? : 11 19 30
Condition66%2/3
$mark and $Game::Gin::Terminal::MARK_INK{$mark}
!ll&&!rl&&r
19011
MC/DC50%1/2
$mark and $Game::Gin::Terminal::MARK_INK{$mark}
$mark $Game::Gin::Terminal::MARK_INK{$mark}
Truth table66%2/3
A and B
A= $markB= $Game::Gin::Terminal::MARK_INK{$mark}
ABresult
0X0
100
111
299 30 return [ map { $self->paint($_, $ink) } @$rows ];
300 }
301  
302 6 ▶sub card_space {
Subroutine card_space: called CC=2 SCAR=7.0
Pod documented
303 6 my ($self) = @_;
304 6 ▶ my ($h, $v) = $self->_ascii ? ('-', ':') : ("\x{2504}", "\x{250A}");
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 6 0 6
305 54 return [ map { $self->paint($_, '90') } (
306 ' ' . ($h x 9) . ' ',
307 6 (map { $v . (' ' x 9) . $v } 1 .. 7),
308 ' ' . ($h x 9) . ' ',
309 ) ];
310 }
311  
312 34 ▶sub card_back {
Subroutine card_back: called CC=2 SCAR=6.9
Pod documented
313 34 my ($self) = @_;
314 34 ▶ my ($tl, $tr, $bl, $br, $h, $v, $fill) = $self->_ascii
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 30 4 34
315 ? ('+', '+', '+', '+', '-', '|', '#')
316 : ("\x{250C}", "\x{2510}", "\x{2514}", "\x{2518}", "\x{2500}", "\x{2502}", "\x{2591}");
317 return [
318 $tl . ($h x 9) . $tr,
319 34 (map { $v . ($fill x 9) . $v } 1 .. 7),
320 $bl . ($h x 9) . $br,
321 ];
322 }
323  
324 35 ▶sub fan {
Subroutine fan: called CC=9 SCAR=22.5
Pod documented
325 35 my ($self, $ids, $marks) = @_;
326 35 ▶ return [ ('') x 9 ] unless $ids && @$ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids and @$ids 0 35 35
Condition33%1/3
$ids and @$ids
!ll&&!rl&&r
0035
MC/DC0%0/2
$ids and @$ids
$ids @$ids
Truth table33%1/3
A and B
A= $idsB= @$ids
ABresult
0X0
100
111
327 35 ▶ $marks ||= {};
Condition50%1/2
$marks ||= {}
l!l
350
MC/DC0%0/1
$marks ||= {}
$marks
Truth table50%1/2
A
A= $marks
Aresult
11
00
328 35 my @out = ('') x 9;
329 35 for my $i (0 .. $#$ids) {
330 367 my $mark = $marks->{ $ids->[$i] };
331 367 my ($rows, $colour) = $self->_card_rows($ids->[$i], $mark);
332 367 ▶ my $ink = $mark && $MARK_INK{$mark} ? $MARK_INK{$mark} : $colour;
Branch: 1 branch
BranchTrueFalseTotal
$mark && $Game::Gin::Terminal::MARK_INK{$mark} ? : 27 340 367
Condition66%2/3
$mark and $Game::Gin::Terminal::MARK_INK{$mark}
!ll&&!rl&&r
340027
MC/DC50%1/2
$mark and $Game::Gin::Terminal::MARK_INK{$mark}
$mark $Game::Gin::Terminal::MARK_INK{$mark}
Truth table66%2/3
A and B
A= $markB= $Game::Gin::Terminal::MARK_INK{$mark}
ABresult
0X0
100
111
333 367 my $last = $i == $#$ids;
334 367 for my $row (0 .. 8) {
335 3303 ▶ my $part = $last ? $rows->[$row] : substr $rows->[$row], 0, FAN_WIDTH;
Branch: 1 branch
BranchTrueFalseTotal
$last ? : 315 2988 3303
336 3303 $out[$row] .= $self->paint($part, $ink);
337 }
338 }
339 35 return \@out;
340 }
341  
342 36 ▶sub fan_width {
Subroutine fan_width: called CC=2 SCAR=7.1
Pod documented
343 36 my ($self, $n) = @_;
344 36 ▶ return 0 unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 36 36
345 36 return (($n - 1) * FAN_WIDTH) + CARD_WIDTH;
346 }
347  
348 34 ▶sub _beside {
Subroutine _beside: called CC=8 SCAR=20.8
349 34 my ($self, $gap, @blocks) = @_;
350 34 my $height = 0;
351 34 for my $block (@blocks) {
352 68 ▶ $height = scalar @{ $block->{lines} } if @{ $block->{lines} } > $height;
Branch: 1 branch
BranchTrueFalseTotal
if @{$block->{"lines"};} > $height 34 34 68
353 }
354 34 my @rows = ('') x $height;
355 34 for my $i (0 .. $#blocks) {
356 68 my $block = $blocks[$i];
357 68 for my $row (0 .. $height - 1) {
358 612 ▶ $rows[$row] .= ' ' x $gap if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 306 306 612
359 612 my $line = $block->{lines}[$row];
360 612 ▶ $rows[$row] .= defined $line ? $line : ' ' x $block->{width};
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 612 0 612
361 }
362 }
363 34 s/\s+\z// for @rows;
364 34 return @rows;
365 }
366  
367 34 ▶sub render {
Subroutine render: called CC=2 SCAR=6.9
Pod documented
368 34 my ($self, $seat, $without, $marks) = @_;
369 34 my $deal = $self->game->deal;
370 34 my $hand = $deal->hand_of($seat);
371  
372 34 my @cards = @{ $hand->cards };
373 34 ▶ @cards = grep { $_ != $without } @cards if defined $without;
Branch: 1 branch
BranchTrueFalseTotal
if defined $without 1 33 34
374 34 my $melding = best(\@cards);
375  
376 34 my @lines;
377 34 push @lines, sprintf('deal %d, dealt by %s', $deal->number, $deal->dealer);
378 push @lines, sprintf('score p1 %d p2 %d (to %d)',
379 $self->game->scores->{p1}, $self->game->scores->{p2},
380 34 $self->game->target);
381 34 push @lines, '';
382 34 push @lines, $self->table_lines($deal, $seat);
383 34 push @lines, '';
384 34 push @lines, $self->hand_lines($melding, $marks);
385 34 push @lines, $self->knock_line($melding);
386 34 return \@lines;
387 }
388  
389 34 ▶sub knock_line {
Subroutine knock_line: called CC=2 SCAR=7.2
Pod documented
390 34 my ($self, $melding) = @_;
391 return $melding->{deadwood} <= KNOCK_AT
392 ? sprintf('you may knock (%d)', $melding->{deadwood})
393 34 ▶ : sprintf('%d to go before you may knock', $melding->{deadwood} - KNOCK_AT);
Branch: 1 branch
BranchTrueFalseTotal
$melding->{'deadwood'} <= 10 ? : 0 34 34
394 }
395  
396 16 ▶sub preview {
Subroutine preview: called CC=1 SCAR=0.0
Pod documented
397 16 my ($self, $seat, $card) = @_;
398 16 my @rest = grep { $_ != $card } @{ $self->game->deal->hand_of($seat)->cards };
399 16 return best(\@rest);
400 }
401  
402 18 ▶sub choice_cards {
Subroutine choice_cards: called CC=8 SCAR=20.8
Pod documented
403 18 my ($self, $seat) = @_;
404  
405 18 my (%playable, %knock);
406 18 for my $move (@{ $self->game->deal->legal($seat) }) {
407 196 ▶ next unless $move->{kind} eq 'discard';
Branch: 1 branch
BranchTrueFalseTotal
unless $move->{"kind"} eq "discard" 0 196 196
408 196 $playable{ $move->{card} } = 1;
409 196 ▶ $knock{ $move->{card} } = 1 if $move->{knock};
Branch: 1 branch
BranchTrueFalseTotal
if $move->{"knock"} 4 192 196
410 }
411  
412 18 my $groups = $self->hand_groups(best($self->game->deal->hand_of($seat)->cards));
413  
414 18 my @out;
415 18 for my $group (@$groups) {
416 35 for my $card (@{ $group->{cards} }) {
417 198 ▶ next unless $playable{$card};
Branch: 1 branch
BranchTrueFalseTotal
unless $playable{$card} 6 192 198
418 push @out, {
419 card => $card,
420 knock => $knock{$card} ? 1 : 0,
421 loose => $group->{loose},
422 192 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$knock{$card} ? : 4 188 192
423 }
424 }
425  
426 18 return \@out;
427 }
428  
429 16 ▶sub first_loose {
Subroutine first_loose: called CC=2 SCAR=7.0
Pod documented
430 16 my ($self, $choices) = @_;
431 16 my ($at) = grep { $choices->[$_]{loose} } 0 .. $#$choices;
432 16 ▶ return defined $at ? $at : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $at ? : 16 0 16
433 }
434  
435 17 ▶sub legend {
Subroutine legend: called CC=3 SCAR=11.0
Pod documented
436 17 my ($self, $choice) = @_;
437 17 my @key = ('left and right to choose', 'enter to discard');
438 17 ▶ push @key, 'k to discard and knock' if $choice && $choice->{knock};
Branch: 1 branch
BranchTrueFalseTotal
if $choice and $choice->{"knock"} 1 16 17
Condition66%2/3
$choice and $choice->{"knock"}
!ll&&!rl&&r
0161
MC/DC50%1/2
$choice and $choice->{"knock"}
$choice $choice->{"knock"}
Truth table66%2/3
A and B
A= $choiceB= $choice->{"knock"}
ABresult
0X0
100
111
439 17 push @key, '? for the rest', 'q to stop';
440 17 return join ', ', @key;
441 }
442  
443 12 ▶sub pick_discard {
Subroutine pick_discard: called CC=23 SCAR=37.4
Pod documented
444 12 my ($self, $seat) = @_;
445  
446 12 my $choices = $self->choice_cards($seat);
447 12 ▶ return undef unless @$choices;
Branch: 1 branch
BranchTrueFalseTotal
unless @$choices 0 12 12
448  
449 12 my $legal = $self->game->deal->legal($seat);
450 12 my ($big) = grep { $_->{kind} eq 'big_gin' } @$legal;
451  
452 12 my $at = $self->first_loose($choices);
453 12 my $remelded = 0;
454 12 my @notice;
455  
456 12 while (1) {
457 17 $self->show_choice($seat, $choices, $at, \@notice, $big, $remelded);
458 17 @notice = ();
459  
460 17 my $key = $self->read_key;
461 17 ▶ return undef if !defined $key || $key eq 'eof' || $key eq 'interrupt';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "eof" or $key eq "interrupt" 1 16 17
Condition66%4/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
1016
not defined $key or $key eq "eof"
l!l&&r!l&&!r
1016
MC/DC33%1/3
not defined $key or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "eof" $key eq "interrupt"
Truth table50%2/4
not(A) or B or C
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"
ABCresult
1XX1
01X1
0011
0000
462  
463 16 ▶ if ($key eq 'q') { $self->quit(1); return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "q") 7 9 16
464  
465 9 ▶ if ($key eq 'left' || $key eq 'down') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "left" or $key eq "down") 0 9 9
Condition33%1/3
$key eq "left" or $key eq "down"
l!l&&r!l&&!r
009
MC/DC0%0/2
$key eq "left" or $key eq "down"
$key eq "left" $key eq "down"
Truth table33%1/3
A or B
A= $key eq "left"B= $key eq "down"
ABresult
1X1
011
000
466 0 $at = ($at - 1) % @$choices;
467 0 next;
468 }
469 9 ▶ if ($key eq 'right' || $key eq 'up' || $key eq 'tab') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "right" or $key eq "up" or $key eq "tab") 3 6 9
Condition66%4/6
$key eq "right" or $key eq "up" or $key eq "tab"
l!l&&r!l&&!r
306
$key eq "right" or $key eq "up"
l!l&&r!l&&!r
306
MC/DC33%1/3
$key eq "right" or $key eq "up" or $key eq "tab"
$key eq "right" $key eq "up" $key eq "tab"
Truth table50%2/4
A or B or C
A= $key eq "right"B= $key eq "up"C= $key eq "tab"
ABCresult
1XX1
01X1
0011
0000
470 3 $at = ($at + 1) % @$choices;
471 3 next;
472 }
473 6 ▶ if ($key eq 'home' || $key eq 'page_up') { $at = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "home" or $key eq "page_up") 0 6 6
Condition33%1/3
$key eq "home" or $key eq "page_up"
l!l&&r!l&&!r
006
MC/DC0%0/2
$key eq "home" or $key eq "page_up"
$key eq "home" $key eq "page_up"
Truth table33%1/3
A or B
A= $key eq "home"B= $key eq "page_up"
ABresult
1X1
011
000
474 6 ▶ if ($key eq 'end' || $key eq 'page_down') { $at = $#$choices; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "end" or $key eq "page_down") 0 6 6
Condition33%1/3
$key eq "end" or $key eq "page_down"
l!l&&r!l&&!r
006
MC/DC0%0/2
$key eq "end" or $key eq "page_down"
$key eq "end" $key eq "page_down"
Truth table33%1/3
A or B
A= $key eq "end"B= $key eq "page_down"
ABresult
1X1
011
000
475  
476 6 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 3 3 6
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
303
MC/DC50%1/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table66%2/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
477 3 return { kind => 'discard', card => $choices->[$at]{card} };
478 }
479  
480 3 ▶ if ($key eq 'k') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "k") 2 1 3
481 return { kind => 'discard', card => $choices->[$at]{card}, knock => 1 }
482 2 ▶ if $choices->[$at]{knock};
Branch: 1 branch
BranchTrueFalseTotal
if $choices->[$at]{"knock"} 1 1 2
483 1 @notice = ('you cannot knock on that one');
484 1 next;
485 }
486  
487 1 ▶ if ($key eq 'g' && $big) { return $big }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "g" and $big) 0 1 1
Condition33%1/3
$key eq "g" and $big
!ll&&!rl&&r
100
MC/DC0%0/2
$key eq "g" and $big
$key eq "g" $big
Truth table33%1/3
A and B
A= $key eq "g"B= $big
ABresult
0X0
100
111
488  
489 1 ▶ if ($key eq 'v') { $remelded = !$remelded; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "v") 1 0 1
490 0 ▶ if ($key eq '?') { @notice = $self->help_lines; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 0 0
491  
492 0 @notice = ('that key does nothing here. ? for the ones that do');
493 }
494 }
495  
496 17 ▶sub show_choice {
Subroutine show_choice: called CC=7 SCAR=19.5
Pod documented
497 17 my ($self, $seat, $choices, $at, $notice, $big, $remelded) = @_;
498  
499 17 my $card = $choices->[$at]{card};
500 17 my $deal = $self->game->deal;
501  
502 17 $self->clear;
503 17 $self->say_to('');
504  
505 17 ▶ if ($remelded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remelded) { } 1 16 17
506 1 $self->say_to($_) for @{ $self->render($seat, $card) };
507 1 $self->say_to('');
508 1 $self->say_to(' that is the hand WITHOUT ' . $self->pretty($card)
509 . '. v puts it back.');
510 }
511 else {
512 16 my %mark = (%{ $self->marks }, $card => 'cursor');
513 16 $self->say_to($_) for @{ $self->render($seat, undef, \%mark) };
514 16 $self->say_to('');
515 16 $self->say_to(' ' . $self->without_line($seat, $choices->[$at]));
516 }
517  
518 17 ▶ $self->say_to(' big gin is there for the taking: press g') if $big;
Branch: 1 branch
BranchTrueFalseTotal
if $big 0 17 17
519  
520 17 ▶ if (@$notice) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$notice) 1 16 17
521 1 $self->say_to('');
522 1 $self->say_to(' ', $_) for @$notice;
523 }
524  
525 17 $self->say_to('');
526 17 $self->say_to(' ', $self->legend($choices->[$at]));
527 17 return;
528 }
529  
530 16 ▶sub without_line {
Subroutine without_line: called CC=2 SCAR=6.9
Pod documented
531 16 my ($self, $seat, $choice) = @_;
532 16 my $melding = $self->preview($seat, $choice->{card});
533 my $line = 'throw ' . $self->pretty($choice->{card})
534 16 . ' and your count is ' . $melding->{deadwood};
535 16 ▶ $line .= ', which lets you knock' if $choice->{knock};
Branch: 1 branch
BranchTrueFalseTotal
if $choice->{"knock"} 1 15 16
536 16 return $line;
537 }
538  
539 0 ▶sub help_lines {
Subroutine help_lines: not called CC=1 SCAR=6.9
Pod documented
540 return (
541 0 'left and right walk your hand. the card under the cursor is the one',
542 'you would throw, and the line under the fan says what it leaves you.',
543 'enter discards it. k discards it and knocks, when that is allowed.',
544 'v shows the hand remelded without it. g declares big gin when it is',
545 'offered. q stops. ? is this.',
546 );
547 }
548  
549 34 ▶sub table_lines {
Subroutine table_lines: called CC=4 SCAR=13.9
Pod documented
550 34 my ($self, $deal, $seat) = @_;
551 34 my $upcard = $deal->upcard;
552 34 ▶ my @label = (
Branch: 1 branch
BranchTrueFalseTotal
defined $upcard ? : 28 6 34
553 sprintf('stock %d', $deal->stock_left),
554 defined $upcard ? 'upcard ' . $self->pretty($upcard) : 'no upcard',
555 );
556  
557 my @blocks = ({
558 width => CARD_WIDTH,
559 lines => $deal->stock_left ? $self->card_back : $self->card_space,
560 }, {
561 width => CARD_WIDTH,
562 lines => defined $upcard
563 34 ▶ ? $self->card_art($upcard, $self->marks->{$upcard})
Branch: 2 branches
BranchTrueFalseTotal
$deal->stock_left ? : 34 0 34
defined $upcard ? : 28 6 34
564 : $self->card_space,
565 });
566  
567 34 my $head = sprintf('%-*s', CARD_WIDTH + 5, $label[0]) . $label[1];
568 34 return ($head, $self->_beside(5, @blocks));
569 }
570  
571 54 ▶sub hand_groups {
Subroutine hand_groups: called CC=6 SCAR=17.9
Pod documented
572 54 my ($self, $melding) = @_;
573 54 my @group;
574 54 for my $meld (@{ $melding->{melds} }) {
575 36 my @cards = sort { $a <=> $b } @$meld;
576 36 my $same = 1;
577 36 ▶ $same &&= rank_of($_) == rank_of($cards[0]) for @cards;
Condition100%3/3
$same &&= rank_of($_) == rank_of($cards[0])
!ll&&!rl&&r
9990
MC/DC100%2/2
$same &&= rank_of($_) == rank_of($cards[0])
$same rank_of($_) == rank_of($cards[0])
Truth table100%3/3
A &&= B
A= $sameB= rank_of($_) == rank_of($cards[0])
ABresult
0X0
100
111
578 36 ▶ push @group, {
Branch: 1 branch
BranchTrueFalseTotal
$same ? : 27 9 36
579 cards => \@cards,
580 label => $same ? 'set' : 'run',
581 loose => 0,
582 };
583 }
584 push @group, {
585 1048 cards => [ sort { $a <=> $b } @{ $melding->{unmatched} } ],
586 label => 'loose ' . $melding->{deadwood},
587 loose => 1,
588 54 ▶ } if @{ $melding->{unmatched} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$melding->{"unmatched"};} 54 0 54
589 54 return \@group;
590 }
591  
592 35 ▶sub hand_lines {
Subroutine hand_lines: called CC=3 SCAR=11.0
Pod documented
593 35 my ($self, $melding, $marks) = @_;
594 35 my @group = @{ $self->hand_groups($melding) };
595  
596 35 my @ids = map { @{ $_->{cards} } } @group;
597 35 ▶ return ('you are holding nothing') unless @ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @ids 0 35 35
598  
599 35 ▶ my @lines = @{ $self->fan(\@ids, $marks || $self->marks) };
Condition66%2/3
$marks || $self->marks
l!l&&r!l&&!r
16190
MC/DC0%0/2
$marks || $self->marks
$marks $self->marks
Truth table66%2/3
A || B
A= $marksB= $self->marks
ABresult
1X1
011
000
600 35 push @lines, $self->_brackets(\@group, scalar @ids);
601 35 return @lines;
602 }
603  
604 35 ▶sub _brackets {
Subroutine _brackets: called CC=5 SCAR=16.1
605 35 my ($self, $groups, $total) = @_;
606 35 ▶ my ($h, $bl, $br) = $self->_ascii
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 31 4 35
607 ? ('-', '+', '+') : ("\x{2500}", "\x{2514}", "\x{2518}");
608 35 my $line = '';
609 35 my $seen = 0;
610 35 for my $group (@$groups) {
611 52 my $n = scalar @{ $group->{cards} };
612 52 $seen += $n;
613 52 ▶ my $width = $seen == $total ? $self->fan_width($n) : $n * FAN_WIDTH;
Branch: 1 branch
BranchTrueFalseTotal
$seen == $total ? : 35 17 52
614 52 my $label = $group->{label};
615 52 ▶ $label = '' if length($label) + 4 > $width;
Branch: 1 branch
BranchTrueFalseTotal
if length($label) + 4 > $width 0 52 52
616 52 my $rule = $width - 2 - length $label;
617 52 my $left = int($rule / 2);
618 52 $line .= $self->paint(
619 $bl . ($h x $left) . $label . ($h x ($rule - $left)) . $br, '90'
620 );
621 }
622 35 return $line;
623 }
624  
625 5 ▶sub show { my ($self, $seat) = @_; $self->say_to($_) for @{ $self->render($seat) }; return }
Subroutine show: called CC=2 SCAR=6.9
Pod documented
626  
627 44 ▶sub card_named {
Subroutine card_named: called CC=3 SCAR=11.0
Pod documented
628 44 my ($self, $text) = @_;
629 44 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 44 44
630 44 $text =~ s/\s+//g;
631 44 for my $suit (keys %SUIT) {
632 176 my $pip = $SUIT{$suit}{wide};
633 176 $text =~ s/\Q$pip\E/$suit/g;
634 }
635 44 $text =~ s/\A10/T/;
636 44 return id_of($text);
637 }
638  
639 1118 ▶sub bot_move { my ($self, $seat) = @_;
Subroutine bot_move: called CC=1 SCAR=0.0
Pod documented
640 1118 return Game::Gin::Bot->new(level => $self->_level)->choose($self->game, $seat) }
641  
642 3409 ▶sub is_human {
Subroutine is_human: called CC=4 SCAR=13.9
Pod documented
643 3409 my ($self, $seat) = @_;
644 3409 my $mode = $self->_mode;
645 3409 ▶ return 0 if $mode eq 'watch';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "watch" 3285 124 3409
646 124 ▶ return 1 if $mode eq 'hotseat';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "hotseat" 14 110 124
647 110 ▶ return $seat eq $self->_seat ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$seat eq $self->_seat ? : 41 69 110
648 }
649  
650 28 ▶sub human_move {
Subroutine human_move: called CC=25 SCAR=43.4
Pod documented
651 28 my ($self, $seat) = @_;
652 28 my $deal = $self->game->deal;
653 28 my $legal = $deal->legal($seat);
654 28 my $phase = $deal->phase;
655  
656 28 ▶ if ($phase eq 'forced_draw') { return { kind => 'draw' } }
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq "forced_draw") 0 28 28
657  
658 28 ▶ return $self->pick_move($seat, $phase, $legal) if $self->picking;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking 21 7 28
659  
660 7 ▶ if ($phase eq 'upcard') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq "upcard") 2 5 7
661 2 my $a = $self->ask('take the ' . $self->pretty($deal->upcard) . '? (y/n) ');
662 2 ▶ return undef unless defined $a;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 0 2 2
663 2 ▶ return undef if $self->_stopping($a);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_stopping($a) 0 2 2
664 2 ▶ return { kind => $a =~ /^y/i ? 'take' : 'pass' };
Branch: 1 branch
BranchTrueFalseTotal
$a =~ /^y/i ? : 0 2 2
665 }
666 5 ▶ if ($phase eq 'draw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq "draw") 3 2 5
667 3 my $a = $self->ask('(d)raw or (t)ake ' . $self->pretty($deal->upcard) . '? ');
668 3 ▶ return undef unless defined $a;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 0 3 3
669 3 ▶ return undef if $self->_stopping($a);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_stopping($a) 2 1 3
670 1 ▶ return { kind => $a =~ /^t/i ? 'take' : 'draw' };
Branch: 1 branch
BranchTrueFalseTotal
$a =~ /^t/i ? : 0 1 1
671 }
672  
673 2 my ($big) = grep { $_->{kind} eq 'big_gin' } @$legal;
674 2 ▶ if ($big) {
Branch: 1 branch
BranchTrueFalseTotal
if ($big) 0 2 2
675 0 my $a = $self->ask('big gin! declare it? (y/n) ');
676 0 ▶ return $big if defined $a && $a =~ /^y/i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a and $a =~ /^y/i 0 0 0
Condition0%0/3
defined $a and $a =~ /^y/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $a and $a =~ /^y/i
defined $a $a =~ /^y/i
Truth table0%0/3
A and B
A= defined $aB= $a =~ /^y/i
ABresult
0X0
100
111
677 }
678  
679 2 while (1) {
680 41 my $a = $self->ask('discard which card? (e.g. KH, or KH! to knock) ');
681 41 ▶ return undef unless defined $a;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 2 39 41
682 39 ▶ return undef if $self->_stopping($a);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_stopping($a) 0 39 39
683  
684 39 ▶ if ($a =~ /\A\s*(?:\?|h|help)\s*\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ /\A\s*(?:\?|h|help)\s*\z/i) 0 39 39
685 0 $self->say_to($_) for $self->typed_help;
686 0 next;
687 }
688  
689 39 my $knock = $a =~ s/!\s*$//;
690 39 my $card = $self->card_named($a);
691 39 ▶ unless (defined $card) { $self->say_to('not a card'); next }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card) 39 0 39
692 0 my ($move) = grep { $_->{kind} eq 'discard' && $_->{card} == $card
693 0 ▶ && (($_->{knock} ? 1 : 0) == ($knock ? 1 : 0)) } @$legal;
Branch: 2 branches
BranchTrueFalseTotal
$_->{'knock'} ? : 0 0 0
$knock ? : 0 0 0
Condition0%0/6
$_->{'kind'} eq 'discard' && $_->{'card'} == $card && ($_->{'knock'} ? 1 : 0) == ($knock ? 1 : 0)
!ll&&!rl&&r
000
$_->{"kind"} eq "discard" and $_->{"card"} == $card
!ll&&!rl&&r
000
MC/DC0%0/3
$_->{'kind'} eq 'discard' && $_->{'card'} == $card && ($_->{'knock'} ? 1 : 0) == ($knock ? 1 : 0)
$_->{"kind"} eq "discard" $_->{"card"} == $card ($_->{'knock'} ? 1 : 0) == ($knock ? 1 : 0)
Truth table0%0/4
A and B && C
A= $_->{"kind"} eq "discard"B= $_->{"card"} == $cardC= ($_->{'knock'} ? 1 : 0) == ($knock ? 1 : 0)
ABCresult
0XX0
10X0
1100
1111
694 0 ▶ return $move if $move;
Branch: 1 branch
BranchTrueFalseTotal
if $move 0 0 0
695 0 ▶ $self->say_to($knock ? 'you cannot knock on that' : 'you are not holding that');
Branch: 1 branch
BranchTrueFalseTotal
$knock ? : 0 0 0
696 }
697 }
698  
699 44 ▶sub _stopping {
Subroutine _stopping: called CC=3 SCAR=11.0
700 44 my ($self, $answer) = @_;
701 44 ▶ return 0 unless defined $answer && $answer =~ /\A\s*(?:q|quit|exit)\s*\z/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer and $answer =~ /\A\s*(?:q|quit|exit)\s*\z/i 42 2 44
Condition66%2/3
defined $answer and $answer =~ /\A\s*(?:q|quit|exit)\s*\z/i
!ll&&!rl&&r
0422
MC/DC50%1/2
defined $answer and $answer =~ /\A\s*(?:q|quit|exit)\s*\z/i
defined $answer $answer =~ /\A\s*(?:q|quit|exit)\s*\z/i
Truth table66%2/3
A and B
A= defined $answerB= $answer =~ /\A\s*(?:q|quit|exit)\s*\z/i
ABresult
0X0
100
111
702 2 $self->quit(1);
703 2 return 1;
704 }
705  
706 0 ▶sub typed_help {
Subroutine typed_help: not called CC=1 SCAR=6.9
Pod documented
707 return (
708 0 'name a card to discard it: KH, or 10H, or K with the pip itself.',
709 'add a ! to knock with it: KH!',
710 'quit stops. ? is this.',
711 );
712 }
713  
714 21 ▶sub pick_move {
Subroutine pick_move: called CC=4 SCAR=13.9
Pod documented
715 21 my ($self, $seat, $phase, $legal) = @_;
716  
717 21 ▶ unless ($self->enter_raw) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->enter_raw) 1 20 21
718 1 $self->picking(0);
719 1 return $self->human_move($seat);
720 }
721  
722 20 ▶ return $self->pick_draw($seat, $phase) if $phase eq 'upcard' || $phase eq 'draw';
Branch: 1 branch
BranchTrueFalseTotal
if $phase eq "upcard" or $phase eq "draw" 11 9 20
Condition100%3/3
$phase eq "upcard" or $phase eq "draw"
l!l&&r!l&&!r
1109
MC/DC100%2/2
$phase eq "upcard" or $phase eq "draw"
$phase eq "upcard" $phase eq "draw"
Truth table100%3/3
A or B
A= $phase eq "upcard"B= $phase eq "draw"
ABresult
1X1
011
000
723 9 return $self->pick_discard($seat);
724 }
725  
726 11 ▶sub pick_draw {
Subroutine pick_draw: called CC=18 SCAR=39.8
Pod documented
727 11 my ($self, $seat, $phase) = @_;
728  
729 11 my $deal = $self->game->deal;
730 11 my $upcard = $deal->upcard;
731 11 my @notice;
732  
733 11 while (1) {
734 11 $self->clear;
735 11 $self->say_to('');
736 11 $self->say_to($_) for @{ $self->render($seat) };
737 11 $self->say_to('');
738 11 ▶ $self->say_to(' u takes the ' . $self->pretty($upcard)
Branch: 1 branch
BranchTrueFalseTotal
$phase eq 'upcard' ? : 1 10 11
739 . ', d draws from the stock'
740 . ($phase eq 'upcard' ? ', n passes it up' : ''));
741  
742 11 ▶ if (@notice) {
Branch: 1 branch
BranchTrueFalseTotal
if (@notice) 0 11 11
743 0 $self->say_to('');
744 0 $self->say_to(' ', $_) for @notice;
745 0 @notice = ();
746 }
747  
748 11 $self->say_to('');
749 11 $self->say_to(' ? for the rest, q to stop');
750  
751 11 my $key = $self->read_key;
752 11 ▶ return undef if !defined $key || $key eq 'eof' || $key eq 'interrupt';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "eof" or $key eq "interrupt" 0 11 11
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
0011
not defined $key or $key eq "eof"
l!l&&r!l&&!r
0011
MC/DC0%0/3
not defined $key or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "eof" $key eq "interrupt"
Truth table25%1/4
not(A) or B or C
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"
ABCresult
1XX1
01X1
0011
0000
753 11 ▶ if ($key eq 'q') { $self->quit(1); return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "q") 2 9 11
754  
755 9 ▶ return { kind => 'take' } if $key eq 'u' || $key eq 't';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "u" or $key eq "t" 1 8 9
Condition66%2/3
$key eq "u" or $key eq "t"
l!l&&r!l&&!r
108
MC/DC50%1/2
$key eq "u" or $key eq "t"
$key eq "u" $key eq "t"
Truth table66%2/3
A or B
A= $key eq "u"B= $key eq "t"
ABresult
1X1
011
000
756  
757 8 ▶ if ($key eq 'd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "d") 8 0 8
758 8 ▶ return { kind => 'draw' } if $phase eq 'draw';
Branch: 1 branch
BranchTrueFalseTotal
if $phase eq "draw" 8 0 8
759 0 @notice = ('on the first turn it is take it or pass it up: u or n');
760 0 next;
761 }
762  
763 0 ▶ if ($key eq 'n' || $key eq 'p') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "n" or $key eq "p") 0 0 0
Condition0%0/3
$key eq "n" or $key eq "p"
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "n" or $key eq "p"
$key eq "n" $key eq "p"
Truth table0%0/3
A or B
A= $key eq "n"B= $key eq "p"
ABresult
1X1
011
000
764 0 ▶ return { kind => 'pass' } if $phase eq 'upcard';
Branch: 1 branch
BranchTrueFalseTotal
if $phase eq "upcard" 0 0 0
765 0 @notice = ('you cannot pass now: u to take it, d to draw');
766 0 next;
767 }
768  
769 0 ▶ if ($key eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 0 0
770 0 ▶ @notice = ($phase eq 'upcard'
Branch: 1 branch
BranchTrueFalseTotal
$phase eq 'upcard' ? : 0 0 0
771 ? 'u takes the upcard, n passes it up. that is the whole choice.'
772 : 'u takes the upcard, d draws the card nobody has seen.');
773 0 next;
774 }
775  
776 0 @notice = ('that key does nothing here. ? for the ones that do');
777 }
778 }
779  
780 16 ▶sub play {
Subroutine play: called CC=20 SCAR=30.5
Pod documented
781 16 my ($self, %o) = @_;
782 16 ▶ my $seed = $o{seed} || Digest::SHA::sha256(join ':', 'gin', $$, time, rand);
Condition33%1/3
$o{'seed'} || &Digest::SHA::sha256(join(':', 'gin', $$, time, rand))
l!l&&r!l&&!r
1600
MC/DC0%0/2
$o{'seed'} || &Digest::SHA::sha256(join(':', 'gin', $$, time, rand))
$o{'seed'} &Digest::SHA::sha256(join(':', 'gin', $$, time, rand))
Truth table33%1/3
A || B
A= $o{'seed'}B= &Digest::SHA::sha256(join(':', 'gin', $$, time, rand))
ABresult
1X1
011
000
783 16 ▶ $self->game(Game::Gin->build(seed => $seed, dealer => $o{dealer} || 'p1'));
Condition50%1/2
$o{'dealer'} || 'p1'
l!l
016
MC/DC0%0/1
$o{'dealer'} || 'p1'
$o{'dealer'}
Truth table50%1/2
A
A= $o{'dealer'}
Aresult
11
00
784  
785 16 ▶ $self->say_to(sprintf('deal %d, dealt by %s',
Branch: 1 branch
BranchTrueFalseTotal
if $self->_mode eq "watch" 3 13 16
786 $self->game->number, $self->game->dealer))
787 if $self->_mode eq 'watch';
788  
789 16 ▶ my $limit = $o{limit} || 20_000;
Condition100%2/2
$o{'limit'} || 20000
l!l
142
MC/DC100%1/1
$o{'limit'} || 20000
$o{'limit'}
Truth table100%2/2
A
A= $o{'limit'}
Aresult
11
00
790 16 my $moves = 0;
791 16 my $last_seat;
792 16 ▶ while (!$self->game->over && !$self->quit && $moves++ < $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->game->over || $self->quit and $moves++ < $limit) 1145 3 1148
Condition66%4/6
not $self->game->over || $self->quit and $moves++ < $limit
!ll&&!rl&&r
301145
$self->game->over || $self->quit
l!l&&r!l&&!r
301145
MC/DC33%1/3
not $self->game->over || $self->quit and $moves++ < $limit
$self->game->over $self->quit $moves++ < $limit
Truth table50%2/4
not(A || B) and C
A= $self->game->overB= $self->quitC= $moves++ < $limit
ABCresult
1XX0
01X0
0000
0011
793 1145 ▶ my $seat = $self->game->turn or last;
Branch: 1 branch
BranchTrueFalseTotal
my $seat = $self->game->turn or last 1145 0 1145
794 1145 my $number = $self->game->number;
795  
796 1145 my $move;
797 1145 ▶ if ($self->is_human($seat)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_human($seat)) { } 27 1118 1145
798 27 ▶ if (!defined $last_seat || $last_seat ne $seat) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $last_seat or $last_seat ne $seat) 15 12 27
Condition100%3/3
not defined $last_seat or $last_seat ne $seat
l!l&&r!l&&!r
13212
MC/DC100%2/2
not defined $last_seat or $last_seat ne $seat
not defined $last_seat $last_seat ne $seat
Truth table100%3/3
not(A) or B
A= not defined $last_seatB= $last_seat ne $seat
ABresult
1X1
011
000
799 15 ▶ last unless $self->handover($seat);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->handover($seat) 1 14 15
800 14 $last_seat = $seat;
801 }
802 26 ▶ $self->show($seat) unless $self->picking;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->picking 5 21 26
803 26 $move = $self->human_move($seat);
804 26 ▶ last unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 12 14 26
805 }
806 else {
807 1118 $move = $self->bot_move($seat);
808 1118 ▶ last unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 0 1118 1118
809 }
810  
811 my %held = $self->is_human($seat)
812 1132 ▶ ? (map { $_ => 1 } @{ $self->game->deal->hand_of($seat)->cards })
Branch: 1 branch
BranchTrueFalseTotal
$self->is_human($seat) ? : 14 1118 1132
813 : ();
814  
815 1132 my @out = $self->game->apply($seat, $move);
816 1132 ▶ if (ref $out[0] eq 'Game::Gin::Error') { $self->say_to($out[0]->message); next }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $out[0] eq "Game::Gin::Error") 0 1132 1132
817 1132 $self->remember($seat, \%held, \@out);
818 1132 $self->announce($_) for @out;
819 1132 ▶ $self->say_to('') if $self->game->number != $number;
Branch: 1 branch
BranchTrueFalseTotal
if $self->game->number != $number 33 1099 1132
820 }
821  
822 16 $self->leave_raw;
823  
824 16 $self->announce_result;
825 16 return $self->game;
826 }
827  
828 1132 ▶sub remember {
Subroutine remember: called CC=9 SCAR=22.0
Pod documented
829 1132 my ($self, $seat, $held, $out) = @_;
830  
831 1132 my $discard = grep { $_->{kind} eq 'discard' } @$out;
832 1132 my $deal = $self->game->deal;
833  
834 1132 ▶ if ($discard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($discard) 551 581 1132
835 551 my $upcard = $deal->upcard;
836 551 ▶ $self->marks(defined $upcard && !$self->is_human($seat)
Branch: 1 branch
BranchTrueFalseTotal
defined $upcard && !$self->is_human($seat) ? : 549 2 551
Condition66%2/3
defined $upcard and not $self->is_human($seat)
!ll&&!rl&&r
02549
MC/DC50%1/2
defined $upcard and not $self->is_human($seat)
defined $upcard not $self->is_human($seat)
Truth table66%2/3
A and not(B)
A= defined $upcardB= not $self->is_human($seat)
ABresult
0X0
100
111
837 ? { $upcard => 'took' } : {});
838 551 return $self;
839 }
840  
841 581 ▶ return $self->marks({}) unless $self->is_human($seat);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_human($seat) 569 12 581
842  
843 12 ▶ my $hand = $deal->hand_of($seat) or return $self->marks({});
Branch: 1 branch
BranchTrueFalseTotal
my $hand = $deal->hand_of($seat) or return $self->marks({}) 12 0 12
844 12 my $took = grep { $_->{kind} eq 'take' } @$out;
845  
846 12 my %mark;
847 12 for my $card (@{ $hand->cards }) {
848 130 ▶ next if $held->{$card};
Branch: 1 branch
BranchTrueFalseTotal
if $held->{$card} 120 10 130
849 10 ▶ $mark{$card} = $took ? 'took' : 'drew';
Branch: 1 branch
BranchTrueFalseTotal
$took ? : 1 9 10
850 }
851  
852 12 $self->marks(\%mark);
853 12 return $self;
854 }
855  
856 1204 ▶sub announce {
Subroutine announce: called CC=12 SCAR=25.0
Pod documented
857 1204 my ($self, $out) = @_;
858 1204 my $k = $out->{kind};
859 1204 ▶ return $self->say_to("$out->{seat} takes " . $self->pretty($out->{card})) if $k eq 'take';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "take" 165 1039 1204
860 1039 ▶ return $self->say_to("$out->{seat} draws") if $k eq 'draw';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "draw" 395 644 1039
861 644 ▶ return $self->say_to("$out->{seat} passes") if $k eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "pass" 20 624 644
862 return $self->say_to("$out->{seat} discards " . $self->pretty($out->{card})
863 624 ▶ . ($out->{knock} ? ' and knocks' : '')) if $k eq 'discard';
Branch: 2 branches
BranchTrueFalseTotal
if $k eq "discard" 551 73 624
$out->{'knock'} ? : 35 516 551
864 73 ▶ return $self->say_to("$out->{seat} declares big gin") if $k eq 'big_gin';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "big_gin" 1 72 73
865 72 ▶ return $self->say_to('the stock ran out: the hand is cancelled') if $k eq 'cancelled';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "cancelled" 0 72 72
866 72 ▶ if ($k eq 'hand_end') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "hand_end") 36 36 72
867 return $self->say_to(sprintf('%s wins the hand by %s, %d points',
868 $out->{winner}, $out->{how} // '?', $out->{points}))
869 36 ▶ if $out->{winner};
Branch: 1 branch
BranchTrueFalseTotal
if $out->{"winner"} 36 0 36
Condition50%1/2
$out->{"how"} // "?"
l!l
360
MC/DC0%0/1
$out->{"how"} // "?"
$out->{"how"}
Truth table50%1/2
A
A= $out->{"how"}
Aresult
11
00
870 0 return $self->say_to('the hand is over');
871 }
872 36 ▶ return $self->say_to(sprintf('deal %d, dealt by %s', $out->{number}, $out->{dealer}))
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "deal" 33 3 36
873 if $k eq 'deal';
874 3 return;
875 }
876  
877 16 ▶sub announce_result {
Subroutine announce_result: called CC=6 SCAR=19.2
Pod documented
878 16 my ($self) = @_;
879 16 ▶ my $r = $self->game->result or return $self->say_to('unfinished');
Branch: 1 branch
BranchTrueFalseTotal
my $r = $self->game->result or return $self->say_to("unfinished") 3 13 16
880 $self->say_to(sprintf('%s wins: %d to %d%s',
881 $r->{winner} // 'nobody',
882 $r->{totals}{ $r->{winner} // 'p1' },
883 $r->{totals}{ $r->{loser} // 'p2' },
884 3 ▶ $r->{shutout} ? ', a shutout' : ''));
Branch: 1 branch
BranchTrueFalseTotal
$r->{'shutout'} ? : 0 3 3
Condition50%3/6
$r->{"winner"} // "nobody"
l!l
30
$r->{"winner"} // "p1"
l!l
30
$r->{"loser"} // "p2"
l!l
30
MC/DC0%0/1
$r->{"winner"} // "nobody"
$r->{"winner"}
MC/DC0%0/1
$r->{"winner"} // "p1"
$r->{"winner"}
MC/DC0%0/1
$r->{"loser"} // "p2"
$r->{"loser"}
Truth table50%1/2
A
A= $r->{"winner"}
Aresult
11
00
Truth table50%1/2
A
A= $r->{"winner"}
Aresult
11
00
Truth table50%1/2
A
A= $r->{"loser"}
Aresult
11
00
885 3 return;
886 }
887  
888 0 ▶sub _usage {
Subroutine _usage: not called CC=1 SCAR=6.9
889 0 return <<'USAGE';
890 gin - gin rummy at a prompt
891  
892 gin play the bot as p1
893 gin --seat p2 play the bot as p2
894 gin --mode hotseat two people at one keyboard
895 gin --mode watch two bots, and you read
896 gin --level 1 a weaker opponent (1 or 2)
897 gin --seed tuesday deal the same game every time
898 gin --ascii letters instead of the suit pips
899 gin --nocolour no colour (NO_COLOR does the same)
900 gin --nopick type the moves out instead of pointing at them
901  
902 Your hand is fanned like a hand of cards, with a bracket under each meld
903 saying whether it is a set or a run and what the rest of it is costing you.
904  
905 On a terminal you point at the card you mean: the left and right keys walk
906 your own hand, and the fan above shows what the hand WOULD be without the
907 card under the cursor, remelded, with the deadwood that leaves you. Enter
908 discards it, k discards and knocks when that is allowed, u takes the upcard,
909 d draws, ? lists the keys and q stops. Needs Term::ReadKey; without it, or
910 off a terminal, every move is typed.
911  
912 Typed instead: answer y or n for the upcard, d or t to draw or take, and name
913 a card to discard it (KH). Add a ! to knock with it (KH!). What the card
914 itself says is taken too, so the suit pip and 10H work as well as KH and TH.
915 Type quit to stop, or help for the rest.
916  
917 The card you just drew or took is marked in your hand, and so is the upcard
918 when your opponent took it rather than drawing.
919  
920 Exit status is 0 if your seat won, or if nobody was playing a seat, 1 if you
921 lost or stopped early, and 2 for a bad option.
922 USAGE
923 }
924  
925 0 ▶sub run {
Subroutine run: not called CC=24 SCAR=64.0
Pod documented
926 0 my ($class, @argv) = @_;
927 0 my %o = (mode => 'bot', level => 2, seat => 'p1');
928  
929 0 my $ok = eval {
930 0 ▶ local $SIG{__WARN__} = sub { print STDERR "gin: $_[0]" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
931 Getopt::Long::GetOptionsFromArray(\@argv,
932 'mode=s' => \$o{mode},
933 'level=i' => \$o{level},
934 'seat=s' => \$o{seat},
935 'seed=s' => \$o{seed},
936 'ascii' => \$o{ascii},
937 'colour!' => \$o{colour},
938 'color!' => \$o{colour},
939 'pick!' => \$o{pick},
940 'help' => \$o{help},
941 0 );
942 };
943 0 ▶ if (!$ok) { print STDERR _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
944 0 ▶ if ($o{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($o{"help"}) 0 0 0
945 0 ▶ if (@argv) { print STDERR "gin: not an option: $argv[0]\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 0 0 0
946  
947 0 my $levels = Game::Gin::Search::LEVELS;
948 my $refused
949 = $o{mode} !~ /\A(?:bot|hotseat|watch)\z/ ? 'mode must be bot, hotseat or watch'
950 : $o{seat} !~ /\A p[12] \z/x ? 'seat must be p1 or p2'
951 0 ▶ : !($o{level} =~ /\A\d+\z/ && $o{level} >= 1 && $o{level} <= $levels)
Branch: 3 branches
BranchTrueFalseTotal
!($o{'mode'} =~ /\A(?:bot|hotseat|watch)\z/) ? : 0 0 0
!($o{'seat'} =~ /\A p[12] \z/x) ? : 0 0 0
!($o{'level'} =~ /\A\d+\z/ && $o{'level'} >= 1 && $o{'level'} <= $levels) ? : 0 0 0
Condition0%0/6
$o{'level'} =~ /\A\d+\z/ && $o{'level'} >= 1 && $o{'level'} <= $levels
!ll&&!rl&&r
000
$o{"level"} =~ /\A\d+\z/ and $o{"level"} >= 1
!ll&&!rl&&r
000
MC/DC0%0/3
$o{'level'} =~ /\A\d+\z/ && $o{'level'} >= 1 && $o{'level'} <= $levels
$o{"level"} =~ /\A\d+\z/ $o{"level"} >= 1 $o{'level'} <= $levels
Truth table0%0/4
A and B && C
A= $o{"level"} =~ /\A\d+\z/B= $o{"level"} >= 1C= $o{'level'} <= $levels
ABCresult
0XX0
10X0
1100
1111
952 ? "level must be 1 to $levels"
953 : undef;
954 0 ▶ if (defined $refused) { print STDERR "gin: $refused\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $refused) 0 0 0
955  
956 0 ▶ my $seed = defined $o{seed} ? Digest::SHA::sha256($o{seed}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'seed'} ? : 0 0 0
957  
958 my $ui = $class->new(
959 mode => $o{mode},
960 level => $o{level},
961 seat => $o{seat},
962 ($o{ascii} ? (ascii => 1) : ()),
963 (defined $o{colour} ? (colour => $o{colour}) : ()),
964 0 ▶ (defined $o{pick} ? (picking => $o{pick} ? 1 : 0) : ()),
Branch: 4 branches
BranchTrueFalseTotal
$o{'ascii'} ? : 0 0 0
defined $o{'colour'} ? : 0 0 0
defined $o{'pick'} ? : 0 0 0
$o{'pick'} ? : 0 0 0
965 );
966  
967 0 local @SIG{qw(INT TERM HUP)};
968 0 for my $sig (qw(INT TERM HUP)) {
969 0 ▶ $SIG{$sig} = sub { $ui->leave_raw; $SIG{$sig} = 'DEFAULT'; kill $sig, $$ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
970 }
971  
972 0 ▶ my $game = eval { $ui->play(defined $seed ? (seed => $seed) : ()) };
Branch: 1 branch
BranchTrueFalseTotal
defined $seed ? : 0 0 0
973 0 my $died = $@;
974 0 $ui->leave_raw;
975 0 ▶ die $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 0 0 0
976  
977 0 ▶ my $result = $game && $game->result;
Condition0%0/3
$game && $game->result
!ll&&!rl&&r
000
MC/DC0%0/2
$game && $game->result
$game $game->result
Truth table0%0/3
A && B
A= $gameB= $game->result
ABresult
0X0
100
111
978 0 ▶ return 1 unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
979 0 ▶ return 0 if $o{mode} eq 'watch' || $o{mode} eq 'hotseat';
Branch: 1 branch
BranchTrueFalseTotal
if $o{"mode"} eq "watch" or $o{"mode"} eq "hotseat" 0 0 0
Condition0%0/3
$o{"mode"} eq "watch" or $o{"mode"} eq "hotseat"
l!l&&r!l&&!r
000
MC/DC0%0/2
$o{"mode"} eq "watch" or $o{"mode"} eq "hotseat"
$o{"mode"} eq "watch" $o{"mode"} eq "hotseat"
Truth table0%0/3
A or B
A= $o{"mode"} eq "watch"B= $o{"mode"} eq "hotseat"
ABresult
1X1
011
000
980 0 ▶ return 0 if ($result->{winner} // '') eq $o{seat};
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"winner"} // "") eq $o{"seat"} 0 0 0
Condition0%0/2
$result->{"winner"} // ""
l!l
00
MC/DC0%0/1
$result->{"winner"} // ""
$result->{"winner"}
Truth table0%0/2
A
A= $result->{"winner"}
Aresult
11
00
981 0 return 1;
982 }
983  
984  
985 2 1;
986  
987 __END__
↑ Summary Gin/Bot.pm »