Gin/Terminal.pm

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