Change witch function

This commit is contained in:
yoxu 2025-09-04 23:17:30 +02:00
commit 9e8b7a3e40
Signed by: yoxu
GPG key ID: CC1DC0EBE5E7D4E8

85
main.py
View file

@ -23,7 +23,7 @@ class Game:
# Dead this night # Dead this night
self.dead_this_night: [List[str]] = [] self.dead_this_night: [List[str]] = []
# Used potions # Used potions
self.used_potions: [list[str]] = [] self.used_potions: [list[str]] = []
# ------------------------- # -------------------------
@ -120,6 +120,31 @@ class Game:
log.debug("Fuzz score: %s" % fuzz_score) log.debug("Fuzz score: %s" % fuzz_score)
return fuzz_player return fuzz_player
def choose_between(self, options):
#Prompt the user to choose between options until a valid one is entered
options_available = ', '.join(map(str, options))
#Make a prompt with a list of options, accepting either a list or a tuple.
prompt = f"Choose between {options_available}: "
while True:
selected = input(prompt)
if selected in options:
return selected
# fuzzy matching
match = process.extract(selected, options, limit=1)
log.debug(match)
if match:
fuzz_option, fuzz_score = match[0][0], match[0][1] #options, score
log.debug(fuzz_option)
log.debug(fuzz_score)
if fuzz_score >= 60:
log.info("You meant %s!", fuzz_option)
log.debug("Fuzz score: %s" % fuzz_score)
return fuzz_option
# ------------------------- # -------------------------
# game actions # game actions
# ------------------------- # -------------------------
@ -248,35 +273,37 @@ class Game:
@role @role
def witch(self) -> None: def witch(self) -> None:
"""Interactively choose to kill or revive someone""" """Interactively choose to kill or revive someone"""
log.info("Choose if you want to use your revive potion") log.info("With the Revive Potion, you can revive someone, and with the Death Potion, you can kill someone. You can only use each potion once, and you can also choose to do nothing.")
print(self.players) if len(self.used_potions) == 2:
while True: log.info("You already used all of your potions. ")
potionchoice = input("What do you want to use Revive or Death potion: ") else:
if potionchoice == "Revive" and "Revive" not in self.used_potions: while True:
player = self.select_someone() options = ("Death", "Revive", "Nothing")
if player in self.dead_this_night: potionchoice = self.choose_between(options)
self.used_potions.append("Revive") if potionchoice == "Revive" and "Revive" not in self.used_potions:
self.revive(player) player = self.select_someone()
if player in self.dead_this_night:
self.used_potions.append("Revive")
self.revive(player)
return
elif player not in self.dead_this_night and not self.players[player].alive:
log.info("You cannot bring this person back to life because they have been buried.")
else:
log.info("This player is not dead.")
elif potionchoice == "Death" and "Death" not in self.used_potions:
player = self.select_someone()
if self.players[player].alive:
self.used_potions.append("Death")
self.kill(player)
return
elif not self.players[player].alive:
self.used_potions.append("Death")
return
elif potionchoice == "Nothing":
log.info("You are not doing anything tonight.")
return return
elif player not in self.dead_this_night: elif potionchoice in self.used_potions:
log.info("This player didn't died this night.") log.info("You already used this potion.")
elif "Revive" not in self.used_potions:
log.info("This potion is already used.")
elif potionchoice == "Death" and "Death" not in self.used_potions:
player = self.select_someone()
if self.players[player]:
self.used_potions.append("Death")
self.kill(player)
return
elif not self.players[player]:
self.used_potions.append("Death")
return
elif potionchoice in self.used_potions:
log.info("You already used all of your potions")
return
else:
log.info("Please write Revive or Death")
# ------------------------- # -------------------------
# game flow # game flow