Algorithm-MasterMind

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

 6.16   0.744 11.625 138572   0.0000 0.0001  Algorithm::MasterMind::matches
 5.06   0.611 10.881 499779   0.0000 0.0000  Algorithm::MasterMind::check_rule
 0.58   0.070  0.090      1   0.0699 0.0897  YAML::Type::code::BEGIN
 0.50   0.060  0.389      7   0.0085 0.0555  main::BEGIN
 0.49   0.059  0.195  40252   0.0000 0.0000  YAML::Base::__ANON__
 0.49   0.059  0.059    454   0.0001 0.0001  Params::Validate::_validate
 0.33   0.040  0.192      7   0.0057 0.0274  DateTime::Locale::BEGIN
 0.31   0.038  0.097    449   0.0001 0.0002  DateTime::Locale::_register
 0.30   0.036  0.065   5404   0.0000 0.0000  overload::AddrRef
 0.24   0.029  0.029    322   0.0001 0.0001  Params::Validate::_validate_pos
 0.22   0.026  0.026   5404   0.0000 0.0000  Scalar::Util::blessed
 0.18   0.022  0.029   2335   0.0000 0.0000  YAML::Dumper::_emit_str
 0.17   0.021  0.086   2235   0.0000 0.0000  YAML::Dumper::_emit_node
	

2009-10-03    <jmerelo@localhost.localdomain>

	* t/00_functions.t: Fixed typo in test

2009-10-02    <jmerelo@localhost.localdomain>

lib/Algorithm/MasterMind.pm  view on Meta::CPAN


sub new {

  my $class = shift;
  my $options = shift || croak "Need options here in Algorithm::MasterMind::New\n";

  my $self =  { _rules => [],
		_evaluated => 0,
		_hash_rules => {} };

  bless $self, $class;
  $self->initialize( $options );
  return $self;
}

sub random_combination {
  my $self = shift;
  return random_string( $self->{'_alphabet'}, $self->{'_length'});
}

sub random_string {

lib/Algorithm/MasterMind.pm  view on Meta::CPAN

sub issue_first { #Default implementation
  my $self = shift;
  return $self->{'_last'} = $self->random_combination;
}

sub start_from {
  my $class = shift;
  my $options = shift || croak "Options needed to start!";

  my $self = {};
  bless $self, $class;
  for my $o ( qw( consistent alphabet rules evaluated ) ) {
    $self->{"_$o"} = $options->{$o};
  }
  return $self;
}

sub issue_first_Knuth {
  my $self = shift;
  my $string;
  my @alphabet = @{ $self->{'_alphabet'}};

lib/Algorithm/MasterMind/Consistent_Set.pm  view on Meta::CPAN


use Algorithm::MasterMind qw(partitions);
use Algorithm::MasterMind::Secret;

sub new {
  my $class = shift;
  my $combinations = shift;
  my @secrets = map ( (new Algorithm::MasterMind::Secret $_), @$combinations );
  my $self = {  _combinations => \@secrets,
		_partitions => {}};
  bless $self, $class;
  $self->{'_partitions'} = compute_partitions( \@secrets );
  $self->{'_score'} = {}; # To store scores when they're available.
  return $self;
}

sub compute_partitions {
  my $secrets_ref = shift;
  my @secrets = @$secrets_ref;
  my %partitions;
  my %hash_results;

lib/Algorithm/MasterMind/Consistent_Set.pm  view on Meta::CPAN

  return \%partitions
}

sub create_consistent_with {
  my $class = shift;
  my $combinations = shift;
  my $rules = shift;
  my @secrets = map ( (new Algorithm::MasterMind::Secret $_), @$combinations );
  my $self = {  _combinations => [],
		_partitions => {}};
  bless $self, $class;
  my %rule_secrets;
  map( ($rule_secrets{$_->{'combination'}} = new Algorithm::MasterMind::Secret $_->{'combination'}),
       @$rules );
  for my $s (@secrets ) {
    my $matches;
    for my $r (@$rules ) {
      my $this_result = { blacks => 0,
			  whites => 0 };
      $s->check_secret( $rule_secrets{$r->{'combination'}}, $this_result);
      $matches +=  result_as_string( $this_result ) eq result_as_string( $r );

lib/Algorithm/MasterMind/Secret.pm  view on Meta::CPAN

  my $string = shift || croak "No default string\n";

  my $self =  { _string => $string,
		_chars => [],
		_hash => {}};
  while ( my $c = chop( $string ) ) {
    push @{$self->{'_chars'}}, $c;
    $self->{'_hash'}{ $c }++;
  }
  @{$self->{'_alphabet'}} = keys %{$self->{'_hash'}};
  bless $self, $class;
  return $self;
}

sub string {
  return shift->{'_string'};
}

sub check {
  my %hash_secret = %{$_[0]->{'_hash'}};
  my %hash_string ;



( run in 0.894 second using v1.01-cache-2.11-cpan-de7293f3b23 )