From 1485f171befaa9761c9b2c6be8b48a6ea2440fe6 Mon Sep 17 00:00:00 2001 From: Kwarde Date: Thu, 18 Jan 2024 16:31:43 +0100 Subject: [PATCH] Make game.py compatible with the new dicts --- game.py | 54 +++++++++++++++++++++++++++++------------------------- player.py | 7 ++++--- spells.py | 22 ++++++++++++---------- wands.py | 2 +- 4 files changed, 46 insertions(+), 39 deletions(-) diff --git a/game.py b/game.py index c99e496..3f9001f 100644 --- a/game.py +++ b/game.py @@ -1,5 +1,8 @@ +#import random from player import * from wands import * +from spells import SPELL_TYPE_COMMON, SPELL_TYPE_POWERFUL, _INVALID_SPELL +from spells import random_combat_spell, print_spells, find_spell_by_name ## ## Definitions @@ -34,19 +37,20 @@ def get_player_spell_from_input(player: Player): player_input = input(print_turn_message(player)) if not player_input: - return [random_combat_spell(), 0] + spell_name, spell_obj = random_combat_spell() + return ((spell_name, spell_obj), 0) else: if player_input == "help": print_spells() continue elif player_input.find("help", 0) != -1: find_what = player_input[5:] - spell = find_spell_by_name(find_what)[0] + spell = spells.get(find_spell_by_name(find_what)[0][0], None) - if spell is spell_object_none: + if spell is spells[_INVALID_SPELL]: print(" Spell '{what}' does not exist!".format(what=find_what)) else: - print(spell) + print("'{spell_name}':{spell_desc}".format(spell_name=find_what, spell_desc=spells[find_what])) continue else: return find_spell_by_name(player_input) @@ -72,8 +76,8 @@ while True: #GET: WANDS print("Welcome {p1_name} and {p2_name}! You're about to choose a wand to use in this duel! Available wands are:".format(p1_name=player1_name, p2_name=player2_name)) -for i in Wand.wandList: - print(i) +for i in wands.items(): + print("{wand_id}:{wand_desc}".format(wand_id=i[0], wand_desc=i[1])) #Player 1 while (True): @@ -81,10 +85,10 @@ while (True): wand_input = int(input("What wand do you want {name}? (Enter one of the numbers): ".format(name=player1_name))) except ValueError: continue - if wand_input < 1 or wand_input > len(Wand.wandList): + if wand_input < 1 or wand_input > len(wands): continue - player1_wand = Wand.wandList[wand_input-1] + player1_wand = wands[wand_input] break #Player 2 while (True): @@ -92,10 +96,10 @@ while (True): wand_input = int(input("What wand do you want {name}? (Enter one of the numbers): ".format(name=player2_name))) except ValueError: continue - if wand_input < 1 or wand_input > len(Wand.wandList): + if wand_input < 1 or wand_input > len(wands): continue - player2_wand = Wand.wandList[wand_input-1] + player2_wand = wands[wand_input] break player1 = Player(player1_name, player1_wand) @@ -131,13 +135,18 @@ try: round_end() print("== Round {round} ==".format(round=current_round)) - player1.active_spell, player1.active_spell_levenshtein_distance = get_player_spell_from_input(player1) - player2.active_spell, player2.active_spell_levenshtein_distance = get_player_spell_from_input(player2) + spell, dist = get_player_spell_from_input(player1) + player1.active_spell = spells.get(spell[0]) + player1.active_spell_levenshtein_distance = dist - if (player1.stunned_rounds > 0 and player1.active_spell is not spell_finite_incantatem): - player1.active_spell = spell_object_stunned - if (player2.stunned_rounds > 0 and player2.active_spell is not spell_finite_incantatem): - player2.active_spell = spell_object_stunned + spell, dist = get_player_spell_from_input(player2) + player2.active_spell = spells.get(spell[0]) + player2.active_spell_levenshtein_distance = dist + + if (player1.stunned_rounds > 0 and player1.active_spell is not spells["Finite Incantatem"]): + player1.active_spell = None + if (player2.stunned_rounds > 0 and player2.active_spell is not spells["Finite Incantatem"]): + player2.active_spell = None # OUTCOME: SPELLS # > Get spell succes @@ -150,17 +159,17 @@ try: # > Add health if defensive spell was lucky (partial heal, fully heal) if player1.active_spell_succes and player1.active_spell.chance_heal_partly_succes(): player1.give_health(MAX_PLAYER_HEALTH * 0.05) - print(" {name} casted {spell} above expectations, and receives {hp} health!".format(name=player1.name, spell=player1.active_spell.name, hp=MAX_PLAYER_HEALTH*0.05)) + print(" {name} casted {spell} above expectations, and receives {hp} health!".format(name=player1.name, spell=player1.active_spell.get_spell_name(), hp=MAX_PLAYER_HEALTH*0.05)) elif player1.active_spell_succes and player1.active_spell.chance_heal_fully_succes() and player1.health < MAX_PLAYER_HEALTH: player1.give_health(MAX_PLAYER_HEALTH) - print(" {name} casted {spell} outstanding! {name} now has full health!".format(name=player1.name, spell=player1.active_spell.name, hp=MAX_PLAYER_HEALTH*0.05)) + print(" {name} casted {spell} outstanding! {name} now has full health!".format(name=player1.name, spell=player1.active_spell.get_spell_name(), hp=MAX_PLAYER_HEALTH*0.05)) # if player2.active_spell_succes and player2.active_spell.chance_heal_partly_succes(): player2.give_health(MAX_PLAYER_HEALTH * 0.05) - print(" {name} casted {spell} above expectations, and receives {hp} health!".format(name=player2.name, spell=player2.active_spell.name, hp=MAX_PLAYER_HEALTH*0.05)) + print(" {name} casted {spell} above expectations, and receives {hp} health!".format(name=player2.name, spell=player2.active_spell.get_spell_name(), hp=MAX_PLAYER_HEALTH*0.05)) elif player2.active_spell_succes and player2.active_spell.chance_heal_fully_succes() and player2.health < MAX_PLAYER_HEALTH: player2.give_health(MAX_PLAYER_HEALTH) - print(" {name} casted {spell} outstanding! {name} now has full health!".format(name=player2.name, spell=player2.active_spell.name, hp=MAX_PLAYER_HEALTH*0.05)) + print(" {name} casted {spell} outstanding! {name} now has full health!".format(name=player2.name, spell=player2.active_spell.get_spell_name(), hp=MAX_PLAYER_HEALTH*0.05)) # > Determine instant winner or skip to next round if not player1.active_spell_succes and not player2.active_spell_succes: @@ -207,11 +216,6 @@ try: if slowest_caster.health > 0: slowest_caster.cast_spell(fastest_caster) - fastest_caster.active_spell = spell_object_none - fastest_caster.active_spell_levenshtein_distance = 0 - slowest_caster.active_spell = spell_object_none - slowest_caster.active_spell_levenshtein_distance = 0 - except KeyboardInterrupt: print() print(" Duel ended because both {} and {} suddenly decided to test out their apparition skill!".format(player1.name, player2.name)) \ No newline at end of file diff --git a/player.py b/player.py index 4c1553f..a0a43e1 100644 --- a/player.py +++ b/player.py @@ -1,3 +1,4 @@ +import random from wands import Wand from spells import SPELL_TYPE_NONE, SPELL_TYPE_USELESS, SPELL_TYPE_DEFENSE, SPELL_TYPE_UNFORGIVABLE#, SPELL_TYPE_COMMON, SPELL_TYPE_POWERFUL from spells import Spell, spells, _INVALID_SPELL @@ -39,6 +40,8 @@ class Player: self.stunned_rounds = MAX_STUNNED_ROUNDS def get_spell_succes_rate(self, spell: Spell): + if spell == None: + return 0 return 1 * self.wand.succes_rate * spell.succes_rate def get_queued_effects(self): @@ -58,7 +61,7 @@ class Player: def cast_spell_result(self, spell: Spell, succes: bool): if spell == None: - spell[None].type = SPELL_TYPE_NONE + return " {name} can't cast anything but Finite Incantatem since they are stunned".format(name=self.name) if succes: message = "{name} casted '{spell}' with succes" @@ -68,8 +71,6 @@ class Player: elif spell.type == SPELL_TYPE_NONE: if spell == spells[_INVALID_SPELL]: return "{name} must not be feeling well, since they casted a non existing spell. Cast FAILED!".format(name=self.name) - elif spell == None: - return " {name} can't cast anything but {spell} since they are stunned".format(name=self.name, spell=spells["Finite Incantatem"].name) else: message = "{name} tried to cast '{spell}' but mispronounced it. Cast FAILED!" return message.format(name=self.name, spell=spell.get_spell_name()) diff --git a/spells.py b/spells.py index ec3d534..8660d01 100644 --- a/spells.py +++ b/spells.py @@ -13,7 +13,7 @@ CHANCE_HEAL_FULLY = 5 #Maximum Levenshtein distance. Eg if a user casts 'Pritrgo' instead of 'Protego', distance would be 2 and Protego would still be cast if MAX_LEVENSHTEIN_DISTANCE is at least 2 #Set to 0 to disable -MAX_LEVENSHTEIN_DISTANCE = 3 +MAX_LEVENSHTEIN_DISTANCE = 0 if MAX_LEVENSHTEIN_DISTANCE > 0: from Levenshtein import distance @@ -98,17 +98,19 @@ spells = { ## Standalone spell functions ## def random_combat_spell(): - return random.choice([i for i in spell.items() if i[1].type == SPELL_TYPE_COMMON]) # note: returns tuple ('spell_name', spell_obj) + return random.choice([i for i in spells.items() if i[1].type == SPELL_TYPE_COMMON]) # note: returns tuple ('spell_name', spell_obj) def find_spell_by_name(input: str): # Returns a multidimensional tuple: ( ('spell_name', spell_object), levenshtein_distance ) - ret = (input, spell.get(input.title(), spell[_INVALID_SPELL])) + ret = (input, spells.get(input.title(), spells[_INVALID_SPELL])) dist = 0 - if ret[1] == spell[_INVALID_SPELL] and MAX_LEVENSHTEIN_DISTANCE > 0: - for i in spell.items(): - dist = distance(i[0].title(), input.title()) - if dist <= MAX_LEVENSHTEIN_DISTANCE: - ret = i - break + if ret[1] == spells[_INVALID_SPELL]: + ret = (_INVALID_SPELL, ret[1]) + if MAX_LEVENSHTEIN_DISTANCE > 0: + for i in spells.items(): + dist = distance(i[0].title(), input.title()) + if dist <= MAX_LEVENSHTEIN_DISTANCE: + ret[1] = i + break return (ret, dist) def print_spells(): @@ -118,7 +120,7 @@ def print_spells(): header_spells_powerful = "== POWERFUL COMBAT SPELLS ==" header_spells_unforgivable = "== UNFORGIVABLE CURSES ==" - for i in spell.items(): + for i in spells.items(): if i[1].type == SPELL_TYPE_UNFORGIVABLE or i[1].type == SPELL_TYPE_USELESS or i[1].type == SPELL_TYPE_NONE: continue diff --git a/wands.py b/wands.py index 734cbef..834539d 100644 --- a/wands.py +++ b/wands.py @@ -49,7 +49,7 @@ class Wand: ## ## Wands ## -wand = { +wands = { 1: Wand(WAND_CORE_UNICORN, WAND_WOOD_ASH), 2: Wand(WAND_CORE_UNICORN, WAND_WOOD_ELDER), 3: Wand(WAND_CORE_UNICORN, WAND_WOOD_APPLE),