Advertisement
here2share

# b_CrazyEights.py

Apr 24th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.04 KB | None | 0 0
  1. # b_CrazyEights.py
  2.  
  3. import random, os
  4.  
  5. class Cards():
  6.     '''suit, rank, value, short_name, long_name'''
  7.     oSuit = ('Diamonds','Hearts','Spades','Clubs')
  8.     suit_dict = dict(zip('dhsc', oSuit))
  9. cv = Cards()
  10.  
  11. def init_cards():
  12.     oDeck = []
  13.     oRank = {1:'Ace',11:'Jack',12:'Queen',13:'King'}
  14.     card = {}
  15.     for suit_id in cv.oSuit:
  16.         for rank_id in range(1, 14):
  17.             if rank_id in oRank:
  18.                 rank = oRank[rank_id]
  19.                 value = 10
  20.                 if rank_id == 1:
  21.                     value = 1
  22.             else:
  23.                 rank = str(rank_id)
  24.                 value = rank_id
  25.             if rank == 8:
  26.                 value = 50
  27.             short_name = rank[0] + suit_id[0]
  28.             if rank == '10':
  29.                 short_name = rank + suit_id[0]
  30.             card[short_name] = Cards()
  31.             obj = card[short_name]
  32.             obj.short_name = short_name
  33.             obj.long_name = rank + ' of ' + suit_id
  34.             obj.suit = suit_id
  35.             obj.value = value
  36.             obj.rank = rank
  37.             oDeck.append(obj)
  38.     return oDeck
  39.  
  40. def clr():
  41.     os.system('cls||clear')
  42.     print 'Points In Total... \n\t\t You: %i \t\t Computer: %i \n' % (p_total, c_total)
  43.     print 'Cards In The Deck: %i \n' % (len(deck))
  44.    
  45. def get_new_suit():
  46.     while 1:
  47.         suit = raw_input('Pick A Suit: ').lower()
  48.         if suit in cv.suit_dict:
  49.             active_suit = cv.suit_dict[suit]
  50.             break
  51.         else:
  52.             print 'Invalid Entry... Try Again'
  53.     cv.active_suit = active_suit
  54.  
  55. def computer_turn():
  56.     options = []
  57.     an_eight = False
  58.     for card in c_hand:
  59.         if card.rank == '8' and len(c_hand) > 1:
  60.             an_eight = card
  61.             if len(c_hand) == 2:
  62.                 options = 0
  63.                 break
  64.         else:
  65.             if card.suit == cv.active_suit:
  66.                 options.append(card)
  67.             elif card.rank == cv.upcard.rank:
  68.                 options.append(card)
  69.     if options:
  70.         strategy = options[0]
  71.         for card in options:
  72.             if card.value > strategy.value:
  73.                 strategy = card
  74.         c_hand.remove(strategy)
  75.         cv.upcard = strategy
  76.         cv.active_suit = cv.upcard.suit
  77.         print '  Computer Played %s (%s)' % (strategy.long_name, strategy.short_name)
  78.         if strategy.rank == '2' and deck:
  79.             print '  -- What You Had To Pick Up: ',
  80.             for _ in 'zz':
  81.                 hand_card = deck.pop()
  82.                 print hand_card.short_name,
  83.                 p_hand.append(hand_card)
  84.             print
  85.     elif an_eight:
  86.         suit_total = {}
  87.         for card in c_hand:
  88.             if card.rank != '8':
  89.                 try:
  90.                     suit_total[card.suit] += 1
  91.                 except:
  92.                     suit_total[card.suit] = 0
  93.         strategy = max(suit_total, key=suit_total.get)
  94.         c_hand.remove(an_eight)
  95.         cv.upcard = an_eight
  96.         cv.active_suit = strategy
  97.         print '  Computer Played %s (%s)' % (an_eight.long_name, an_eight.short_name)
  98.         print '  -- Changed Suit To %s --' % (strategy)
  99.     else:
  100.         if deck:
  101.             c_hand.append(deck.pop())
  102.             print '  Computer Drew A Card'
  103.         else:
  104.             print '  Computer Is Blocked'
  105.             cv.blocked += 1
  106.     print '  -- Computer Cards Held: %i --' % (len(c_hand))
  107.  
  108. def player_turn():
  109.     valid_play = False
  110.     an_eight = False
  111.     print '\nYour Hand: ',
  112.     for card in p_hand:
  113.         print card.short_name,
  114.     print
  115.     print '   Up Card: %s [---%s---]' % (cv.upcard.long_name, cv.upcard.short_name)
  116.     if cv.upcard.rank == '8':
  117.         print '  *** Suit Is %s ***' % (cv.active_suit)
  118.     response = raw_input("\nType A Card To Play Or Type 'Z' To Take A Card: ").lower()
  119.     while not valid_play:
  120.         selected_card = None
  121.         while selected_card == None:
  122.             if response == 'z':
  123.                 valid_play = True
  124.                 if deck:
  125.                     p_hand.append(deck.pop())
  126.                     clr()
  127.                     print 'You Drew: %s (%s)' % (card.long_name, card.short_name)
  128.                 else:
  129.                     print 'There Are No Cards Left In The Deck'
  130.                     cv.blocked += 1
  131.                 return
  132.             else:
  133.                 for card in p_hand:
  134.                     if response.upper() == card.short_name:
  135.                         selected_card = card
  136.                 if selected_card == None:
  137.                     response = raw_input("You Don't Have That Card. Try Again: ")
  138.         if selected_card.rank == '8':
  139.             an_eight = True
  140.             if len(p_hand) > 1:
  141.                 valid_play = True
  142.                 get_new_suit()
  143.         elif selected_card.suit == cv.active_suit:
  144.             valid_play = True
  145.         elif selected_card.rank == cv.upcard.rank:
  146.             valid_play = True
  147.  
  148.         if valid_play:
  149.             p_hand.remove(selected_card)
  150.             cv.upcard = selected_card
  151.             if not an_eight:
  152.                 cv.active_suit = cv.upcard.suit
  153.             clr()
  154.             print 'You Played %s (%s)' % (selected_card.long_name, selected_card.short_name)
  155.             if cv.upcard.rank == '8':
  156.                 print '  *** Suit Called: %s ***' % (cv.active_suit)
  157.             elif cv.upcard.rank == '2' and deck:
  158.                 print '  -- Computer Has To Pick Up 2 Cards From The Deck As Available -- '
  159.                 for _ in 'zz':
  160.                     hand_card = deck.pop()
  161.                     c_hand.append(hand_card)
  162.         if not valid_play:
  163.             if an_eight:
  164.                 print 'Wildcards Can Not Be Played Last...',
  165.             else:
  166.                 print 'That Card Does Not Relate...',
  167.             response = raw_input("Try Again: ")
  168.  
  169. p_total = c_total = 0
  170. card_value = {}
  171. card_suit = {}
  172. long_name = {}
  173. fulldeck = init_cards()
  174.  
  175. done = False
  176. game_done = False
  177. while not done:
  178.     p_hand = []
  179.     c_hand = []
  180.     deck = fulldeck[:]
  181.     random.shuffle(deck)
  182.     for _ in range(7):
  183.         hand_card = deck.pop()
  184.         p_hand.append(hand_card)
  185.     for _ in range(7):
  186.         hand_card = deck.pop()
  187.         c_hand.append(hand_card)
  188.     cv.blocked = 0
  189.     cv.upcard = deck.pop()
  190.     cv.active_suit = cv.upcard.suit
  191.     clr()
  192.     while not game_done:
  193.         player_turn()
  194.         if not p_hand:
  195.             game_done = True
  196.             p_points = 0
  197.             for card in c_hand:
  198.                 p_points += card.value
  199.             p_total += p_points
  200.             clr()
  201.             print 'You Won!'
  202.             print "You Got %i Points For Computer's Hand" % p_points
  203.         else:
  204.             computer_turn()
  205.         if not c_hand:
  206.             game_done = True
  207.             c_points = 0
  208.             for card in p_hand:
  209.                 c_points += card.value
  210.             c_total += c_points
  211.             clr()
  212.             print 'Computer Won!'
  213.             print 'Computer Got %i Points For Your Hand' % c_points
  214.         if cv.blocked >= 2:
  215.             game_done = True
  216.             p_points = 0
  217.             for card in c_hand:
  218.                 p_points += card.value
  219.             p_total += p_points
  220.             c_points = 0
  221.             for card in p_hand:
  222.                 c_points += card.value
  223.             c_total += c_points
  224.             clr()
  225.             print 'Both Players Blocked. GAME OVER.'
  226.             print "You Got %i Points For Computer's Hand" % p_points
  227.             print 'Computer Got %i Points For Your Hand' % c_points
  228.     play_again = raw_input('Play Again (Y/N)? ')
  229.     if play_again.lower().startswith('n'):
  230.         done = True
  231.     else:
  232.         game_done = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement