From 96e69e6daf00658c19254a19a56591fda9c84757 Mon Sep 17 00:00:00 2001 From: Kirill Date: Thu, 24 Dec 2020 15:37:10 +0200 Subject: [PATCH 1/8] module --- .gitignore | 1 + exceptions.py | 8 +++++ game.py | 67 +++++++++++++++++++++++++++++++++++++ models.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ settings.py | 9 +++++ 5 files changed, 176 insertions(+) create mode 100644 .gitignore create mode 100644 exceptions.py create mode 100644 game.py create mode 100644 models.py create mode 100644 settings.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/exceptions.py b/exceptions.py new file mode 100644 index 0000000..8854034 --- /dev/null +++ b/exceptions.py @@ -0,0 +1,8 @@ +class GameOver(Exception): + @classmethod + def write_score(cls,gm_score,gm_name): + with open("score.txt", "a") as f: + f.write(gm_name + " -" " Очков: " + str(gm_score) + "\n") + +class EnemyDown(Exception): + pass \ No newline at end of file diff --git a/game.py b/game.py new file mode 100644 index 0000000..7daff61 --- /dev/null +++ b/game.py @@ -0,0 +1,67 @@ +from exceptions import GameOver, EnemyDown +from models import Player, Enemy, Score +import settings + +score_list = [0] +name_list = [] + + +def play(): + while True: + command = input("What you want to do? START, SHOW, HELP, TOP, EXIT: ") + if command == "START": + next_lvl = 1 + names = input("Enter your name: ") + print() + while True: + if Player.validate_names(names) == 0: + print("Name already exists") + names = input("Enter your name: ") + else: + player = Player(names, settings.LIVES, 0, 0) + break + enemy = Enemy(1, 1) + while True: + try: + player.allowed_attacks = int(input("Choose your hero: 1-Wizard, 2-Warrior, 3-Rogue ")) + if player.validate_allowed_attacks() == 0: + print("The selected hero does not exist") + player.allowed_attacks = int(input("Choose your hero: 1-Wizard, 2-Warrior, 3-Rogue ")) + else: + player.attack(enemy) + player.allowed_attacks = int(input("Choose defense: 1-Wizard, 2-Warrior, 3-Rogue ")) + if player.validate_allowed_attacks() == 0: + print("The selected hero does not exist") + player.allowed_attacks = int(input("Choose defense: 1-Wizard, 2-Warrior, 3-Rogue ")) + else: + player.defence(enemy) + except EnemyDown: + next_lvl += 1 + print("------------------------------------") + print("You killed enemy!", "Score:", player.score, "Level:", enemy.level) + print("------------------------------------") + enemy = Enemy(enemy.level+next_lvl, enemy.lives+next_lvl) + player.score += 5 + score_list.append(player.score) + name_list.append(player.name) + if command == "SHOW": + with open("score.txt", "r") as f: + print(f.read()) + if command == "HELP": + print("All commands:", ",".join(settings.HELP)) + if command == "TOP": + Score.player_top() + if command == "EXIT": + raise KeyboardInterrupt + + +if __name__ == '__main__': + try: + play() + except GameOver: + GameOver.write_score(score_list[-1], name_list[-1]) + except KeyboardInterrupt: + pass + finally: + print() + print("Good bye!") \ No newline at end of file diff --git a/models.py b/models.py new file mode 100644 index 0000000..3f5f945 --- /dev/null +++ b/models.py @@ -0,0 +1,91 @@ +import random + + +from exceptions import GameOver, EnemyDown + + +class Enemy: + """Creates enemy""" + def __init__(self, level: int, lives: int): + self.level = level + self.lives = lives + + @staticmethod + def select_attack(): + return random.randint(1, 3) + + def decrease_lives(self): + self.lives -= 1 + print("Enemy HP:", self.lives) + if self.lives == 0: + raise EnemyDown + + +class Player: + """Creates player""" + def __init__(self, name: str, lives: int, score: int, allowed_attacks: int): + self.name = name + self.lives = lives + self.score = score + self.allowed_attacks = allowed_attacks + + @staticmethod + def fight(attack, defense): + if attack == defense: + return 0 + elif attack == 1 and defense == 3: + return -1 + elif attack == 2 and defense == 1: + return -1 + elif attack == 3 and defense == 2: + return -1 + else: + return 1 + + def decrease_lives(self): + self.lives -= 1 + if self.lives == 0: + raise GameOver + + def attack(self, enemy_obj): + self.fight(self.allowed_attacks, enemy_obj.select_attack()) + if self.fight(self.allowed_attacks, enemy_obj.select_attack()) == 0: + print("Attack: It's a draw!") + elif self.fight(self.allowed_attacks, enemy_obj.select_attack()) == 1: + print("Attack: You attacked successfully!") + enemy_obj.decrease_lives() + else: + print("Attack: You missed!") + + def defence(self, enemy_obj): + self.fight(enemy_obj.select_attack(), self.allowed_attacks) + if self.fight(enemy_obj.select_attack(), self.allowed_attacks) == 0: + print("Defence: It's a draw!") + elif self.fight(enemy_obj.select_attack(), self.allowed_attacks) == 1: + print("Defence: You missed!") + self.decrease_lives() + else: + print("Defence: You attacked successfully!") + self.score += 1 + + @classmethod + def validate_names(cls, names): + with open("score.txt", "r") as f: + for i in f.readlines(): + if names in i.split()[0]: + return False + + def validate_allowed_attacks(self): + if self.allowed_attacks > 3 and self.allowed_attacks >= 0: + return False + + +class Score: + """Shows the top ten players by score""" + @classmethod + def player_top(cls): + with open("score.txt", "r") as f: + lines = f.readlines() + lines.sort(key=lambda x: int(x.split()[-1]), reverse=True) + for i in lines[:10]: + print(i.strip()) diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..f6ea00a --- /dev/null +++ b/settings.py @@ -0,0 +1,9 @@ +LIVES = 1 + +HELP = { + "START", + "SHOW", + "HELP", + "EXIT", + "TOP" +} \ No newline at end of file From 4753f3c9c1f1b9cbf75d1567e626ffd47736fc97 Mon Sep 17 00:00:00 2001 From: Kiriill <72931844+hideman1231@users.noreply.github.com> Date: Thu, 24 Dec 2020 15:38:50 +0200 Subject: [PATCH 2/8] Delete requirements.txt --- requirements.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index e69de29..0000000 From e2699465b21ef266fd66c35b368c2e20a4aa25b2 Mon Sep 17 00:00:00 2001 From: Kirill Date: Thu, 24 Dec 2020 15:54:53 +0200 Subject: [PATCH 3/8] up --- .gitignore | 3 ++- exceptions.py | 13 +++++++------ game.py | 2 +- settings.py | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index ed8ebf5..341e31e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -__pycache__ \ No newline at end of file +__pycache__ +.idea \ No newline at end of file diff --git a/exceptions.py b/exceptions.py index 8854034..ee601d7 100644 --- a/exceptions.py +++ b/exceptions.py @@ -1,8 +1,9 @@ class GameOver(Exception): - @classmethod - def write_score(cls,gm_score,gm_name): - with open("score.txt", "a") as f: - f.write(gm_name + " -" " Очков: " + str(gm_score) + "\n") - + @classmethod + def write_score(cls, gm_score, gm_name): + with open("score.txt", "a") as f: + f.write(gm_name + " -" " Очков: " + str(gm_score) + "\n") + + class EnemyDown(Exception): - pass \ No newline at end of file + pass diff --git a/game.py b/game.py index 7daff61..db2c333 100644 --- a/game.py +++ b/game.py @@ -64,4 +64,4 @@ def play(): pass finally: print() - print("Good bye!") \ No newline at end of file + print("Good bye!") diff --git a/settings.py b/settings.py index f6ea00a..07ed8c4 100644 --- a/settings.py +++ b/settings.py @@ -6,4 +6,4 @@ "HELP", "EXIT", "TOP" -} \ No newline at end of file +} From d7f67fa22b4d5f397c7e43825e4ac2beb761261d Mon Sep 17 00:00:00 2001 From: Kirill Date: Fri, 25 Dec 2020 13:10:03 +0200 Subject: [PATCH 4/8] finally --- exceptions.py | 7 ++- game.py | 112 ++++++++++++++++++------------------ models.py | 153 +++++++++++++++++++++++++------------------------- score.txt | 1 + settings.py | 2 + 5 files changed, 142 insertions(+), 133 deletions(-) diff --git a/exceptions.py b/exceptions.py index ee601d7..97a4533 100644 --- a/exceptions.py +++ b/exceptions.py @@ -1,8 +1,11 @@ +"""exception module""" + class GameOver(Exception): + """finishes the game""" @classmethod def write_score(cls, gm_score, gm_name): - with open("score.txt", "a") as f: - f.write(gm_name + " -" " Очков: " + str(gm_score) + "\n") + with open("score.txt", "a") as file: + file.write(gm_name + " -" " Очков: " + str(gm_score) + "\n") class EnemyDown(Exception): diff --git a/game.py b/game.py index db2c333..1a5e9a3 100644 --- a/game.py +++ b/game.py @@ -1,3 +1,4 @@ +"""main executable""" from exceptions import GameOver, EnemyDown from models import Player, Enemy, Score import settings @@ -6,62 +7,63 @@ name_list = [] + def play(): - while True: - command = input("What you want to do? START, SHOW, HELP, TOP, EXIT: ") - if command == "START": - next_lvl = 1 - names = input("Enter your name: ") - print() - while True: - if Player.validate_names(names) == 0: - print("Name already exists") - names = input("Enter your name: ") - else: - player = Player(names, settings.LIVES, 0, 0) - break - enemy = Enemy(1, 1) - while True: - try: - player.allowed_attacks = int(input("Choose your hero: 1-Wizard, 2-Warrior, 3-Rogue ")) - if player.validate_allowed_attacks() == 0: - print("The selected hero does not exist") - player.allowed_attacks = int(input("Choose your hero: 1-Wizard, 2-Warrior, 3-Rogue ")) - else: - player.attack(enemy) - player.allowed_attacks = int(input("Choose defense: 1-Wizard, 2-Warrior, 3-Rogue ")) - if player.validate_allowed_attacks() == 0: - print("The selected hero does not exist") - player.allowed_attacks = int(input("Choose defense: 1-Wizard, 2-Warrior, 3-Rogue ")) - else: - player.defence(enemy) - except EnemyDown: - next_lvl += 1 - print("------------------------------------") - print("You killed enemy!", "Score:", player.score, "Level:", enemy.level) - print("------------------------------------") - enemy = Enemy(enemy.level+next_lvl, enemy.lives+next_lvl) - player.score += 5 - score_list.append(player.score) - name_list.append(player.name) - if command == "SHOW": - with open("score.txt", "r") as f: - print(f.read()) - if command == "HELP": - print("All commands:", ",".join(settings.HELP)) - if command == "TOP": - Score.player_top() - if command == "EXIT": - raise KeyboardInterrupt + while True: + command = input("What you want to do? START, SHOW, HELP, TOP, EXIT: ") + if command == "START": + next_lvl = 1 + names = input("Enter your name: ") + print() + while True: + if Player.validate_names(names) == 0: + print("Name already exists") + names = input("Enter your name: ") + else: + player = Player(names, settings.LIVES, 0, 0) + break + enemy = Enemy(1, 1) + while True: + try: + player.allowed_attacks = int(input("Choose your hero: 1-Wizard, 2-Warrior, 3-Rogue ")) + if player.validate_allowed_attacks() == 0: + print("The selected hero does not exist") + player.allowed_attacks = int(input("Choose your hero: 1-Wizard, 2-Warrior, 3-Rogue ")) + else: + player.attack(enemy) + player.allowed_attacks = int(input("Choose defense: 1-Wizard, 2-Warrior, 3-Rogue ")) + if player.validate_allowed_attacks() == 0: + print("The selected hero does not exist") + player.allowed_attacks = int(input("Choose defense: 1-Wizard, 2-Warrior, 3-Rogue ")) + else: + player.defence(enemy) + except EnemyDown: + next_lvl += 1 + print("------------------------------------") + print("You killed enemy!", "Score:", player.score, "Level:", enemy.level) + print("------------------------------------") + enemy = Enemy(enemy.level+next_lvl, enemy.lives+next_lvl) + player.score += 5 + score_list.append(player.score) + name_list.append(player.name) + if command == "SHOW": + with open("score.txt", "r") as file: + print(file.read()) + if command == "HELP": + print("All commands:", ",".join(settings.HELP)) + if command == "TOP": + Score.player_top() + if command == "EXIT": + raise KeyboardInterrupt if __name__ == '__main__': - try: - play() - except GameOver: - GameOver.write_score(score_list[-1], name_list[-1]) - except KeyboardInterrupt: - pass - finally: - print() - print("Good bye!") + try: + play() + except GameOver: + GameOver.write_score(score_list[-1], name_list[-1]) + except KeyboardInterrupt: + pass + finally: + print() + print("Good bye!") diff --git a/models.py b/models.py index 3f5f945..105cc93 100644 --- a/models.py +++ b/models.py @@ -1,3 +1,4 @@ +"""all logic""" import random @@ -5,87 +6,87 @@ class Enemy: - """Creates enemy""" - def __init__(self, level: int, lives: int): - self.level = level - self.lives = lives + """Creates enemy""" + def __init__(self, level: int, lives: int): + self.level = level + self.lives = lives - @staticmethod - def select_attack(): - return random.randint(1, 3) + @staticmethod + def select_attack(): + return random.randint(1, 3) - def decrease_lives(self): - self.lives -= 1 - print("Enemy HP:", self.lives) - if self.lives == 0: - raise EnemyDown + def decrease_lives(self): + self.lives -= 1 + print("Enemy HP:", self.lives) + if self.lives == 0: + raise EnemyDown class Player: - """Creates player""" - def __init__(self, name: str, lives: int, score: int, allowed_attacks: int): - self.name = name - self.lives = lives - self.score = score - self.allowed_attacks = allowed_attacks - - @staticmethod - def fight(attack, defense): - if attack == defense: - return 0 - elif attack == 1 and defense == 3: - return -1 - elif attack == 2 and defense == 1: - return -1 - elif attack == 3 and defense == 2: - return -1 - else: - return 1 - - def decrease_lives(self): - self.lives -= 1 - if self.lives == 0: - raise GameOver - - def attack(self, enemy_obj): - self.fight(self.allowed_attacks, enemy_obj.select_attack()) - if self.fight(self.allowed_attacks, enemy_obj.select_attack()) == 0: - print("Attack: It's a draw!") - elif self.fight(self.allowed_attacks, enemy_obj.select_attack()) == 1: - print("Attack: You attacked successfully!") - enemy_obj.decrease_lives() - else: - print("Attack: You missed!") - - def defence(self, enemy_obj): - self.fight(enemy_obj.select_attack(), self.allowed_attacks) - if self.fight(enemy_obj.select_attack(), self.allowed_attacks) == 0: - print("Defence: It's a draw!") - elif self.fight(enemy_obj.select_attack(), self.allowed_attacks) == 1: - print("Defence: You missed!") - self.decrease_lives() - else: - print("Defence: You attacked successfully!") - self.score += 1 - - @classmethod - def validate_names(cls, names): - with open("score.txt", "r") as f: - for i in f.readlines(): - if names in i.split()[0]: - return False - - def validate_allowed_attacks(self): - if self.allowed_attacks > 3 and self.allowed_attacks >= 0: - return False + """Creates player""" + def __init__(self, name: str, lives: int, score: int, allowed_attacks: int): + self.name = name + self.lives = lives + self.score = score + self.allowed_attacks = allowed_attacks + + @staticmethod + def fight(attack, defense): + if attack == defense: + return 0 + elif attack == 1 and defense == 3: + return -1 + elif attack == 2 and defense == 1: + return -1 + elif attack == 3 and defense == 2: + return -1 + else: + return 1 + + def decrease_lives(self): + self.lives -= 1 + if self.lives == 0: + raise GameOver + + def attack(self, enemy_obj): + self.fight(self.allowed_attacks, enemy_obj.select_attack()) + if self.fight(self.allowed_attacks, enemy_obj.select_attack()) == 0: + print("Attack: It's a draw!") + elif self.fight(self.allowed_attacks, enemy_obj.select_attack()) == 1: + print("Attack: You attacked successfully!") + enemy_obj.decrease_lives() + else: + print("Attack: You missed!") + + def defence(self, enemy_obj): + self.fight(enemy_obj.select_attack(), self.allowed_attacks) + if self.fight(enemy_obj.select_attack(), self.allowed_attacks) == 0: + print("Defence: It's a draw!") + elif self.fight(enemy_obj.select_attack(), self.allowed_attacks) == 1: + print("Defence: You missed!") + self.decrease_lives() + else: + print("Defence: You attacked successfully!") + self.score += 1 + + @classmethod + def validate_names(cls, names): + with open("score.txt", "r") as file: + for i in file.readlines(): + if names in i.split()[0]: + return False + + def validate_allowed_attacks(self): + if self.allowed_attacks > 3 and self.allowed_attacks >= 0: + return False class Score: - """Shows the top ten players by score""" - @classmethod - def player_top(cls): - with open("score.txt", "r") as f: - lines = f.readlines() - lines.sort(key=lambda x: int(x.split()[-1]), reverse=True) - for i in lines[:10]: - print(i.strip()) + """Shows the top ten players by score""" + @classmethod + def player_top(cls): + with open("score.txt", "r") as file: + lines = file.readlines() + lines.sort(key=lambda x: int(x.split()[-1]), reverse=True) + for i in lines[:10]: + print(i.strip()) diff --git a/score.txt b/score.txt index 1be574b..d3dbe86 100644 --- a/score.txt +++ b/score.txt @@ -18,3 +18,4 @@ ursa - Igor - Î÷êîâ: 6 Igor - Î÷êîâ: 9 fe - Î÷êîâ: 6 +kip[ - Î÷êîâ: 3 diff --git a/settings.py b/settings.py index 07ed8c4..79b7021 100644 --- a/settings.py +++ b/settings.py @@ -1,3 +1,5 @@ +"""const and command list""" + LIVES = 1 HELP = { From 449fdf0c51c3a6199bdfb5ecdc88cd1490135023 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sat, 26 Dec 2020 15:30:26 +0200 Subject: [PATCH 5/8] corrected --- game.py | 34 ++++++++++++++++++---------------- models.py | 2 +- score.txt | 21 +++++++++++++++++++-- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/game.py b/game.py index 1a5e9a3..646a1dc 100644 --- a/game.py +++ b/game.py @@ -3,8 +3,7 @@ from models import Player, Enemy, Score import settings -score_list = [0] -name_list = [] +player_list = [0] @@ -16,7 +15,7 @@ def play(): names = input("Enter your name: ") print() while True: - if Player.validate_names(names) == 0: + if Player.validate_names(names) is False: print("Name already exists") names = input("Enter your name: ") else: @@ -25,18 +24,22 @@ def play(): enemy = Enemy(1, 1) while True: try: - player.allowed_attacks = int(input("Choose your hero: 1-Wizard, 2-Warrior, 3-Rogue ")) - if player.validate_allowed_attacks() == 0: - print("The selected hero does not exist") + while True: player.allowed_attacks = int(input("Choose your hero: 1-Wizard, 2-Warrior, 3-Rogue ")) - else: - player.attack(enemy) + if player.validate_allowed_attacks() is False: + print("The selected hero does not exist") + continue + else: + player.attack(enemy) + break + while True: player.allowed_attacks = int(input("Choose defense: 1-Wizard, 2-Warrior, 3-Rogue ")) - if player.validate_allowed_attacks() == 0: - print("The selected hero does not exist") - player.allowed_attacks = int(input("Choose defense: 1-Wizard, 2-Warrior, 3-Rogue ")) - else: - player.defence(enemy) + if player.validate_allowed_attacks() is False: + print("The selected hero does not exist") + continue + else: + player.defence(enemy) + break except EnemyDown: next_lvl += 1 print("------------------------------------") @@ -44,8 +47,7 @@ def play(): print("------------------------------------") enemy = Enemy(enemy.level+next_lvl, enemy.lives+next_lvl) player.score += 5 - score_list.append(player.score) - name_list.append(player.name) + player_list.append(player) if command == "SHOW": with open("score.txt", "r") as file: print(file.read()) @@ -61,7 +63,7 @@ def play(): try: play() except GameOver: - GameOver.write_score(score_list[-1], name_list[-1]) + GameOver.write_score(player_list[-1].score, player_list[-1].name) except KeyboardInterrupt: pass finally: diff --git a/models.py b/models.py index 105cc93..ba4523c 100644 --- a/models.py +++ b/models.py @@ -77,7 +77,7 @@ def validate_names(cls, names): return False def validate_allowed_attacks(self): - if self.allowed_attacks > 3 and self.allowed_attacks >= 0: + if self.allowed_attacks not in [1,2,3]: return False diff --git a/score.txt b/score.txt index d3dbe86..f2848e7 100644 --- a/score.txt +++ b/score.txt @@ -15,7 +15,24 @@ Igor - lor - Î÷êîâ: 13 nisha - Î÷êîâ: 1 ursa - Î÷êîâ: 2 -Igor - Î÷êîâ: 6 -Igor - Î÷êîâ: 9 fe - Î÷êîâ: 6 kip[ - Î÷êîâ: 3 +gosha - Î÷êîâ: 5 +vala - Î÷êîâ: 10 +llll - Î÷êîâ: 10 +igol - Î÷êîâ: 10 +gg - Î÷êîâ: 10 +fjkfkf - Î÷êîâ: 6 +fjfjfj - Î÷êîâ: 2 +fkfkfkfk - Î÷êîâ: 1 +jfjfjf - Î÷êîâ: 6 +fkfkfkkf - Î÷êîâ: 11 +kiiie - Î÷êîâ: 1 +fpfpfppf - Î÷êîâ: 1 +fffjfjfj - Î÷êîâ: 17 +flflflflflf - Î÷êîâ: 6 +2 - Î÷êîâ: 1 +jffjfj - Î÷êîâ: 4 +fkfkfkfk2 - Î÷êîâ: 5 +qqqw - Î÷êîâ: 3 +sasuke - Î÷êîâ: 11 From b7810a020f326cb52472990bddb9f17956086d04 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 27 Dec 2020 21:19:44 +0200 Subject: [PATCH 6/8] corrected --- game.py | 11 ++++------- models.py | 6 +----- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/game.py b/game.py index 646a1dc..e2af450 100644 --- a/game.py +++ b/game.py @@ -14,13 +14,10 @@ def play(): next_lvl = 1 names = input("Enter your name: ") print() - while True: - if Player.validate_names(names) is False: - print("Name already exists") - names = input("Enter your name: ") - else: - player = Player(names, settings.LIVES, 0, 0) - break + while Player.validate_names(names) is False: + print("Name already exists") + names = input("Enter your name: ") + player = Player(names, settings.LIVES, 0, 0) enemy = Enemy(1, 1) while True: try: diff --git a/models.py b/models.py index ba4523c..daa6931 100644 --- a/models.py +++ b/models.py @@ -34,11 +34,7 @@ def __init__(self, name: str, lives: int, score: int, allowed_attacks: int): def fight(attack, defense): if attack == defense: return 0 - elif attack == 1 and defense == 3: - return -1 - elif attack == 2 and defense == 1: - return -1 - elif attack == 3 and defense == 2: + elif attack == 1 and defense == 3 or attack == 2 and defense == 1 or attack == 3 and defense == 2: return -1 else: return 1 From 59db6f3ab2f676e317cbf088c83ca2d48350ea56 Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 23 Feb 2021 10:34:20 +0200 Subject: [PATCH 7/8] tests --- test.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..6feeaab --- /dev/null +++ b/test.py @@ -0,0 +1,34 @@ +from unittest import TestCase +from unittest.mock import patch, Mock, mock_open + +from models import Enemy, Player + + +class TestEnemy(TestCase): + + @patch('models.Enemy.select_attack', return_value=1) + def test_select_attack(self, select_attack): + self.assertEqual(select_attack(), 1) + + +class TestPlayer(TestCase): + + def setUp(self): + self.enemy = Enemy(1, 1) + self.player = Player('test', 1, 1, 1) + self.mock = Mock() + + def test_fight(self): + self.assertEqual(self.player.fight(1, 1), 0) + self.assertEqual(self.player.fight(1, 3), -1) + self.assertEqual(self.player.fight(3, 1), 1) + + def test_validate_names(self): + self.assertIsNone(self.player.validate_names(self.player.name)) + + def test_validate_names_mock(self): + with patch('builtins.open', mock_open(read_data='name')): + self.assertIsNone(self.player.validate_names(self.player.name)) + + def test_validate_allowed_attacks(self): + self.assertIsNone(self.player.validate_allowed_attacks()) From d480691cc7a94a8843e18a0f495c3a4ce69bd02a Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 23 Feb 2021 10:59:34 +0200 Subject: [PATCH 8/8] test --- test.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test.py b/test.py index 6feeaab..0b8d397 100644 --- a/test.py +++ b/test.py @@ -1,7 +1,7 @@ from unittest import TestCase -from unittest.mock import patch, Mock, mock_open +from unittest.mock import patch, mock_open -from models import Enemy, Player +from models import Player class TestEnemy(TestCase): @@ -14,9 +14,7 @@ def test_select_attack(self, select_attack): class TestPlayer(TestCase): def setUp(self): - self.enemy = Enemy(1, 1) self.player = Player('test', 1, 1, 1) - self.mock = Mock() def test_fight(self): self.assertEqual(self.player.fight(1, 1), 0)