-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate_method.py
55 lines (40 loc) · 1.17 KB
/
template_method.py
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
from abc import ABC
class Game(ABC):
def __init__(self, number_of_players):
self.number_of_players = number_of_players
self.current_player = 0
def run(self):
self.start()
while not self.have_winner:
self.take_turn()
print(f"Player {self.winning_player} wins!")
def start(self):
pass
@property
def have_winner(self):
pass
def take_turn(self):
pass
@property
def winning_player(self):
pass
class Chess(Game):
def __init__(self):
super().__init__(2)
self.max_turns = 10
self.turn = 1
def start(self):
print(f"Starting a game of chess with {self.number_of_players} players.")
@property
def have_winner(self):
return self.turn == self.max_turns
def take_turn(self):
print(f"Turn {self.turn} taken by player {self.current_player}")
self.turn += 1
self.current_player = 1 - self.current_player
@property
def winning_player(self):
return self.current_player
if __name__ == "__main__":
chess = Chess()
chess.run()