Jump to content

Playing cards: Difference between revisions

→‎{{header|REXX}}: added version 2. -- ~~~~
(→‎{{header|REXX}}: changed Rexx to REXX in the header definition. -- ~~~~)
(→‎{{header|REXX}}: added version 2. -- ~~~~)
Line 2,311:
 
=={{header|REXX}}==
===version 1===
<lang rexx>/* REXX ***************************************************************
* 1) Build ordered Card deck
Line 2,376 ⟶ 2,377:
DJ D7 C6 DA HJ CJ D8 SA D2 DK C5 HA H9 SK ST DQ C3 SQ D5 CQ D3 S7 C9 HT CA CK
S5 H3 S4 H5 S2 C2
</pre>
 
===version 2===
<lang rexx>/*REXX pgm shows methods (subroutines) to build/shuffle/deal a card deck*/
call buildDeck ; say ' new deck:' newDeck /*new 52-card deck*/
call shuffleDeck; say 'shuffled deck:' theDeck /*shuffled deck. */
call dealHands 5,4 /*5 cards, 4 hands*/
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 'remainder of deck:' theDeck
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────BUILDDECK subroutine────────────────*/
buildDeck: _=''; ranks='A 2 3 4 5 6 7 8 9 10 J Q K' /*ranks. */
if 1=='f1'x then suits='h d c s' /*EBCDIC?*/
else suits='♥ ♦ ♣ ♠' /*ASCII. */
do s=1 for words(suits)
do r=1 for words(ranks)
_=_ word(ranks,r)word(suits,s)
end /*dealR*/
end; /*dealS*/ newDeck=_
return
/*──────────────────────────────────SHUFFLEDECK subroutine──────────────*/
shuffleDeck: theDeck=''; _=newDeck; #cards=words(_)
do shuffler=1 for #cards /*shuffle all the cards in deck. */
r=random(1,#cards+1-shuffler) /*random # decreases each time. */
theDeck=theDeck word(_,r) /*sufffled deck, 1 card at-a-time*/
_=delword(_,r,1) /*delete the just-chosen card. */
end /*shuffler*/
return
/*──────────────────────────────────DEALHANDS subroutine────────────────*/
dealHands: parse arg numberOfCards,hands; hand.=''
do numberOfCards /*deal the hand to the players. */
do player=1 for hands /*deal a card to the players. */
hand.player=hand.player subword(theDeck,1,1) /*deal top card.*/
theDeck=subword(theDeck,2 ) /*diminish deck, remove one card.*/
end /*player*/
end /*numberOfCards*/
return</lang>
'''output'''
<pre style="overflow:scroll">
new deck: 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♣ 3♠ 5♥ J♣ K♦ 10♥ 9♣ Q♥ 2♣ 3♦ Q♠ A♠ 4♥ 8♠ K♠ 10♣ 8♦ 6♥ 7♦ 3♥ 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♥
 
[north] A♣ K♦ 2♣ 4♥ 8♦
 
[west] J♣ Q♥ A♠ 10♣ 3♥ [east] 3♠ 10♥ 3♦ 8♠ 6♥
 
[south] 5♥ 9♣ Q♠ K♠ 7♦
 
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♥
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.