Create the external TicTacToeModel


Add the game logic as a standalone model under models

Back to 014b: add fragments to the drawer

למה מפרידים Model מן ה־Activity?

ה־Activity יודעת על Android: כפתורים, Toast ומחזור חיים. TicTacToeModel אמור לדעת על המשחק: איזה תא פנוי, מי בתור ומתי יש ניצחון. כאשר חוקי המשחק נמצאים במודל:

  • אפשר לבדוק אותם בבדיקת יחידה בלי אמולטור;
  • אפשר להחליף XML או Activity בלי לכתוב מחדש את החוקים;
  • גם משחק מקומי וגם משחק רשת יכולים להשתמש באותו ייצוג לוח.

המערך board[row][col] הוא מקור המצב. הכפתורים יציגו את המצב, אך אינם אמורים להיות המקום היחיד שבו הוא נשמר. זהו כלל שימושי מעבר למשחק: ה־UI מציג מצב ומעביר אירועים; המודל מגן על הכללים.

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. The current lesson chain does not complete them; they remain a focused rules exercise or a future extension chosen by the teacher.

At this checkpoint, checkWin() and isTie() deliberately always return false. The board can accept legal moves, but it cannot announce a win or tie. A placeholder that compiles is useful scaffolding; it is not a completed rule. Do not wait for a later networking lesson to implement these methods automatically: complete them as a separate rules exercise if your project needs them.

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

Continue

מורה־עזר ב־Gemini

פתחו את Gem: מעבדת TicTacToe: Model, View Binding ועיצוב כדי לקבל רמזים, שאלות אבחון והסברים המתאימים לשלב שבו אתם נמצאים.