uprm-inso4116-2024-2025-s1 / semester-project-multiplayer-dominoes

semester-project-multiplayer-dominoes created by GitHub Classroom
9 stars 0 forks source link

[Lecture Topic Task] 4.14.5 Observer Pattern #403

Closed Adriana-Isabel closed 4 hours ago

Adriana-Isabel commented 3 days ago

Points: 8pts.

Adriana-Isabel commented 3 days ago

The Observer Pattern is a crucial aspect of the Domino project, enabling seamless synchronization between the game’s core logic and various interfaces that depend on real-time updates. This pattern allows multiple components (known as observers) to subscribe to a central entity (the subject) and automatically receive notifications when the state changes. This design decouples the game's logic from its interfaces, improving scalability and maintainability.

In the context of the Domino game, the Game Board acts as the Subject, responsible for maintaining the state of the board, the players’ turns, and the tiles in play. The observers, such as Player Interfaces, Scoreboards, and Spectator Views, register themselves with the Game Board to stay updated on state changes. This ensures that all users and components have synchronized and up-to-date information about the game.

For example:

Player Interfaces: When a player makes a move, the Game Board notifies all players of the updated board state and whose turn is next. This ensures that each player has an accurate representation of the game, including their available moves and tiles. Scoreboards: Each time a move is completed, the Game Board calculates the scores and notifies the Scoreboard Observer, which updates the display with the latest scores. This can be useful for spectators or tournament tracking. Spectator Views: Observers for spectators are notified in real-time of changes to the board state, enabling dynamic updates for those watching the game. This feature supports live matches, where spectators can follow the game without requiring manual refreshes. Chat System (Optional): Observers can also be extended to include live chat or notifications, ensuring that updates about game progress (e.g., game over, player disconnected) are shared with all participants. The Observer Pattern is especially beneficial for multiplayer games where synchronization is critical. It provides a mechanism to notify all relevant components of state changes without requiring them to continuously poll for updates. This results in better performance and user experience.

Advantages in the Domino Project Real-Time Updates: Ensures that players and spectators always see the current state of the game. Scalability: Adding new observers, such as advanced analytics or third-party integrations, does not require modifying the Game Board logic. Decoupled Design: Separates game logic from interface logic, making the system easier to debug, test, and extend. Dynamic Flexibility: Observers can dynamically attach and detach as needed. For instance, a new player joining as a spectator can easily register as an observer. Implementation in the Domino Project Subject (Game Board):

Maintains a list of observers and the current state of the game. Provides methods to attach, detach, and notify observers. Triggers updates to observers whenever a move is made or game state changes. Observers:

Implement an update(state) method that processes notifications. Can be customized to handle specific state changes, such as updating the player’s tile list, scoreboard, or board visualization. Flow Example:

A player places a tile on the board. The Game Board updates its state and calls the notify() method. All registered observers (e.g., Player Interfaces, Scoreboards, Spectator Views) are informed of the update and refresh their displays.

Adriana-Isabel commented 1 day ago

@AngelCIICMorales @Keiven-Soto I finished all of my LTTs.

Keiven-Soto commented 1 day ago

Once again, great work!

Adriana-Isabel commented 4 hours ago

@Keiven-Soto I updated the issue.