Create the external TicTacToeModel


Add the game logic as a standalone model under models

Create the model package and file

Add a new file at:

  • app/src/main/java/com/example/tictacmenu/models/TicTacToeModel.java

Paste the full model below. This keeps the rules outside the Activity so the UI stays simple.

package com.example.tictacmenu.models;

public class TicTacToeModel {
    private String[][] board;
    private String currentPlayer;

    public TicTacToeModel() {
        board = new String[3][3];
        currentPlayer = "X";
        resetGame();
    }

    public void changePlayer() {
        if (currentPlayer.equals("X")) {
            currentPlayer = "O";
        } else {
            currentPlayer = "X";
        }
    }

    public boolean isLegal(int row, int col) {
        if (row < 0 || row > 2 || col < 0 || col > 2) {
            return false;
        }
        return board[row][col].isEmpty();
    }

    public void makeMove(int row, int col) {
        if (isLegal(row, col)) {
            board[row][col] = currentPlayer;
        }
    }

    public boolean checkWin() {
        // TODO: Implement win checking logic
        return false;
    }

    public boolean isTie() {
        // TODO: Implement tie checking logic
        return false;
    }

    public void resetGame() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                board[i][j] = "";
            }
        }
        currentPlayer = "X";
    }

    public String getCurrentPlayer() {
        return currentPlayer;
    }
}

Quick notes

  • board holds the current game state.
  • currentPlayer starts at X and toggles with changePlayer().
  • checkWin() and isTie() are placeholders that will be completed later.

At the end of this step: the project has an external model file under models. Commit if you are tracking steps.