-
Notifications
You must be signed in to change notification settings - Fork 0
init module #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
init module #4
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| """ | ||
| Playing engine for WARRIORS, ROBBERS AND WIZARDS GAME | ||
| """ | ||
|
|
||
| from models import Player, Enemy | ||
| from exceptions import EnemyDown, GameOver | ||
| import settings | ||
|
|
||
|
|
||
| def get_player_name(): | ||
| """Getting player name from terminal""" | ||
| player_name = "" | ||
| while not player_name: | ||
| player_name = input("ENTER YOUR NAME: ").strip() | ||
| return player_name | ||
|
|
||
|
|
||
| def play(): | ||
| """Playing engine""" | ||
| player_name = get_player_name() | ||
| player = Player(player_name) | ||
| enemy = Enemy() | ||
|
|
||
| while True: | ||
| try: | ||
| player.attack(enemy) | ||
| player.defence(enemy) | ||
| except EnemyDown: | ||
| player.score_points += settings.EXTRA_SCORE_POINTS_ADD | ||
| enemy = Enemy(enemy.level + 1) | ||
| except GameOver: | ||
| break | ||
| except KeyboardInterrupt: | ||
| raise GameOver(player, "KeyboardInterrupt") | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| play() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """ | ||
| Exceptions for WARRIORS, ROBBERS AND WIZARDS GAME | ||
| """ | ||
|
|
||
|
|
||
| class EnemyDown(Exception): | ||
| """Raised when enemy is defeated""" | ||
|
|
||
| def __init__(self, level): | ||
| super().__init__() | ||
| self.level = level | ||
| print(f"Enemy level {self.level} is defeated") | ||
|
|
||
| def __dir__(self): | ||
| return "EnemyDown Exception" | ||
|
|
||
|
|
||
| class GameOver(Exception): | ||
| """Raised when player is defeated""" | ||
|
|
||
| def __init__(self, player, *args): | ||
| super().__init__() | ||
| self.name = player.name | ||
| self.points = player.score_points | ||
| print(f"\n{self.name} is defeated \n" | ||
| f"SCORE POINTS: {self.points}") | ||
| if not args: | ||
| print("GOOD BYE!") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| """ | ||
| Behavior description for WARRIORS, ROBBERS AND WIZARDS GAME | ||
| """ | ||
|
|
||
| from random import randint | ||
|
|
||
| import settings | ||
| from exceptions import EnemyDown, GameOver | ||
|
|
||
|
|
||
| class Enemy: | ||
| """Create game enemy""" | ||
|
|
||
| def __init__(self, level: int = 1): | ||
| self.health = level | ||
| self.level = level | ||
|
|
||
| @staticmethod | ||
| def fight_choice_select(): | ||
| """Select person choice for enemy""" | ||
| random_person = str(randint(1, 3)) | ||
| return random_person | ||
|
|
||
| def decrease_health(self, score_down): | ||
|
||
| """Decrease enemy health""" | ||
| self.health -= score_down | ||
| if self.health > 0: | ||
| return self.health | ||
| else: | ||
| raise EnemyDown(self.level) | ||
|
|
||
| def select_attack(self): | ||
| """Select person choice for attack""" | ||
| attack_choice = self.fight_choice_select() | ||
| return attack_choice | ||
|
|
||
| def select_defence(self): | ||
| """Select person choice for defence""" | ||
| defence_choice = self.fight_choice_select() | ||
| return defence_choice | ||
|
|
||
|
|
||
| class Player: | ||
| """Create game player""" | ||
|
|
||
| def __init__(self, name: str): | ||
| self.name = name | ||
| self.health_points = settings.INITIAL_PLAYER_HEALTH | ||
| self.score_points = 0 | ||
|
|
||
| def __repr__(self): | ||
| return f"Player - {self.name}" | ||
|
|
||
| def write_to_file(self): | ||
| """Write game results to the file""" | ||
| data = f"Name - {self.name}, score points: {self.score_points}\n" | ||
| with open("scores.txt", "a") as file: | ||
| file.write(data) | ||
|
|
||
| @staticmethod | ||
| def fight_choice_select(): | ||
| """Select person choice for player""" | ||
|
|
||
| fight_choice = '' | ||
| while fight_choice not in ["1", "2", "3"]: | ||
| fight_choice = input( | ||
| "MAKE A FIGHT CHOICE FROM (WARRIOR - 1, ROBBER - 2, WIZARD - 3): " | ||
| ) | ||
| return fight_choice | ||
|
|
||
| def decrease_health(self, score_down): | ||
|
||
| """Decrease player health""" | ||
|
|
||
| self.health_points -= score_down | ||
| if self.health_points > 0: | ||
| return self.health_points | ||
| else: | ||
| self.write_to_file() | ||
| raise GameOver(self) | ||
|
|
||
| def select_attack(self): | ||
| """Select person choice for attack manually""" | ||
|
|
||
| attack_choice = self.fight_choice_select() | ||
| return attack_choice | ||
|
|
||
| def select_defence(self): | ||
| """Select person choice for defence manually""" | ||
|
|
||
| defence_choice = self.fight_choice_select() | ||
| return defence_choice | ||
|
|
||
| @staticmethod | ||
| def fight(attack_choice, defence_choice): | ||
| """Fight results determination""" | ||
| if [attack_choice, defence_choice] in [ | ||
| ["1", "2"], | ||
| ["2", "3"], | ||
| ["3", "1"] | ||
| ]: | ||
| return 'win' | ||
| elif attack_choice == defence_choice: | ||
| return 'draw' | ||
| else: | ||
| return 'loss' | ||
|
|
||
| def attack(self, enemy: Enemy): | ||
| """Attack an enemy""" | ||
| attack_choice = self.select_attack() | ||
| defence_choice = enemy.select_defence() | ||
| fight_result = self.fight(attack_choice, defence_choice) | ||
| if fight_result == 'win': | ||
| try: | ||
| print('YOUR ATTACK IS SUCCESSFUL!') | ||
| self.score_points += settings.SCORE_PLAYER_ADD | ||
| enemy.decrease_health(settings.SCORE_ENEMY_DOWN) | ||
| except EnemyDown: | ||
|
||
| raise | ||
| elif fight_result == 'loss': | ||
| print('YOUR ATTACK IS FAILED!') | ||
| elif fight_result == 'draw': | ||
| print("IT'S A DRAW!") | ||
|
|
||
| def defence(self, enemy: Enemy): | ||
| """Defence from an enemy""" | ||
|
|
||
| defence_choice = self.select_attack() | ||
| attack_choice = enemy.select_defence() | ||
| fight_result = self.fight(attack_choice, defence_choice) | ||
| if fight_result == 'win': | ||
| try: | ||
| print('YOUR DEFENCE IS FAILED!') | ||
| self.decrease_health(settings.SCORE_PLAYER_DOWN) | ||
| except GameOver: | ||
|
||
| raise | ||
| elif fight_result == 'loss': | ||
| print('YOUR DEFENCE IS SUCCESSFUL!') | ||
|
|
||
| elif fight_result == 'draw': | ||
| print("IT'S A DRAW!") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """ | ||
| Constants for WARRIORS, ROBBERS AND WIZARDS GAME | ||
| """ | ||
|
|
||
| INITIAL_PLAYER_HEALTH = 5 | ||
| SCORE_PLAYER_ADD = 1 | ||
| SCORE_PLAYER_DOWN = 1 | ||
| EXTRA_SCORE_POINTS_ADD = 2 | ||
| SCORE_ENEMY_DOWN = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Нормально.
Цей коментар більше для інформації - щоб не модифікувати стан обʼєкта напряму, краще зробити метод, що буде додавати гравцю бали. Якийсь
add_score(points).