Playing cards: Difference between revisions
Content deleted Content added
m →{{header|J}}: <nowiki> tags no longer required |
→{{header|J}}: define sayCards and DeckPrototype in parent class, simplify creation of new deck. |
||
Line 1,082: | Line 1,082: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:'''<br> |
|||
<lang j> |
<lang j>NB. playingcards.ijs |
||
This is a class script. |
|||
NB. Defines a Rosetta Code playing cards class |
|||
Multiple decks may be used, one for each instance of this class. |
NB. Multiple decks may be used, one for each instance of this class. |
||
⚫ | |||
coclass 'rcpc' NB. Rosetta Code playing cards class |
coclass 'rcpc' NB. Rosetta Code playing cards class |
||
NB. Class objects |
|||
⚫ | |||
n0=. > ;:'Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King' |
|||
n1=. > ;:'Spades Hearts Diamonds Clubs' |
|||
DeckPrototype=: |. ,/(i.#n0),."1 0 i.#n1 |
|||
⚫ | |||
⚫ | |||
) |
|||
NB. Class methods |
|||
⚫ | |||
destroy=: codestroy |
destroy=: codestroy |
||
⚫ | |||
startNewDeck=: 3 : 0 |
|||
⚫ | |||
1: TheDeck=: DeckPrototype |
1: TheDeck=: DeckPrototype |
||
) |
) |
||
shuffle=: |
shuffle=: monad define |
||
1: TheDeck=: TheDeck {~ ?~ # TheDeck |
1: TheDeck=: TheDeck {~ ?~ # TheDeck |
||
) |
) |
||
dealCards |
NB.*dealCards v Deals y cards [to x players] |
||
⚫ | |||
⚫ | |||
dealCards=: verb define |
|||
{. 1 dealCards y |
{. 1 dealCards y |
||
: |
: |
||
Line 1,114: | Line 1,118: | ||
TheDeck =: ToBeDealt }. TheDeck |
TheDeck =: ToBeDealt }. TheDeck |
||
(1 0 2)|:(y,x)$ CardsOffTop |
(1 0 2)|:(y,x)$ CardsOffTop |
||
) |
|||
Note 'dealCards' |
|||
⚫ | |||
Right parameter (y) is number of cards to be dealt to each player. |
|||
⚫ | |||
) |
) |
||
pcc |
NB.*pcc v "Print" current contents of the deck. |
||
pcc=: monad define |
|||
sayCards TheDeck |
sayCards TheDeck |
||
⚫ | |||
)</lang> |
|||
⚫ | |||
newDeck_z_=: conew&'rcpc'</lang> |
|||
⚫ | |||
load 'c:\documents and settings\user_name\j602-user\playingcards.ijs' |
|||
⚫ | |||
⚫ | |||
⚫ | |||
coinsert 'rcpc' NB. inserts rcpc class in the path of current locale |
|||
⚫ | |||
$TheDeck__pc |
$TheDeck__pc |
||
52 2 |
52 2 |
||
shuffle__pc '' |
shuffle__pc '' |
||
1 |
1 |
||
sayCards 2 dealCards__pc 5 NB. deal two hands of five cards |
|||
Nine of Hearts |
Nine of Hearts |
||
Three of Clubs |
Three of Clubs |