System Design + Java Program for Tic-Tac-Toe (TicTac)

 

🎮 System Design + Java Program for Tic-Tac-Toe (TicTac)

In this mini system design, we’ll build a console-based Tic-Tac-Toe game in Java.

We’ll keep it:

  • Simple

  • Readable

  • Data-structure focused


🧠 High-Level Design

We’ll design 3 main parts:

  1. Board – stores the grid and game state

  2. Player – represents X and O

  3. Game – controls turns, input, and winning logic

Data Structures Used

  • char[][] board = new char[3][3];
    → 3x3 grid for the game

  • char currentPlayer = 'X';
    → Tracks who is playing now

  • Simple methods to:

    • printBoard()

    • makeMove(row, col)

    • checkWin()

    • isDraw()

No complex frameworks. Just core Java + basic data structures.


📦 Class Design Overview

1️⃣ Board Class

Responsibilities:

  • Initialize empty board

  • Print the board

  • Validate and update moves

  • Check winner / draw

2️⃣ TicTacToeGame Class

Responsibilities:

  • Run the game loop

  • Switch players

  • Take input from console

  • Use Board to perform actions


✅ Full Java Program – Simple Tic-Tac-Toe

import java.util.Scanner; /** * Simple Tic-Tac-Toe game using basic OOP and array data structure. * * Design: * - Board: handles the grid and game state * - TicTacToeGame: controls the game flow */ class Board { private char[][] grid; // 3x3 board public Board() { grid = new char[3][3]; // Initialize board with spaces for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { grid[i][j] = ' '; } } } // Print board in a simple format public void printBoard() { System.out.println("-------------"); for (int i = 0; i < 3; i++) { System.out.print("| "); for (int j = 0; j < 3; j++) { System.out.print(grid[i][j] + " | "); } System.out.println(); System.out.println("-------------"); } } // Try to place a move on the board public boolean placeMove(int row, int col, char playerSymbol) { // Check if row and col are in range and cell is empty if (row < 0 || row >= 3 || col < 0 || col >= 3) { System.out.println("Invalid position! Choose row and col between 0 and 2."); return false; } if (grid[row][col] != ' ') { System.out.println("Cell already taken! Choose another position."); return false; } grid[row][col] = playerSymbol; return true; } public boolean hasWinner(char playerSymbol) { // Check rows for (int i = 0; i < 3; i++) { if (grid[i][0] == playerSymbol && grid[i][1] == playerSymbol && grid[i][2] == playerSymbol) { return true; } } // Check columns for (int j = 0; j < 3; j++) { if (grid[0][j] == playerSymbol && grid[1][j] == playerSymbol && grid[2][j] == playerSymbol) { return true; } } // Check diagonals if (grid[0][0] == playerSymbol && grid[1][1] == playerSymbol && grid[2][2] == playerSymbol) { return true; } if (grid[0][2] == playerSymbol && grid[1][1] == playerSymbol && grid[2][0] == playerSymbol) { return true; } return false; } public boolean isFull() { // If any cell is an empty space, board is not full for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (grid[i][j] == ' ') { return false; } } } return true; } } public class TicTacToeGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Board board = new Board(); char currentPlayer = 'X'; boolean gameFinished = false; System.out.println("Welcome to Tic-Tac-Toe!"); System.out.println("Player X and Player O take turns."); System.out.println("Enter row and column (0, 1, or 2)."); System.out.println(); while (!gameFinished) { board.printBoard(); System.out.println("Player " + currentPlayer + ", it's your turn."); System.out.print("Enter row (0-2): "); int row = scanner.nextInt(); System.out.print("Enter col (0-2): "); int col = scanner.nextInt(); boolean moveSuccess = board.placeMove(row, col, currentPlayer); if (!moveSuccess) { // Invalid move, ask again continue; } // Check win if (board.hasWinner(currentPlayer)) { board.printBoard(); System.out.println("Player " + currentPlayer + " wins! 🎉"); gameFinished = true; } // Check draw else if (board.isFull()) { board.printBoard(); System.out.println("It's a draw! 🤝"); gameFinished = true; } else { // Switch player currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; } } scanner.close(); System.out.println("Game over. Thanks for playing!"); } }

🧩 How This Relates to System Design (in a Simple Way)

Even though this is a small console program, it still uses good design ideas:

  • Separation of Concerns

    • Board → game state & logic

    • TicTacToeGame → input + game loop

  • Encapsulation

    • Board hides its internal grid and exposes only methods

  • Data Structures

    • 2D array char[][] is the core structure

No comments:

Post a Comment

System Design + Java Program for Tic-Tac-Toe (TicTac)

  🎮 System Design + Java Program for Tic-Tac-Toe (TicTac) In this mini system design, we’ll build a console-based Tic-Tac-Toe game in Java...

Featured Posts