Advertisement
rfmonk

random_shuffle.py

Jan 23rd, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import random
  5. import itertools
  6.  
  7. FACE_CARDS = ('J', 'Q', 'K', 'A')
  8. SUITS = ('H', 'D', 'C', 'S')
  9.  
  10.  
  11. def new_deck():
  12.     return list(itertools.product(
  13.         itertools.chain(xrange(2, 11), FACE_CARDS),
  14.         SUITS,
  15.     ))
  16.  
  17.  
  18. def show_deck(deck):
  19.     p_deck = deck[:]
  20.     while p_deck:
  21.         row = p_deck[:13]
  22.         p_deck = p_deck[13:]
  23.         for j in row:
  24.             print '%2s%s' % j,
  25.         print
  26.  
  27. # Make a new deck, with the cards in order
  28. deck = new_deck()
  29. print 'Initial deck:'
  30. show_deck(deck)
  31.  
  32. # Shuffle the deck to randomize the order
  33. random.shuffle(deck)
  34. print '\nShuffled deck:'
  35. show_deck(deck)
  36.  
  37. # Deal 4 hands of 5 cards each
  38. hands = [[], [], [], []]
  39.  
  40. for i in xrange(5):
  41.     for h in hands:
  42.         h.append(deck.pop())
  43.  
  44. # Show the hands
  45. print '\nHands:'
  46. for n, h in enumerate(hands):
  47.     print '%d:' % (n + 1),
  48.     for c in h:
  49.         print '%2s%s' % c,
  50. print
  51.  
  52. # Show the remaining deck
  53. print '\nRemianing deck:'
  54. show_deck(deck)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement