Here is a cute trick that you might want to contemplate.
If you make each card a 4-"digit" octal number, then three cards form a "set" if and only if ($a|$b|$c) equals ($a^$b^$c). You can use octal strings or octal numbers. I use octal strings below:
#!/usr/bin/perl -w use strict; my %Desc= qw( 0001 One 0002 Two 0004 Three 0010 Red 0020 Blue 0040 Green 0100 Hollow 0200 Striped 0400 Solid 1000 Diamond 2000 Squiggle 4000 Oval ); my @deck; for my $a ( 1,2,4 ) { for my $b ( 1,2,4 ) { for my $c ( 1,2,4 ) { for my $d ( 1,2,4 ) { push @deck, "$a$b$c$d"; } } } } sub isSet { my( $a, $b, $c )= @_; return ($a|$b|$c) eq ($a^$b^$c); } sub showCard { my( $card )= @_; my @desc; for my $bit ( sort keys %Desc ) { if( $bit eq ( $bit & $card ) ) { push @desc, $Desc{$bit}; } } return "@desc"; } while( 1 ) { for my $n ( reverse @deck-2..@deck ) { my $r = rand $n; @deck[$n-1,$r]= @deck[$r,$n-1]; } if( isSet( @deck[-3..-1] ) ) { print showCard( $_ ), $/ for @deck[-3..-1]; exit; } }
Update: Replaced nested maps with clearer, shorter for()s.
- tye
In reply to Re: Solver for the game "Set" matches three times (octal)
by tye
in thread Solver for the game "Set" matches three times
by skrapasor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |