Tic Tac Toe

Requirements

  1. A simple Tic Tac Toe game for two players.

  2. A 3x3 board where players alternately place their pieces (X or O).

  3. The game ends when:

    • A player gets 3 of their pieces in a row, column, or diagonal.

    • The board is full, resulting in a draw.


Class Diagram

+------------------+
|      Player      |
+------------------+
| - name: String   |
| - piece: PieceType|
+------------------+
| + getName(): String   |
| + getPiece(): PieceType|
+------------------+

+------------------+
|   PlayingPiece   |
+------------------+
| - type: PieceType|
+------------------+
| + getType(): PieceType|
+------------------+

+------------------+
|   PlayingPieceO  |
+------------------+
| - type: O        |
+------------------+
| + getType(): PieceType|
+------------------+

+------------------+
|   PlayingPieceX  |
+------------------+
| - type: X        |
+------------------+
| + getType(): PieceType|
+------------------+

+------------------+
|      Board       |
+------------------+
| - grid: PieceType[][]|
+------------------+
| + placePiece(row: int, col: int, piece: PlayingPiece): boolean|
| + isFull(): boolean|
| + display(): void|
| + checkWin(row: int, col: int, piece: PieceType): boolean|
+------------------+

+------------------+
|       Game       |
+------------------+
| - board: Board   |
| - players: Player[]|
| - currentPlayer: int|
+------------------+
| + play(): void   |
| + switchPlayer(): void|
+------------------+

Implementation

1. Enum: PieceType

Defines the type of pieces (X, O, or EMPTY).


2. Abstract Class: PlayingPiece

Represents a generic playing piece.


3. Concrete Classes: PlayingPieceO and PlayingPieceX

Define specific implementations for X and O.


4. Player Class

Represents a player with a name and their associated piece.


5. Board Class

Handles the game board and its operations.


6. Game Class

Manages the overall gameplay and switching turns.


7. Main Class

The entry point for the application.


Execution Flow

  1. Start: The game initializes with a 3x3 board and two players.

  2. Gameplay:

    • Each player takes turns placing their piece on the board.

    • The system validates moves and checks for a win or draw after every move.

  3. End:

    • A player wins if they align three of their pieces.

    • The game ends in a draw if the board is full with no winner.


Output Example

Game Starts:

Winning Condition:

Draw Condition:

Last updated