Even though there are some other shorter versions, I have to show you my version here, cause I spend a few sleeping-hours on it ;^)
I use a package for the Color and Position stuff.

#!/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 = <STDIN>; 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}->{$_} i +f exists $self->{COLOR_GUESS}->{$_}} keys %{$self->{COLOR_ORIGINAL}}; return $count || 0; } 1;
And the good thing: It works *g*.
----------------------------------- --the good, the bad and the physi-- -----------------------------------

In reply to Re: Re: Need help with a Mastermind game by physi
in thread Need help with a Mastermind game by PsionicMan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.