blib/lib/Game/Durak/Terminal.pm

filter: Statementstmt 74.5% 349 / 468 Branchbran 60.9% 173 / 284 Conditioncond 52.8% 64 / 121 MC/DCmcdc 37.1% 26 / 70 Subroutinesub 83.6% 46 / 55 Podpod 97.1% 34 / 35 totaltotal 66.9% 692 / 1033 CC 193 SCAR 36.7
CC
193
Cov
67%
pick_move
184.5
headline
56.0
run
47.7
CRAP 39.1
↑ Summary blib/lib/Game/Durak/Table.pm »
1 package Game::Durak::Terminal;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Game::Durak ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Game::Durak::Bot ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Game::Durak::Card qw(name_of long_name_of suit_of rank_of face_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Game::Durak::Table ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 our $VERSION = '0.03';
15  
16 5 my %SUIT = (
17 S => { wide => "\x{2660}", ascii => 'S', colour => '37' },
18 H => { wide => "\x{2665}", ascii => 'H', colour => '31' },
19 D => { wide => "\x{2666}", ascii => 'D', colour => '31' },
20 C => { wide => "\x{2663}", ascii => 'C', colour => '37' },
21 );
22  
23 5 my %SAID = (
24 attack => 'attack with',
25 beat => 'beat it with',
26 take => 'take the bout',
27 done => 'are done',
28 swap => 'exchange the trump six',
29 resign => 'give up',
30 );
31  
32 5 has game => (is => 'rw', isa => Any);
33 5 has out => (is => 'ro', isa => Any);
34 5 has in => (is => 'ro', isa => Any);
35 5 has seed => (is => 'ro', isa => Str);
36 5 has level => (is => 'ro', isa => Int);
37 5 has seat => (is => 'ro', isa => Int);
38 5 has ascii => (is => 'ro', isa => Any);
39 5 has colour => (is => 'ro', isa => Any);
40 5 has paint => (is => 'ro', isa => Any);
41 5 has width => (is => 'ro', isa => Int);
42 5 has height => (is => 'ro', isa => Int);
43 5 has _painting => (is => 'rw', isa => Any);
44  
45 5 has raw => (is => 'rw', isa => Any, default => 0);
46  
47 5 has keysource => (is => 'rw', isa => Any);
48  
49 16 ▶has pending => (is => 'rw', isa => ArrayRef, default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
50  
51 5 has _picking => (is => 'rw', isa => Any, init_arg => 'picking');
52  
53 5 our %SEQUENCE = (
54 'A' => 'up', 'B' => 'down', 'C' => 'right', 'D' => 'left',
55 'H' => 'home', 'F' => 'end',
56 '1~' => 'home', '4~' => 'end', '5~' => 'page_up', '6~' => 'page_down',
57 '7~' => 'home', '8~' => 'end',
58 );
59  
60 5 our %CONTROL = (
61 "\r" => 'enter', "\n" => 'enter', "\t" => 'tab',
62 "\x7f" => 'backspace', "\x08" => 'backspace',
63 "\x03" => 'interrupt', "\x04" => 'eof',
64 );
65 5 has _table => (is => 'rw', isa => Any);
66 5 has _said => (is => 'rw', isa => Any);
67  
68 373 ▶sub _out { return $_[0]->out || \*STDOUT }
Subroutine _out: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]->out || \*STDOUT
l!l
35716
MC/DC100%1/1
$_[0]->out || \*STDOUT
$_[0]->out
Truth table100%2/2
A
A= $_[0]->out
Aresult
11
00
69 84 ▶sub _in { return $_[0]->in || \*STDIN }
Subroutine _in: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]->in || \*STDIN
l!l
804
MC/DC100%1/1
$_[0]->in || \*STDIN
$_[0]->in
Truth table100%2/2
A
A= $_[0]->in
Aresult
11
00
70 4 ▶sub _level { return defined $_[0]->level ? $_[0]->level : 3 }
Subroutine _level: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->level ? : 4 0 4
71 4 ▶sub _seat { return defined $_[0]->seat ? $_[0]->seat : 1 }
Subroutine _seat: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->seat ? : 3 1 4
72 1239 ▶sub _ascii { return $_[0]->ascii ? 1 : 0 }
Subroutine _ascii: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->ascii ? : 1217 22 1239
73 0 ▶sub _them { return 3 - $_[0]->_seat }
Subroutine _them: not called CC=1 SCAR=6.9
74  
75 16 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
76 16 my ($self) = @_;
77 16 ▶ binmode $self->_out, ':encoding(UTF-8)' unless $self->_ascii;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_ascii 2 14 16
78 16 my $previous = select $self->_out;
79 16 $| = 1;
80 16 select $previous;
81 16 return $self;
82 }
83  
84 1417 ▶sub _colour {
Subroutine _colour: called CC=6 SCAR=18.6
85 1417 my ($self) = @_;
86 1417 my $on = $self->_painting;
87 1417 ▶ return $on if defined $on;
Branch: 1 branch
BranchTrueFalseTotal
if defined $on 1406 11 1417
88 $on = defined $self->colour ? ($self->colour ? 1 : 0)
89 11 ▶ : ((eval { -t $self->_out } && !$ENV{NO_COLOR}) ? 1 : 0);
Branch: 3 branches
BranchTrueFalseTotal
defined $self->colour ? : 9 2 11
$self->colour ? : 0 9 9
eval { do { -t $self->_out } } && !$ENV{'NO_COLOR'} ? : 0 2 2
Condition33%1/3
eval { do { -t $self->_out } } and not $ENV{"NO_COLOR"}
!ll&&!rl&&r
200
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
90 11 $self->_painting($on);
91 11 return $on;
92 }
93  
94 214 ▶sub _paint {
Subroutine _paint: called CC=3 SCAR=11.1
95 214 my ($self) = @_;
96 214 ▶ return $self->paint ? 1 : 0 if defined $self->paint;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->paint 21 193 214
$self->paint ? : 0 21 21
97 193 return $self->_colour;
98 }
99  
100 0 ▶sub table_of {
Subroutine table_of: not called CC=4 SCAR=30.0
Pod documented
101 0 my ($self) = @_;
102 0 my $table = $self->_table;
103 0 ▶ return $table if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 0 0 0
104 0 ▶ $table = Game::Durak::Table->new(
Condition0%0/4
$self->width || 80
l!l
00
$self->height || 24
l!l
00
MC/DC0%0/1
$self->width || 80
$self->width
MC/DC0%0/1
$self->height || 24
$self->height
Truth table0%0/2
A
A= $self->width
Aresult
11
00
Truth table0%0/2
A
A= $self->height
Aresult
11
00
105 width => $self->width || 80,
106 height => $self->height || 24,
107 ascii => $self->ascii,
108 );
109 0 $self->_table($table);
110 0 return $table;
111 }
112  
113 0 ▶sub frame {
Subroutine frame: not called CC=2 SCAR=17.9
Pod documented
114 0 my ($self, $view, %o) = @_;
115 0 my $table = $self->table_of;
116 0 my $grid = $table->screen($view, %o);
117 0 ▶ return $self->_colour ? $table->paint($grid) : $table->lines($grid);
Branch: 1 branch
BranchTrueFalseTotal
$self->_colour ? : 0 0 0
118 }
119  
120 0 ▶sub headline {
Subroutine headline: not called CC=7 SCAR=40.3
Pod documented
121 0 my ($self, $room) = @_;
122 0 ▶ my @said = @{ $self->_said || [] };
Condition0%0/2
$self->_said || []
l!l
00
MC/DC0%0/1
$self->_said || []
$self->_said
Truth table0%0/2
A
A= $self->_said
Aresult
11
00
123 0 ▶ return '' unless @said;
Branch: 1 branch
BranchTrueFalseTotal
unless @said 0 0 0
124 0 my $line = '';
125 0 ▶ while (@said) {
Branch: 1 branch
BranchTrueFalseTotal
if (@said) 0 0 0
126 0 my $next = pop @said;
127 0 ▶ my $grown = length $line ? "$next, $line" : $next;
Branch: 1 branch
BranchTrueFalseTotal
length $line ? : 0 0 0
128 0 ▶ last if length $grown > $room;
Branch: 1 branch
BranchTrueFalseTotal
if length $grown > $room 0 0 0
129 0 $line = $grown;
130 }
131 0 ▶ $line = substr((pop @{ $self->_said }), 0, $room) unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 0 0
132 0 return $line;
133 }
134  
135 0 ▶sub show {
Subroutine show: not called CC=4 SCAR=30.0
Pod documented
136 0 my ($self, $view, %o) = @_;
137 0 my $handle = $self->_out;
138 0 my $table = $self->table_of;
139 0 ▶ my $footer = defined $o{footer} ? $o{footer} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'footer'} ? : 0 0 0
140 my $lines = $self->frame($view,
141 header => (defined $o{header} ? $o{header} : $self->headline($table->width - 2)),
142 footer => $footer,
143 0 ▶ (defined $o{cursor} ? (cursor => $o{cursor}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $o{'header'} ? : 0 0 0
defined $o{'cursor'} ? : 0 0 0
144 );
145 0 print {$handle} "\e[H", join("\n", @$lines);
146 0 print {$handle} sprintf "\e[%d;%dH", $table->height, length($footer) + 3;
147 0 $self->_said([]);
148 0 return;
149 }
150  
151 119 ▶sub told {
Subroutine told: called CC=5 SCAR=21.7
Pod documented
152 119 my ($self, @lines) = @_;
153 119 ▶ return unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 119 119
154 119 ▶ if ($self->_paint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_paint) 0 119 119
155 0 ▶ my $said = $self->_said || [];
Condition0%0/2
$self->_said || []
l!l
00
MC/DC0%0/1
$self->_said || []
$self->_said
Truth table0%0/2
A
A= $self->_said
Aresult
11
00
156 0 push @$said, @lines;
157 0 $self->_said($said);
158 0 return;
159 }
160 119 $self->say(" $_") for @lines;
161 119 return;
162 }
163  
164 353 ▶sub say {
Subroutine say: called CC=1 SCAR=0.0
Pod documented
165 353 my ($self, @what) = @_;
166 353 my $handle = $self->_out;
167 353 print {$handle} @what, "\n";
168 353 return;
169 }
170  
171 1199 ▶sub card {
Subroutine card: called CC=4 SCAR=14.0
Pod documented
172 1199 my ($self, $id) = @_;
173 1199 ▶ return '--' unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 70 1129 1199
174 1129 my $suit = suit_of($id);
175 my $face = face_of($id)
176 1129 ▶ . ($self->_ascii ? $SUIT{$suit}{ascii} : $SUIT{$suit}{wide});
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 1113 16 1129
177 1129 ▶ return $face unless $self->_colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_colour 1129 0 1129
178 0 return "\e[$SUIT{$suit}{colour}m$face\e[0m";
179 }
180  
181 92 ▶sub hand_line {
Subroutine hand_line: called CC=3 SCAR=11.0
Pod documented
182 92 my ($self, $hand, $cursor) = @_;
183 92 my $n = 0;
184  
185 92 ▶ return join ' ', map { ++$n . ') ' . $self->card($_) } @$hand
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cursor 83 9 92
186 unless defined $cursor;
187  
188 return join ' ', map {
189 9 $n++;
190 40 ▶ ($cursor == $n ? '>' : ' ') . $n . ') ' . $self->card($_);
Branch: 1 branch
BranchTrueFalseTotal
$cursor == $n ? : 9 31 40
191 } @$hand;
192 }
193  
194 90 ▶sub bout_line {
Subroutine bout_line: called CC=3 SCAR=11.0
Pod documented
195 90 my ($self, $bout) = @_;
196 90 ▶ return 'nothing on the table' unless $bout && @{ $bout->{pairs} };
Branch: 1 branch
BranchTrueFalseTotal
unless $bout and @{$bout->{"pairs"};} 8 82 90
Condition66%2/3
$bout and @{$bout->{"pairs"};}
!ll&&!rl&&r
0882
MC/DC50%1/2
$bout and @{$bout->{"pairs"};}
$bout @{$bout->{"pairs"};}
Truth table66%2/3
A and B
A= $boutB= @{$bout->{"pairs"};}
ABresult
0X0
100
111
197 return join ' ', map {
198 $self->card($_->{attack}) . ' / ' . $self->card($_->{beat})
199 82 } @{ $bout->{pairs} };
200 }
201  
202 89 ▶sub table {
Subroutine table: called CC=2 SCAR=6.9
Pod documented
203 89 my ($self, $view, %o) = @_;
204  
205 89 my @lines;
206 89 push @lines, '';
207 push @lines, sprintf 'trump %s%s talon %d heap %d they hold %d',
208 $self->card_suit($view->{trump}),
209 (defined $view->{trump_card}
210 ? ' (' . $self->card($view->{trump_card}) . ' face up)' : ''),
211 $view->{talon}, $view->{discard},
212 89 ▶ $view->{counts}{ 3 - $view->{seat} };
Branch: 1 branch
BranchTrueFalseTotal
defined $view->{'trump_card'} ? : 68 21 89
213 89 push @lines, '';
214 89 push @lines, ' ' . $self->bout_line($view->{bout});
215 89 push @lines, '';
216 89 push @lines, ' ' . $self->hand_line($view->{hand}, $o{cursor});
217 89 return join "\n", @lines;
218 }
219  
220 94 ▶sub card_suit {
Subroutine card_suit: called CC=3 SCAR=11.1
Pod documented
221 94 my ($self, $suit) = @_;
222 94 ▶ my $face = $self->_ascii ? $SUIT{$suit}{ascii} : $SUIT{$suit}{wide};
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 90 4 94
223 94 ▶ return $self->_colour ? "\e[$SUIT{$suit}{colour}m$face\e[0m" : $face;
Branch: 1 branch
BranchTrueFalseTotal
$self->_colour ? : 0 94 94
224 }
225  
226 89 ▶sub playable {
Subroutine playable: called CC=3 SCAR=11.0
Pod documented
227 89 my ($self, $view) = @_;
228 250 my %card = map { $_->{card} => 1 }
229 89 grep { defined $_->{card} } @{ $view->{legal} };
230 89 my @at;
231 89 my $n = 0;
232 89 for my $held (@{ $view->{hand} }) {
233 920 $n++;
234 920 ▶ push @at, $n if $card{$held};
Branch: 1 branch
BranchTrueFalseTotal
if $card{$held} 250 670 920
235 }
236 89 return \@at;
237 }
238  
239 82 ▶sub prompt_for {
Subroutine prompt_for: called CC=5 SCAR=16.1
Pod documented
240 82 my ($self, $view) = @_;
241 82 my %kinds = map { $_->{kind} => 1 } @{ $view->{legal} };
242 82 my @what;
243  
244 82 my $playable = $self->playable($view);
245 82 ▶ push @what, '[' . join(' ', @$playable) . '] a card' if @$playable;
Branch: 1 branch
BranchTrueFalseTotal
if @$playable 82 0 82
246  
247 82 ▶ push @what, 't take' if $kinds{take};
Branch: 1 branch
BranchTrueFalseTotal
if $kinds{"take"} 62 20 82
248 82 ▶ push @what, 'd done' if $kinds{done};
Branch: 1 branch
BranchTrueFalseTotal
if $kinds{"done"} 13 69 82
249 82 ▶ push @what, 'x exchange' if $kinds{swap};
Branch: 1 branch
BranchTrueFalseTotal
if $kinds{"swap"} 1 81 82
250 82 push @what, 'r resign';
251 82 push @what, 'q quit';
252 82 return join ' ', @what;
253 }
254  
255 5 ▶sub keys_available {
Subroutine keys_available: called CC=4 SCAR=18.7
Pod documented
256 5 my ($self) = @_;
257 5 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 0 5 5
258 5 ▶ return 0 unless eval { -t $self->_in };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { -t $self->_in } } 5 0 5
259 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
260 }
261  
262 159 ▶sub picking {
Subroutine picking: called CC=5 SCAR=16.1
Pod documented
263 159 my ($self, @set) = @_;
264 159 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 79 80 159
$set[0] ? : 0 79 79
265 159 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 156 3 159
$self->_picking ? : 1 155 156
266 3 return $self->keys_available;
267 }
268  
269 5 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=20.4
Pod documented
270 5 my ($self) = @_;
271 5 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 5 5
272 5 ▶ if ($self->keysource) { $self->raw(1); return $self }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 4 1 5
273 1 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 1 0 1
274 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
275 0 $self->raw(1);
276 0 return $self;
277 }
278  
279 6 ▶sub leave_raw {
Subroutine leave_raw: called CC=3 SCAR=18.1
Pod documented
280 6 my ($self) = @_;
281 6 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 6 0 6
282 0 ▶ eval { Term::ReadKey::ReadMode(0, $self->_in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 0 0
283 0 $self->raw(0);
284 0 return $self;
285 }
286  
287 51 ▶sub read_char {
Subroutine read_char: called CC=6 SCAR=27.1
Pod documented
288 51 my ($self, $wait) = @_;
289 51 my $pending = $self->pending;
290 51 ▶ return shift @$pending if @$pending;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending 2 49 51
291 49 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 49 0 49
292 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->_in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
293 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
294 0 select undef, undef, undef, 0.05;
295 0 return Term::ReadKey::ReadKey(-1, $self->_in);
296 }
297  
298 27 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
299 27 my ($self) = @_;
300 27 my $char = $self->read_char(1);
301 27 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 1 26 27
302 26 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Durak::Terminal::CONTROL{$char} 5 21 26
303 21 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 12 9 21
304 9 return $char;
305 }
306  
307 12 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=21.0
Pod documented
308 12 my ($self) = @_;
309 12 my $opener = $self->read_char(0);
310 12 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 12 12
311 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
312 2 unshift @{ $self->pending }, $opener;
313 2 return 'escape';
314 }
315 10 my $tail = '';
316 10 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 12 0 12
317 12 my $char = $self->read_char(0);
318 12 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 12 12
319 12 $tail .= $char;
320 12 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 10 2 12
321 }
322 10 ▶ return $SEQUENCE{$tail} || 'escape';
Condition50%1/2
$Game::Durak::Terminal::SEQUENCE{$tail} || 'escape'
l!l
100
MC/DC0%0/1
$Game::Durak::Terminal::SEQUENCE{$tail} || 'escape'
$Game::Durak::Terminal::SEQUENCE{$tail}
Truth table50%1/2
A
A= $Game::Durak::Terminal::SEQUENCE{$tail}
Aresult
11
00
323 }
324  
325 11 ▶sub card_line {
Subroutine card_line: called CC=10 SCAR=27.8
Pod documented
326 11 my ($self, $view, $card) = @_;
327  
328 44 ▶ my ($move) = grep { defined $_->{card} && $_->{card} == $card }
Condition100%3/3
defined $_->{'card'} && $_->{'card'} == $card
!ll&&!rl&&r
82511
MC/DC100%2/2
defined $_->{'card'} && $_->{'card'} == $card
defined $_->{'card'} $_->{'card'} == $card
Truth table100%3/3
A && B
A= defined $_->{'card'}B= $_->{'card'} == $card
ABresult
0X0
100
111
329 11 @{ $view->{legal} };
330 11 ▶ return $self->card($card) . ': not one you can play' unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 0 11 11
331  
332 11 ▶ my $trump = suit_of($card) eq $view->{trump} ? ', a trump' : '';
Branch: 1 branch
BranchTrueFalseTotal
suit_of($card) eq $view->{'trump'} ? : 1 10 11
333 11 my $bout = $view->{bout};
334  
335 11 ▶ if ($move->{kind} eq 'defend') {
Branch: 1 branch
BranchTrueFalseTotal
if ($move->{"kind"} eq "defend") 0 11 11
336 my $beats = defined $bout && defined $bout->{unbeaten}
337 0 ▶ ? ' the ' . $self->card($bout->{unbeaten}) : ' the attack';
Branch: 1 branch
BranchTrueFalseTotal
defined $bout && defined $bout->{'unbeaten'} ? : 0 0 0
Condition0%0/3
defined $bout and defined $bout->{"unbeaten"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bout and defined $bout->{"unbeaten"}
defined $bout defined $bout->{"unbeaten"}
Truth table0%0/3
A and B
A= defined $boutB= defined $bout->{"unbeaten"}
ABresult
0X0
100
111
338 0 return $self->card($card) . ": beats$beats$trump";
339 }
340  
341 11 ▶ my $room = defined $bout ? ($bout->{cap} - $bout->{size}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $bout ? : 11 0 11
342 11 ▶ my $with = $room > 1 ? sprintf ', %d more could follow', $room - 1
Branch: 2 branches
BranchTrueFalseTotal
$room > 1 ? : 11 0 11
$room == 1 ? : 0 0 0
343 : $room == 1 ? ', the last this bout can hold' : '';
344  
345 11 return $self->card($card) . ": they must beat it$trump$with";
346 }
347  
348 8 ▶sub pick_keys {
Subroutine pick_keys: called CC=1 SCAR=0.0
Pod documented
349 8 return 'arrows to choose, enter to play, ? for the keys';
350 }
351  
352 0 ▶sub pick_help {
Subroutine pick_help: not called CC=1 SCAR=6.9
Pod documented
353 return (
354 0 'arrows walk the cards you may play, enter plays the one under the cursor',
355 'the line above says what it would do',
356 't take, d done, x exchange, r resign, q quit',
357 );
358 }
359  
360 4 ▶sub pick_move {
Subroutine pick_move: called CC=36 SCAR=52.2
Pod documented
361 4 my ($self, $view) = @_;
362  
363 4 my $at_list = $self->playable($view);
364 4 my @slot = @$at_list;
365  
366 4 my %by;
367 4 push @{ $by{ $_->{kind} } }, $_ for @{ $view->{legal} };
368  
369 4 my $at = 0;
370 4 my @notice;
371  
372 4 while (1) {
373 8 ▶ my $card = @slot ? $view->{hand}[ $slot[$at] - 1 ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@slot ? : 8 0 8
374  
375 8 ▶ $self->told(@notice) if @notice;
Branch: 1 branch
BranchTrueFalseTotal
if @notice 1 7 8
376 8 @notice = ();
377 8 ▶ $self->told($self->card_line($view, $card)) if defined $card;
Branch: 1 branch
BranchTrueFalseTotal
if defined $card 8 0 8
378  
379 8 ▶ $self->position($view,
Branch: 1 branch
BranchTrueFalseTotal
defined $card ? : 8 0 8
380 cursor => (defined $card ? $slot[$at] : undef),
381 footer => $self->pick_keys);
382  
383 8 my $key = $self->read_key;
384 8 ▶ return { kind => 'quit' }
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q" 3 5 8
Condition44%4/9
not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q"
l!l&&r!l&&!r
035
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
008
not defined $key or $key eq "eof"
l!l&&r!l&&!r
008
MC/DC25%1/4
not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q"
not defined $key $key eq "eof" $key eq "interrupt" $key eq "q"
Truth table40%2/5
not(A) or B or C or D
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"D= $key eq "q"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
385 if !defined $key || $key eq 'eof' || $key eq 'interrupt'
386 || $key eq 'q';
387  
388 5 ▶ if (@slot && ($key eq 'left' || $key eq 'up')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@slot and $key eq "left" || $key eq "up") 0 5 5
Condition33%2/6
@slot and $key eq "left" || $key eq "up"
!ll&&!rl&&r
050
$key eq "left" or $key eq "up"
l!l&&r!l&&!r
005
MC/DC0%0/3
@slot and $key eq "left" || $key eq "up"
@slot $key eq "left" $key eq "up"
Truth table25%1/4
A and B or C
A= @slotB= $key eq "left"C= $key eq "up"
ABCresult
0XX0
1000
11X1
1011
389 0 $at = ($at - 1) % @slot;
390 0 next;
391 }
392 5 ▶ if (@slot && ($key eq 'right' || $key eq 'down' || $key eq 'tab')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@slot and $key eq "right" || $key eq "down" || $key eq "tab") 3 2 5
Condition66%6/9
@slot and $key eq "right" || $key eq "down" || $key eq "tab"
!ll&&!rl&&r
023
$key eq "right" or $key eq "down" or $key eq "tab"
l!l&&r!l&&!r
302
$key eq "right" or $key eq "down"
l!l&&r!l&&!r
302
MC/DC25%1/4
@slot and $key eq "right" || $key eq "down" || $key eq "tab"
@slot $key eq "right" $key eq "down" $key eq "tab"
Truth table40%2/5
A and B or C or D
A= @slotB= $key eq "right"C= $key eq "down"D= $key eq "tab"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
393 3 $at = ($at + 1) % @slot;
394 3 next;
395 }
396 2 ▶ if (@slot && ($key eq 'home')) { $at = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if (@slot and $key eq "home") 0 2 2
Condition33%1/3
@slot and $key eq "home"
!ll&&!rl&&r
020
MC/DC0%0/2
@slot and $key eq "home"
@slot $key eq "home"
Truth table33%1/3
A and B
A= @slotB= $key eq "home"
ABresult
0X0
100
111
397 2 ▶ if (@slot && ($key eq 'end')) { $at = $#slot; next }
Branch: 1 branch
BranchTrueFalseTotal
if (@slot and $key eq "end") 0 2 2
Condition33%1/3
@slot and $key eq "end"
!ll&&!rl&&r
020
MC/DC0%0/2
@slot and $key eq "end"
@slot $key eq "end"
Truth table33%1/3
A and B
A= @slotB= $key eq "end"
ABresult
0X0
100
111
398  
399 2 ▶ if (@slot && ($key eq 'enter' || $key eq ' ')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@slot and $key eq "enter" || $key eq " ") 0 2 2
Condition33%2/6
@slot and $key eq "enter" || $key eq " "
!ll&&!rl&&r
020
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
002
MC/DC0%0/3
@slot and $key eq "enter" || $key eq " "
@slot $key eq "enter" $key eq " "
Truth table25%1/4
A and B or C
A= @slotB= $key eq "enter"C= $key eq " "
ABCresult
0XX0
1000
11X1
1011
400 0 ▶ my ($move) = grep { defined $_->{card} && $_->{card} == $card }
Condition0%0/3
defined $_->{'card'} && $_->{'card'} == $card
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_->{'card'} && $_->{'card'} == $card
defined $_->{'card'} $_->{'card'} == $card
Truth table0%0/3
A && B
A= defined $_->{'card'}B= $_->{'card'} == $card
ABresult
0X0
100
111
401 0 @{ $view->{legal} };
402 0 ▶ return $move if $move;
Branch: 1 branch
BranchTrueFalseTotal
if $move 0 0 0
403 0 @notice = ('you cannot play that one');
404 0 next;
405 }
406  
407 2 ▶ if ($key eq 't' && $by{take}) { return $by{take}[0] }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "t" and $by{"take"}) 1 1 2
Condition66%2/3
$key eq "t" and $by{"take"}
!ll&&!rl&&r
101
MC/DC50%1/2
$key eq "t" and $by{"take"}
$key eq "t" $by{"take"}
Truth table66%2/3
A and B
A= $key eq "t"B= $by{"take"}
ABresult
0X0
100
111
408 1 ▶ if ($key eq 'd' && $by{done}) { return $by{done}[0] }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "d" and $by{"done"}) 0 1 1
Condition33%1/3
$key eq "d" and $by{"done"}
!ll&&!rl&&r
100
MC/DC0%0/2
$key eq "d" and $by{"done"}
$key eq "d" $by{"done"}
Truth table33%1/3
A and B
A= $key eq "d"B= $by{"done"}
ABresult
0X0
100
111
409 1 ▶ if ($key eq 'x' && $by{swap}) { return $by{swap}[0] }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "x" and $by{"swap"}) 0 1 1
Condition33%1/3
$key eq "x" and $by{"swap"}
!ll&&!rl&&r
100
MC/DC0%0/2
$key eq "x" and $by{"swap"}
$key eq "x" $by{"swap"}
Truth table33%1/3
A and B
A= $key eq "x"B= $by{"swap"}
ABresult
0X0
100
111
410 1 ▶ if ($key eq 'r') { return { kind => 'resign' } }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "r") 0 1 1
411  
412 1 ▶ if ($key =~ /\A[1-9]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A[1-9]\z/) 0 1 1
413 0 my ($found) = grep { $slot[$_] == $key } 0 .. $#slot;
414 0 ▶ if (defined $found) { $at = $found; next }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found) 0 0 0
415 0 @notice = ("you cannot play card $key");
416 0 next;
417 }
418  
419 1 ▶ if ($key eq '?') { @notice = $self->pick_help; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 1 1
420  
421 1 @notice = ('that key does nothing here');
422 }
423 }
424  
425 95 ▶sub move_for {
Subroutine move_for: called CC=16 SCAR=27.7
Pod documented
426 95 my ($self, $view, $said) = @_;
427  
428 95 ▶ return undef unless defined $said;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $said 1 94 95
429 94 $said =~ s/\A\s+|\s+\z//g;
430 94 ▶ return { kind => 'quit' } if lc $said eq 'q' || lc $said eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if lc $said eq "q" or lc $said eq "quit" 6 88 94
Condition100%3/3
lc $said eq "q" or lc $said eq "quit"
l!l&&r!l&&!r
4288
MC/DC100%2/2
lc $said eq "q" or lc $said eq "quit"
lc $said eq "q" lc $said eq "quit"
Truth table100%3/3
A or B
A= lc $said eq "q"B= lc $said eq "quit"
ABresult
1X1
011
000
431  
432 88 my %by;
433 88 push @{ $by{ $_->{kind} } }, $_ for @{ $view->{legal} };
434  
435 88 ▶ return $by{take}[0] if lc $said eq 't' && $by{take};
Branch: 1 branch
BranchTrueFalseTotal
if lc $said eq "t" and $by{"take"} 5 83 88
Condition100%3/3
lc $said eq "t" and $by{"take"}
!ll&&!rl&&r
7855
MC/DC100%2/2
lc $said eq "t" and $by{"take"}
lc $said eq "t" $by{"take"}
Truth table100%3/3
A and B
A= lc $said eq "t"B= $by{"take"}
ABresult
0X0
100
111
436 83 ▶ return $by{done}[0] if lc $said eq 'd' && $by{done};
Branch: 1 branch
BranchTrueFalseTotal
if lc $said eq "d" and $by{"done"} 4 79 83
Condition100%3/3
lc $said eq "d" and $by{"done"}
!ll&&!rl&&r
7544
MC/DC100%2/2
lc $said eq "d" and $by{"done"}
lc $said eq "d" $by{"done"}
Truth table100%3/3
A and B
A= lc $said eq "d"B= $by{"done"}
ABresult
0X0
100
111
437 79 ▶ return $by{swap}[0] if lc $said eq 'x' && $by{swap};
Branch: 1 branch
BranchTrueFalseTotal
if lc $said eq "x" and $by{"swap"} 1 78 79
Condition100%3/3
lc $said eq "x" and $by{"swap"}
!ll&&!rl&&r
7081
MC/DC100%2/2
lc $said eq "x" and $by{"swap"}
lc $said eq "x" $by{"swap"}
Truth table100%3/3
A and B
A= lc $said eq "x"B= $by{"swap"}
ABresult
0X0
100
111
438 78 ▶ return { kind => 'resign' } if lc $said eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if lc $said eq "r" 1 77 78
439  
440 77 ▶ return undef unless $said =~ /\A[0-9]+\z/ && $said >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $said =~ /\A[0-9]+\z/ and $said >= 1 20 57 77
Condition100%3/3
$said =~ /\A[0-9]+\z/ and $said >= 1
!ll&&!rl&&r
19157
MC/DC100%2/2
$said =~ /\A[0-9]+\z/ and $said >= 1
$said =~ /\A[0-9]+\z/ $said >= 1
Truth table100%3/3
A and B
A= $said =~ /\A[0-9]+\z/B= $said >= 1
ABresult
0X0
100
111
441 57 my $card = $view->{hand}[ $said - 1 ];
442 57 ▶ return undef unless defined $card;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $card 2 55 57
443  
444 my ($move) = grep {
445 209 ▶ defined $_->{card} && $_->{card} == $card
Condition100%3/3
defined $_->{'card'} && $_->{'card'} == $card
!ll&&!rl&&r
4814120
MC/DC100%2/2
defined $_->{'card'} && $_->{'card'} == $card
defined $_->{'card'} $_->{'card'} == $card
Truth table100%3/3
A && B
A= defined $_->{'card'}B= $_->{'card'} == $card
ABresult
0X0
100
111
446 55 } @{ $view->{legal} };
447  
448 55 return $move;
449 }
450  
451 25 ▶sub cards {
Subroutine cards: called CC=2 SCAR=6.9
Pod documented
452 25 my ($self, $n) = @_;
453 25 ▶ return $n == 1 ? '1 card' : "$n cards";
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 5 20 25
454 }
455  
456 61 ▶sub narration {
Subroutine narration: called CC=17 SCAR=28.4
Pod documented
457 61 my ($self, $view, @events) = @_;
458 61 my $me = $view->{seat};
459 61 my @said;
460  
461 61 for my $event (@events) {
462 114 my $kind = $event->{kind};
463  
464 114 ▶ if ($SAID{$kind}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SAID{$kind}) 64 50 114
465 my $who = !defined $event->{seat} ? 'somebody'
466 64 ▶ : $event->{seat} == $me ? 'you' : 'they';
Branch: 2 branches
BranchTrueFalseTotal
!defined($event->{'seat'}) ? : 0 64 64
$event->{'seat'} == $me ? : 27 37 64
467 my $what = defined $event->{card}
468 64 ▶ ? ' ' . long_name_of($event->{card}) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $event->{'card'} ? : 53 11 64
469 64 push @said, "$who $SAID{$kind}$what";
470 64 next;
471 }
472  
473 50 ▶ if ($kind eq 'bout_end') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "bout_end") 23 27 50
474 23 ▶ if ($event->{taken}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{'taken'}) { } 11 12 23
475 11 my $took = 3 - $event->{next_attacker};
476 push @said, sprintf '%s pick up %s',
477 11 ▶ $took == $me ? 'you' : 'they', $self->cards($event->{cards});
Branch: 1 branch
BranchTrueFalseTotal
$took == $me ? : 10 1 11
478 }
479 else {
480 push @said, sprintf 'the bout is beaten off, %s',
481 12 $self->cards($event->{cards});
482 }
483 23 next;
484 }
485  
486 27 ▶ if ($kind eq 'refill') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "refill") 22 5 27
487 22 ▶ next unless $event->{drawn}{1} || $event->{drawn}{2};
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"drawn"}{1} or $event->{"drawn"}{2} 5 17 22
Condition100%3/3
$event->{"drawn"}{1} or $event->{"drawn"}{2}
l!l&&r!l&&!r
1165
MC/DC100%2/2
$event->{"drawn"}{1} or $event->{"drawn"}{2}
$event->{"drawn"}{1} $event->{"drawn"}{2}
Truth table100%3/3
A or B
A= $event->{"drawn"}{1}B= $event->{"drawn"}{2}
ABresult
1X1
011
000
488 push @said, sprintf 'you draw %d, they draw %d, %d left',
489 $event->{drawn}{$me}, $event->{drawn}{ 3 - $me },
490 17 $event->{talon};
491 17 next;
492 }
493  
494 5 ▶ if ($kind eq 'out') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "out") 2 3 5
495 2 ▶ push @said, $event->{seat} == $me ? 'you are out' : 'they are out';
Branch: 1 branch
BranchTrueFalseTotal
$event->{'seat'} == $me ? : 0 2 2
496 2 next;
497 }
498  
499 3 ▶ if ($kind eq 'game_end') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "game_end") 3 0 3
500 push @said, !defined $event->{fool} ? 'a draw, and no fool'
501 3 ▶ : $event->{fool} == $me ? 'you are the durak'
Branch: 2 branches
BranchTrueFalseTotal
!defined($event->{'fool'}) ? : 1 2 3
$event->{'fool'} == $me ? : 2 0 2
502 : 'they are the durak';
503 }
504 }
505  
506 61 return @said;
507 }
508  
509 2 ▶sub narrate {
Subroutine narrate: called CC=4 SCAR=14.1
Pod documented
510 2 my ($self, $view, @events) = @_;
511 2 my @said = $self->narration($view, @events);
512 2 ▶ return unless @said;
Branch: 1 branch
BranchTrueFalseTotal
unless @said 0 2 2
513 2 ▶ $self->say('') if grep { $_ =~ /durak|a draw/ } @said;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ =~ /durak|a draw/;} @said 2 0 2
514 2 $self->say(" $_") for @said;
515 2 return;
516 }
517  
518 4 ▶sub start {
Subroutine start: called CC=2 SCAR=7.0
Pod documented
519 4 my ($self) = @_;
520 4 my $game = Game::Durak->build(seed => $self->seed);
521 4 ▶ die $game->message . "\n" if ref $game eq 'Game::Durak::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $game eq "Game::Durak::Error" 0 4 4
522 4 $self->game($game);
523 4 return $game;
524 }
525  
526 4 ▶sub play {
Subroutine play: called CC=14 SCAR=27.7
Pod documented
527 4 my ($self) = @_;
528  
529 4 ▶ my $game = $self->game || $self->start;
Condition33%1/3
$self->game || $self->start
l!l&&r!l&&!r
040
MC/DC0%0/2
$self->game || $self->start
$self->game $self->start
Truth table33%1/3
A || B
A= $self->gameB= $self->start
ABresult
1X1
011
000
530 4 my $me = $self->_seat;
531 4 ▶ my $bot = Game::Durak::Bot->new(
Condition50%1/2
$self->seed || ''
l!l
40
MC/DC0%0/1
$self->seed || ''
$self->seed
Truth table50%1/2
A
A= $self->seed
Aresult
11
00
532 level => Game::Durak::Bot->level_for($self->_level),
533 seed => ($self->seed || '') . ':bot',
534 );
535  
536 4 $self->open_screen($game);
537  
538 4 ▶ while (!$game->over) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game->over) 113 1 114
539 113 my $seat = $game->turn;
540 113 my $view = $game->view($seat);
541  
542 113 ▶ if ($seat != $me) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seat != $me) 34 79 113
543 34 my $move = $bot->choose($view);
544 34 ▶ last unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 0 34 34
545 34 my @events = $game->apply($seat, $move);
546 34 ▶ last if ref $events[0] eq 'Game::Durak::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $events[0] eq "Game::Durak::Error" 0 34 34
547 34 $self->told($self->narration($game->view($me), @events));
548 34 next;
549 }
550  
551 79 my $move;
552 79 ▶ if ($self->picking && $self->enter_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->picking and $self->enter_raw) { } 0 79 79
Condition66%2/3
$self->picking and $self->enter_raw
!ll&&!rl&&r
7810
MC/DC0%0/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
553 0 $move = $self->pick_move($view);
554 0 ▶ last unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 0 0 0
555 }
556 else {
557 79 $self->picking(0);
558 79 $self->position($view);
559  
560 79 my $handle = $self->_in;
561 79 my $said = <$handle>;
562 79 ▶ last unless defined $said;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $said 2 77 79
563  
564 77 $move = $self->move_for($view, $said);
565 77 ▶ unless ($move) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($move) 51 26 77
566 51 $self->told('that is not one of the answers');
567 51 next;
568 }
569 }
570  
571 26 ▶ last if $move->{kind} eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $move->{"kind"} eq "quit" 1 25 26
572  
573 25 my @events = $game->apply($seat, $move);
574 25 ▶ if (ref $events[0] eq 'Game::Durak::Error') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $events[0] eq "Game::Durak::Error") 0 25 25
575 0 $self->told($events[0]->message);
576 0 next;
577 }
578  
579 25 $self->told($self->narration($game->view($me), @events));
580 }
581  
582 4 $self->leave_raw;
583 4 $self->close_screen($game);
584  
585 4 return $game->result;
586 }
587  
588 4 ▶sub open_screen {
Subroutine open_screen: called CC=2 SCAR=9.2
Pod documented
589 4 my ($self, $game) = @_;
590  
591 4 ▶ if ($self->_paint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_paint) 0 4 4
592 0 my $handle = $self->_out;
593 0 print {$handle} "\e[2J\e[H";
594 0 $self->told(sprintf 'durak: the loser is the fool, and seat %d opens',
595 $game->turn);
596 0 return;
597 }
598  
599 4 $self->say('durak: the loser is the fool');
600 4 $self->say('trump is ' . $self->card_suit($game->trump)
601 . ', and seat ' . $game->turn . ' opens');
602 4 return;
603 }
604  
605 87 ▶sub position {
Subroutine position: called CC=4 SCAR=14.5
Pod documented
606 87 my ($self, $view, %o) = @_;
607  
608 87 ▶ my $footer = defined $o{footer} ? $o{footer} : $self->prompt_for($view);
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'footer'} ? : 8 79 87
609  
610 return $self->show($view, footer => $footer,
611 87 ▶ (defined $o{cursor} ? (cursor => $o{cursor}) : ()))
Branch: 2 branches
BranchTrueFalseTotal
if $self->_paint 0 87 87
defined $o{'cursor'} ? : 0 0 0
612 if $self->_paint;
613  
614 87 $self->say($self->table($view, %o));
615 87 $self->say(' ' . $footer);
616 87 return;
617 }
618  
619 4 ▶sub close_screen {
Subroutine close_screen: called CC=6 SCAR=33.6
Pod documented
620 4 my ($self, $game) = @_;
621 4 ▶ return unless $self->_paint;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_paint 4 0 4
622  
623 0 my $view = $game->view($self->_seat);
624 0 my $result = $game->result;
625 0 ▶ my $fool = $result ? $result->{fool} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 0 0 0
626 0 ▶ my $over = !$result ? 'you left the table'
Branch: 3 branches
BranchTrueFalseTotal
!$result ? : 0 0 0
!defined($fool) ? : 0 0 0
$fool == $self->_seat ? : 0 0 0
627 : !defined $fool ? 'a draw, and no fool'
628 : $fool == $self->_seat ? 'you are the durak'
629 : 'they are the durak';
630  
631 0 $self->show($view, header => $over, footer => 'the deal is over');
632 0 my $handle = $self->_out;
633 0 print {$handle} "\e[0m\n";
634 0 return;
635 }
636  
637  
638 0 ▶sub _usage {
Subroutine _usage: not called CC=1 SCAR=6.9
639 0 return <<'USAGE';
640 usage: durak [--level 1-5] [--seat 1|2] [--seed STRING] [--ascii] [--no-colour]
641 [--no-paint] [--width N] [--height N]
642  
643 A deal of Podkidnoy Durak against the bot. You are the seat given by --seat
644 and the bot plays the other one. The seed decides the deal, and the same seed
645 deals the same cards every time.
646  
647 The table is drawn on one painted screen at a terminal and printed as plain
648 lines when the output is a pipe or a file. --paint and --no-paint say so
649 either way, and --width and --height fit the screen to a window that is not
650 eighty by twenty-four.
651  
652 At a terminal the arrow keys walk the cards you may play and the line above
653 the table says what the one under the cursor would do: whether they must beat
654 it, which attack it beats, whether it is a trump, and how much room the bout
655 has left. Enter plays it. The moves that are not cards keep their own keys.
656 Needs Term::ReadKey; --no-pick, or no terminal, numbers them at a prompt
657 instead.
658  
659 At the prompt, a number plays that card. t takes, d is done, x exchanges the
660 trump, r resigns and q (or quit) stops.
661  
662 Exit status is 0 if you won or the deal was stopped, 1 if you were the fool,
663 2 for an option the game will not take.
664 USAGE
665 }
666  
667 4 ▶sub run {
Subroutine run: called CC=17 SCAR=38.7
Pod documented
668 4 my ($class, @argv) = @_;
669 4 my %opt = (level => 3, seat => 1);
670  
671 4 my $ok = eval {
672 4 ▶ local $SIG{__WARN__} = sub { print STDERR "durak: $_[0]" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
673 4 Getopt::Long::GetOptionsFromArray(\@argv, \%opt,
674 'level=i', 'seat=i', 'seed=s', 'ascii', 'colour!',
675 'paint!', 'pick!', 'width=i', 'height=i', 'help');
676 };
677 4 ▶ if (!$ok) { print STDERR _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 4 4
678 4 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 0 4 4
679 4 ▶ if (@argv) { print STDERR "durak: not an option: $argv[0]\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 0 4 4
680  
681 4 my $levels = Game::Durak::Bot->ladder;
682 4 ▶ unless ($opt{seat} =~ /\A[12]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"seat"} =~ /\A[12]\z/) 0 4 4
683 0 print STDERR "durak: --seat must be 1 or 2, not '$opt{seat}'\n";
684 0 return 2;
685 }
686 4 ▶ unless ($opt{level} =~ /\A[0-9]+\z/ && $opt{level} >= 1 && $opt{level} <= $levels) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"level"} =~ /\A[0-9]+\z/ and $opt{"level"} >= 1 and $opt{"level"} <= $levels) 2 2 4
Condition83%5/6
$opt{"level"} =~ /\A[0-9]+\z/ and $opt{"level"} >= 1 and $opt{"level"} <= $levels
!ll&&!rl&&r
112
$opt{"level"} =~ /\A[0-9]+\z/ and $opt{"level"} >= 1
!ll&&!rl&&r
013
MC/DC66%2/3
$opt{"level"} =~ /\A[0-9]+\z/ and $opt{"level"} >= 1 and $opt{"level"} <= $levels
$opt{"level"} =~ /\A[0-9]+\z/ $opt{"level"} >= 1 $opt{"level"} <= $levels
Truth table75%3/4
A and B and C
A= $opt{"level"} =~ /\A[0-9]+\z/B= $opt{"level"} >= 1C= $opt{"level"} <= $levels
ABCresult
0XX0
10X0
1100
1111
687 2 print STDERR "durak: --level must be 1 to $levels, not '$opt{level}'\n";
688 2 return 2;
689 }
690  
691 my $said = defined $opt{seed} ? $opt{seed}
692 2 ▶ : join q{}, map { chr 97 + int rand 26 } 1 .. 32;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'seed'} ? : 2 0 2
693 2 my $seed = sprintf q{%-32.32s}, $said;
694  
695 my $terminal = $class->new(
696 seed => $seed,
697 level => $opt{level},
698 seat => $opt{seat},
699 ascii => $opt{ascii},
700 colour => $opt{colour},
701 paint => $opt{paint},
702 width => $opt{width},
703 height => $opt{height},
704 2 ▶ (defined $opt{pick} ? (picking => $opt{pick} ? 1 : 0) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $opt{'pick'} ? : 0 2 2
$opt{'pick'} ? : 0 0 0
705 );
706  
707 2 local @SIG{qw(INT TERM HUP)};
708 2 for my $sig (qw(INT TERM HUP)) {
709 6 ▶ $SIG{$sig} = sub { $terminal->leave_raw; $SIG{$sig} = 'DEFAULT'; kill $sig, $$ };
Subroutine __ANON__: not called CC=1 SCAR=3.5
710 }
711  
712 2 my $result = eval { $terminal->play };
713 2 my $died = $@;
714 2 $terminal->leave_raw;
715 2 ▶ die $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 0 2 2
716 2 ▶ return 0 unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 2 0 2
717  
718 0 $terminal->say('');
719 0 $terminal->say("the seed was $said");
720  
721 return 1 if ref $result eq 'HASH'
722 0 ▶ && defined $result->{fool} && $result->{fool} == $opt{seat};
Branch: 1 branch
BranchTrueFalseTotal
if ref $result eq "HASH" and defined $result->{"fool"} and $result->{"fool"} == $opt{"seat"} 0 0 0
Condition0%0/6
ref $result eq "HASH" and defined $result->{"fool"} and $result->{"fool"} == $opt{"seat"}
!ll&&!rl&&r
000
ref $result eq "HASH" and defined $result->{"fool"}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $result eq "HASH" and defined $result->{"fool"} and $result->{"fool"} == $opt{"seat"}
ref $result eq "HASH" defined $result->{"fool"} $result->{"fool"} == $opt{"seat"}
Truth table0%0/4
A and B and C
A= ref $result eq "HASH"B= defined $result->{"fool"}C= $result->{"fool"} == $opt{"seat"}
ABCresult
0XX0
10X0
1100
1111
723 0 return 0;
724 }
725  
726 5 1;
727  
728 __END__
↑ Summary blib/lib/Game/Durak/Table.pm »