2 array compare
#1
Hello , I would like to compare 2 arrays together . is in the first array
first_array ( 0 ) = "green"
first_array ( 1 ) = "green"
first_array (2) = "red"
first_array ( 3 ) = "yellow"

the 2 array contains e.g.
second_array ( 0 ) = "yellow"
second_array (1) = "red"
second_array (2) = "red"
second_array ( 3 ) = "yellow"

The result should be as follows .
0 = for no coincidence
1 = for color in second array but not in the right place
2 = right color and in the correct place .

who can help.

lorglas
Reply
#2
As the expected results have no logic, this is not possible.

0 ...What does coincidence mean in this context?

1 ... What is the right place?

2 ... What is the right color and what is the corect place?
Reply
#3
(03-26-2016, 01:29 PM)bbjimmy Wrote: As the expected results have no logic, this is not possible.

0 ...What does coincidence mean in this context?

1 ... What is the right place?

2 ... What is the right color and what is the corect place?

It sounds like he is writing a Mastermind clone: https://en.wikipedia.org/wiki/Mastermind_(board_game)
Reply
#4
We still need more information to help him.
Reply
#5
Yes, i write a mastermind clone.

The a problem is when through the array.
The following case queries are done:
0 = No match
2 = Complies

at 1 = I still have a problem. I just do not know how I can program a loop in which I remember where I was.

namely it is to compare the first array with the second, until he finds some of the first array in the second array.

So in my example would have at the third pass of the loop stop at red, show me something, then restart the last pass and show something.

Sorry for the englisch (translate with google)
Reply
#6
(03-26-2016, 10:43 PM)lorglas Wrote: Yes, i write a mastermind clone.

Not sure if this will help, but on my repo is a mm clone called slavemind, which has source included in the data/src directory. Perhaps seeing how they did it in C++ will help you do it in yab.

Also, the game has been recreated in BASIC before: https://www.google.com/search?q=mastermind%20basic Maybe you can grab some code from those examples.

Finally, there is an older version of the game called moo. Here it is implemented in perl:
Code:
#!/bin/env perl

=begin metadata

Name: moo
Description: play a game of MOO
Author: Abigail, perlpowertools@abigail.be
License: perl

=end metadata

=cut


use strict;

my ($VERSION) = '1.2';

# Print a usuage message on a unknown option.
$SIG {__WARN__} = sub {
    require File::Basename;
    $0 = File::Basename::basename ($0);
    if (substr ($_ [0], 0, 14) eq "Unknown option" ||
        substr ($_ [0], 0,  5) eq "Usage") {
        warn <<EOF;
$0 (Perl bin utils) $VERSION
$0 [size]
EOF
        exit 1;
    }
    else {
        warn "$0: @_";
    }
};

$SIG {__DIE__} = sub {
    require File::Basename;
    $0 = File::Basename::basename ($0);
    die "$0: @_";
};

my $size = 4;

warn "Usage" if @ARGV > 1;  # Will exit.

if (@ARGV) {
    $size = shift;
    warn "Usage" if !$size || $size =~ /\D/;  # Will exit;
}

print "MOO\n";
{
    my   @secret_by_value = (0) x 10;
    map {$secret_by_value [$_] ++} my @secret = map {int rand 10} 1 .. $size;

    my $attempts = 0;

    print "New game\n";

    {
        print "Your guess? ";
        chomp (my $guess = <>);

        exit if 'q' eq substr lc $guess, 0, 1;

        if ($guess =~ /\D/ || length $guess != $size) {
            print "Bad guess\n";
            redo
        }

        ++ $attempts;

        my @guess = split // => $guess;

        # Count the number of bulls and cows. We need a copy of
        # @secret_by_value for that.
        my $bulls = 0;
        my $cows  = 0;
        my @cows  = @secret_by_value;

        # We have to count the bulls before counting the cows.
        for (my $i = 0; $i < @guess; $i ++) {
            if ($secret [$i] == $guess [$i]) {
                $bulls ++;
                $cows [$guess [$i]] -- if $cows [$guess [$i]];
            }
        }

        for (my $i = 0; $i < @guess; $i ++) {
            next if $secret [$i] == $guess [$i]; # Counted the bulls already.
            if ($cows [$guess [$i]]) {
                $cows [$guess [$i]] --;
                $cows ++;
            }
        }

        print "Bulls = $bulls\tCows = $cows\n";

        if ($bulls == $size) {
            # Won the game!
            print "Attempts = $attempts\n";
            last;
        }

        redo;
    }

    redo;
}

__END__

=pod

=head1 NAME

moo - play a game of MOO

=head1 SYNOPSIS

moo [size]

=head1 DESCRIPTION

I<moo> is a game where the user has to guess a number choosen by
the computer. By default, the computer takes a number of four digits
(including 0's), but that can be changed by giving I<moo> the number of
digits to take.  After each guess, the number of B<bull>s and B<cow>s
is displayed.  A B<bull> is a correctly guessed digit, in the right
place, while a B<cow> is a correct digit, not in the right place. Once
a game has finished because all the digits have been guessed correctly,
a new game will be started. Exiting the program can be done by typing
'q' or 'Q' on a guess, or hitting the interrupt key (usually control-C).

=head2 OPTIONS

The only option I<moo> takes is optional, and is the number of digits to
use for the number to guess.

=head1 ENVIRONMENT

The working of I<moo> is not influenced by any environment variables.

=head1 BUGS

I<moo> does not have any known bugs.

=head1 AUTHOR

The Perl implementation of I<moo> was written by Abigail, I<perlpowertools@abigail.be>.

=head1 COPYRIGHT and LICENSE

This program is copyright by Abigail 1999.

This program is free and open software. You may use, copy, modify, distribute
and sell this program (and any modified variants) in any way you wish,
provided you do not restrict others to do the same.

=cut
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)
Free Web Hosting