-
Notifications
You must be signed in to change notification settings - Fork 0
Develop #2
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
DelDaria
wants to merge
4
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 #2
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| /.idea | ||
| /venv | ||
| /__pycache__ | ||
| /scores.txt | ||
| /test.txt | ||
|
|
||
|
|
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,39 @@ | ||
| import csv | ||
| import datetime | ||
|
|
||
|
|
||
| class GameOver(Exception): | ||
| @staticmethod | ||
| def add_score(name, score): | ||
| scores = [] | ||
| dict = { | ||
| 'id': '', | ||
| 'name': '', | ||
| 'score': '', | ||
| 'date': ''} | ||
| fieldnames = dict.keys() | ||
|
|
||
| with open('scores.txt', 'r', newline='') as csvfile: | ||
| reader = csv.DictReader(csvfile) | ||
| for row in reader: | ||
| row['score'] = int(row['score']) | ||
| scores.append(row) | ||
| new = { | ||
| 'name': name, | ||
| 'score': score, | ||
| 'date': str(datetime.datetime.today()) | ||
| } | ||
| scores.append(new) | ||
| scores = sorted(scores, key=lambda j: j['score'], reverse=True) | ||
| with open('scores.txt', 'w', newline='') as csvfile: | ||
| writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | ||
| writer.writeheader() | ||
| i = 0 | ||
| while i < len(scores) and i < 10: | ||
| scores[i]['id'] = i+1 | ||
| writer.writerow(scores[i]) | ||
| i += 1 | ||
|
|
||
|
|
||
| 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,77 @@ | ||
| import os | ||
| import models | ||
| import exceptions | ||
| import settings | ||
|
|
||
|
|
||
| def start(name): | ||
| player = models.Player(name) | ||
| level = 1 | ||
| enemy = models.Enemy(level) | ||
| while True: | ||
| attack = 0 | ||
| while str(attack) not in ['1', '2', '3']: | ||
| attack = input('Choose fighter. Enter "1" for Waterbender, "2" for Firebender, "3" for Earthbender: ') | ||
|
|
||
| player.actor = int(attack) | ||
| enemy.select_attack() | ||
|
|
||
| try: | ||
| print('_'*46) | ||
| attack_result = player.attack(enemy) | ||
| print(attack_result) | ||
| defence_result = player.defence(enemy) | ||
| print(defence_result) | ||
| print('_'*46) | ||
|
|
||
| except exceptions.EnemyDown: | ||
| level += 1 | ||
| player.score += 5 | ||
| print("You attacked successfully! Score is {}".format(player.score)) | ||
| print("{} wins! Level up to {}.".format(name, level)) | ||
| print('_'*46) | ||
| enemy = models.Enemy(level) | ||
| except exceptions.GameOver as err: | ||
| print("You were hit!") | ||
| print('{} dies! Score is {}.'.format(name, player.score)) | ||
| err.add_score(name, player.score) | ||
| break | ||
|
|
||
|
|
||
| def show_help(): | ||
| print(settings.ALLOWED_COMMAND) | ||
|
|
||
|
|
||
| def show_scores(): | ||
| with open('scores.txt', 'r') as f: | ||
| if os.path.getsize('scores.txt') == 0: | ||
| print('There are no results yet!') | ||
| else: | ||
| line = f.readlines() | ||
| print(' '.join(line)) | ||
|
|
||
|
|
||
| def play(): | ||
| name = input('Enter your Name: ') | ||
| while True: | ||
| act = input('Enter "Start" for beginning or "Help" for command list: ') | ||
| act = act.lower() | ||
| if act == "start": | ||
| start(name) | ||
| elif act == 'show scores': | ||
| show_scores() | ||
| elif act == "exit": | ||
| raise exceptions.GameOver | ||
| else: | ||
| show_help() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| try: | ||
| play() | ||
| except exceptions.GameOver as err: | ||
| pass | ||
| except KeyboardInterrupt: | ||
| pass | ||
| finally: | ||
| 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,73 @@ | ||
| import random | ||
| import exceptions | ||
| import settings | ||
| from datetime import datetime | ||
|
|
||
|
|
||
| class Enemy: | ||
| level = None | ||
| lives = None | ||
| actor = None | ||
|
|
||
| def __init__(self, level): | ||
| self.level = level | ||
| self.lives = level | ||
|
|
||
| def select_attack(self): | ||
| random.seed(datetime.now()) | ||
| self.actor = random.randint(1, 3) | ||
|
|
||
| def decrease_lives(self): | ||
| if self.lives > 1: | ||
| self.lives -= 1 | ||
| else: | ||
| raise exceptions.EnemyDown | ||
|
|
||
|
|
||
| class Player: | ||
| name = None | ||
| lives = None | ||
| score = 0 | ||
| actor = None | ||
|
|
||
| def __init__(self, name): | ||
| self.name = name | ||
| self.lives = settings.LIVES | ||
|
|
||
| @staticmethod | ||
| def fight(attack_side, defense_side): | ||
| if attack_side.actor == defense_side.actor: | ||
| return 0 | ||
| elif attack_side.actor == 3 and defense_side.actor == 1: | ||
| return 1 | ||
| elif attack_side.actor < defense_side.actor: | ||
| return 1 | ||
| else: | ||
| return -1 | ||
|
|
||
| def decrease_lives(self): | ||
| if self.lives > 1: | ||
| self.lives -= 1 | ||
| else: | ||
| raise exceptions.GameOver | ||
|
|
||
| def attack(self, enemy): | ||
| res = self.fight(self, enemy) | ||
| if res == 0: | ||
| return "It's a draw!" | ||
| elif res == 1: | ||
| self.score += 1 | ||
| enemy.decrease_lives() | ||
| return "You attacked successfully! Score is {}".format(self.score) | ||
| elif res == -1: | ||
| return "You missed!" | ||
|
|
||
| def defence(self, enemy): | ||
| res = self.fight(enemy, self) | ||
| if res == 0: | ||
| return "It's a draw!" | ||
| elif res == 1: | ||
| self.decrease_lives() | ||
| return "You were hit!" | ||
| elif res == -1: | ||
| return "You evaded!" |
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 @@ | ||
| LIVES = 1 | ||
| ALLOWED_COMMAND = 'Start, Help, Show Scores, Exit' |
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.
Uh oh!
There was an error while loading. Please reload this page.