Python - Máy tính thống kê trò chơi
Vì vậy, đây là mã của một máy tính tôi đã tạo cho một trò chơi. Về cơ bản những gì máy tính làm là, nó tính toán chi phí mua chỉ số tiếp theo.
Vì vậy, tôi đã viết mã chỉ cho Loài người và chỉ số sức mạnh. Theo cách tôi thấy, tôi sẽ phải làm cho mỗi cuộc đua cùng một mã 3 lần cho mỗi chỉ số.
Tôi đã hy vọng sẽ có một cách ngắn hơn để giải quyết vấn đề này như
thay vì human.strengthtôi muốn nó race.strengthở đâu race = user_race.
Cảm ơn
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))
```
Trả lời
Nếu bạn chỉ đang cố gắng tạo một máy tính tương tác, các lớp học, v.v. là không cần thiết.
Đầu tiên, hãy tạo một bảng đơn giản cho phép bạn tra cứu số liệu thống kê dựa trên chủng tộc. Giúp con người (như bạn) dễ dàng chỉnh sửa, thực hiện thay đổi, thêm chủng tộc hoặc số liệu thống kê mới, v.v.
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",
]
Nó chỉ là một danh sách các chuỗi. Bây giờ hãy chuyển đổi nó thành một định dạng giúp bạn dễ dàng sử dụng trong một chương trình. Chúng tôi sẽ biến nó thành một dict của dicts vì vậy chúng tôi có thể tra cứu các giá trị sử dụng một cái gì đó như: stat["elf"]["base_agility"]. Đây là mã:
stats = {}
for row in traits:
row = row.strip().split()
stats[row[0]] = dict(zip(keys, map(int, row[1:])))
Giờ đây, mã của bạn để tính toán chi phí thay đổi sức mạnh của con người, có thể được chuyển thành một hàm chung hoạt động cho bất kỳ chủng tộc hoặc chỉ số nào:
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
Bạn sẽ nhận thấy mã lặp lại để tính toán total_present_costvà total_desired_cost. Những dòng đó có thể được cấu trúc lại thành một chức năng khác (một bài tập cho người đọc).
Bây giờ, chương trình chính chỉ thu thập dữ liệu đầu vào của người dùng, gọi hàm trên và in kết quả:
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}")