Create a simple Card game in which there are 8 cards which are randomly chosen from a deck. The first card is shown face up. The game asks the player to predict whether the next card in the selection will have a higher or lower value than the currently showing card.
For example, say the card that’s shown is a 3. The player chooses “higher,” and the next card is shown. If that card has a higher value, the player is correct. In this example, if the player had chosen “lower,” they would have been incorrect. If the player guesses correctly, they get 20 points. If they choose incorrectly, they lose 15 points. If the next card to be turned over has the same value as the previous card, the player is incorrect.
"""
Here is correction for two functions
def take_cards(num_of_cards: int) -> list:
def play_game(cards_taken) -> None:
where now logic of the progam design is observed and simplified
"""
from random import choice as rand_choice
class Deck:
def __init__(self):
self.deck = dict()
self.suit = ("Clubs", "Diamonds", "Hearts", "Spades")
self.card = ("Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King")
for suit in self.suit:
self.deck[suit] = list(self.card)
@property
def is_empty(self) -> bool:
result = True
for suit in self.suit:
if self.deck[suit]:
result = False
break
return result
def has_a_card(self, suit: str, card: str | int) -> bool:
result = False
if card in self.deck[suit]:
result = True
return result
def remove_a_card(self, suit: str, card: str | int) -> bool:
assert isinstance(suit, str)
assert isinstance(card, str) or isinstance(card, int)
try:
if not self.is_empty and self.has_a_card(suit, card):
self.deck[suit].remove(card)
return True
else:
print(f'===WARNING!!!===: Randomly chosen "{card} of {suit}" was removed from the deck before!')
return False
except AssertionError as e:
print(e)
def clearify(self) -> None:
"""
Take all cards away from deck
:return: None
"""
if not self.is_empty:
for suit in self.deck:
self.deck[suit] = []
def __repr__(self):
return f'Deck:\n{self.deck}'
class Card:
def __init__(self, suit: str, card: str | int) -> None:
"""
:type card: str or int
"""
assert isinstance(suit, str)
assert (isinstance(card, str) or isinstance(card, int))
assert suit in ("Clubs", "Diamonds", "Hearts", "Spades")
assert card in ("Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King")
try:
self.suit = suit
if type(card) == str:
if card == "Jack":
self.card = ("Jack", 11)
elif card == "Queen":
self.card = ("Queen", 12)
elif card == "King":
self.card = ("King", 13)
elif card == "Ace":
self.card = ("Ace", 14)
elif type(card) == int:
self.card = (card, card)
except AssertionError as e:
print(e)
def __lt__(self, other):
if self.card[1] < other.card[1]:
return True
else:
return False
def __gt__(self, other):
if self.card[1] > other.card[1]:
return True
else:
return False
def __eq__(self, other):
if self.card[1] == other.card[1]:
return True
else:
return False
def __repr__(self):
return f'"{self.card[0]} of {self.suit}"'
def take_cards(num_of_cards: int) -> list:
deck = Deck()
cards_taken: list[Card] = []
i = 0
while i < num_of_cards:
suit = rand_choice(deck.suit)
card = rand_choice(deck.deck[suit])
# if randomly chosen card is in the deck, remove it from the deck
# else repeat randomly choosing a card (at the same iteration),
# while index "i" is not incremented
if deck.remove_a_card(suit, card):
card = Card(suit, card)
cards_taken.append(card)
i += 1
return cards_taken
def play_game(cards_taken) -> None:
score = 0
print(f'ONLY FOR DEBUGGING!!! Do not show all {len(cards_taken)} card(s) to a player!')
print(f'ONLY FOR DEBUGGING!!! Cards randomly taken from the deck:\n{cards_taken}\n')
curr_card = cards_taken[0]
cards_taken.pop(0)
i = 1
while cards_taken:
# print(f'ONLY FOR DEBUGGING!!! Remaining cards randomly taken from the deck:\n{cards_taken}\n')
print(f'Card taken from remaining {len(cards_taken) + 1} chosen card(s): {curr_card}')
while True:
try:
print(f'{i}. Guess, if the next card would be higher or lower of the current one.')
guess = int(input(f'(For "higher" enter 1, for "lower" enter 0): '))
assert guess in (0, 1)
break
except ValueError:
print('\nVALUE WARNING: Incorrect input!\n')
print(f'Card taken from remaining {len(cards_taken) + 1} chosen card(s): {curr_card}')
except AssertionError:
print('\nASSERT WARNING: Incorrect input!\n')
print(f'Card taken from remaining {len(cards_taken) + 1} chosen card(s): {curr_card}')
next_card = cards_taken[0]
cards_taken.pop(0)
if guess == 1:
if curr_card < next_card:
score += 20
print(f"\nCORRECT: You get 20 points. Current score: {score}\n")
else:
score -= 15
print(f"\nINCORRECT: You lose 15 points. Current score: {score}\n")
elif guess == 0:
if curr_card > next_card:
score += 20
print(f"\nCORRECT: You get 20 points. Current score: {score}\n")
else:
score -= 15
print(f"\nINCORRECT: You lose 15 points. Current score: {score}\n")
curr_card = next_card
i += 1
print(f'Card taken from remaining {len(cards_taken) + 1} chosen card(s): {curr_card}')
print(f"Total score: {score}")
cards_from_deck = take_cards(8)
play_game(cards_from_deck)
Comments
Leave a comment