Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # b_CrazyEights.py
- import random, os
- class Cards():
- '''suit, rank, value, short_name, long_name'''
- oSuit = ('Diamonds','Hearts','Spades','Clubs')
- suit_dict = dict(zip('dhsc', oSuit))
- cv = Cards()
- def init_cards():
- oDeck = []
- oRank = {1:'Ace',11:'Jack',12:'Queen',13:'King'}
- card = {}
- for suit_id in cv.oSuit:
- for rank_id in range(1, 14):
- if rank_id in oRank:
- rank = oRank[rank_id]
- value = 10
- if rank_id == 1:
- value = 1
- else:
- rank = str(rank_id)
- value = rank_id
- if rank == 8:
- value = 50
- short_name = rank[0] + suit_id[0]
- if rank == '10':
- short_name = rank + suit_id[0]
- card[short_name] = Cards()
- obj = card[short_name]
- obj.short_name = short_name
- obj.long_name = rank + ' of ' + suit_id
- obj.suit = suit_id
- obj.value = value
- obj.rank = rank
- oDeck.append(obj)
- return oDeck
- def clr():
- os.system('cls||clear')
- print 'Points In Total... \n\t\t You: %i \t\t Computer: %i \n' % (p_total, c_total)
- print 'Cards In The Deck: %i \n' % (len(deck))
- def get_new_suit():
- while 1:
- suit = raw_input('Pick A Suit: ').lower()
- if suit in cv.suit_dict:
- active_suit = cv.suit_dict[suit]
- break
- else:
- print 'Invalid Entry... Try Again'
- cv.active_suit = active_suit
- def computer_turn():
- options = []
- an_eight = False
- for card in c_hand:
- if card.rank == '8' and len(c_hand) > 1:
- an_eight = card
- if len(c_hand) == 2:
- options = 0
- break
- else:
- if card.suit == cv.active_suit:
- options.append(card)
- elif card.rank == cv.upcard.rank:
- options.append(card)
- if options:
- strategy = options[0]
- for card in options:
- if card.value > strategy.value:
- strategy = card
- c_hand.remove(strategy)
- cv.upcard = strategy
- cv.active_suit = cv.upcard.suit
- print ' Computer Played %s (%s)' % (strategy.long_name, strategy.short_name)
- if strategy.rank == '2' and deck:
- print ' -- What You Had To Pick Up: ',
- for _ in 'zz':
- hand_card = deck.pop()
- print hand_card.short_name,
- p_hand.append(hand_card)
- print
- elif an_eight:
- suit_total = {}
- for card in c_hand:
- if card.rank != '8':
- try:
- suit_total[card.suit] += 1
- except:
- suit_total[card.suit] = 0
- strategy = max(suit_total, key=suit_total.get)
- c_hand.remove(an_eight)
- cv.upcard = an_eight
- cv.active_suit = strategy
- print ' Computer Played %s (%s)' % (an_eight.long_name, an_eight.short_name)
- print ' -- Changed Suit To %s --' % (strategy)
- else:
- if deck:
- c_hand.append(deck.pop())
- print ' Computer Drew A Card'
- else:
- print ' Computer Is Blocked'
- cv.blocked += 1
- print ' -- Computer Cards Held: %i --' % (len(c_hand))
- def player_turn():
- valid_play = False
- an_eight = False
- print '\nYour Hand: ',
- for card in p_hand:
- print card.short_name,
- print
- print ' Up Card: %s [---%s---]' % (cv.upcard.long_name, cv.upcard.short_name)
- if cv.upcard.rank == '8':
- print ' *** Suit Is %s ***' % (cv.active_suit)
- response = raw_input("\nType A Card To Play Or Type 'Z' To Take A Card: ").lower()
- while not valid_play:
- selected_card = None
- while selected_card == None:
- if response == 'z':
- valid_play = True
- if deck:
- p_hand.append(deck.pop())
- clr()
- print 'You Drew: %s (%s)' % (card.long_name, card.short_name)
- else:
- print 'There Are No Cards Left In The Deck'
- cv.blocked += 1
- return
- else:
- for card in p_hand:
- if response.upper() == card.short_name:
- selected_card = card
- if selected_card == None:
- response = raw_input("You Don't Have That Card. Try Again: ")
- if selected_card.rank == '8':
- an_eight = True
- if len(p_hand) > 1:
- valid_play = True
- get_new_suit()
- elif selected_card.suit == cv.active_suit:
- valid_play = True
- elif selected_card.rank == cv.upcard.rank:
- valid_play = True
- if valid_play:
- p_hand.remove(selected_card)
- cv.upcard = selected_card
- if not an_eight:
- cv.active_suit = cv.upcard.suit
- clr()
- print 'You Played %s (%s)' % (selected_card.long_name, selected_card.short_name)
- if cv.upcard.rank == '8':
- print ' *** Suit Called: %s ***' % (cv.active_suit)
- elif cv.upcard.rank == '2' and deck:
- print ' -- Computer Has To Pick Up 2 Cards From The Deck As Available -- '
- for _ in 'zz':
- hand_card = deck.pop()
- c_hand.append(hand_card)
- if not valid_play:
- if an_eight:
- print 'Wildcards Can Not Be Played Last...',
- else:
- print 'That Card Does Not Relate...',
- response = raw_input("Try Again: ")
- p_total = c_total = 0
- card_value = {}
- card_suit = {}
- long_name = {}
- fulldeck = init_cards()
- done = False
- game_done = False
- while not done:
- p_hand = []
- c_hand = []
- deck = fulldeck[:]
- random.shuffle(deck)
- for _ in range(7):
- hand_card = deck.pop()
- p_hand.append(hand_card)
- for _ in range(7):
- hand_card = deck.pop()
- c_hand.append(hand_card)
- cv.blocked = 0
- cv.upcard = deck.pop()
- cv.active_suit = cv.upcard.suit
- clr()
- while not game_done:
- player_turn()
- if not p_hand:
- game_done = True
- p_points = 0
- for card in c_hand:
- p_points += card.value
- p_total += p_points
- clr()
- print 'You Won!'
- print "You Got %i Points For Computer's Hand" % p_points
- else:
- computer_turn()
- if not c_hand:
- game_done = True
- c_points = 0
- for card in p_hand:
- c_points += card.value
- c_total += c_points
- clr()
- print 'Computer Won!'
- print 'Computer Got %i Points For Your Hand' % c_points
- if cv.blocked >= 2:
- game_done = True
- p_points = 0
- for card in c_hand:
- p_points += card.value
- p_total += p_points
- c_points = 0
- for card in p_hand:
- c_points += card.value
- c_total += c_points
- clr()
- print 'Both Players Blocked. GAME OVER.'
- print "You Got %i Points For Computer's Hand" % p_points
- print 'Computer Got %i Points For Your Hand' % c_points
- play_again = raw_input('Play Again (Y/N)? ')
- if play_again.lower().startswith('n'):
- done = True
- else:
- game_done = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement