1
0

Make game.py compatible with the new dicts

This commit is contained in:
Kwarde 2024-01-18 16:31:43 +01:00
parent aea461499c
commit 1485f171be
4 changed files with 46 additions and 39 deletions

54
game.py
View File

@ -1,5 +1,8 @@
#import random
from player import * from player import *
from wands 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 ## Definitions
@ -34,19 +37,20 @@ def get_player_spell_from_input(player: Player):
player_input = input(print_turn_message(player)) player_input = input(print_turn_message(player))
if not player_input: if not player_input:
return [random_combat_spell(), 0] spell_name, spell_obj = random_combat_spell()
return ((spell_name, spell_obj), 0)
else: else:
if player_input == "help": if player_input == "help":
print_spells() print_spells()
continue continue
elif player_input.find("help", 0) != -1: elif player_input.find("help", 0) != -1:
find_what = player_input[5:] 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)) print("<!> Spell '{what}' does not exist!".format(what=find_what))
else: else:
print(spell) print("'{spell_name}':{spell_desc}".format(spell_name=find_what, spell_desc=spells[find_what]))
continue continue
else: else:
return find_spell_by_name(player_input) return find_spell_by_name(player_input)
@ -72,8 +76,8 @@ while True:
#GET: WANDS #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)) 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: for i in wands.items():
print(i) print("{wand_id}:{wand_desc}".format(wand_id=i[0], wand_desc=i[1]))
#Player 1 #Player 1
while (True): 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))) wand_input = int(input("What wand do you want {name}? (Enter one of the numbers): ".format(name=player1_name)))
except ValueError: except ValueError:
continue continue
if wand_input < 1 or wand_input > len(Wand.wandList): if wand_input < 1 or wand_input > len(wands):
continue continue
player1_wand = Wand.wandList[wand_input-1] player1_wand = wands[wand_input]
break break
#Player 2 #Player 2
while (True): 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))) wand_input = int(input("What wand do you want {name}? (Enter one of the numbers): ".format(name=player2_name)))
except ValueError: except ValueError:
continue continue
if wand_input < 1 or wand_input > len(Wand.wandList): if wand_input < 1 or wand_input > len(wands):
continue continue
player2_wand = Wand.wandList[wand_input-1] player2_wand = wands[wand_input]
break break
player1 = Player(player1_name, player1_wand) player1 = Player(player1_name, player1_wand)
@ -131,13 +135,18 @@ try:
round_end() round_end()
print("== Round {round} ==".format(round=current_round)) print("== Round {round} ==".format(round=current_round))
player1.active_spell, player1.active_spell_levenshtein_distance = get_player_spell_from_input(player1) spell, dist = get_player_spell_from_input(player1)
player2.active_spell, player2.active_spell_levenshtein_distance = get_player_spell_from_input(player2) 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): spell, dist = get_player_spell_from_input(player2)
player1.active_spell = spell_object_stunned player2.active_spell = spells.get(spell[0])
if (player2.stunned_rounds > 0 and player2.active_spell is not spell_finite_incantatem): player2.active_spell_levenshtein_distance = dist
player2.active_spell = spell_object_stunned
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 # OUTCOME: SPELLS
# > Get spell succes # > Get spell succes
@ -150,17 +159,17 @@ try:
# > Add health if defensive spell was lucky (partial heal, fully heal) # > Add health if defensive spell was lucky (partial heal, fully heal)
if player1.active_spell_succes and player1.active_spell.chance_heal_partly_succes(): if player1.active_spell_succes and player1.active_spell.chance_heal_partly_succes():
player1.give_health(MAX_PLAYER_HEALTH * 0.05) 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: 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) 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(): if player2.active_spell_succes and player2.active_spell.chance_heal_partly_succes():
player2.give_health(MAX_PLAYER_HEALTH * 0.05) 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: 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) 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 # > Determine instant winner or skip to next round
if not player1.active_spell_succes and not player2.active_spell_succes: if not player1.active_spell_succes and not player2.active_spell_succes:
@ -207,11 +216,6 @@ try:
if slowest_caster.health > 0: if slowest_caster.health > 0:
slowest_caster.cast_spell(fastest_caster) 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: except KeyboardInterrupt:
print() print()
print("<!> Duel ended because both {} and {} suddenly decided to test out their apparition skill!".format(player1.name, player2.name)) print("<!> Duel ended because both {} and {} suddenly decided to test out their apparition skill!".format(player1.name, player2.name))

View File

@ -1,3 +1,4 @@
import random
from wands import Wand 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_TYPE_NONE, SPELL_TYPE_USELESS, SPELL_TYPE_DEFENSE, SPELL_TYPE_UNFORGIVABLE#, SPELL_TYPE_COMMON, SPELL_TYPE_POWERFUL
from spells import Spell, spells, _INVALID_SPELL from spells import Spell, spells, _INVALID_SPELL
@ -39,6 +40,8 @@ class Player:
self.stunned_rounds = MAX_STUNNED_ROUNDS self.stunned_rounds = MAX_STUNNED_ROUNDS
def get_spell_succes_rate(self, spell: Spell): def get_spell_succes_rate(self, spell: Spell):
if spell == None:
return 0
return 1 * self.wand.succes_rate * spell.succes_rate return 1 * self.wand.succes_rate * spell.succes_rate
def get_queued_effects(self): def get_queued_effects(self):
@ -58,7 +61,7 @@ class Player:
def cast_spell_result(self, spell: Spell, succes: bool): def cast_spell_result(self, spell: Spell, succes: bool):
if spell == None: 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: if succes:
message = "{name} casted '{spell}' with succes" message = "{name} casted '{spell}' with succes"
@ -68,8 +71,6 @@ class Player:
elif spell.type == SPELL_TYPE_NONE: elif spell.type == SPELL_TYPE_NONE:
if spell == spells[_INVALID_SPELL]: 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) 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!" else: message = "{name} tried to cast '{spell}' but mispronounced it. Cast FAILED!"
return message.format(name=self.name, spell=spell.get_spell_name()) return message.format(name=self.name, spell=spell.get_spell_name())

View File

@ -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 #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 #Set to 0 to disable
MAX_LEVENSHTEIN_DISTANCE = 3 MAX_LEVENSHTEIN_DISTANCE = 0
if MAX_LEVENSHTEIN_DISTANCE > 0: if MAX_LEVENSHTEIN_DISTANCE > 0:
from Levenshtein import distance from Levenshtein import distance
@ -98,16 +98,18 @@ spells = {
## Standalone spell functions ## Standalone spell functions
## ##
def random_combat_spell(): 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 ) 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 dist = 0
if ret[1] == spell[_INVALID_SPELL] and MAX_LEVENSHTEIN_DISTANCE > 0: if ret[1] == spells[_INVALID_SPELL]:
for i in spell.items(): ret = (_INVALID_SPELL, ret[1])
if MAX_LEVENSHTEIN_DISTANCE > 0:
for i in spells.items():
dist = distance(i[0].title(), input.title()) dist = distance(i[0].title(), input.title())
if dist <= MAX_LEVENSHTEIN_DISTANCE: if dist <= MAX_LEVENSHTEIN_DISTANCE:
ret = i ret[1] = i
break break
return (ret, dist) return (ret, dist)
@ -118,7 +120,7 @@ def print_spells():
header_spells_powerful = "== POWERFUL COMBAT SPELLS ==" header_spells_powerful = "== POWERFUL COMBAT SPELLS =="
header_spells_unforgivable = "== UNFORGIVABLE CURSES ==" 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: if i[1].type == SPELL_TYPE_UNFORGIVABLE or i[1].type == SPELL_TYPE_USELESS or i[1].type == SPELL_TYPE_NONE:
continue continue

View File

@ -49,7 +49,7 @@ class Wand:
## ##
## Wands ## Wands
## ##
wand = { wands = {
1: Wand(WAND_CORE_UNICORN, WAND_WOOD_ASH), 1: Wand(WAND_CORE_UNICORN, WAND_WOOD_ASH),
2: Wand(WAND_CORE_UNICORN, WAND_WOOD_ELDER), 2: Wand(WAND_CORE_UNICORN, WAND_WOOD_ELDER),
3: Wand(WAND_CORE_UNICORN, WAND_WOOD_APPLE), 3: Wand(WAND_CORE_UNICORN, WAND_WOOD_APPLE),