Reveal a card in a short deck.
★★★Cutting the deck is a common way to resolve draws in playing card games. One player takes any number of cards (usually approximately half) from a face down full deck and then reveals the card on the bottom of their pile. The second player then takes any card from those remaining in the deck and reveals their card. Higher numbers beat lower numbers regardless of suit. J is 11, Q is 12, K is 13 and A is 14. The alphabetical order of the suit resolves draws. Clubs (C) is lowest followed by diamonds (D), hearts (H) and spades (S) is the highest. In a short deck the lowest number is a 6. Therefore the 6C (the six of clubs) is the lowest card and AS (the ace of spades) is the highest card.
Write a program that asks the user to pick one of the first thirty-five cards in the deck. All the previous cards are discarded. The computer then picks one of the remaining cards. Whoever has the highest card wins. The result is output to the user.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
play_game procedure.play_game that:output_winner procedure with the two cards as parameters.output_winner that:Hint
Modulus 9 will tell you the value of the card because there are 9 cards in each suit. In the case of draws you can look up the index of the card drawn in the cards list to know a card’s relative value.
What number card do you want to draw? 0-34 :20
You reveal the 8H
The CPU reveals the AS
CPU wins.
What number card do you want to draw? 0-34 :13
You reveal the 10D
The CPU reveals the 9S
You win!
What number card do you want to draw? 0-34 :18
You reveal the 6H
The CPU reveals the 6S
CPU wins.
output_winner
# Output who won the game
def output_winner(player_card, cpu_card):
---
# Find the card value of each card from the position in the ordered array of cards
player_card_value = cards.index(player_card)
---
cpu_card_value = cards.index(cpu_card)
---
# If both cards have the same value...
if player_card_value % 9 == cpu_card_value % 9:
---
# ...and the player's suit is higher, the player wins.
if player_card_value > cpu_card_value:
---
print("You win!")
---
# If player card has a higher value, the player wins
elif player_card_value % 9 > cpu_card_value % 9:
---
print("You win!")
---
# In all other cases the CPU wins.
else:
---
print("CPU wins.")
play_winner, main program
# Play the game
def play_game():
---
random.shuffle(deck)
---
# Player cuts the deck
player_choice = int(input("What number card do you want to draw? 0-34 :"))
---
player_card = deck[player_choice]
---
print("You reveal the", player_card)
---
# CPU cuts the deck
cpu_choice = random.randint(player_choice + 1, 35)
---
cpu_card = deck[cpu_choice]
---
print("The CPU reveals the", cpu_card)
---
output_winner(player_card, cpu_card)
---
# -------------------------
# Main program
# -------------------------
random.seed()
# The value order of the cards for reference
---
cards = ["6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AC",
"6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AD",
"6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", "AH",
"6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", "AS"]
---
# The deck to be shuffled and used to play the game
deck = cards.copy()
---
play_game()