Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to play a very modified version of spades. The program will sele

ID: 3838437 • Letter: W

Question

Write a program to play a very modified version of spades. The program will select two hands of 5 cards for a single player and the computer dealer. The hands should be chosen using a function and the random methods we discussed in class. Your function should check the cards to be sure that they have not been used in the other hand. If so, then another card will be chosen at random until a hand can be formed. After the second hand is dealt, all cards are available again. A winner of the game will be chosen based on the sum of all cards in the hand. The sum of the cards will be calculated as follows: 2, 3, ... 10 = face value Jacks = 11 Queens = 12 Kings = 13 Aces = 14 The suit of the cards is used as a multiplier. Spades are worth 2 times the face value giving an advantage to any hands containing spades. All other suits are worth face value only

Explanation / Answer

from random import randint
def generateHand():
'''
This function generates a two random hand
'''
hand = {}
count = 0
while count != 10:
while True:
suit_rand = randint(1,4)
card_rand = randint(2,14)
card = (suit_rand, card_rand)
if not card in hand:
break
hand[card] = 1
count += 1
  
hands = hand.keys()
return (hands[:len(hands)/2], hands[len(hands)/2:])

def scoreHand(hand):
score = 0
for card in hand:
# spade has double score
if(card[0] == 4):
score += card[1]*2
else:
score += card[1]
return score

def spade():
# cards are geerated with 2 to 14 , 14 being ace
# each card is represented by a tuple, first number indicating type of card and other indicate value
# 1 clubes, 2 diamond, 3 heart, 4 spade
(hand1, hand2) = generateHand()
  
suit = {}
suit[1] = "clubes"
suit[2] = "diamond"
suit[3] = "heart"
suit[4] = "spade"
  
hand = {}
for i in range (2, 11):
hand[i] = str(i)
hand[11] = "Jack"
hand[12] = "Queen"
hand[13] = "King"
hand[14] = "Ace"
  
print "Player 1 card: "
count = 1
for card in hand1:
print "Card #%d: Suit: %s Card: %s" % (count, suit[card[0]], hand[card[1]])
count += 1
  
print "Player 2 card: "
count = 1
for card in hand2:
print "Card #%d: Suit: %s Card: %s" % (count, suit[card[0]], hand[card[1]])
count += 1
  
  
score1 = scoreHand(hand1)
score2 = scoreHand(hand2)
if score1 > score2:
print "Player 1 win"
elif score1 < score2:
print "Player 2 win"
else:
print "Tie"

spade()

# link for code https://goo.gl/QXb28D

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote