class Player:
action = [
("MP-Charge", +1, 0),
("Barrier", -1, 0),
("Weak Attack", -2, 1),
("Strong Attack", -3, 2)
]
def __init__(self, name: str, hp: int = 5, mp: int = 0):
self.name: str = name
self.hp: int = hp
self.mp: int = mp
def select_action(self):
print(f"Choise Your Action Number:")
for n, (act, cost, damage) in enumerate(self.action):
# MPに応じて何の技が出せるかの判定,可能な技を表示
if -cost <= self.mp:
print(f"No.{n} is {act:13}: {cost:+} and Damage: {damage}")
# MPに応じて何の技が出せるかの判定, 不可な選択であれば再選択
self.act: int = int(input("> "))
while 0 > self.act or self.act >= len(self.action) or -self.action[self.act][1] > self.mp:
self.act = int(input("choise correct number > "))
def take_action(self, other):
self.mp += self.action[self.act][1]
self.hp -= other.action[other.act][2]
def __str__(self):
return f"{self.name}, (HP, MP): ({self.hp}, {self.mp})"
def __lt__(self, other):
return self.hp < other.hp
def __le__(self, other):
return self.hp <= other.hp
def __gt__(self, other):
return self.hp > other.hp
def __ge__(self, other):
return self.hp >= other.hp
def __eq__(self, other):
return self.hp == other.hp
def __ne__(self, other):
return self.hp != other.hp
from random import randint
class CPU(Player):
def __init__(self, name: str, hp: int = 5, mp: int = 0):
super().__init__(name, hp, mp)
def select_action(self):
self.act = randint(0, len(self.action) - 1)
# MPに応じて何の技が出せるかの判定, 不可な選択であれば再選択
while -self.action[self.act][1] > self.mp:
self.act = randint(0, len(self.action) - 1)
return self.act
class Cheater(CPU):
def __init__(self, name: str = "Cheater", hp: int = 100, mp: int = 100):
super().__init__(name, hp, mp)
class GameMaster:
def __init__(self, p1: Player, p2: Player = CPU("CPU")):
self.turn = 1
self.p1: Player = p1
self.p2: Player = p2
def loop(self):
print(f"Turn {self.turn}: {self.p1} vs {self.p2}")
self.p1.select_action()
self.p2.select_action()
self.p1.take_action(self.p2)
self.p2.take_action(self.p1)
if max(self.p1.hp, self.p2.hp) <= 0:
print("Draw")
return False
if self.p1.hp <= 0 or self.p2.hp <= 0:
print(f"Winner: {max(self.p1, self.p2)}")
return False
self.turn += 1
return True
if __name__ == "__main__":
gm = GameMaster(Player("upskill"))
while gm.loop():
pass
gm = GameMaster(Player("PondVillege"), Cheater())
while gm.loop():
pass