blib/lib/Game/Schnapsen.pm

filter: Statementstmt 98.8% 88 / 89 Branchbran 96.8% 31 / 32 Conditioncond 81.8% 9 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 94.7% 18 / 19 Podpod 100% 9 / 9 totaltotal 95.2% 159 / 167 CC 21 SCAR 11.1
CC
21
Cov
97%
build
6.0
apply
5.0
_next_dealer
3.0
CRAP 3.0
« blib/lib/Game/Schnapsen/Trick.pm ↑ Summary blib/lib/Game/Schnapsen/Declare.pm »
1 package Game::Schnapsen;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use Game::Schnapsen::Card ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Game::Schnapsen::Deck ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Game::Schnapsen::Deal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Game::Schnapsen::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Game::Schnapsen::Variant ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 our $VERSION = '0.03';
15  
16 12 has variant => (is => 'ro', isa => Str);
17 12 has seed => (is => 'ro', isa => Str);
18 12 has dealer => (is => 'rw', isa => Str);
19 12 has deal => (is => 'rw', isa => Object);
20 12 has number => (is => 'rw', isa => Int);
21 12 has scores => (is => 'rw', isa => HashRef);
22 12 has deals => (is => 'rw', isa => ArrayRef);
23 12 has result => (is => 'rw', isa => HashRef);
24  
25 0 ▶sub variants { return Game::Schnapsen::Variant::variants() }
Subroutine variants: not called CC=1 SCAR=6.9
Pod documented
26  
27 2228 ▶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' ? : 1207 1021 2228
28  
29 539 ▶sub build {
Subroutine build: called CC=6 SCAR=18.0
Pod documented
30 539 my ($class, %o) = @_;
31  
32 539 my $bad = Game::Schnapsen::Variant::check_variant($o{variant});
33 539 ▶ return $bad if $bad;
Branch: 1 branch
BranchTrueFalseTotal
if $bad 2 537 539
34  
35 537 my $seed = $o{seed};
36 537 ▶ return Game::Schnapsen::Error->new_code('no_seed')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seed and length $seed == 32 1 536 537
Condition66%2/3
defined $seed and length $seed == 32
!ll&&!rl&&r
01536
MC/DC50%1/2
defined $seed and length $seed == 32
defined $seed length $seed == 32
Truth table66%2/3
A and B
A= defined $seedB= length $seed == 32
ABresult
0X0
100
111
37 unless defined $seed && length $seed == 32;
38  
39 536 ▶ my $dealer = $o{dealer} || 'p1';
Condition100%2/2
$o{'dealer'} || 'p1'
l!l
50630
MC/DC100%1/1
$o{'dealer'} || 'p1'
$o{'dealer'}
Truth table100%2/2
A
A= $o{'dealer'}
Aresult
11
00
40 536 my $start = Game::Schnapsen::Variant::match_start($o{variant});
41  
42 my $deal = Game::Schnapsen::Deal->build(
43 536 variant => $o{variant}, seed => $seed, number => 1, dealer => $dealer);
44 536 ▶ return $deal if ref $deal eq 'Game::Schnapsen::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $deal eq "Game::Schnapsen::Error" 0 536 536
45  
46 return $class->new(
47 variant => $o{variant},
48 536 seed => $seed,
49 dealer => $dealer,
50 number => 1,
51 deal => $deal,
52 scores => { p1 => $start, p2 => $start },
53 deals => [],
54 result => undef,
55 );
56 }
57  
58 432558 ▶sub over { return $_[0]->result ? 1 : 0 }
Subroutine over: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->result ? : 564 431994 432558
59 46 ▶sub winner { return $_[0]->result ? $_[0]->result->{winner} : undef }
Subroutine winner: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->result ? : 41 5 46
60  
61 80611 ▶sub turn { my $s = $_[0]; return undef if $s->over; return $s->deal->turn }
Subroutine turn: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $s->over 2 80609 80611
62 127237 ▶sub legal { my ($s, $seat) = @_; return [] if $s->over; return $s->deal->legal($seat) }
Subroutine legal: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $s->over 4 127233 127237
63  
64 6 ▶sub to_win {
Subroutine to_win: called CC=3 SCAR=11.0
Pod documented
65 6 my ($self, $seat) = @_;
66 6 my $v = $self->variant;
67 6 my $target = Game::Schnapsen::Variant::match_target($v);
68 6 my $score = $self->scores->{$seat};
69 6 ▶ my $left = Game::Schnapsen::Variant::match_direction($v) > 0
Branch: 1 branch
BranchTrueFalseTotal
Game::Schnapsen::Variant::match_direction($v) > 0 ? : 3 3 6
70 ? $target - $score
71 : $score - $target;
72 6 ▶ return $left > 0 ? $left : 0;
Branch: 1 branch
BranchTrueFalseTotal
$left > 0 ? : 3 3 6
73 }
74  
75 81670 ▶sub apply {
Subroutine apply: called CC=5 SCAR=16.1
Pod documented
76 81670 my ($self, $seat, $move) = @_;
77 81670 ▶ return Game::Schnapsen::Error->new_code('game_over') if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 2 81668 81670
78  
79 81668 my @out = $self->deal->apply($seat, $move);
80 81668 ▶ return $out[0] if @out == 1 && ref $out[0] eq 'Game::Schnapsen::Error';
Branch: 1 branch
BranchTrueFalseTotal
if @out == 1 and ref $out[0] eq "Game::Schnapsen::Error" 7 81661 81668
Condition100%3/3
@out == 1 and ref $out[0] eq "Game::Schnapsen::Error"
!ll&&!rl&&r
34808468537
MC/DC100%2/2
@out == 1 and ref $out[0] eq "Game::Schnapsen::Error"
@out == 1 ref $out[0] eq "Game::Schnapsen::Error"
Truth table100%3/3
A and B
A= @out == 1B= ref $out[0] eq "Game::Schnapsen::Error"
ABresult
0X0
100
111
81  
82 81661 ▶ return @out unless $self->deal->over;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->deal->over 77879 3782 81661
83 3782 return (@out, $self->_settle);
84 }
85  
86 3289 ▶sub _next_dealer {
Subroutine _next_dealer: called CC=3 SCAR=11.0
87 3289 my ($self, $r) = @_;
88 3289 ▶ return other($self->dealer)
Branch: 1 branch
BranchTrueFalseTotal
if Game::Schnapsen::Variant::next_dealer($self->variant) eq "alternate" 1735 1554 3289
89 if Game::Schnapsen::Variant::next_dealer($self->variant) eq 'alternate';
90 1554 ▶ return $r->{winner} || $self->dealer;
Condition66%2/3
$r->{'winner'} || $self->dealer
l!l&&r!l&&!r
1536180
MC/DC0%0/2
$r->{'winner'} || $self->dealer
$r->{'winner'} $self->dealer
Truth table66%2/3
A || B
A= $r->{'winner'}B= $self->dealer
ABresult
1X1
011
000
91 }
92  
93 3782 ▶sub _settle {
Subroutine _settle: called CC=3 SCAR=11.0
94 3782 my ($self) = @_;
95 3782 my $v = $self->variant;
96 3782 my $r = $self->deal->result;
97  
98 3782 $self->deals([ @{ $self->deals }, { %$r, number => $self->number } ]);
99  
100 3782 ▶ if ($r->{winner}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"winner"}) 3764 18 3782
101 3764 my $s = { %{ $self->scores } };
102 $s->{ $r->{winner} } +=
103 3764 Game::Schnapsen::Variant::match_direction($v) * $r->{game_points};
104 3764 $self->scores($s);
105  
106 3764 ▶ if (Game::Schnapsen::Variant::match_over($v, $s->{ $r->{winner} })) {
Branch: 1 branch
BranchTrueFalseTotal
if (Game::Schnapsen::Variant::match_over($v, $$s{$r->{"winner"}})) 493 3271 3764
107 my $m = {
108 winner => $r->{winner},
109 loser => other($r->{winner}),
110 scores => { %$s },
111 493 deals => scalar @{ $self->deals },
112 };
113 493 $self->result($m);
114 493 return { kind => 'game_end', %$m };
115 }
116 }
117  
118 3289 $self->dealer($self->_next_dealer($r));
119 3289 $self->number($self->number + 1);
120  
121 3289 my $d = Game::Schnapsen::Deal->build(
122 variant => $v, seed => $self->seed,
123 number => $self->number, dealer => $self->dealer);
124 3289 $self->deal($d);
125  
126 3289 return { kind => 'deal', number => $self->number, dealer => $self->dealer,
127 trump => $d->trump, turn_up => $d->turn_up };
128 }
129  
130 12 1;
131  
132 __END__
« blib/lib/Game/Schnapsen/Trick.pm ↑ Summary blib/lib/Game/Schnapsen/Declare.pm »