-
Notifications
You must be signed in to change notification settings - Fork 0
Develop #1
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
Open
hideman1231
wants to merge
9
commits into
master
Choose a base branch
from
develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Develop #1
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
96e69e6
module
hideman1231 4753f3c
Delete requirements.txt
hideman1231 e269946
up
hideman1231 a20360e
Merge branch 'develop' of https://github.com/hideman1231/OOP-module i…
hideman1231 d7f67fa
finally
hideman1231 449fdf0
corrected
hideman1231 b7810a0
corrected
hideman1231 59db6f3
tests
hideman1231 d480691
test
hideman1231 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| __pycache__ | ||
| .idea |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| """exception module""" | ||
|
|
||
| class GameOver(Exception): | ||
| """finishes the game""" | ||
| @classmethod | ||
| def write_score(cls, gm_score, gm_name): | ||
| with open("score.txt", "a") as file: | ||
| file.write(gm_name + " -" " Очков: " + str(gm_score) + "\n") | ||
|
|
||
|
|
||
| class EnemyDown(Exception): | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| """main executable""" | ||
| from exceptions import GameOver, EnemyDown | ||
| from models import Player, Enemy, Score | ||
| import settings | ||
|
|
||
| player_list = [0] | ||
|
|
||
|
|
||
|
|
||
| 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 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: | ||
| while True: | ||
| player.allowed_attacks = int(input("Choose your hero: 1-Wizard, 2-Warrior, 3-Rogue ")) | ||
| 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() is False: | ||
| print("The selected hero does not exist") | ||
| continue | ||
| else: | ||
| player.defence(enemy) | ||
| break | ||
| 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 | ||
| player_list.append(player) | ||
| 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(player_list[-1].score, player_list[-1].name) | ||
| except KeyboardInterrupt: | ||
| pass | ||
| finally: | ||
| print() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| print("Good bye!") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| """all logic""" | ||
| 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 or attack == 2 and defense == 1 or attack == 3 and defense == 2: | ||
| return -1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or? |
||
| 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 not in [1,2,3]: | ||
| return False | ||
|
|
||
|
|
||
| class Score: | ||
| """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()) | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| """const and command list""" | ||
|
|
||
| LIVES = 1 | ||
|
|
||
| HELP = { | ||
| "START", | ||
| "SHOW", | ||
| "HELP", | ||
| "EXIT", | ||
| "TOP" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from unittest import TestCase | ||
| from unittest.mock import patch, mock_open | ||
|
|
||
| from models import 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.player = Player('test', 1, 1, 1) | ||
|
|
||
| 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()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
?