Gin/Bot.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 151 CC 14 SCAR 49.5
CC
14
Cov
0%
choose
210.0
seed_for
2.0
BEGIN
2.0
CRAP 140.7
1 package Game::Gin::Bot;
2  
3 use strict;
4 use warnings;
5  
6 use Object::Proto::Sugar -types;
7  
8 use Digest::SHA ();
9  
10 use Game::Gin::Deadwood qw(deadwood);
11 use Game::Gin::Search qw(worth_taking best_discard knock_now LEVELS);
12  
13 our $VERSION = '0.04';
14  
15 has level => (is => 'ro', isa => Int);
16 has seed => (is => 'ro', isa => Str);
17  
18 sub choose {
19 my ($self, $game, $seat) = @_;
20 my $deal = $game->can('deal') ? $game->deal : $game;
21 return undef unless $deal && !$deal->over;
22 return undef unless ($deal->turn // '') eq $seat;
23  
24 my $legal = $deal->legal($seat);
25 return undef unless @$legal;
26  
27 my $level = $self->level || LEVELS;
28 my $hand = $deal->hand_of($seat)->cards;
29 my $phase = $deal->phase;
30  
31 if ($phase eq 'upcard' || $phase eq 'draw') {
32 my $take = worth_taking(hand => $hand, upcard => $deal->upcard, level => $level);
33 return { kind => 'take' } if $take && grep { $_->{kind} eq 'take' } @$legal;
34 return { kind => 'draw' } if grep { $_->{kind} eq 'draw' } @$legal;
35 return { kind => 'pass' } if grep { $_->{kind} eq 'pass' } @$legal;
36 return $legal->[0];
37 }
38 return { kind => 'draw' } if $phase eq 'forced_draw';
39  
40 my ($big) = grep { $_->{kind} eq 'big_gin' } @$legal;
41 return $big if $big;
42  
43 my $card = best_discard(
44 hand => $hand,
45 level => $level,
46 just_taken => ($deal->taken || undef),
47 );
48 return $legal->[0] unless defined $card;
49  
50 my $left = deadwood([ grep { $_ != $card } @$hand ]);
51 my $knock = knock_now(deadwood => $left, stock_left => $deal->stock_left, level => $level);
52  
53 if ($knock) {
54 my ($move) = grep { $_->{kind} eq 'discard' && $_->{card} == $card && $_->{knock} } @$legal;
55 return $move if $move;
56 }
57 my ($move) = grep { $_->{kind} eq 'discard' && $_->{card} == $card && !$_->{knock} } @$legal;
58 return $move || $legal->[0];
59 }
60  
61 sub seed_for {
62 my ($class, $game_seed, $seat) = @_;
63 return unpack 'H16', Digest::SHA::sha256('gin-bot:' . ($game_seed // '') . ':' . ($seat // '?'));
64 }
65  
66 1;
67  
68 __END__