Updated Game() function to repeat user_input if player enter an invalid number of cards to be pulled.
This commit is contained in:
parent
6227c5b3a8
commit
4d45d1168e
@ -82,22 +82,29 @@ class Game:
|
|||||||
self.player2 = Player("Player 2", self.deck)
|
self.player2 = Player("Player 2", self.deck)
|
||||||
|
|
||||||
def prompt_card_replacement(self, player: Player):
|
def prompt_card_replacement(self, player: Player):
|
||||||
num = int(input(f"{player.name}, how many cards do you want to pull? (0-3): "))
|
while True:
|
||||||
if not (0 <= num <= 3):
|
try:
|
||||||
print("Invalid number. Choose between 0 and 3.")
|
num = int(input(f"{player.name}, how many cards do you want to pull? (0–3): "))
|
||||||
return
|
if 0 <= num <= 3:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print("Invalid number. Please choose between 0 and 3.")
|
||||||
|
except ValueError:
|
||||||
|
print("Invalid input. Please enter a number between 0 and 3.")
|
||||||
|
|
||||||
for _ in range(num):
|
for _ in range(num):
|
||||||
print("Your hand:\n", player.show_hand())
|
print("Your hand:\n", player.show_hand())
|
||||||
user_input = input("Enter a card to replace (e.g., A of Spades): ").strip()
|
user_input = input("Enter a card to replace (e.g., A of Spades): ").strip()
|
||||||
if ' of ' not in user_input:
|
if ' of ' not in user_input:
|
||||||
print("Invalid format.")
|
print("Invalid format. Use 'Rank of Suit'.")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
rank, suit = user_input.split(' of ', 1)
|
rank, suit = user_input.split(' of ', 1)
|
||||||
old_card = Card(rank, suit)
|
old_card = Card(rank, suit)
|
||||||
if old_card not in player.hand:
|
if old_card not in player.hand:
|
||||||
print(f"{old_card} not found in your hand.")
|
print(f"{old_card} not found in your hand.")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
new_card = self.deck.deal_card()
|
new_card = self.deck.deal_card()
|
||||||
if player.replace_card(old_card, new_card):
|
if player.replace_card(old_card, new_card):
|
||||||
print(f"Replaced {old_card} with {new_card}")
|
print(f"Replaced {old_card} with {new_card}")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user