Set puzzle: Difference between revisions

m (→‎{{header|Perl 6}}: fixed deck creation & display of results)
Line 2,271:
red three oval striped
green three diamond solid</pre>
 
=={{header|Phix}}==
Converts cards 1..81 (that idea from C) to 1/2/4 [/7] (that idea from Perl) but inverts the validation
<lang Phix>function comb(sequence pool, integer needed, sequence res={}, integer done=0, sequence chosen={})
if needed=0 then -- got a full set
sequence {a,b,c} = chosen
if not find_any({3,5,6},flatten(sq_or_bits(sq_or_bits(a,b),c))) then
res = append(res,chosen)
end if
elsif done+needed<=length(pool) then
-- get all combinations with and without the next item:
done += 1
res = comb(pool,needed-1,res,done,append(chosen,pool[done]))
res = comb(pool,needed,res,done,chosen)
end if
return res
end function
 
constant m124 = {1,2,4}
 
function card(integer n)
--returns the nth card (n is 1..81, res is length 4 of 1/2/4)
n -= 1
sequence res = repeat(0,4)
for i=1 to 4 do
res[i] = m124[remainder(n,3)+1]
n = floor(n/3)
end for
return res
end function
 
constant colours = {"red", "green", 0, "purple"},
symbols = {"oval", "squiggle", 0, "diamond"},
numbers = {"one", "two", 0, "three"},
shades = {"solid", "open", 0, "striped"}
 
procedure print_cards(sequence hand, sequence cards)
for i=1 to length(cards) do
integer {c,m,n,g} = cards[i],
id = find(cards[i],hand)
printf(1,"%3d: %-7s %-9s %-6s %s\n",{id,colours[c],symbols[m],numbers[n],shades[g]})
end for
printf(1,"\n")
end procedure
 
procedure play(integer cards=9, integer sets=4)
integer deals = 1
while 1 do
sequence deck = shuffle(tagset(81))
sequence hand = deck[1..cards]
for i=1 to length(hand) do
hand[i] = card(hand[i])
end for
sequence res = comb(hand,3)
if length(res)=sets then
printf(1,"dealt %d cards (%d deals)\n",{cards,deals})
print_cards(hand,hand)
printf(1,"with %d sets\n",{sets})
for i=1 to sets do
print_cards(hand,res[i])
end for
exit
end if
deals += 1
end while
end procedure
play()
--play(12,6)
--play(9,6)</lang>
{{out}}
<pre>
dealt 9 cards (172 deals)
1: red oval two open
2: green oval one solid
3: purple diamond two striped
4: green diamond one striped
5: green oval one striped
6: purple squiggle three solid
7: green diamond two solid
8: red diamond two open
9: green squiggle one striped
 
with 4 sets
1: red oval two open
4: green diamond one striped
6: purple squiggle three solid
 
3: purple diamond two striped
7: green diamond two solid
8: red diamond two open
 
4: green diamond one striped
5: green oval one striped
9: green squiggle one striped
 
5: green oval one striped
6: purple squiggle three solid
8: red diamond two open
</pre>
 
=={{header|Python}}==
7,794

edits