wikipedia article on Set for convenience. It's a simple game, and I wrote a small script to find matches in a group of cards. My script finds all the combinations of 2 cards, finds the potential match for each combination, and looks to see if that match is in the group of cards. The problem is that each match will be found three times because it finds 2 cards which can match one card, and then it finds 2 other cards, which could both be part of the previous match, and would make the same match. Basically, my script thinks order matters but it doesnt, and making order unimportant requires *extra* work. Here is my script:
#!/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; } } }
Any other coding suggestions would also be helpful. To be clear, I can't figure out how I would check to see if a match was already found. But have another question about dereferencing the arrays of references. When I tried @$combo[0] it didn't work, but when I put $combo[0] into $first, and then used @$first, it worked, which seemed to me like the same thing, so I don't understand that either. Edit: BTW, some of the things I did served no purpose (my @altered_cards = @cards)

In reply to Solver for the game "Set" matches three times by skrapasor

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.