In class assignment 1. Write a program to play a very modified version of spades
ID: 3348767 • Letter: I
Question
In class assignment 1. 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 call another function to check the individual cards in a hand 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 using a function and is 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(Diamonds, Clubs and Hearts) are worth face value only You can use the following code to create a sorted list of cards by suit or create it manually if you have trouble with the syntax: cards sorted(sorted((str(n)ts for n in range(1,15) for s in ('D',C ),key lambda x: int(x( len(x)-1]), key lambda x: x(-1])Explanation / Answer
#Import the header files
from random import randint
#Define a method
def hand_method_generation():
#Initialize the variables
o_hand = {}
cnt = 0
#while loop until "cnt"
while cnt != 10:
#while loop until "true"
while True:
#update the variable
s_ran = randint(1,4)
c_rand = randint(2,14)
o_card = (s_ran, c_rand)
#"if" condition until "o_hand"
if not o_card in o_hand:
#break statement
break
#updation
o_hand[o_card] = 1
cnt += 1
#updation
o_hnds = o_hand.keys()
#return statement
return o_hnds[:len(o_hnds)/2], o_hnds[len(o_hnds)/2:]
#Define a method "met_scr_hand"
def met_scr_hand(o_hand):
#Initialize the variable
o_sre = 0
#"for" loop until "o_hand"
for o_card in o_hand:
#Verification statement
if(o_card[0] == 4):
#updation
o_sre += o_card[1]*2
#"else" part
else:
#updation
o_sre += o_card[1]
#returning statement
return o_sre
#define a method "spd_met"
def spd_met():
#Initialize the variable
(h1, h2) = hand_method_generation()
#Initialize the variable
o_suit = {}
o_suit[1] = "clubes"
o_suit[2] = "diamond"
o_suit[3] = "heart"
o_suit[4] = "spade"
#Initialize the variable
o_hand = {}
#"for" loop until "ind"
for ind in range (2, 11):
#Initialize the variable
o_hand[ind] = str(ind)
#Initialize the variable
o_hand[11] = "Jack"
o_hand[12] = "Queen"
o_hand[13] = "King"
o_hand[14] = "Ace"
#Printing statement
print ("Player 1 Card: ")
#Initialize the variable
cnt = 1
#"for" loop until "o_card"
for o_card in h1:
#Printing statement
print ("Card #%d: Suit: %s Card: %s" % (cnt, o_suit[o_card[0]], o_hand[o_card[1]]))
#Updation
cnt += 1
#Printing statement
print ("Player 2 Card: ")
#Initialize the variable
cnt = 1
#"for" loop until "o_card"
for o_card in h2:
#Printing statement
print ("Card #%d: Suit: %s Card: %s" % (cnt, o_suit[o_card[0]], o_hand[o_card[1]]))
#Updation
cnt += 1
#Initialize the variable
s1 = met_scr_hand(h1)
s2 = met_scr_hand(h2)
#Verification statement
if s1 > s2:
#Printing statement
print ("Player 1 win")
#Verification statement
elif s1 < s2:
#Printing statement
print ("Player 2 win")
#Verification statement
else:
#Printing statement
print ("Tie")
#Call the function "spd_met"
spd_met()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.