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 = {None}
- hand1won = {None}
- hand2won = {None}
- # print "Hand 1\tHand2"
- # for i in range(26):
- # print "%s of %s\t%s of %s" % (card_name(hand1[i][0]), hand1[i][1], card_name(hand2[i][0]), hand2[i][1])
- for i in range(26):
- ontable[len(ontable)] = hand1.pop[i]
- ontable[len(ontable)] = hand2.pop[i]
- print "Player 1 plays the %s of %s." % (card_name(ontable[len(ontable)-1][0]), ontable[len(ontable)-1][1])
- print "Player 2 plays the %s of %s." % (card_name(ontable[len(ontable)][0]), ontable[len(ontable)][1])
- play_winner = check_cards(ontable[len(ontable)-1][0], ontable[len(ontable)][0])
- if play_winner == 'player1':
- print "Player 1 wins the round."
- for x in range(1,len(ontable)):
- hand1won[len(hand1won)] = ontable[i]
- elif play_winner == 'player2':
- print "Player 2 wins the round."
- for x in range(1,len(ontable)):
- hand2won[len(hand2won)] = ontable[i]
- elif play_winner == 'tied':
- print "The players tie for this round and the cards remain on the table."
- print "The score is now Player 1: %r, Player 2: %r" % (len(hand1won-1), len(hand2won-1))
- if len(hand1won) > len(hand2won):
- print "Player 1 wins the game!"
- elif len(hand1won) < len(hand2won):
- print "Player 2 wins the game!"
- else:
- print "Nobody wins. Such is the nature of War."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement