Playing cards: Difference between revisions
→{{header|J}}: define sayCards and DeckPrototype in parent class, simplify creation of new deck.
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:
=={{header|J}}==
'''Solution:'''<br>
<lang j>
NB. Defines a Rosetta Code playing cards class
NB. Multiple decks may be used, one for each instance of this class.
)▼
coclass 'rcpc' NB. Rosetta Code playing cards class
NB. Class objects
create=: 3 : 0▼
sayCards=: ((N0{~{.),' of ',(N1{~{:))"1▼
startNewDeck ''▼
NB. Class methods
destroy=: codestroy
1: TheDeck=: DeckPrototype
)
shuffle=:
1: TheDeck=: TheDeck {~ ?~ # TheDeck
)
NB.*dealCards
NB. Used monadically, the player-axis is omitted from output.▼
dealCards=: verb define
{. 1 dealCards y
:
Line 1,114 ⟶ 1,118:
TheDeck =: ToBeDealt }. TheDeck
(1 0 2)|:(y,x)$ CardsOffTop
▲Left parameter (x) is number of players, with default to one.
▲Used monadically, the player-axis is omitted from output.
)
NB.*pcc
pcc=: monad define
sayCards TheDeck
▲)
Example use:▼
newDeck_z_=: conew&'rcpc'</lang>
<lang j> load 'coutil'▼
▲'''Example use:'''
pc=: '' conew 'rcpc'▼
coinsert 'rcpc' NB. inserts rcpc class in the path of current locale
$TheDeck__pc
52 2
shuffle__pc ''
1
Nine of Hearts
Three of Clubs
|