Set puzzle: Difference between revisions

1,274 bytes added ,  9 years ago
→‎{{header|REXX}}: re-read the requirements, found a hand with 4 sets, then showed the dealt hand.
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
(→‎{{header|REXX}}: re-read the requirements, found a hand with 4 sets, then showed the dealt hand.)
Line 1,849:
<lang rexx>/*REXX program finds "sets" (solutions) for the SET puzzle (game). */
parse arg game seed . /*get optional # cards to deal. */
if game ==',' | game=='' then game=9 /*Not specified? Then use default*/
if seed\==',' & | seed\=='' then callseed=77 random/* ,,seed" /*for repeatability " " " " */
call genFeaturesaGame 0 /*generatewith varioustell=0, card featuressuppress output. */
call genDeckaGame 1 /*generatewith atell=1, allow deck (with 81output. cards)*/
call dealer game /*deal a number of cards. */
call findSets game%2 /*find sets from the dealt cards.*/
exit sets /*stick a fork in it, we're done.*/
/*──────────────────────────────────AGAME subroutine────────────────────*/
aGame: tell=arg(1); good=game%2 /*enable or disable the output. */
/* [↑] GOOD is the right # sets.*/
do seed=seed until good==sets /*generate deals until good# sets*/
call random ,,seed /*repeatability for last invoke. */
call genFeatures /*generate various card features.*/
call genDeck /*generate a deck (with 81 cards)*/
call dealer game call dealer game /*deal a number of cards (game). */
call findSets game%2 call findSets game%2 /*find sets from the dealt cards.*/
end /*until*/ /*when leaving, SETS is right num*/
return /*return to invoker of this sub. */
/*──────────────────────────────────DEALER subroutine───────────────────*/
dealer: saycall sey 'dealing' game "cards:"; say ,,. /*shuffle and deal cards*/
do cards=1 until cards==game /*keep dealing 'til done*/
_=random(1,words(##)); ##=delword(##,_,1) /*pick card; delete it. */
@.cards=deck._ /*add it to the tableau.*/
saycall sey right('card' cards,1630) " " @.cards /*display card to screen*/
do j=1 for words(@.cards) /*define cells for card.*/
@.cards.j=word(@.cards,j) /*define a cell for card*/
Line 1,869 ⟶ 1,878:
/*──────────────────────────────────DEFFEATURES subroutine──────────────*/
defFeatures: parse arg what,v; _=words(v) /*obtain what to define.*/
if _\==values then do; call say sey 'error,' what "features ¬=" values,.,.
exit -1
end /* [↑] check for typos.*/
do k=1 for words(values) /*define all possibles. */
Line 1,907 ⟶ 1,916:
/*──────────────────────────────────FINDSETS subroutine─────────────────*/
findSets: parse arg n; call genPoss /*N: the number of sets to find.*/
say call sey /*find any sets generated above. */
do j=1 for p /*P is the # of possible sets. */
do f=1 for features
Line 1,929 ⟶ 1,938:
if \ok then iterate /*Is this set OK? Nope, skip it.*/
sets=sets+1 /*bump the number of sets found. */
saycall sey right('set' sets": ",15) !.j.1 sep !.j.2 sep !.j.3
end /*j*/
 
say;call sey say sets 'sets found.',.
return</lang>
/*──────────────────────────────────SEY subroutine──────────────────────*/
sey: if \tell then return /*should output be suppressed? */
if arg(2)==. then say; say arg(1); if arg(3)==. then say; return</lang>
'''outpout''' when using the default input:
<pre>
dealing 9 cards:
 
card 1 one two redgreen squiggleoval solidopen
card 2 two three greenpurple diamondsquiggle striped
card 3 one purplegreen diamond stripedsolid
card 4 onethree red ovaldiamond open
card 5 two one redpurple ovalsquiggle openstriped
card 6 two three greenpurple diamondoval striped
card 7 two three greenpurple diamond striped
card 8 three one purplered ovalsquiggle open
card 9 two purplered diamondoval solid
 
set 41: threetwo greenpurple diamondsquiggle striped ───── threetwo greenpurple diamondoval striped ───── threetwo greenpurple diamond striped
set 2: twoone redgreen squigglediamond solid ───── three greenred diamond stripedopen ───── onetwo purple ovaldiamond openstriped
set 3: twoone redgreen squigglediamond solid ───── threetwo greenpurple diamondoval striped ───── onethree purplered ovalsquiggle open
set 14: two redpurple squiggle solidstriped ───── threetwo greenpurple diamondoval striped ───── onetwo purple ovaldiamond openstriped
 
4 sets found.
set 1: two red squiggle solid ───── three green diamond striped ───── one purple oval open
set 2: two red squiggle solid ───── three green diamond striped ───── one purple oval open
set 3: two red squiggle solid ───── three green diamond striped ───── one purple oval open
set 4: three green diamond striped ───── three green diamond striped ───── three green diamond striped
</pre>