line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Games::Mastermind::Solver::BruteForce; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
57556
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
102
|
|
4
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
90
|
|
5
|
3
|
|
|
3
|
|
15
|
use base qw(Games::Mastermind::Solver); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1913
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub guess { |
10
|
6
|
|
|
6
|
1
|
577
|
my( $self ) = @_; |
11
|
|
|
|
|
|
|
|
12
|
6
|
|
|
|
|
19
|
return [ _from_number( $self->_guess, $self->_pegs, $self->_holes ) ]; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub _guess { |
16
|
3
|
|
|
3
|
|
520
|
my( $self ) = @_; |
17
|
3
|
100
|
|
|
|
12
|
die 'Cheat!' unless $self->remaining; |
18
|
2
|
|
|
|
|
25
|
return $self->_possibility( rand $self->remaining ); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub remaining { |
22
|
14
|
|
|
14
|
1
|
7749
|
my $p = $_[0]->_possibility; |
23
|
14
|
100
|
|
|
|
81
|
return $p ? scalar @$p : $_[0]->_peg_number ** $_[0]->_holes; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub reset { |
27
|
3
|
|
|
3
|
1
|
9
|
my( $self ) = @_; |
28
|
3
|
|
|
|
|
25
|
$self->SUPER::reset; |
29
|
3
|
|
|
|
|
8
|
$self->{possibility} = undef; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _possibility { |
33
|
35
|
|
|
35
|
|
1267
|
my( $self, $idx ) = @_; |
34
|
|
|
|
|
|
|
|
35
|
35
|
100
|
|
|
|
128
|
return $self->{possibility} if @_ == 1; |
36
|
13
|
100
|
|
|
|
652
|
return $self->{possibility} ? $self->{possibility}[$idx] : $idx; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub check { |
40
|
6
|
|
|
6
|
1
|
8
|
my( $self, $guess, $result ) = @_; |
41
|
6
|
|
|
|
|
22
|
my $game = Games::Mastermind->new; |
42
|
6
|
|
|
|
|
150
|
my( $pegs, $holes, @new ) = ( $self->_pegs, $self->_holes ); |
43
|
|
|
|
|
|
|
|
44
|
6
|
100
|
|
|
|
40
|
foreach my $try ( @{$self->_possibility || [0 .. $self->remaining - 1]} ) { |
|
6
|
|
|
|
|
11
|
|
45
|
3570
|
|
|
|
|
6429
|
$game->code( [ _from_number( $try, $pegs, $holes ) ] ); |
46
|
3570
|
|
|
|
|
86355
|
my $try_res = $game->play( @$guess ); |
47
|
3570
|
100
|
100
|
|
|
200556
|
push @new, $try if $try_res->[0] == $result->[0] |
48
|
|
|
|
|
|
|
&& $try_res->[1] == $result->[1]; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
6
|
|
|
|
|
109
|
$self->{possibility} = \@new; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _from_number { |
55
|
3579
|
|
|
3579
|
|
5166
|
my( $number, $pegs, $holes ) = @_; |
56
|
3579
|
|
|
|
|
4490
|
my $peg_number = @$pegs; |
57
|
3579
|
|
|
|
|
5640
|
return map { my $peg = $number % $peg_number; |
|
14316
|
|
|
|
|
23796
|
|
58
|
14316
|
|
|
|
|
16956
|
$number = int( $number / $peg_number ); |
59
|
14316
|
|
|
|
|
34680
|
$pegs->[$peg] |
60
|
|
|
|
|
|
|
} ( 1 .. $holes ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
7
|
|
|
7
|
|
11
|
sub _peg_number { scalar @{$_[0]->game->pegs} } |
|
7
|
|
|
|
|
25
|
|
64
|
13
|
|
|
13
|
|
561
|
sub _pegs { $_[0]->game->pegs } |
65
|
19
|
|
|
19
|
|
935
|
sub _holes { $_[0]->game->holes } |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
1; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
__END__ |