-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
95 lines (81 loc) · 2.49 KB
/
models.py
File metadata and controls
95 lines (81 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from random import randint
from exceptions import EnemyDown
from exceptions import GameOver
import settings
# Mag
# Fighter
# Robber
class Enemy(object):
level = 0
lives = 3
def __init__(self, level):
self.level = level
@staticmethod
def select_attack():
return randint(1, 3)
def decrease_lives(self):
self.lives -= 1
if self.lives == 0:
raise EnemyDown
class Player(object):
lives = 3
score = 0
allowed_attacks = 0
def __init__(self, name):
self.name = name
@staticmethod
def fight(attack, defense): # 1, 2, 3
round_result = 0
if attack == defense:
round_result = 0
elif attack == 1 and defense == 2:
round_result = 1 # 0, -1, 1
elif attack == 1 and defense == 3:
round_result = -1
elif attack == 2 and defense == 1:
round_result = -1
elif attack == 2 and defense == 3:
round_result = 1
elif attack == 3 and defense == 1:
round_result = 1
elif attack == 3 and defense == 2:
round_result = -1
return round_result
def decrease_lives(self):
self.lives -= 1
if self.lives == 0:
raise GameOver
def attack(self, enemy_obj):
while True:
print(settings.enter_attack_message)
player_attack = input()
if player_attack in ('1', '2', '3'):
break
enemy_attack = enemy_obj.select_attack()
result = self.fight(int(player_attack), enemy_attack)
if result == 0:
print(settings.draw_message)
elif result == 1:
print(settings.win_message)
self.score += 1
enemy_obj.decrease_lives()
else:
print(settings.defeat_message)
self.decrease_lives()
def defence(self, enemy_obj):
while True:
print(settings.enter_defense_message)
player_defence = input()
if player_defence in ('1', '2', '3'):
break
enemy_attack = enemy_obj.select_attack()
result = self.fight(int(player_defence), enemy_attack)
if result == 0:
print(settings.draw_message)
elif result == 1:
print(settings.win_message)
self.score += 1
enemy_obj.decrease_lives()
else:
print(settings.defeat_message)
self.decrease_lives()