githubEdit

3. Design Tic-Tac-Toe

Difficulty: Beginner Topics: Object-Oriented Design, Board Game Logic, 2D Arrays Extension: NxN Board, AI Player (Minimax).

Phase 1: Requirements Gathering

Goals

  • Design a Tic-Tac-Toe game for two players.

  • Identify core entities: Board, Player, Piece.

  • Define game rules and winning conditions.

1. Who are the actors?

  • Players: Two players (X and O) who take turns.

  • Game System: Validates moves, checks for winners, and manages the turn flow.

2. What are the must-have features? (Core)

  • Game Board: 3x3 grid (extensible to NxN).

  • Move Logic: Players place their piece on an empty cell.

  • Win Detection: Check row, column, and diagonal for a match.

  • Draw Detection: Identify when the board is full with no winner.

3. What are the constraints?

  • Turn-based: Strict alternation between players.

  • Validity: Cannot place a piece on an occupied cell.


Phase 2: Use Cases

UC1: Make Move

Actor: Player Flow:

  1. Player chooses a cell (row, col).

  2. System validates if the cell is empty and within bounds.

  3. System places the piece.

  4. System checks for a win or draw.

  5. If game over, announce result.

  6. If not, switch turn to the next player.


Phase 3: Class Diagram

Step 1: Core Entities

  • Game: Manages the flow.

  • Board: Manages the grid.

  • Player: Has a name and a playing piece.

  • PlayingPiece: Represents 'X' or 'O'.

  • PieceType: Enum for X/O.

UML Diagram

spinner

Phase 4: Design Patterns

1. Strategy Pattern

  • Description: Defines a family of algorithms, encapsulates each one, and makes them interchangeable.

  • Why used: Enables switching between different playing strategies for an AI opponent (e.g., EasyStrategy (Random), HardStrategy (Minimax)) without modifying the Game class.

2. Factory Pattern

  • Description: A creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

  • Why used: Centralizes the creation of game pieces (PlayingPieceX, PlayingPieceO) or players, making the system extensible for new piece types or player types.


Phase 5: Code Key Methods

Java Implementation


Phase 6: Discussion

Scalability

Q: How to scale to NxN board?

  • A: "The logic in isThereWinner already uses board.size. The main change would be input validation and potentially the WIN condition (e.g., in a 100x100 grid, maybe 5 in a row wins)."

Undo Feature

Q: How to add Undo feature?

  • A: "Use the Command Pattern. Encapsulate each move as a Command object with execute() and undo() methods. Store these commands in a stack. When undo() is called, pop the stack and reverse the move (set cell to null)."

AI Opponent (SDE-3 Concept)

Q: How to implement an unbeatable single-player mode?

  • A: "Create an AIPlayer class. Use the Minimax Algorithm to determine the best move by simulating all possible future game states. Since Minimax explores the entire game tree ($O(b^d)$ where $b$ is branching factor and $d$ is depth), optimize it using Alpha-Beta Pruning. This eliminates branches that cannot possibly influence the final decision, drastically reducing the search space, especially crucial for larger $N \times N$ boards."


SOLID Principles Checklist

  • S (Single Responsibility): Board handles grid, Game handles flow, Player holds data.

  • O (Open/Closed): New PieceType (e.g., Triangle) can be added by extending PlayingPiece.

  • L (Liskov Substitution): PlayingPieceX works wherever PlayingPiece is expected.

  • I (Interface Segregation): Not heavily used here, but interfaces are kept simple.

  • D (Dependency Inversion): Game depends on PlayingPiece abstraction, not concrete X/O classes.

Last updated