Skip to content

Sweta040799/Tic-Tac-Toe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Machine Coding Round: Tic Tac Toe (Advanced)


🎯 Problem Statement (Advanced)

Design and implement a production-ready version of a Tic Tac Toe game that fulfills the following:

Core Requirements:

  1. Game is played between 2 players on a NxN board (not limited to 3x3).
  2. Support for dynamic board size (N >= 3).
  3. Players take turns placing their mark (X or O).
  4. Detect Win/Draw conditions dynamically for any size N.
  5. Validate inputs to prevent overwrites or invalid moves.

Advanced Requirements:

  1. Extendable to more than 2 players in the future.
  2. Pluggable win strategy (horizontal, vertical, diagonal, custom win rules).
  3. AI player (computer opponent) with simple strategies.
  4. Game logs should be printed after every move.
  5. Easily testable and extendable for mobile or web frontends.

📂 Folder Structure

src/
|-- model/               # Game Models (Player, Cell, Board)
|-- service/             # Business Logic (GameService)
|-- factory/             # Different Players
|-- strategy/            # Win Strategy (Interface + Implementations)
|-- util/                # Helper classes (Logger, Validator)
|-- controller/          # Entry point, input/output control
|-- Main.java            # Game bootstrapper

🧩 Design Patterns Used

  1. Strategy Pattern

    • Where: strategy/WinStrategy.java
    • Why: The game needs to check winning conditions in different ways (horizontal, vertical, diagonal, custom rules). Using Strategy Pattern allows us to plug in new winning rules without modifying the existing code, just by implementing the WinStrategy interface.
    • Benefit: Promotes Open/Closed Principle (OCP) and allows easy testing of win logic independently.
  2. Factory Pattern (optional but powerful)

    • Where: Can be added in PlayerFactory.
    • Why: Helps decouple the object creation logic from the main controller. Useful if we need to dynamically create HumanPlayer, AIPlayer, etc.
    • Benefit: Simplifies extensibility and object creation logic. Future-proof design.
  3. Singleton Pattern

    • Where: Logger.java
    • Why: There should be only one logging instance in the game. Singleton ensures controlled access to logging.
    • Benefit: Global point of access, ensures logging consistency.

🔐 SOLID Principles Usage

  • S - Single Responsibility Principle (SRP)

    • Each class has only one responsibility. GameService manages game flow, Board manages grid state, WinStrategy only checks win logic.
  • O - Open/Closed Principle (OCP)

    • New WinStrategy implementations can be added without modifying the game logic. The code is open to extension but closed for modification.
  • L - Liskov Substitution Principle (LSP)

    • Any class implementing the WinStrategy interface can be used in place of any other, without breaking the game logic. Ensures flexibility and reusability.
  • I - Interface Segregation Principle (ISP)

    • Interfaces like WinStrategy are small and specific. We avoid having large interfaces that force implementation of unused methods.
  • D - Dependency Inversion Principle (DIP)

    • GameService depends on abstractions like WinStrategy rather than concrete classes. This allows flexibility and improves testability.

📘 OOP Concepts Used

  • Encapsulation: Internal state like player details, cell status, and board dimensions are encapsulated.
  • Abstraction: WinStrategy abstracts the winning logic, and GameService abstracts the game engine.
  • Inheritance/Polymorphism: Multiple classes implement WinStrategy and can be used interchangeably.
  • Composition: Game is composed of models like Board, Player, and strategies to drive behavior.

Would you like me to continue with detailed class implementations in this document?

About

Design patterns used and explained

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages