File Coverage

lib/Algorithm/MasterMind/Partition_Worst.pm
Criterion Covered Total %
statement 57 59 96.6
branch 8 10 80.0
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 77 81 95.0


line stmt bran cond sub pod time code
1             package Algorithm::MasterMind::Partition_Worst;
2              
3 1     1   924 use warnings;
  1         1  
  1         41  
4 1     1   5 use strict;
  1         2  
  1         40  
5 1     1   5 use Carp;
  1         1  
  1         65  
6              
7 1     1   4 use lib qw(../../lib ../../../lib);
  1         2  
  1         6  
8              
9             our $VERSION = sprintf "%d.%03d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/g;
10              
11 1     1   220 use base 'Algorithm::MasterMind';
  1         2  
  1         113  
12              
13 1     1   7 use Algorithm::MasterMind qw( partitions );
  1         1  
  1         531  
14              
15             sub initialize {
16 1     1 1 3 my $self = shift;
17 1         2 my $options = shift;
18 1         5 for my $o ( keys %$options ) {
19 2         16 $self->{"_$o"} = $options->{$o};
20             }
21 1         6 $self->{'_partitions'} = {};
22             }
23              
24             sub issue_first {
25 1     1 1 550 my $self = shift;
26 1         10 my @combinations = $self->all_combinations();
27 1         228 $self->{'_consistent'} = \@combinations;
28 1         17 return $self->{'_last'} = $self->issue_first_Knuth();
29              
30             }
31              
32             sub issue_next {
33 3     3 1 5 my $self = shift;
34 3         12 my $rules = $self->number_of_rules();
35              
36             # Check consistency
37 3         8 for ( my $i = 0; $i <= $#{$self->{'_consistent'}}; $i++ ) {
  1349         3811  
38 1346         4493 my $match = $self->matches($self->{'_consistent'}->[$i]);
39 1346         1775 $self->{'_evaluated'}++;
40 1346 100       3319 if ( $match->{'matches'} < $rules ) {
41 1294         5181 delete $self->{'_consistent'}->[$i];
42             }
43             }
44              
45             #Eliminate null
46 3         7 @{$self->{'_consistent'}} = grep( $_, @{$self->{'_consistent'}} );
  3         594  
  3         91  
47              
48 3 50       9 if ( @{$self->{'_consistent'}} > 1 ) {
  3         11  
49             # Compute partitions
50 3         6 my $partitions = partitions( @{$self->{'_consistent'}} );
  3         15  
51            
52             # Obtain best
53 3         6 my %min_c;
54 3         11 my $min_max = keys %$partitions ;
55 3         13 for my $c ( keys %$partitions ) {
56 52         57 my $this_max = 0;
57 52         54 for my $p ( keys %{$partitions->{$c}} ) {
  52         437  
58 384 100       816 if ( $partitions->{$c}{$p} > $this_max ) {
59 131         449 $this_max = $partitions->{$c}{$p};
60             }
61             }
62 52         128 $min_c{ $c } = $this_max;
63 52 100       123 if ( $this_max < $min_max ) {
64 4         6 $min_max = $this_max;
65             }
66             }
67            
68             # Find all partitions with that max
69 3         48 my @minimal_c = grep( $min_c{$_} == $min_max, keys %min_c );
70            
71             # Break ties
72 3         116 my $string = $minimal_c[ rand( @minimal_c )];
73             # Obtain next
74 3 50       11 if ( $string eq '' ) {
75 0         0 warn "Something is wrong\n";
76             }
77 3         78 return $self->{'_last'} = $string;
78             } else {
79 0           return $self->{'_last'} = $self->{'_consistent'}->[0];
80             }
81             }
82              
83             "some blacks, 0 white"; # Magic true value required at end of module
84              
85             __END__