#!/usr/local/bin/perl -w use strict; my $round = 0; my $colors; my @colors; my @result=(); $"="\n"; my $mm = MM->new(); while ($round < 10) { my $result; while (length $colors != 4){ print "Your 4 colors : "; $colors = ; chomp $colors; } @colors = split //, $colors; $result = $mm->check_allright(@colors); push @result, "$colors --> $result"; if ($result =~ /4:0/){ print "Yeah, you got it after $round rounds!\n CONGRATULATIONS\n"; exit; } print "@result\n"; $colors=''; $round++; } package MM; sub new { my $self = {}; my $class = shift; bless $self, $class; my @numbers_to_colors = qw{y b g r k w}; for my $i (0..3){ $self->{ORIGINAL}->{$i}=$numbers_to_colors[int rand(6)] } return $self; } sub check_allright { my $self = shift; $self->{guess}->{0} = shift; $self->{guess}->{1} = shift; $self->{guess}->{2} = shift; $self->{guess}->{3} = shift; my $count; $self->{COLOR_GUESS}=(); $self->{COLOR_ORIGINAL}=(); for my $i (0..3){ if ($self->{guess}->{$i} eq $self->{ORIGINAL}->{$i}) { $count++ ; } else { $self->{COLOR_GUESS}->{$self->{guess}->{$i}}++; $self->{COLOR_ORIGINAL}->{$self->{ORIGINAL}->{$i}}++; } } $count ||= 0; my $color= $self->check_color(); return "$count:$color"; } sub check_color { my $self = shift; my $count; map { $count += $self->{COLOR_GUESS}->{$_} < $self->{COLOR_ORIGINAL}->{$_} ? $self->{COLOR_GUESS}->{$_} : $self->{COLOR_ORIGINAL}->{$_} if exists $self->{COLOR_GUESS}->{$_}} keys %{$self->{COLOR_ORIGINAL}}; return $count || 0; } 1; #### ----------------------------------- --the good, the bad and the physi-- -----------------------------------