blib/lib/Game/Durak/Table.pm

filter: Statementstmt 100% 172 / 172 Branchbran 85.7% 72 / 84 Conditioncond 72.7% 24 / 33 MC/DCmcdc 55.0% 11 / 20 Subroutinesub 100% 24 / 24 Podpod 100% 17 / 17 totaltotal 91.4% 320 / 350 CC 65 SCAR 22.2
CC
65
Cov
93%
screen
21.0
fan
12.0
put
8.3
CRAP 9.2
« blib/lib/Game/Durak/Terminal.pm ↑ Summary blib/lib/Game/Durak/Search.pm »
1 package Game::Durak::Table;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use Game::Durak::Card qw(face_of suit_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 our $VERSION = '0.02';
11  
12 6 my %SUIT = (
13 S => { wide => "\x{2660}", ascii => 'S', ink => 'black' },
14 H => { wide => "\x{2665}", ascii => 'H', ink => 'red' },
15 D => { wide => "\x{2666}", ascii => 'D', ink => 'red' },
16 C => { wide => "\x{2663}", ascii => 'C', ink => 'black' },
17 );
18  
19 6 my %PAINT = (
20 baize => "\e[0m\e[42m",
21 label => "\e[0m\e[42;97m",
22 quiet => "\e[0m\e[42;37m",
23 bar => "\e[0m\e[40;97m",
24 black => "\e[0m\e[47;30m",
25 red => "\e[0m\e[47;31m",
26 grey => "\e[0m\e[47;90m",
27 back => "\e[0m\e[41;97m",
28 pick => "\e[0m\e[43;30m",
29 );
30  
31 6 my %ROLE = (
32 attack => 'you attack',
33 defend => 'you defend',
34 pile_on => 'they took it, throw in what you can',
35 );
36  
37 6 my %WATCH = (
38 attack => 'they attack',
39 defend => 'they defend',
40 pile_on => 'you took it, they are throwing in',
41 );
42  
43 6 my %LINE = (
44 wide => { tl => "\x{250c}", tr => "\x{2510}", bl => "\x{2514}",
45 br => "\x{2518}", h => "\x{2500}", v => "\x{2502}",
46 weave => "\x{259a}",
47 ring_tl => "\x{2554}", ring_tr => "\x{2557}",
48 ring_bl => "\x{255a}", ring_br => "\x{255d}",
49 ring_h => "\x{2550}", ring_v => "\x{2551}" },
50 ascii => { tl => '+', tr => '+', bl => '+', br => '+',
51 h => '-', v => '|', weave => '#',
52 ring_tl => '+', ring_tr => '+', ring_bl => '+', ring_br => '+',
53 ring_h => '=', ring_v => '|' },
54 );
55  
56 6 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
57 6 CARD_W => 7,
58 CARD_H => 5,
59 FAN => 4,
60 6 };
61  
62 6 has width => (is => 'ro', isa => Int, default => 80);
63 6 has height => (is => 'ro', isa => Int, default => 24);
64 6 has ascii => (is => 'ro', isa => Any);
65  
66 8203 ▶sub _ascii { return $_[0]->ascii ? 1 : 0 }
Subroutine _ascii: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->ascii ? : 65 8138 8203
67 4408 ▶sub _art { return $_[0]->_ascii ? $LINE{ascii} : $LINE{wide} }
Subroutine _art: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_ascii ? : 36 4372 4408
68  
69 3795 ▶sub suit_glyph {
Subroutine suit_glyph: called CC=4 SCAR=14.9
Pod documented
70 3795 my ($self, $suit) = @_;
71 3795 ▶ return '?' unless $suit && $SUIT{$suit};
Branch: 1 branch
BranchTrueFalseTotal
unless $suit and $SUIT{$suit} 0 3795 3795
Condition33%1/3
$suit and $SUIT{$suit}
!ll&&!rl&&r
003795
MC/DC0%0/2
$suit and $SUIT{$suit}
$suit $SUIT{$suit}
Truth table33%1/3
A and B
A= $suitB= $SUIT{$suit}
ABresult
0X0
100
111
72 3795 ▶ return $self->_ascii ? $SUIT{$suit}{ascii} : $SUIT{$suit}{wide};
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 29 3766 3795
73 }
74  
75 1737 ▶sub ink_of {
Subroutine ink_of: called CC=3 SCAR=11.4
Pod documented
76 1737 my ($self, $id) = @_;
77 1737 ▶ return 'black' unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 1737 1737
78 1737 my $suit = suit_of($id);
79 1737 ▶ return $SUIT{$suit} ? $SUIT{$suit}{ink} : 'black';
Branch: 1 branch
BranchTrueFalseTotal
$SUIT{$suit} ? : 1737 0 1737
80 }
81  
82 1099 ▶sub index_of {
Subroutine index_of: called CC=2 SCAR=7.1
Pod documented
83 1099 my ($self, $id) = @_;
84 1099 ▶ return '' unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 1099 1099
85 1099 return face_of($id) . $self->suit_glyph(suit_of($id));
86 }
87  
88 297 ▶sub blank {
Subroutine blank: called CC=2 SCAR=6.9
Pod documented
89 297 my ($self) = @_;
90 297 my @cells;
91 my @paint;
92 297 for my $row (0 .. $self->height - 1) {
93 7116 $cells[$row] = [ (' ') x $self->width ];
94 7116 $paint[$row] = [ ('baize') x $self->width ];
95 }
96 297 return { cells => \@cells, paint => \@paint };
97 }
98  
99 24300 ▶sub put {
Subroutine put: called CC=8 SCAR=21.2
Pod documented
100 24300 my ($self, $grid, $row, $col, $text, $paint) = @_;
101 24300 ▶ return $grid if $row < 0 || $row >= $self->height;
Branch: 1 branch
BranchTrueFalseTotal
if $row < 0 or $row >= $self->height 14 24286 24300
Condition66%2/3
$row < 0 or $row >= $self->height
l!l&&r!l&&!r
01424286
MC/DC50%1/2
$row < 0 or $row >= $self->height
$row < 0 $row >= $self->height
Truth table66%2/3
A or B
A= $row < 0B= $row >= $self->height
ABresult
1X1
011
000
102 24286 ▶ my @chars = split //, defined $text ? $text : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 24286 0 24286
103 24286 for my $at (0 .. $#chars) {
104 178785 my $x = $col + $at;
105 178785 ▶ next if $x < 0 || $x >= $self->width;
Branch: 1 branch
BranchTrueFalseTotal
if $x < 0 or $x >= $self->width 342 178443 178785
Condition66%2/3
$x < 0 or $x >= $self->width
l!l&&r!l&&!r
0342178443
MC/DC50%1/2
$x < 0 or $x >= $self->width
$x < 0 $x >= $self->width
Truth table66%2/3
A or B
A= $x < 0B= $x >= $self->width
ABresult
1X1
011
000
106 178443 $grid->{cells}[$row][$x] = $chars[$at];
107 178443 ▶ $grid->{paint}[$row][$x] = $paint if defined $paint;
Branch: 1 branch
BranchTrueFalseTotal
if defined $paint 178443 0 178443
108 }
109 24286 return $grid;
110 }
111  
112 594 ▶sub band {
Subroutine band: called CC=1 SCAR=0.0
Pod documented
113 594 my ($self, $grid, $row, $paint) = @_;
114 594 return $self->put($grid, $row, 0, ' ' x $self->width, $paint);
115 }
116  
117 1099 ▶sub card {
Subroutine card: called CC=2 SCAR=6.9
Pod documented
118 1099 my ($self, $id, $ring) = @_;
119 1099 my $art = $self->_art;
120 1099 my $idx = $self->index_of($id);
121 1099 my $pip = $self->suit_glyph(suit_of($id));
122 my ($tl, $tr, $bl, $br, $h, $v) = $ring
123 149 ? @{$art}{qw(ring_tl ring_tr ring_bl ring_br ring_h ring_v)}
124 1099 ▶ : @{$art}{qw(tl tr bl br h v)};
Branch: 1 branch
BranchTrueFalseTotal
$ring ? : 149 950 1099
125 return [
126 1099 $tl . ($h x 5) . $tr,
127 sprintf('%s%-5s%s', $v, $idx, $v),
128 sprintf('%s %s %s', $v, $pip, $v),
129 sprintf('%s%5s%s', $v, $idx, $v),
130 $bl . ($h x 5) . $br,
131 ];
132 }
133  
134 523 ▶sub back {
Subroutine back: called CC=1 SCAR=0.0
Pod documented
135 523 my ($self) = @_;
136 523 my $art = $self->_art;
137 523 my $weave = $art->{weave} x 5;
138 return [
139 $art->{tl} . ($art->{h} x 5) . $art->{tr},
140 $art->{v} . $weave . $art->{v},
141 $art->{v} . $weave . $art->{v},
142 $art->{v} . $weave . $art->{v},
143 $art->{bl} . ($art->{h} x 5) . $art->{br},
144 523 ];
145 }
146  
147 1300 ▶sub fan_of {
Subroutine fan_of: called CC=4 SCAR=14.0
Pod documented
148 1300 my ($self, $id, $ring) = @_;
149 1300 my $art = $self->_art;
150 my ($tl, $bl, $h, $v) = $ring
151 1 ? @{$art}{qw(ring_tl ring_bl ring_h ring_v)}
152 1300 ▶ : @{$art}{qw(tl bl h v)};
Branch: 1 branch
BranchTrueFalseTotal
$ring ? : 1 1299 1300
153 return [
154 1300 ▶ $tl . ($h x 3),
Branch: 2 branches
BranchTrueFalseTotal
defined $id ? : 1300 0 1300
defined $id ? : 1300 0 1300
155 sprintf('%s%-3s', $v, defined $id ? face_of($id) : ''),
156 sprintf('%s%-3s', $v,
157 defined $id ? $self->suit_glyph(suit_of($id)) : ''),
158 $v . ' ',
159 $bl . ($h x 3),
160 ];
161 }
162  
163 1189 ▶sub fan_back {
Subroutine fan_back: called CC=1 SCAR=0.0
Pod documented
164 1189 my ($self) = @_;
165 1189 my $art = $self->_art;
166 return [
167 $art->{tl} . ($art->{h} x 3),
168 $art->{v} . ($art->{weave} x 3),
169 $art->{v} . ($art->{weave} x 3),
170 $art->{v} . ($art->{weave} x 3),
171 1189 $art->{bl} . ($art->{h} x 3),
172 ];
173 }
174  
175 4111 ▶sub lay {
Subroutine lay: called CC=2 SCAR=6.9
Pod documented
176 4111 my ($self, $grid, $box, $row, $col, $paint) = @_;
177 4111 my $at = $row;
178 4111 $self->put($grid, $at++, $col, $_, $paint) for @$box;
179 4111 return $grid;
180 }
181  
182 590 ▶sub fan {
Subroutine fan: called CC=12 SCAR=24.9
Pod documented
183 590 my ($self, $grid, $row, $col, $cards, %o) = @_;
184 590 ▶ my $lit = $o{lit} || {};
Condition100%2/2
$o{'lit'} || {}
l!l
297293
MC/DC100%1/1
$o{'lit'} || {}
$o{'lit'}
Truth table100%2/2
A
A= $o{'lit'}
Aresult
11
00
185 590 ▶ my $face = exists $o{face} ? $o{face} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $o{'face'} ? : 293 297 590
186 590 my $count = scalar @$cards;
187 590 ▶ return $col unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 590 590
188  
189 590 my $pitch = $self->pitch_for($col, $count);
190  
191 590 ▶ my $cursor = defined $o{cursor} ? $o{cursor} : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'cursor'} ? : 1 589 590
192  
193 590 my $at = $col;
194 590 for my $i (0 .. $count - 1) {
195 3079 my $id = $cards->[$i];
196 3079 my $last = $i == $count - 1;
197 3079 my $on = $i == $cursor;
198 my $paint = !$face ? 'back'
199 3079 ▶ : $lit->{$i} || !%$lit ? $self->ink_of($id)
Branch: 2 branches
BranchTrueFalseTotal
!$face ? : 1482 1597 3079
$lit->{$i} || !%$lit ? : 935 662 1597
Condition100%3/3
$lit->{$i} or not %$lit
l!l&&r!l&&!r
91520662
MC/DC100%2/2
$lit->{$i} or not %$lit
$lit->{$i} not %$lit
Truth table100%3/3
A or not(B)
A= $lit->{$i}B= not %$lit
ABresult
1X1
011
000
200 : 'grey';
201 3079 ▶ my $box = !$face ? ($last ? $self->back : $self->fan_back)
Branch: 3 branches
BranchTrueFalseTotal
!$face ? : 1482 1597 3079
$last ? : 293 1189 1482
$last ? : 297 1300 1597
202 : ($last ? $self->card($id, $on)
203 : $self->fan_of($id, $on));
204 3079 $self->lay($grid, $box, $row, $at, $paint);
205 3079 $at += $pitch;
206 }
207 590 return $col + ($count - 1) * $pitch + CARD_W;
208 }
209  
210 889 ▶sub pitch_for {
Subroutine pitch_for: called CC=4 SCAR=13.9
Pod documented
211 889 my ($self, $col, $count) = @_;
212 889 ▶ return FAN unless $count > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $count > 1 24 865 889
213 865 my $room = $self->width - $col - CARD_W;
214 865 ▶ my $pitch = ($count - 1) * FAN > $room ? int($room / ($count - 1)) : FAN;
Branch: 1 branch
BranchTrueFalseTotal
($count - 1) * 4 > $room ? : 3 862 865
215 865 ▶ return $pitch < 1 ? 1 : $pitch;
Branch: 1 branch
BranchTrueFalseTotal
$pitch < 1 ? : 0 865 865
216 }
217  
218 297 ▶sub screen {
Subroutine screen: called CC=21 SCAR=30.4
Pod documented
219 297 my ($self, $view, %o) = @_;
220 297 my $grid = $self->blank;
221 297 my $art = $self->_art;
222  
223 297 my $me = $view->{seat};
224 297 my $them = 3 - $me;
225  
226 297 $self->band($grid, 0, 'bar');
227 297 ▶ $self->put($grid, 0, 1, defined $o{header} ? $o{header} : '', 'bar');
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'header'} ? : 281 16 297
228  
229 297 ▶ my $theirs = $view->{counts}{$them} || 0;
Condition100%2/2
$view->{'counts'}{$them} || 0
l!l
2934
MC/DC100%1/1
$view->{'counts'}{$them} || 0
$view->{'counts'}{$them}
Truth table100%2/2
A
A= $view->{'counts'}{$them}
Aresult
11
00
230 297 $self->put($grid, 2, 2,
231 sprintf('they hold %d', $theirs), 'label');
232  
233 my $status = sprintf('trump %s talon %d heap %d',
234 297 $self->suit_glyph($view->{trump}), $view->{talon}, $view->{discard});
235 297 $self->put($grid, 2, $self->width - length($status) - 2, $status, 'label');
236  
237 297 ▶ $self->fan($grid, 3, 2, [ (undef) x $theirs ], face => 0) if $theirs;
Branch: 1 branch
BranchTrueFalseTotal
if $theirs 293 4 297
238  
239 297 my $strip = 9;
240 297 ▶ $self->lay($grid, $self->back, $strip, 2, 'back') if $view->{talon} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $view->{"talon"} > 1 230 67 297
241 $self->lay($grid, $self->card($view->{trump_card}), $strip + 2, 5,
242 $self->ink_of($view->{trump_card}))
243 297 ▶ if defined $view->{trump_card};
Branch: 1 branch
BranchTrueFalseTotal
if defined $view->{"trump_card"} 231 66 297
244  
245 297 ▶ my $phase = $view->{phase} || '';
Condition100%2/2
$view->{'phase'} || ''
l!l
28710
MC/DC100%1/1
$view->{'phase'} || ''
$view->{'phase'}
Truth table100%2/2
A
A= $view->{'phase'}
Aresult
11
00
246 297 ▶ my $mine = defined $view->{turn} && $view->{turn} == $me;
Condition100%3/3
defined $view->{'turn'} && $view->{'turn'} == $me
!ll&&!rl&&r
103284
MC/DC100%2/2
defined $view->{'turn'} && $view->{'turn'} == $me
defined $view->{'turn'} $view->{'turn'} == $me
Truth table100%3/3
A && B
A= defined $view->{'turn'}B= $view->{'turn'} == $me
ABresult
0X0
100
111
247 297 ▶ my $role = $mine ? $ROLE{$phase} : $WATCH{$phase};
Branch: 1 branch
BranchTrueFalseTotal
$mine ? : 284 13 297
248 297 ▶ $self->put($grid, $strip - 1, 15, $role, 'label') if $role;
Branch: 1 branch
BranchTrueFalseTotal
if $role 287 10 297
249  
250 297 ▶ my $pairs = $view->{bout} ? $view->{bout}{pairs} : [];
Branch: 1 branch
BranchTrueFalseTotal
$view->{'bout'} ? : 297 0 297
251 297 ▶ if (@$pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pairs) { } 218 79 297
252 218 my $col = 15;
253 218 my $room = $self->width - $col - CARD_W - 3;
254 218 ▶ my $pitch = @$pairs > 1 && (scalar(@$pairs) - 1) * 10 > $room
Branch: 1 branch
BranchTrueFalseTotal
@$pairs > 1 && (scalar(@$pairs) - 1) * 10 > $room ? : 1 217 218
Condition100%3/3
@$pairs > 1 and (scalar(@$pairs) - 1) * 10 > $room
!ll&&!rl&&r
1151021
MC/DC100%2/2
@$pairs > 1 and (scalar(@$pairs) - 1) * 10 > $room
@$pairs > 1 (scalar(@$pairs) - 1) * 10 > $room
Truth table100%3/3
A and B
A= @$pairs > 1B= (scalar(@$pairs) - 1) * 10 > $room
ABresult
0X0
100
111
255 ? int($room / (scalar(@$pairs) - 1)) : 10;
256 218 ▶ $pitch = 4 if $pitch < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $pitch < 4 0 218 218
257 218 for my $pair (@$pairs) {
258 $self->lay($grid,
259 $self->card($pair->{attack}, !defined $pair->{beat}),
260 360 $strip, $col, $self->ink_of($pair->{attack}));
261 $self->lay($grid, $self->card($pair->{beat}), $strip + 2, $col + 3,
262 $self->ink_of($pair->{beat}))
263 360 ▶ if defined $pair->{beat};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pair->{"beat"} 211 149 360
264 360 $col += $pitch;
265 }
266 }
267 else {
268 79 $self->put($grid, $strip + 2, 16, 'nothing on the table', 'quiet');
269 }
270  
271 297 my $hand = $view->{hand};
272 297 my $numbers = $self->height - 7;
273 297 my $lit = $self->lit_for($view);
274 297 my $pitch = $self->pitch_for(2, scalar @$hand);
275  
276 297 for my $i (0 .. $#$hand) {
277 1597 my $n = $i + 1;
278 $self->put($grid, $numbers, 2 + $i * $pitch, sprintf('%-2d', $n),
279 1597 ▶ $lit->{$i} ? 'pick' : 'quiet');
Branch: 1 branch
BranchTrueFalseTotal
$lit->{$i} ? : 915 682 1597
280 }
281 $self->fan($grid, $numbers + 1, 2, $hand, lit => $lit,
282 297 ▶ (defined $o{cursor} ? (cursor => $o{cursor} - 1) : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'cursor'} ? : 1 296 297
283  
284 297 $self->band($grid, $self->height - 1, 'bar');
285 $self->put($grid, $self->height - 1, 1,
286 297 ▶ defined $o{footer} ? $o{footer} : '', 'bar');
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'footer'} ? : 281 16 297
287  
288 297 return $grid;
289 }
290  
291 297 ▶sub lit_for {
Subroutine lit_for: called CC=5 SCAR=16.2
Pod documented
292 297 my ($self, $view) = @_;
293 915 my %card = map { $_->{card} => 1 }
294 297 ▶ grep { defined $_->{card} } @{ $view->{legal} || [] };
Condition50%1/2
$view->{'legal'} || []
l!l
2970
MC/DC0%0/1
$view->{'legal'} || []
$view->{'legal'}
Truth table50%1/2
A
A= $view->{'legal'}
Aresult
11
00
295 297 my %lit;
296 297 ▶ my $hand = $view->{hand} || [];
Condition50%1/2
$view->{'hand'} || []
l!l
2970
MC/DC0%0/1
$view->{'hand'} || []
$view->{'hand'}
Truth table50%1/2
A
A= $view->{'hand'}
Aresult
11
00
297 297 for my $i (0 .. $#$hand) {
298 1597 ▶ $lit{$i} = 1 if $card{ $hand->[$i] };
Branch: 1 branch
BranchTrueFalseTotal
if $card{$hand->[$i]} 915 682 1597
299 }
300 297 return \%lit;
301 }
302  
303 295 ▶sub lines {
Subroutine lines: called CC=1 SCAR=0.0
Pod documented
304 295 my ($self, $grid) = @_;
305 295 return [ map { my $row = $_; join '', @$row } @{ $grid->{cells} } ];
306 }
307  
308 1 ▶sub paint {
Subroutine paint: called CC=6 SCAR=18.1
Pod documented
309 1 my ($self, $grid) = @_;
310 1 my @out;
311 1 for my $row (0 .. $self->height - 1) {
312 24 my $cells = $grid->{cells}[$row];
313 24 my $paint = $grid->{paint}[$row];
314 24 my $line = '';
315 24 my $held = '';
316 24 for my $col (0 .. $self->width - 1) {
317 1920 ▶ my $want = $paint->[$col] || 'baize';
Condition50%1/2
$paint->[$col] || 'baize'
l!l
19200
MC/DC0%0/1
$paint->[$col] || 'baize'
$paint->[$col]
Truth table50%1/2
A
A= $paint->[$col]
Aresult
11
00
318 1920 ▶ if ($want ne $held) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want ne $held) 102 1818 1920
319 102 ▶ $line .= $PAINT{$want} || $PAINT{baize};
Condition33%1/3
$PAINT{$want} || $PAINT{'baize'}
l!l&&r!l&&!r
10200
MC/DC0%0/2
$PAINT{$want} || $PAINT{'baize'}
$PAINT{$want} $PAINT{'baize'}
Truth table33%1/3
A || B
A= $PAINT{$want}B= $PAINT{'baize'}
ABresult
1X1
011
000
320 102 $held = $want;
321 }
322 1920 $line .= $cells->[$col];
323 }
324 24 push @out, $line . "\e[0m";
325 }
326 1 return \@out;
327 }
328  
329 6 1;
330  
331 __END__
« blib/lib/Game/Durak/Terminal.pm ↑ Summary blib/lib/Game/Durak/Search.pm »