Playing cards: Difference between revisions

→‎version 2: changed whitespace and comments, uses simpler variable names, optimized the dealing & shuffling.
m (→‎{{header|Julia}}: just added a small bit of polish)
(→‎version 2: changed whitespace and comments, uses simpler variable names, optimized the dealing & shuffling.)
Line 3,470:
 
===version 2===
<lang rexx>/*REXX pgm shows methodsa (subroutines)method to build/shuffle/deal a cardstandard 52─card deck.*/
callbox buildDeck= build(); say ' box of new deckcards:' newDeck box /*a new 52-cardbox of deck52─cards.*/
call shuffleDeckdeck=shuffle(); say 'shuffled deck:' theDeck deck /*randomly shuffled deck. */
call deal 5, 4 /* ◄═════════════════════════════════ 5 cards, 4 hands*/
call dealHands 5,4 /*5 cards, 4 hands*/
say; say; say right('[north]' hand.1,50)
say; say '[west]' hand.4 right('[east]' hand.2,60)
say; say right('[south]' hand.3,50)
say; say; say; say 'remainder of deck: ' theDeckdeck
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────BUILD subroutine────────────────────*/
/*──────────────────────────────────BUILDDECK subroutine────────────────*/
buildDeckbuild: _=''; ranks=' "A 2 3 4 5 6 7 8 9 10 J Q K'" /*ranks. */
if 15=='f1f5'x then suits=' "h d c s' " /*EBCDIC? */
else suits=' "♥ ♦ ♣ ♠' " /*ASCII. */
#ranks=words(ranks); do s=1 for words(suits); @=word(suits,s)
do r=1 for words(#ranks)
_=_ word(ranks,r)word(suits,s)@
end /*dealRs*/
end end; /*dealSr*/ newDeck=_
return _
/*──────────────────────────────────SHUFFLE subroutine──────────────────*/
/*──────────────────────────────────SHUFFLEDECK subroutine──────────────*/
shuffleDeckshuffle: theDeck=''; _ y=newDeck; _=box; #cards=words(_) /*define REXX vars.*/
do shuffler=1 for #cards /*shuffle all the cards in deck. */
r?=random(1,#cards+1-shuffler) /*each shuffle, random # decreases each time. */
theDecky=theDecky word(_,r ?) /*sufffledshuffled deck, 1 card at-a-timeat─a─time*/
_=delword(_,r ?, 1) /*delete the just-chosen card.just─chosen card. */
end /*shuffler*/
return y
/*──────────────────────────────────DEAL subroutine─────────────────────*/
/*──────────────────────────────────DEALHANDS subroutine────────────────*/
dealHandsdeal: parse arg numberOfCards#cards, hands; hand.=''
do numberOfCards #cards /*deal the hand to the players. */
do player=1 for hands /*deal asome cardcards to the players. */
hand.player=hand.player subword word(theDeck,1deck, 1) /*deal top card.*/
theDeck deck=subword(theDeckdeck,2 2) /*diminish deck, remove one card.*/
end /*player*/
end /*numberOfCards#cards*/
return</lang>
'''output'''
<pre>
box of new deckcards: A♥ 2♥ 3♥ 4♥ 5♥ 6♥ 7♥ 8♥ 9♥ 10♥ J♥ Q♥ K♥ A♦ 2♦ 3♦ 4♦ 5♦ 6♦ 7♦ 8♦ 9♦ 10♦ J♦ Q♦ K♦ A♣ 2♣ 3♣ 4♣ 5♣ 6♣ 7♣ 8♣ 9♣ 10♣ J♣ Q♣ K♣ A♠ 2♠ 3♠ 4♠ 5♠ 6♠ 7♠ 8♠ 9♠ 10♠ J♠ Q♠ K♠
shuffled deck: A♣7♦ 3♠2♥ 5♥A♥ J♣J♥ 7♣ 4♠ K♦ 10♥5♣ 9♣6♣ Q♥Q♣ 2♣Q♦ 3♦5♠ Q♠ A♠9♠ 4♥K♥ 8♠7♥ K♠ 10♣3♠ 8♦3♣ 6♥10♦ 7♦10♠ 3♥K♣ 3♣7♠ Q♣3♦ 9♦5♥ 9♥6♦ J♠J♦ A♥A♠ Q♦6♠ 4♦8♠ J♦10♣ 5♠8♣ 10♠10♥ 4♠J♠ 9♠Q♥ A♦2♣ 2♦6♥ 2♥A♦ 6♠4♦ 7♣ K♥2♦ 8♥ 6♦ 8♣3♥ 4♣ 5♦A♣ 7♥J♣ 6♣2♠ 7♠5♦ K♣8♦ 5♣9♦ 2♠9♣ 10♦9♥ J♥4♥
 
 
[north] A♣7♦ K♦7♣ 2♣6♣ 4♥Q♠ 8♦K♠
 
[west] J♣J♥ Q♥5♣ A♠5♠ 10♣7♥ 10♦ 3♥ [east] 3♠2♥ 10♥4♠ 3♦Q♣ 8♠9♠ 6♥3♠
 
[south] 5♥A♥ 9♣K♦ Q♠Q♦ K♠K♥ 7♦3♣
 
 
 
remainder of deck: 3♣ Q♣ 9♦ 9♥ J♠ A♥ Q♦ 4♦ J♦ 5♠ 10♠ 4♠ 9♠ A♦ 2♦ 2♥ 6♠ 7♣ K♥ 8♥ 6♦ 8♣ 4♣ 5♦ 7♥ 6♣ 7♠ K♣ 5♣ 2♠ 10♦ J♥
remainder of deck: 10♠ K♣ 7♠ 3♦ 5♥ 6♦ J♦ A♠ 6♠ 8♠ 10♣ 8♣ 10♥ J♠ Q♥ 2♣ 6♥ A♦ 4♦ 2♦ 8♥ 3♥ 4♣ A♣ J♣ 2♠ 5♦ 8♦ 9♦ 9♣ 9♥ 4♥
</pre>