-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-comprehensive-oop-system.py
More file actions
516 lines (389 loc) · 16.1 KB
/
05-comprehensive-oop-system.py
File metadata and controls
516 lines (389 loc) · 16.1 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
"""Question: Implement a comprehensive OOP system that demonstrates multiple design patterns
working together. Create a simple game engine that uses Observer, Strategy, State, and Factory patterns.
"""
# LEARNING CHALLENGE
#
# Before looking at any solution below, please try to solve this yourself first!
#
# Tips for success:
# - Read the question carefully
# - Think about what classes and methods you need
# - Start with a simple implementation
# - Test your code step by step
# - Don't worry if it's not perfect - learning is a process!
#
# Remember: The best way to learn programming is by doing, not by reading solutions!
#
# Take your time, experiment, and enjoy the learning process!
# Try to implement your solution here:
# (Write your code below this line)
# HINT SECTION (Only look if you're really stuck!)
#
# Think about:
# - How can multiple design patterns work together in a real system?
# - What would be a good example that uses Observer, Strategy, State, and Factory?
# - How do you design classes that are both cohesive and loosely coupled?
# - What are the benefits of combining different patterns?
#
# Remember: Start simple and build up complexity gradually!
# ===============================================================================
# STEP-BY-STEP SOLUTION
# ===============================================================================
#
# CLASSROOM-STYLE WALKTHROUGH
#
# Let's solve this problem step by step, just like in a programming class!
# Each step builds upon the previous one, so you can follow along and understand
# the complete thought process.
#
# ===============================================================================
# Step 1: Define the Observer pattern for game events
# ===============================================================================
# Explanation:
# Let's start with the Observer pattern to handle game events like score changes,
# level completion, and player actions.
class GameEventObserver:
def update(self, event_type, data):
raise NotImplementedError("Subclasses must implement update")
class GameEventSubject:
def __init__(self):
self._observers = []
def add_observer(self, observer):
self._observers.append(observer)
def remove_observer(self, observer):
if observer in self._observers:
self._observers.remove(observer)
def notify_observers(self, event_type, data):
for observer in self._observers:
observer.update(event_type, data)
# What we accomplished in this step:
# - Created Observer pattern foundation for game events
# - Defined interfaces for observers and subjects
# Step 2: Implement Strategy pattern for different AI behaviors
# ===============================================================================
# Explanation:
# Now let's add the Strategy pattern to handle different AI behaviors
# for game entities.
class AIStrategy:
def execute(self, entity, game_state):
raise NotImplementedError("Subclasses must implement execute")
class AggressiveAI(AIStrategy):
def execute(self, entity, game_state):
return f"{entity.name} attacks aggressively!"
class DefensiveAI(AIStrategy):
def execute(self, entity, game_state):
return f"{entity.name} defends cautiously!"
class RandomAI(AIStrategy):
def execute(self, entity, game_state):
import random
actions = ["moves randomly", "waits", "explores"]
action = random.choice(actions)
return f"{entity.name} {action}!"
# What we accomplished in this step:
# - Created Strategy pattern for AI behaviors
# - Implemented different AI strategies (aggressive, defensive, random)
# Step 3: Add State pattern for game entity states
# ===============================================================================
# Explanation:
# Let's implement the State pattern to manage different states that
# game entities can be in (idle, moving, attacking, etc.).
class EntityState:
def handle_input(self, entity, input_type):
raise NotImplementedError("Subclasses must implement handle_input")
def update(self, entity):
raise NotImplementedError("Subclasses must implement update")
def get_state_name(self):
raise NotImplementedError("Subclasses must implement get_state_name")
class IdleState(EntityState):
def handle_input(self, entity, input_type):
if input_type == "move":
entity.change_state(MovingState())
return "Started moving"
elif input_type == "attack":
entity.change_state(AttackingState())
return "Started attacking"
return "Remaining idle"
def update(self, entity):
return f"{entity.name} is idle"
def get_state_name(self):
return "Idle"
class MovingState(EntityState):
def handle_input(self, entity, input_type):
if input_type == "stop":
entity.change_state(IdleState())
return "Stopped moving"
elif input_type == "attack":
entity.change_state(AttackingState())
return "Started attacking while moving"
return "Continuing to move"
def update(self, entity):
return f"{entity.name} is moving"
def get_state_name(self):
return "Moving"
class AttackingState(EntityState):
def handle_input(self, entity, input_type):
if input_type == "stop":
entity.change_state(IdleState())
return "Stopped attacking"
return "Still attacking"
def update(self, entity):
# Attack for a limited time, then return to idle
entity.attack_timer -= 1
if entity.attack_timer <= 0:
entity.change_state(IdleState())
return f"{entity.name} finished attacking"
return f"{entity.name} is attacking"
def get_state_name(self):
return "Attacking"
# What we accomplished in this step:
# - Created State pattern for entity states
# - Implemented different states with transitions and behaviors
# Step 4: Create Factory pattern for game entity creation
# ===============================================================================
# Explanation:
# Now let's implement the Factory pattern to create different types
# of game entities with appropriate configurations.
class GameEntity:
def __init__(self, name, entity_type):
self.name = name
self.entity_type = entity_type
self.health = 100
self.attack_power = 10
self.state = IdleState()
self.ai_strategy = None
self.attack_timer = 0
def change_state(self, new_state):
self.state = new_state
def set_ai_strategy(self, strategy):
self.ai_strategy = strategy
def handle_input(self, input_type):
return self.state.handle_input(self, input_type)
def update(self):
return self.state.update(self)
def execute_ai(self, game_state):
if self.ai_strategy:
return self.ai_strategy.execute(self, game_state)
return f"{self.name} has no AI"
def get_info(self):
return {
'name': self.name,
'type': self.entity_type,
'health': self.health,
'state': self.state.get_state_name(),
'ai': type(self.ai_strategy).__name__ if self.ai_strategy else "None"
}
class EntityFactory:
@staticmethod
def create_player(name):
player = GameEntity(name, "Player")
player.health = 150
player.attack_power = 15
# Players don't need AI
return player
@staticmethod
def create_enemy(name, difficulty="normal"):
enemy = GameEntity(name, "Enemy")
if difficulty == "easy":
enemy.health = 50
enemy.attack_power = 5
enemy.set_ai_strategy(DefensiveAI())
elif difficulty == "normal":
enemy.health = 75
enemy.attack_power = 8
enemy.set_ai_strategy(RandomAI())
elif difficulty == "hard":
enemy.health = 100
enemy.attack_power = 12
enemy.set_ai_strategy(AggressiveAI())
return enemy
@staticmethod
def create_npc(name, role="merchant"):
npc = GameEntity(name, "NPC")
npc.health = 80
npc.attack_power = 0 # NPCs don't attack
if role == "merchant":
npc.set_ai_strategy(DefensiveAI())
elif role == "guard":
npc.set_ai_strategy(AggressiveAI())
else:
npc.set_ai_strategy(RandomAI())
return npc
# What we accomplished in this step:
# - Created Factory pattern for entity creation
# - Implemented different entity types with appropriate configurations
# Step 5: Create the main Game class that ties everything together
# ===============================================================================
# Explanation:
# Let's create the main Game class that uses all the patterns together
# and includes observers for game events.
class ScoreObserver(GameEventObserver):
def __init__(self):
self.score = 0
def update(self, event_type, data):
if event_type == "enemy_defeated":
self.score += data.get("points", 10)
print(f"Score updated! Current score: {self.score}")
elif event_type == "level_complete":
self.score += data.get("bonus", 100)
print(f"Level bonus! Current score: {self.score}")
class HealthObserver(GameEventObserver):
def update(self, event_type, data):
if event_type == "player_damaged":
player = data.get("player")
damage = data.get("damage", 0)
print(f"Player took {damage} damage! Health: {player.health}")
elif event_type == "player_healed":
player = data.get("player")
healing = data.get("healing", 0)
print(f"Player healed {healing} points! Health: {player.health}")
class Game(GameEventSubject):
def __init__(self):
super().__init__()
self.entities = []
self.turn_count = 0
self.game_state = {"level": 1, "difficulty": "normal"}
def add_entity(self, entity):
self.entities.append(entity)
print(f"Added {entity.name} ({entity.entity_type}) to the game")
def remove_entity(self, entity):
if entity in self.entities:
self.entities.remove(entity)
print(f"Removed {entity.name} from the game")
def process_turn(self):
self.turn_count += 1
print(f"\n=== Turn {self.turn_count} ===")
for entity in self.entities:
# Update entity state
state_message = entity.update()
print(f" {state_message}")
# Execute AI if entity has one
if entity.ai_strategy:
ai_message = entity.execute_ai(self.game_state)
print(f" {ai_message}")
def player_action(self, player_name, action):
player = self._find_entity(player_name)
if player and player.entity_type == "Player":
if action == "attack":
player.attack_timer = 3 # Attack for 3 turns
result = player.handle_input(action)
print(f"{player_name}: {result}")
# Simulate combat if attacking
if action == "attack":
self._simulate_combat(player)
else:
print(f"Player {player_name} not found")
def _find_entity(self, name):
for entity in self.entities:
if entity.name == name:
return entity
return None
def _simulate_combat(self, attacker):
# Find an enemy to attack
enemies = [e for e in self.entities if e.entity_type == "Enemy"]
if enemies:
target = enemies[0] # Attack first enemy
damage = attacker.attack_power
target.health -= damage
print(f" {attacker.name} attacks {target.name} for {damage} damage!")
if target.health <= 0:
print(f" {target.name} is defeated!")
self.remove_entity(target)
self.notify_observers("enemy_defeated", {"points": 20})
def get_game_status(self):
status = {
"turn": self.turn_count,
"entities": len(self.entities),
"players": len([e for e in self.entities if e.entity_type == "Player"]),
"enemies": len([e for e in self.entities if e.entity_type == "Enemy"]),
"npcs": len([e for e in self.entities if e.entity_type == "NPC"])
}
return status
# What we accomplished in this step:
# - Created main Game class that integrates all patterns
# - Added observers for score and health tracking
# - Implemented turn-based gameplay with combat
# Step 6: Test the complete system
# ===============================================================================
# Explanation:
# Let's test our complete game system that demonstrates all four patterns
# working together.
print("=== Testing Complete OOP Game System ===")
# Create the game and add observers
game = Game()
score_observer = ScoreObserver()
health_observer = HealthObserver()
game.add_observer(score_observer)
game.add_observer(health_observer)
# Create entities using Factory pattern
print("\nCreating game entities:")
player = EntityFactory.create_player("Hero")
enemy1 = EntityFactory.create_enemy("Goblin", "easy")
enemy2 = EntityFactory.create_enemy("Orc", "normal")
enemy3 = EntityFactory.create_enemy("Dragon", "hard")
npc = EntityFactory.create_npc("Shopkeeper", "merchant")
# Add entities to game
game.add_entity(player)
game.add_entity(enemy1)
game.add_entity(enemy2)
game.add_entity(enemy3)
game.add_entity(npc)
# Display initial status
print(f"\nGame Status: {game.get_game_status()}")
print("\nEntity Information:")
for entity in game.entities:
info = entity.get_info()
print(f" {info}")
# Simulate gameplay
print("\n=== Starting Gameplay ===")
# Turn 1: Player starts moving
game.player_action("Hero", "move")
game.process_turn()
# Turn 2: Player attacks
game.player_action("Hero", "attack")
game.process_turn()
# Turn 3: Continue attacking
game.process_turn()
# Turn 4: Player stops
game.player_action("Hero", "stop")
game.process_turn()
# Turn 5: Player attacks again
game.player_action("Hero", "attack")
game.process_turn()
print(f"\nFinal Game Status: {game.get_game_status()}")
print(f"Final Score: {score_observer.score}")
# What we accomplished in this step:
# - Demonstrated all four patterns working together in a cohesive system
# - Observer pattern: Score and health tracking
# - Strategy pattern: Different AI behaviors for entities
# - State pattern: Entity state management (idle, moving, attacking)
# - Factory pattern: Creating different types of entities
# - Showed how patterns can complement each other in real applications
# ===============================================================================
# CONGRATULATIONS!
#
# You've successfully completed the comprehensive OOP solution!
#
# Key concepts learned:
# - Integrating multiple design patterns in a single system
# - Observer pattern for event handling and notifications
# - Strategy pattern for interchangeable algorithms (AI behaviors)
# - State pattern for managing object state transitions
# - Factory pattern for object creation with different configurations
# - How patterns work together to create flexible, maintainable code
# - Real-world application of OOP principles in game development
#
# This example demonstrates:
# - Separation of concerns (each pattern handles a specific responsibility)
# - Loose coupling (patterns interact through well-defined interfaces)
# - High cohesion (related functionality is grouped together)
# - Extensibility (easy to add new AI strategies, states, entity types, observers)
# - Maintainability (changes to one pattern don't affect others)
#
# Try it yourself:
# 1. Start with Step 1 and code along
# 2. Test each step before moving to the next
# 3. Understand WHY each pattern is used and how they interact
# 4. Experiment with modifications (add new AI strategies, states, entity types!)
#
# Remember: The best way to learn is by doing!
# ===============================================================================