Playing cards: Difference between revisions
Content added Content deleted
(Added Erlang) |
(→{{header|Python}}: Add another example that uses Poker hand analyser#Python,) |
||
Line 2,754: | Line 2,754: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Python 2.x, standalone=== |
|||
<lang python>import random |
<lang python>import random |
||
Line 2,780: | Line 2,781: | ||
self.shuffle() # Can't tell what is next from self.deck |
self.shuffle() # Can't tell what is next from self.deck |
||
return self.deck.pop(0)</lang> |
return self.deck.pop(0)</lang> |
||
===Python 3: extending [[Poker hand analyser#Python]]=== |
|||
Assume the code from [[Poker hand analyser#Python]] is in a file pokerhand.py and importable. |
|||
<lang python>from pokerhand import Card, suit, face |
|||
from itertools import product |
|||
from random import randrange |
|||
class Deck(): |
|||
def __init__(self): |
|||
self.__deck = [Card(f, s) for f,s in product(face, suit)] |
|||
def __repr__(self): |
|||
return 'Deck of ' + ' '.join(repr(card) for card in self.__deck) |
|||
def shuffle(self): |
|||
pass |
|||
def deal(self): |
|||
return self.__deck.pop(randrange(len(self.__deck))) |
|||
if __name__ == '__main__': |
|||
deck = Deck() |
|||
print('40 cards from a deck:\n') |
|||
for i in range(5): |
|||
for j in range(8): |
|||
print(deck.deal(), end=' ') |
|||
print() |
|||
print('\nThe remaining cards are a', deck)</lang> |
|||
{{out}} |
|||
<pre>40 cards from a deck: |
|||
7♥ k♥ 2♣ 3♦ 10♥ 10♠ 6♥ 9♠ |
|||
9♣ k♦ 3♥ k♠ q♥ q♠ a♣ a♥ |
|||
10♣ 5♣ 8♥ 4♣ 9♥ 8♠ a♠ 5♠ |
|||
9♦ 4♥ j♦ j♣ 3♣ 7♠ 5♥ 2♥ |
|||
6♠ q♣ 7♦ 3♠ 7♣ k♣ 10♦ 6♣ |
|||
The remaining cards are a Deck of 2♦ 2♠ 4♦ 4♠ 5♦ 6♦ 8♦ 8♣ j♥ j♠ q♦ a♦</pre> |
|||
=={{header|R}}== |
=={{header|R}}== |