Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def shuffle_deck():
- deck = {}
- for i in range(13):
- deck[i] = [(i+2), 'Clubs']
- for i in range(13):
- deck[i+13] = [(i+2), 'Diamonds']
- for i in range(13):
- deck[i+26] = [(i+2), 'Hearts']
- for i in range(13):
- deck[i+39] = [(i+2), 'Spades']
- random.shuffle(deck)
- return deck
- def card_name(card):
- if card == 11:
- return 'Jack'
- elif card == 12:
- return 'Queen'
- elif card == 13:
- return 'King'
- elif card == 14:
- return 'Ace'
- else:
- return str(card)
- def deal_cards(deck):
- hand1 = {}
- hand2 = {}
- for i in range(52):
- if i % 2 == 0:
- hand1[i/2] = deck.pop(i)
- else:
- hand2[i/2] = deck.pop(i)
- return hand1,hand2
- def check_cards(player1, player2):
- if player1 > player2:
- return 'player1'
- elif player1 < player2:
- return 'player2'
- else:
- return 'tied'
- deck = shuffle_deck()
- hand1,hand2 = deal_cards(deck)
- ontable = {}
- hand1won = {}
- hand2won = {}
- for i in range(26):
- ontable[len(ontable)] = hand1.pop(i)
- ontable[len(ontable)] = hand2.pop(i)
- print "Player One plays the %s of %s." % (card_name(ontable[len(ontable)-2][0]), ontable[len(ontable)-2][1])
- print "Player Two plays the %s of %s." % (card_name(ontable[len(ontable)-1][0]), ontable[len(ontable)-1][1])
- play_winner = check_cards(ontable[len(ontable)-2][0], ontable[len(ontable)-1][0])
- if play_winner == 'player1':
- print "Player One wins the round."
- for x in range(0,len(ontable)):
- hand1won[len(hand1won)] = ontable.pop(x)
- elif play_winner == 'player2':
- print "Player Two wins the round."
- for x in range(0,len(ontable)):
- hand2won[len(hand2won)] = ontable.pop(x)
- elif play_winner == 'tied':
- print "The players tie for this round and the cards remain on the table."
- print "The score is now Player One: %r, Player Two: %r" % (len(hand1won), len(hand2won))
- if len(hand1won) > len(hand2won):
- print "Player One wins the game!"
- elif len(hand1won) < len(hand2won):
- print "Player Two wins the game!"
- else:
- print "Nobody wins. Such is the nature of War."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement