Python - Calcolatore delle statistiche di gioco

Nov 15 2020

Quindi questo è il codice per una calcolatrice che ho creato per un gioco. In sostanza, ciò che fa la calcolatrice è calcolare il costo di acquisto della statistica successiva.

Quindi ho scritto il codice solo per la razza umana e la statistica di forza. Da come la vedo io, dovrò fare per ogni gara lo stesso codice 3 volte per ogni statistica.

Speravo che ci sarebbe stato un modo più breve per aggirare questo tipo

invece di human.strengthvorrei che fosse race.strengthdove race = user_race.

Grazie

class race:
    """The different races in the calculator"""
    def __init__(self, race, strength, agility, health, strength_cost, agility_cost, health_cost):
        self.race = race
        self.strength = strength
        self.agility = agility
        self.health = health
        self.strength_cost = strength_cost
        self.agility_cost = agility_cost
        self.health_cost = health_cost

human = race('Human', 15, 17, 18, 5, 3, 4) 
elf = race('Elf', 11, 21, 14, 4, 3, 5)
giant = race('Giant', 25, 11, 27, 4, 8, 3)

print("Human, Giant, Elf")
user_race = str(input("Enter your race:")).lower()
print("Strength, Agility, Health")
user_stat = str(input("Enter your desired stat:")).lower()
user_present_stat_value = int(input("Enter your present stat value:"))
user_desired_stat_value = int(input("Enter your desired stat value:"))

if user_race == 'human' and user_stat == 'strength':
    human_strength_present_statdif = (user_present_stat_value - human.strength) # difference of present stat with respect of base stat

    human_strength_desired_statdif = (user_desired_stat_value - human.strength) #difference of desired stat with respect of base stat
    
    human_strength_present_stat_cost = (human.strength_cost + (human_strength_present_statdif - 1) * human.strength_cost) #The cost of present stat stat 
    
    human_strength_total_present_cost = ((human_strength_present_statdif / 2) * (human.strength_cost + human_strength_present_stat_cost)) # The total cost from base stat to present stat
    
    human_strength_desired_stat_cost = (human.strength_cost + (human_strength_desired_statdif - 1) * human.strength_cost) #The cost of desired stat
    
    human_strength_total_desired_cost = ((human_strength_desired_statdif / 2) * (human.strength_cost + human_strength_desired_stat_cost)) # The total cost base stat to desired stat
    
    human_strength_net_cost = (human_strength_total_desired_cost - human_strength_total_present_cost) # The Net cost from the difference of Total desired stat and Total present stat
    
    print("Net cost: " + str(human_strength_net_cost))
```

Risposte

1 RootTwo Nov 16 2020 at 14:56

Se stai solo cercando di creare una calcolatrice interattiva, le lezioni, ecc. Non sono necessarie.

Innanzitutto, crea una semplice tabella che ti consenta di cercare le statistiche in base alla razza. Rendi facile per un essere umano (come te) modificarlo, apportare modifiche, aggiungere nuove razze o statistiche, ecc.

keys = "base_strength base_agility base_health strength_cost agility_cost health_cost".split()

traits = [
    #        base     base    base  strength agility health 
    #race  strength agility  health   cost     cost   cost
    "human    15       17      18      5        3      4",
    "elf      11       21      14      4        3      5",
    "giant    25       11      27      4        8      3",
]

È solo un elenco di stringhe. Ora trasformalo in un formato che lo renda facile da usare in un programma. La trasformeremo in un dict di dicts in modo che possiamo cercare i valori che utilizzano qualcosa come: stat["elf"]["base_agility"]. Ecco il codice:

stats = {}

for row in traits:
    row = row.strip().split()
    stats[row[0]] = dict(zip(keys, map(int, row[1:])))

Ora il tuo codice che calcola il costo del cambio di forza per un essere umano, può essere trasformato in una funzione generica che funziona per qualsiasi razza o statistica:

def calc_change_cost(race, stat_name, present_value, desired_value):
    base_value = stats[race][f"base_{stat_name}"]
    stat_cost = stats[race][f"{stat_name}_cost"]

    present_statdif = present_value - base_value
    present_stat_cost = stat_cost + (present_statdif - 1) * stat_cost
    total_present_cost = (present_statdif / 2) * (stat_cost + present_stat_cost)

    desired_statdif = desired_value - base_value
    desired_stat_cost = stat_cost + (desired_statdif - 1) * stat_cost
    total_desired_cost = (desired_statdif / 2) * (stat_cost + desired_stat_cost)

    net_cost = total_desired_cost - total_present_cost

    return net_cost

Noterai il codice ripetuto per il calcolo total_present_coste total_desired_cost. Quelle righe potrebbero essere modificate in un'altra funzione (un esercizio per il lettore).

Ora, il programma principale raccoglie solo gli input dell'utente, chiama la funzione sopra e stampa i risultati:

user_race = str(input("Enter your race (Human, Giant, Elf):")).lower()
user_stat = str(input("Enter your desired stat (Strength, Agility, Health):")).lower()
present_value = int(input("Enter your present stat value:"))
desired_value = int(input("Enter your desired stat value:"))

net_cost = calc_change_cost(user_race, user_stat, present_value, desired_value)
print(f"Net cost to change {user_race} {user_stat} from {present_value} to {desired_value}: {net_cost}")