-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstate.py
More file actions
108 lines (76 loc) · 2.41 KB
/
Copy pathstate.py
File metadata and controls
108 lines (76 loc) · 2.41 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
96
97
98
99
100
101
102
103
104
105
106
107
108
from __future__ import annotations
import random
from abc import ABC, abstractmethod
class Game:
def __init__(self):
self.state = WelcomeScreenState(self)
def change_state(self, state):
self.state = state
class State(ABC):
def __init__(self, game):
self.game = game
print(f"Currently in {self} state")
@abstractmethod
def on_welcome_screen(self):
pass
@abstractmethod
def on_playing(self):
pass
@abstractmethod
def on_break(self):
pass
@abstractmethod
def on_end_game(self):
pass
class WelcomeScreenState(State):
def on_welcome_screen(self):
print("Currently on welcome screen")
def on_playing(self):
self.game.change_state(PlayingState(self.game))
def on_break(self):
print("From welcome to break not allowed")
def on_end_game(self):
print("From welcome to end game not allowed")
class PlayingState(State):
def on_welcome_screen(self):
print("From playing to welcome not allowed")
def on_playing(self):
print("Currently playing")
def on_break(self):
self.game.change_state(BreakState(self.game))
def on_end_game(self):
self.game.change_state(EndGameState(self.game))
class BreakState(State):
def on_welcome_screen(self):
print("From break to welcome not allowed")
def on_playing(self):
self.game.change_state(PlayingState(self.game))
def on_break(self):
print("Currently on break")
def on_end_game(self):
print("From break to end game not allowed")
class EndGameState(State):
def on_welcome_screen(self):
self.game.change_state(WelcomeScreenState(self.game))
def on_playing(self):
print("From end game to playing not allowed")
def on_break(self):
print("From end game to break now allowed")
def on_end_game(self):
print("Currently on end game")
if __name__ == '__main__':
game = Game()
for i in range(20):
state = random.randrange(4)
if state == 0:
print("Move to welcome")
game.state.on_welcome_screen()
elif state == 1:
print("Move to playing")
game.state.on_playing()
elif state == 2:
print("Move to break")
game.state.on_break()
else:
print("Move to end game")
game.state.on_end_game()