Gin/Deal.pm

filter: Statementstmt 98.8% 166 / 168 Branchbran 87.1% 68 / 78 Conditioncond 69.0% 29 / 42 MC/DCmcdc 43.4% 10 / 23 Subroutinesub 100% 22 / 22 Podpod 100% 8 / 8 totaltotal 88.8% 303 / 341 CC 57 SCAR 22.9
CC
57
Cov
91%
apply
19.3
legal
16.3
_discard
8.0
CRAP 9.9
« Gin/Scoring.pm ↑ Summary Gin/Meld.pm »
1 package Game::Gin::Deal;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use Game::Gin::Deck ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Game::Gin::Hand ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Game::Gin::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Game::Gin::Deadwood qw(KNOCK_AT);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Game::Gin::Scoring qw(settle);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 our $VERSION = '0.03';
15  
16 10 has seed => (is => 'ro', isa => Str);
17 10 has number => (is => 'ro', isa => Int);
18 10 has dealer => (is => 'ro', isa => Str);
19 10 has hands => (is => 'rw', isa => HashRef);
20 10 has stock => (is => 'rw', isa => ArrayRef);
21 10 has discard => (is => 'rw', isa => ArrayRef);
22 10 has turn => (is => 'rw', isa => Str);
23 10 has phase => (is => 'rw', isa => Str);
24 10 has taken => (is => 'rw', isa => Int);
25 10 has result => (is => 'rw', isa => HashRef);
26  
27 10 has history => (is => 'rw', isa => ArrayRef);
28  
29 31998 ▶sub other { return $_[0] eq 'p1' ? 'p2' : 'p1' }
Subroutine other: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq 'p1' ? : 15965 16033 31998
30  
31 2069 ▶sub build {
Subroutine build: called CC=5 SCAR=16.7
Pod documented
32 2069 my ($class, %o) = @_;
33 2069 my $seed = $o{seed};
34 2069 ▶ return Game::Gin::Error->new_code('no_seed')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seed and length $seed == 32 0 2069 2069
Condition33%1/3
defined $seed and length $seed == 32
!ll&&!rl&&r
002069
MC/DC0%0/2
defined $seed and length $seed == 32
defined $seed length $seed == 32
Truth table33%1/3
A and B
A= defined $seedB= length $seed == 32
ABresult
0X0
100
111
35 unless defined $seed && length $seed == 32;
36  
37 2069 ▶ my $number = $o{number} || 1;
Condition50%1/2
$o{'number'} || 1
l!l
20690
MC/DC0%0/1
$o{'number'} || 1
$o{'number'}
Truth table50%1/2
A
A= $o{'number'}
Aresult
11
00
38 2069 ▶ my $dealer = $o{dealer} || 'p1';
Condition50%1/2
$o{'dealer'} || 'p1'
l!l
20690
MC/DC0%0/1
$o{'dealer'} || 'p1'
$o{'dealer'}
Truth table50%1/2
A
A= $o{'dealer'}
Aresult
11
00
39 2069 my $pone = other($dealer);
40  
41 2069 my $deal = Game::Gin::Deck::deal_for($seed, $number);
42  
43 my $self = $class->new(
44 seed => $seed,
45 number => $number,
46 dealer => $dealer,
47 hands => {
48 2069 $pone => Game::Gin::Hand->new(cards => [ @{ $deal->{non_dealer} } ]),
49 2069 $dealer => Game::Gin::Hand->new(cards => [ @{ $deal->{dealer} } ]),
50 },
51 2069 stock => [ @{ $deal->{stock} } ],
52 2069 discard => [ $deal->{upcard} ],
53 turn => $pone,
54 phase => 'upcard',
55 taken => 0,
56 history => [],
57 result => undef,
58 );
59 2069 return $self;
60 }
61  
62 217258 ▶sub over { return $_[0]->result ? 1 : 0 }
Subroutine over: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->result ? : 2038 215220 217258
63 54140 ▶sub upcard { my $d = $_[0]->discard; return @$d ? $d->[-1] : undef }
Subroutine upcard: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@$d ? : 54134 6 54140
64 210830 ▶sub hand_of { return $_[0]->hands->{ $_[1] } }
Subroutine hand_of: called CC=1 SCAR=0.0
Pod documented
65 84303 ▶sub stock_left { return scalar @{ $_[0]->stock } }
Subroutine stock_left: called CC=1 SCAR=0.0
Pod documented
66  
67 55939 ▶sub legal {
Subroutine legal: called CC=16 SCAR=27.9
Pod documented
68 55939 my ($self, $seat) = @_;
69 55939 ▶ return [] if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 0 55939 55939
70 55939 ▶ return [] unless defined $seat && ($self->turn // '') eq $seat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and ($self->turn // "") eq $seat 1 55938 55939
Condition60%3/5
defined $seat and ($self->turn // "") eq $seat
!ll&&!rl&&r
0155938
$self->turn // ""
l!l
559390
MC/DC0%0/2
defined $seat and ($self->turn // "") eq $seat
defined $seat $self->turn
Truth table33%1/3
A and B
A= defined $seatB= $self->turn
ABresult
0X0
100
111
71  
72 55938 my $phase = $self->phase;
73 55938 ▶ return [ { kind => 'take' }, { kind => 'pass' } ] if $phase eq 'upcard';
Branch: 1 branch
BranchTrueFalseTotal
if $phase eq "upcard" 2307 53631 55938
74  
75 53631 ▶ return [ { kind => 'draw' } ] if $phase eq 'forced_draw';
Branch: 1 branch
BranchTrueFalseTotal
if $phase eq "forced_draw" 332 53299 53631
76  
77 53299 ▶ if ($phase eq 'draw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq "draw") 25683 27616 53299
78 25683 my @out = ({ kind => 'draw' });
79 25683 ▶ push @out, { kind => 'take' } if defined $self->upcard;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->upcard 25683 0 25683
80 25683 return \@out;
81 }
82  
83 27616 my $hand = $self->hand_of($seat);
84 27616 my @out;
85 27616 ▶ push @out, { kind => 'big_gin' } if $hand->deadwood == 0 && $hand->full;
Branch: 1 branch
BranchTrueFalseTotal
if $hand->deadwood == 0 and $hand->full 6 27610 27616
Condition66%2/3
$hand->deadwood == 0 and $hand->full
!ll&&!rl&&r
2761006
MC/DC50%1/2
$hand->deadwood == 0 and $hand->full
$hand->deadwood == 0 $hand->full
Truth table66%2/3
A and B
A= $hand->deadwood == 0B= $hand->full
ABresult
0X0
100
111
86 27616 for my $card (@{ $hand->cards }) {
87 303776 ▶ next if defined $self->taken && $self->taken && $card == $self->taken;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->taken and $self->taken and $card == $self->taken 6541 297235 303776
Condition83%5/6
defined $self->taken and $self->taken and $card == $self->taken
!ll&&!rl&&r
231825654106541
defined $self->taken and $self->taken
!ll&&!rl&&r
023182571951
MC/DC66%2/3
defined $self->taken and $self->taken and $card == $self->taken
defined $self->taken $self->taken $card == $self->taken
Truth table75%3/4
A and B and C
A= defined $self->takenB= $self->takenC= $card == $self->taken
ABCresult
0XX0
10X0
1100
1111
88 297235 push @out, { kind => 'discard', card => $card };
89 297235 my @rest = grep { $_ != $card } @{ $hand->cards };
90 297235 my $left = Game::Gin::Deadwood::deadwood(\@rest);
91 297235 ▶ push @out, { kind => 'discard', card => $card, knock => 1 } if $left <= KNOCK_AT;
Branch: 1 branch
BranchTrueFalseTotal
if $left <= 10 8948 288287 297235
92 }
93 27616 return \@out;
94 }
95  
96 59994 ▶sub apply {
Subroutine apply: called CC=18 SCAR=29.6
Pod documented
97 59994 my ($self, $seat, $move) = @_;
98 59994 ▶ return Game::Gin::Error->new_code('hand_over') if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 0 59994 59994
99 59994 ▶ return Game::Gin::Error->new_code('not_your_turn')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and ($self->turn // "") eq $seat 2 59992 59994
Condition60%3/5
defined $seat and ($self->turn // "") eq $seat
!ll&&!rl&&r
0259992
$self->turn // ""
l!l
599940
MC/DC0%0/2
defined $seat and ($self->turn // "") eq $seat
defined $seat $self->turn
Truth table33%1/3
A and B
A= defined $seatB= $self->turn
ABresult
0X0
100
111
100 unless defined $seat && ($self->turn // '') eq $seat;
101  
102 59992 ▶ my $kind = ref $move eq 'HASH' ? ($move->{kind} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $move eq 'HASH' ? : 59992 0 59992
Condition50%1/2
$move->{"kind"} // ""
l!l
599920
MC/DC0%0/1
$move->{"kind"} // ""
$move->{"kind"}
Truth table50%1/2
A
A= $move->{"kind"}
Aresult
11
00
103 59992 my $phase = $self->phase;
104  
105 59992 my @out;
106 59992 ▶ if ($phase eq 'upcard') { @out = $self->_first_turn($seat, $kind) }
Branch: 5 branches
BranchTrueFalseTotal
if ($phase eq 'upcard') { } 2503 57489 59992
elsif ($phase eq 'forced_draw') { } 368 57121 57489
elsif ($phase eq 'draw') { } 27536 29585 57121
elsif ($kind eq 'big_gin') { } 6 29579 29585
elsif ($kind eq 'discard') { } 29579 0 29579
107 elsif ($phase eq 'forced_draw') {
108 368 ▶ return Game::Gin::Error->new_code('not_legal') unless $kind eq 'draw';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "draw" 1 367 368
109 367 @out = $self->_draw($seat);
110 }
111 elsif ($phase eq 'draw') {
112 27536 ▶ if ($kind eq 'draw') { @out = $self->_draw($seat) }
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq 'draw') { } 22093 5443 27536
elsif ($kind eq 'take') { } 5443 0 5443
113 5443 elsif ($kind eq 'take') { @out = $self->_take($seat) }
114 0 else { return Game::Gin::Error->new_code('not_legal') }
115 }
116 6 elsif ($kind eq 'big_gin') { @out = $self->_big_gin($seat) }
117 29579 elsif ($kind eq 'discard') { @out = $self->_discard($seat, $move) }
118 0 else { return Game::Gin::Error->new_code('not_legal') }
119  
120 59991 ▶ return $out[0] if @out == 1 && ref $out[0] eq 'Game::Gin::Error';
Branch: 1 branch
BranchTrueFalseTotal
if @out == 1 and ref $out[0] eq "Game::Gin::Error" 6 59985 59991
Condition100%3/3
@out == 1 and ref $out[0] eq "Game::Gin::Error"
!ll&&!rl&&r
2040579456
MC/DC100%2/2
@out == 1 and ref $out[0] eq "Game::Gin::Error"
@out == 1 ref $out[0] eq "Game::Gin::Error"
Truth table100%3/3
A and B
A= @out == 1B= ref $out[0] eq "Game::Gin::Error"
ABresult
0X0
100
111
121 59985 ▶ $self->history([ @{ $self->history || [] }, @out ]);
Condition100%2/2
$self->history || []
l!l
599805
MC/DC100%1/1
$self->history || []
$self->history
Truth table100%2/2
A
A= $self->history
Aresult
11
00
122 59985 return @out;
123 }
124  
125 2503 ▶sub _first_turn {
Subroutine _first_turn: called CC=4 SCAR=13.9
126 2503 my ($self, $seat, $kind) = @_;
127 2503 ▶ return $self->_take($seat) if $kind eq 'take';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "take" 1694 809 2503
128 809 ▶ return Game::Gin::Error->new_code('not_legal') unless $kind eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "pass" 0 809 809
129  
130 809 my @out = ({ kind => 'pass', seat => $seat });
131 809 ▶ if ($seat eq $self->dealer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seat eq $self->dealer) { } 368 441 809
132 368 $self->turn(other($self->dealer));
133 368 $self->phase('forced_draw');
134 }
135 else {
136 441 $self->turn($self->dealer);
137 }
138 809 return @out;
139 }
140  
141 7137 ▶sub _take {
Subroutine _take: called CC=2 SCAR=6.9
142 7137 my ($self, $seat) = @_;
143 7137 my $card = $self->upcard;
144 7137 ▶ return Game::Gin::Error->new_code('not_legal') unless defined $card;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $card 0 7137 7137
145 7137 my @pile = @{ $self->discard };
146 7137 pop @pile;
147 7137 $self->discard(\@pile);
148 7137 $self->hand_of($seat)->add($card);
149 7137 $self->taken($card);
150 7137 $self->phase('discard');
151 7137 return ({ kind => 'take', seat => $seat, card => $card });
152 }
153  
154 22460 ▶sub _draw {
Subroutine _draw: called CC=2 SCAR=6.9
155 22460 my ($self, $seat) = @_;
156 22460 my @stock = @{ $self->stock };
157 22460 ▶ return Game::Gin::Error->new_code('not_legal') unless @stock;
Branch: 1 branch
BranchTrueFalseTotal
unless @stock 1 22459 22460
158 22459 my $card = shift @stock;
159 22459 $self->stock(\@stock);
160 22459 $self->hand_of($seat)->add($card);
161 22459 $self->taken(0);
162 22459 $self->phase('discard');
163 22459 return ({ kind => 'draw', seat => $seat });
164 }
165  
166 29579 ▶sub _discard {
Subroutine _discard: called CC=8 SCAR=20.8
167 29579 my ($self, $seat, $move) = @_;
168 29579 my $card = $move->{card};
169 29579 my $hand = $self->hand_of($seat);
170 29579 ▶ return Game::Gin::Error->new_code('not_held') unless $hand->has_card($card);
Branch: 1 branch
BranchTrueFalseTotal
unless $hand->has_card($card) 3 29576 29579
171 29576 ▶ return Game::Gin::Error->new_code('just_taken')
Branch: 1 branch
BranchTrueFalseTotal
if $self->taken and $card == $self->taken 1 29575 29576
Condition100%3/3
$self->taken and $card == $self->taken
!ll&&!rl&&r
2245071251
MC/DC100%2/2
$self->taken and $card == $self->taken
$self->taken $card == $self->taken
Truth table100%3/3
A and B
A= $self->takenB= $card == $self->taken
ABresult
0X0
100
111
172 if $self->taken && $card == $self->taken;
173  
174 29575 $hand->remove($card);
175 29575 $self->discard([ @{ $self->discard }, $card ]);
176 29575 $self->taken(0);
177  
178 29575 my $left = $hand->deadwood;
179 29575 ▶ if ($move->{knock}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move->{"knock"}) 2016 27559 29575
180 2016 ▶ return Game::Gin::Error->new_code('cannot_knock') if $left > KNOCK_AT;
Branch: 1 branch
BranchTrueFalseTotal
if $left > 10 1 2015 2016
181 2015 my @out = ({ kind => 'discard', seat => $seat, card => $card, knock => 1 });
182 2015 ▶ push @out, $self->_finish($seat, gin => ($left == 0 ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$left == 0 ? : 79 1936 2015
183 2015 return @out;
184 }
185  
186 27559 my @out = ({ kind => 'discard', seat => $seat, card => $card });
187  
188 27559 ▶ if ($self->stock_left <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stock_left <= 2) 19 27540 27559
189 19 $self->result({ kind => 'cancelled', winner => undef, points => 0 });
190 19 $self->turn(undef);
191 19 $self->phase('over');
192 19 push @out, { kind => 'cancelled' };
193 19 return @out;
194 }
195  
196 27540 $self->turn(other($seat));
197 27540 $self->phase('draw');
198 27540 return @out;
199 }
200  
201 6 ▶sub _big_gin {
Subroutine _big_gin: called CC=3 SCAR=12.0
202 6 my ($self, $seat) = @_;
203 6 my $hand = $self->hand_of($seat);
204 6 ▶ return Game::Gin::Error->new_code('not_big_gin')
Branch: 1 branch
BranchTrueFalseTotal
unless $hand->full and $hand->deadwood == 0 0 6 6
Condition33%1/3
$hand->full and $hand->deadwood == 0
!ll&&!rl&&r
006
MC/DC0%0/2
$hand->full and $hand->deadwood == 0
$hand->full $hand->deadwood == 0
Truth table33%1/3
A and B
A= $hand->fullB= $hand->deadwood == 0
ABresult
0X0
100
111
205 unless $hand->full && $hand->deadwood == 0;
206 6 return ({ kind => 'big_gin', seat => $seat }, $self->_finish($seat, big_gin => 1));
207 }
208  
209 2021 ▶sub _finish {
Subroutine _finish: called CC=4 SCAR=13.9
210 2021 my ($self, $knocker, %how) = @_;
211 2021 my $defender = other($knocker);
212 2021 my $hand = $self->hand_of($knocker);
213 2021 my $best = $hand->best;
214  
215 my $r = settle(
216 knocker => $knocker,
217 defender => $defender,
218 knocker_deadwood => $best->{deadwood},
219 knocker_melds => $best->{melds},
220 defender_cards => $self->hand_of($defender)->cards,
221 gin => ($how{gin} || $how{big_gin} ? 1 : 0),
222 2021 ▶ big_gin => ($how{big_gin} ? 1 : 0),
Branch: 2 branches
BranchTrueFalseTotal
$how{'gin'} || $how{'big_gin'} ? : 85 1936 2021
$how{'big_gin'} ? : 6 2015 2021
Condition100%3/3
$how{"gin"} or $how{"big_gin"}
l!l&&r!l&&!r
7961936
MC/DC100%2/2
$how{"gin"} or $how{"big_gin"}
$how{"gin"} $how{"big_gin"}
Truth table100%3/3
A or B
A= $how{"gin"}B= $how{"big_gin"}
ABresult
1X1
011
000
223 );
224 2021 $self->result($r);
225 2021 $self->turn(undef);
226 2021 $self->phase('over');
227 2021 return ({ %$r, how => $r->{kind}, kind => 'hand_end' });
228 }
229  
230 10 1;
231  
232 __END__
« Gin/Scoring.pm ↑ Summary Gin/Meld.pm »