File Coverage

lib/Algorithm/MasterMind/Secret.pm
Criterion Covered Total %
statement 51 51 100.0
branch 2 2 100.0
condition 8 12 66.6
subroutine 7 7 100.0
pod 4 4 100.0
total 72 76 94.7


line stmt bran cond sub pod time code
1             package Algorithm::MasterMind::Secret;
2              
3 3     3   7689 use warnings;
  3         6  
  3         118  
4 3     3   18 use strict;
  3         4  
  3         121  
5 3     3   18 use Carp;
  3         5  
  3         2950  
6              
7             # Module implementation here
8              
9             sub new {
10              
11 1573     1573 1 8030 my $class = shift;
12 1573   33     3271 my $string = shift || croak "No default string\n";
13              
14 1573         5839 my $self = { _string => $string,
15             _chars => [],
16             _hash => {}};
17 1573         5387 while ( my $c = chop( $string ) ) {
18 6292         5971 push @{$self->{'_chars'}}, $c;
  6292         11771  
19 6292         18493 $self->{'_hash'}{ $c }++;
20             }
21 1573         1633 @{$self->{'_alphabet'}} = keys %{$self->{'_hash'}};
  1573         4688  
  1573         4354  
22 1573         3707 bless $self, $class;
23 1573         6093 return $self;
24             }
25              
26             sub string {
27 7     7 1 49 return shift->{'_string'};
28             }
29              
30             sub check {
31 8     8 1 49 my %hash_secret = %{$_[0]->{'_hash'}};
  8         47  
32 8         15 my %hash_string ;
33 8         10 my $blacks = 0;
34 8         16 my $string = $_[1];
35 8         11 my ($c, $s);
36 8         11 for my $c (@{$_[0]->{'_chars'}} ) {
  8         24  
37 32         38 $s = chop( $string );
38 32 100       134 if ( $c ne $s ) {
39 25         56 $hash_string{ $s }++;
40             } else {
41 7         6 $blacks++;
42 7         12 $hash_secret{ $c }--;
43             }
44             }
45 8         20 my $whites = 0;
46 8         100 map( exists $hash_string{$_}
47             && ( $whites += ($hash_secret{$_} > $hash_string{$_})
48             ?$hash_string{$_}
49 8   66     10 :$hash_secret{$_} ), @{$_[0]->{'_alphabet'}} );
50              
51 8         54 return{ blacks => $blacks,
52             whites => $whites } ;
53             }
54              
55             sub check_secret {
56 875503     875503 1 994763 my %hash_secret = %{$_[0]->{'_hash'}};
  875503         24351459  
57 875503         1399253 my %hash_other_secret = %{$_[1]->{'_hash'}};
  875503         12843890  
58             # my $blacks = 0;
59 875503         1269436 my $s;
60 875503         1590283 my $string = $_[1]->{'_string'};
61 875503         19576298 map( ($s = chop( $string ) )
62             && ( $s eq $_ )
63             && ( $_[2]->{'blacks'}++,
64             $hash_secret{ $s }--,
65 875503   66     908501 $hash_other_secret{ $s }-- ), @{$_[0]->{'_chars'}});
66              
67 875503         1179583 my $whites = 0;
68 875503         10884810 map( exists $hash_other_secret{$_}
69             && ( $_[2]->{'whites'} += ($hash_secret{$_} > $hash_other_secret{$_})
70             ?$hash_other_secret{$_}
71 875503   100     989361 :$hash_secret{$_} ), @{$_[0]->{'_alphabet'}} );
72 875503         2832071 return;
73             }
74             "Can't tell"; # Magic true value required at end of module
75              
76             __END__