#!/usr/bin/perl use warnings; sub third { #this returns the the same number if the inputs #are the same, or if they are different it returns the #number that was not input (of the set {1,2,3}) my @numbers = @_; if ( $numbers[0] == $numbers[1] ) { return $numbers[0]; } else { if ( ($numbers[0] + $numbers[1]) == 3 ) { return 3; } elsif ( ( $numbers[0] + $numbers[1]) == 4 ) { return 2; } else { return 1; } } } sub find_match { #this returns the match of two cards in an array of the form #(a,b,c,d) of which a,b,c,d are each a number 1-3 to #represent shape, color, pattern, and number of shapes #on a card my @cards = @_; my @return; $return[0] = third($cards[0]->[0],$cards[1]->[0]); $return[1] = third($cards[0]->[1],$cards[1]->[1]); $return[2] = third($cards[0]->[2],$cards[1]->[2]); $return[3] = third($cards[0]->[3],$cards[1]->[3]); return @return; } sub combo { #this i stole from a perlmonks question to #find every combination of 2 cards in a group my $by = shift; return sub { () } if ! $by || $by =~ /\D/ || @_ < $by; my @list = @_; my @position = (0 .. $by - 2, $by - 2); my @stop = @list - $by .. $#list; my $end_pos = $#position; my $done = undef; return sub { return () if $done; my $cur = $end_pos; { if ( ++$position[ $cur ] > $stop[ $cur ] ) { $position[ --$cur ]++; redo if $position[ $cur ] > $stop[ $cur ]; my $new_pos = $position[ $cur ]; @position[ $cur .. $end_pos ] = $new_pos .. $new_pos + $by; } } $done = 1 if $position[0] == $stop[0]; return @list[ @position ]; } } #example cards @card1=(2,2,1,1); @card2=(2,2,3,3); @card3=(2,2,2,2); @card4=(2,2,3,2); #example group of cards @cards=(\@card1, \@card2, \@card3, \@card4); #$iter finds a subset of length 2 of @cards my $iter = combo( 2, @cards); #this loop loops through every combo of two cards #and looks for a potential match #then checks to see if the match exists in the group of #@cards #the problem occurs because each match is found thrice while ( my @combo = $iter->() ) { my @match = find_match(@combo); my $first = $combo[0]; my $second = $combo[1]; my @altered_cards = @cards; foreach $card (@cards) { if(join("",@match)==join("",@$card)){ print "Match =", @match, "\n"; print "Card =", @$card, "\n"; print "Match!\n"; @$first, "\n", @$second, "\n", @match, "\n"; last; } } }