Skip to main content
Rollback to Revision 1
Source Link
pacmaninbw
  • 24.9k
  • 11
  • 46
  • 109
import java.util.Scanner;

public class Board {
    private char [] [] board;
    private final int SIZE;

    private char currentPlayer;

    private int movesMade;validMoves;

    private static final char PLAYER_X = 'X';
    private static final char PLAYER_O = 'O';
    private static final char EMPTY_CELL = '.';

    private static final int DEFAULT_SIZE = 3;



    public Board() {
        this(DEFAULT_SIZE);
    }
    public Board(int size) {
        this.SIZE = size;
        reset();
    }


    public void reset() {
        board = new char [SIZE][SIZE];
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                board[i][j] = EMPTY_CELL;
            }
        }
        currentPlayer = PLAYER_X;
        movesMadevalidMoves = 0;
    }
    void togglePlayer() {
        currentPlayer = (currentPlayer == PLAYER_X ? PLAYER_O : PLAYER_X);
    }



    public char getCurrentPlayer() {
        return currentPlayer;
    }


    boolean isBoardFull() {
        return movesMadevalidMoves == SIZE * SIZE;
    }
    public void print() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }




    int [] getValidUserInputgetValidCell() {
        int row, col;
        boolean boardPrinted = true;
        do {
            // print the board when user makes  an invalid move
            if (!boardPrinted)
                print();
            System.out.println("It's " + currentPlayer + " turn");
            System.out.println("Enter row and column number you want to play: ");
            System.out.println("Please choose an empty cell from 1 to " + SIZE + " ");
            Scanner sc = new Scanner(System.in);
            row = sc.nextInt() - 1;
            col = sc.nextInt() - 1;

            boardPrinted = false;
        } while(isValidCell(row, col));



        return new int [] {row, col};


    }

    boolean isValidCell(int row, int col) {
        return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] != EMPTY_CELL;
    }
    public boolean checkWin() {
        // row check
   Check rows, columns, and diagonals for (int i = 0; i < SIZE; i++) {
            booleana win = true;
     
        for (int ji = 0; ji < SIZE; j++i++) {
                if (board[i][j] !=// currentPlayer)Check {row
                   if win(board[i][0] != false;
                }
            }
     EMPTY_CELL && board[i][0] == board[i][1] && board[i][1] if== (winboard[i][2]) {
                return true;
            }
        }
        // column check
        for (int i = 0; i < SIZE; i++) {
            boolean win = true;
            for (int j = 0; j < SIZE; j++)Check {column
                if (board[j][i]board[0][i] != currentPlayer) {
                    win = false;
                }
            }
     EMPTY_CELL && board[0][i] == board[1][i] && board[1][i] if== (winboard[2][i]) {
                return true;
            }
        }
        boolean win = true;
        for (int i = 0; i < SIZE;// i++)Check {diagonals
            if (board[i][i]board[0][0] != currentPlayer) {
                win = false;
   EMPTY_CELL && board[0][0] == board[1][1] && board[1][1] == board[2][2]) }{
        }
        if (win) return true;
        win = true;}
        forif (int iboard[0][2] != 0; i < SIZE; i++) {
           EMPTY_CELL if&& (board[i][SIZEboard[0][2] -== iboard[1][1] -&& 1]board[1][1] !=== currentPlayerboard[2][0]) {
                win = false;
           return }true;
        }

        return win;false;
    }


    public boolean isGameOver() {

        return checkWin() || isBoardFull();
    }

    public void makeMove() {
        int [] cell = getValidUserInputgetValidCell();
        int row = cell[0];
        int col = cell[1];
        movesMade++;validMoves++;
        board[row][col] = currentPlayer;

        if (!isGameOver()) {
            togglePlayer();
        }
    }


 




}
public class TicTacToe {

    Board board;

    public TicTacToe(int size) {
        board = new Board(size);

    }

    public void play() {
        while (!board.isGameOver()) {
            board.print();
            board.makeMove();
        }
        board.print();

        System.out.println("Game is over");
        if (!board.checkWinisBoardFull()) {
            System.out.print("Winner is " + board.getCurrentPlayer());
        } else {
            System.out.println("Draw");
        }
    }
}
import java.util.Scanner;

public class Board {
    private char [] [] board;
    private final int SIZE;

    private char currentPlayer;

    private int movesMade;

    private static final char PLAYER_X = 'X';
    private static final char PLAYER_O = 'O';
    private static final char EMPTY_CELL = '.';

    private static final int DEFAULT_SIZE = 3;



    public Board() {
        this(DEFAULT_SIZE);
    }
    public Board(int size) {
        this.SIZE = size;
        reset();
    }


    public void reset() {
        board = new char [SIZE][SIZE];
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                board[i][j] = EMPTY_CELL;
            }
        }
        currentPlayer = PLAYER_X;
        movesMade = 0;
    }
    void togglePlayer() {
        currentPlayer = (currentPlayer == PLAYER_X ? PLAYER_O : PLAYER_X);
    }



    public char getCurrentPlayer() {
        return currentPlayer;
    }


    boolean isBoardFull() {
        return movesMade == SIZE * SIZE;
    }
    public void print() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }




    int [] getValidUserInput() {
        int row, col;
        boolean boardPrinted = true;
        do {
            // print the board when user makes  an invalid move
            if (!boardPrinted)
                print();
            System.out.println("It's " + currentPlayer + " turn");
            System.out.println("Enter row and column number you want to play: ");
            System.out.println("Please choose an empty cell from 1 to " + SIZE + " ");
            Scanner sc = new Scanner(System.in);
            row = sc.nextInt() - 1;
            col = sc.nextInt() - 1;

            boardPrinted = false;
        } while(isValidCell(row, col));



        return new int [] {row, col};


    }

    boolean isValidCell(int row, int col) {
        return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] != EMPTY_CELL;
    }
    public boolean checkWin() {
        // row check
        for (int i = 0; i < SIZE; i++) {
            boolean win = true;
            for (int j = 0; j < SIZE; j++) {
                if (board[i][j] != currentPlayer) {
                    win = false;
                }
            }
            if (win) {
                return true;
            }
        }
        // column check
        for (int i = 0; i < SIZE; i++) {
            boolean win = true;
            for (int j = 0; j < SIZE; j++) {
                if (board[j][i] != currentPlayer) {
                    win = false;
                }
            }
            if (win) {
                return true;
            }
        }
        boolean win = true;
        for (int i = 0; i < SIZE; i++) {
            if (board[i][i] != currentPlayer) {
                win = false;
            }
        }
        if (win) return true;
        win = true;
        for (int i = 0; i < SIZE; i++) {
            if (board[i][SIZE - i - 1] != currentPlayer) {
                win = false;
            }
        }

        return win;
    }


    public boolean isGameOver() {

        return checkWin() || isBoardFull();
    }

    public void makeMove() {
        int [] cell = getValidUserInput();
        int row = cell[0];
        int col = cell[1];
        movesMade++;
        board[row][col] = currentPlayer;

        if (!isGameOver()) {
            togglePlayer();
        }
    }


 




}
public class TicTacToe {

    Board board;

    public TicTacToe(int size) {
        board = new Board(size);

    }

    public void play() {
        while (!board.isGameOver()) {
            board.print();
            board.makeMove();
        }
        board.print();

        System.out.println("Game is over");
        if (board.checkWin()) {
            System.out.print("Winner is " + board.getCurrentPlayer());
        } else {
            System.out.println("Draw");
        }
    }
}
import java.util.Scanner;

public class Board {
    private char [] [] board;
    private final int SIZE;

    private char currentPlayer;

    private int validMoves;

    private static final char PLAYER_X = 'X';
    private static final char PLAYER_O = 'O';
    private static final char EMPTY_CELL = '.';

    private static final int DEFAULT_SIZE = 3;



    public Board() {
        this(DEFAULT_SIZE);
    }
    public Board(int size) {
        this.SIZE = size;
        reset();
    }


    public void reset() {
        board = new char [SIZE][SIZE];
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                board[i][j] = EMPTY_CELL;
            }
        }
        currentPlayer = PLAYER_X;
        validMoves = 0;
    }
    void togglePlayer() {
        currentPlayer = (currentPlayer == PLAYER_X ? PLAYER_O : PLAYER_X);
    }



    public char getCurrentPlayer() {
        return currentPlayer;
    }


    boolean isBoardFull() {
        return validMoves == SIZE * SIZE;
    }
    public void print() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }




    int [] getValidCell() {
        int row, col;
        boolean boardPrinted = true;
        do {
            // print the board when user makes  an invalid move
            if (!boardPrinted)
                print();
            System.out.println("It's " + currentPlayer + " turn");
            System.out.println("Enter row and column number you want to play: ");
            System.out.println("Please choose an empty cell from 1 to " + SIZE + " ");
            Scanner sc = new Scanner(System.in);
            row = sc.nextInt() - 1;
            col = sc.nextInt() - 1;

            boardPrinted = false;
        } while(isValidCell(row, col));



        return new int [] {row, col};


    }

    boolean isValidCell(int row, int col) {
        return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] != EMPTY_CELL;
    }
    public boolean checkWin() {
        // Check rows, columns, and diagonals for a win
 
        for (int i = 0; i < SIZE; i++) {
            // Check row
            if (board[i][0] != EMPTY_CELL && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
                return true;
            }
            // Check column
            if (board[0][i] != EMPTY_CELL && board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
                return true;
            }
        }

        // Check diagonals
        if (board[0][0] != EMPTY_CELL && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
            return true;
        }
        if (board[0][2] != EMPTY_CELL && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
            return true;
        }

        return false;
    }


    public boolean isGameOver() {

        return checkWin() || isBoardFull();
    }

    public void makeMove() {
        int [] cell = getValidCell();
        int row = cell[0];
        int col = cell[1];
        validMoves++;
        board[row][col] = currentPlayer;

        if (!isGameOver()) {
            togglePlayer();
        }
    }




}
public class TicTacToe {

    Board board;

    public TicTacToe(int size) {
        board = new Board(size);

    }

    public void play() {
        while (!board.isGameOver()) {
            board.print();
            board.makeMove();
        }
        board.print();

        System.out.println("Game is over");
        if (!board.isBoardFull()) {
            System.out.print("Winner is " + board.getCurrentPlayer());
        } else {
            System.out.println("Draw");
        }
    }
}
added 388 characters in body
Source Link
import java.util.Scanner;

public class Board {
    private char [] [] board;
    private final int SIZE;

    private char currentPlayer;

    private int validMoves;movesMade;

    private static final char PLAYER_X = 'X';
    private static final char PLAYER_O = 'O';
    private static final char EMPTY_CELL = '.';

    private static final int DEFAULT_SIZE = 3;



    public Board() {
        this(DEFAULT_SIZE);
    }
    public Board(int size) {
        this.SIZE = size;
        reset();
    }


    public void reset() {
        board = new char [SIZE][SIZE];
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                board[i][j] = EMPTY_CELL;
            }
        }
        currentPlayer = PLAYER_X;
        validMovesmovesMade = 0;
    }
    void togglePlayer() {
        currentPlayer = (currentPlayer == PLAYER_X ? PLAYER_O : PLAYER_X);
    }



    public char getCurrentPlayer() {
        return currentPlayer;
    }


    boolean isBoardFull() {
        return validMovesmovesMade == SIZE * SIZE;
    }
    public void print() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }




    int [] getValidCellgetValidUserInput() {
        int row, col;
        boolean boardPrinted = true;
        do {
            // print the board when user makes  an invalid move
            if (!boardPrinted)
                print();
            System.out.println("It's " + currentPlayer + " turn");
            System.out.println("Enter row and column number you want to play: ");
            System.out.println("Please choose an empty cell from 1 to " + SIZE + " ");
            Scanner sc = new Scanner(System.in);
            row = sc.nextInt() - 1;
            col = sc.nextInt() - 1;

            boardPrinted = false;
        } while(isValidCell(row, col));



        return new int [] {row, col};


    }

    boolean isValidCell(int row, int col) {
        return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] != EMPTY_CELL;
    }
    public boolean checkWin() {
        // Checkrow rows,check
 columns, and diagonals     for a(int wini = 0; i < SIZE; i++) {
            boolean win = true;
            for (int ij = 0; ij < SIZE; i++j++) {
            // Check row  if (board[i][j] != currentPlayer) {
            if (board[i][0] !      win = EMPTY_CELLfalse;
 && board[i][0] == board[i][1] && board[i][1] == board[i][2]        }
            }
            if (win) {
                return true;
            }
        }
        // Check column check
        for (int i = 0; i < SIZE; i++) {
            boolean win = true;
            for (int j = 0; j < SIZE; j++) {
                if (board[0][i]board[j][i] != EMPTY_CELLcurrentPlayer) &&{
 board[0][i] == board[1][i] && board[1][i] == board[2][i]             win = false;
                }
            }
            if (win) {
                return true;
            }
        }
        boolean win = true;
        //for Check(int diagonalsi = 0; i < SIZE; i++) {
            if (board[0][0]board[i][i] != EMPTY_CELLcurrentPlayer) &&{
 board[0][0] == board[1][1] && board[1][1] == board[2][2]) {        win = false;
            }
        }
        if (win) return true;
        }win = true;
        iffor (board[0][2]int !i = EMPTY_CELL0; &&i board[0][2]< ==SIZE; board[1][1]i++) &&{
 board[1][1] == board[2][0]         if (board[i][SIZE - i - 1] != currentPlayer) {
            return true;   win = false;
            }
        }

        return false;win;
    }


    public boolean isGameOver() {

        return checkWin() || isBoardFull();
    }

    public void makeMove() {
        int [] cell = getValidCellgetValidUserInput();
        int row = cell[0];
        int col = cell[1];
        validMoves++;movesMade++;
        board[row][col] = currentPlayer;

        if (!isGameOver()) {
            togglePlayer();
        }
    } 







}
public class TicTacToe {

    Board board;

    public TicTacToe(int size) {
        board = new Board(size);

    }

    public void play() {
        while (!board.isGameOver()) {
            board.print();
            board.makeMove();
        }
        board.print();

        System.out.println("Game is over");
        if (!board.isBoardFullcheckWin()) {
            System.out.print("Winner is " + board.getCurrentPlayer());
        } else {
            System.out.println("Draw");
        }
    }
}
import java.util.Scanner;

public class Board {
    private char [] [] board;
    private final int SIZE;

    private char currentPlayer;

    private int validMoves;

    private static final char PLAYER_X = 'X';
    private static final char PLAYER_O = 'O';
    private static final char EMPTY_CELL = '.';

    private static final int DEFAULT_SIZE = 3;



    public Board() {
        this(DEFAULT_SIZE);
    }
    public Board(int size) {
        this.SIZE = size;
        reset();
    }


    public void reset() {
        board = new char [SIZE][SIZE];
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                board[i][j] = EMPTY_CELL;
            }
        }
        currentPlayer = PLAYER_X;
        validMoves = 0;
    }
    void togglePlayer() {
        currentPlayer = (currentPlayer == PLAYER_X ? PLAYER_O : PLAYER_X);
    }



    public char getCurrentPlayer() {
        return currentPlayer;
    }


    boolean isBoardFull() {
        return validMoves == SIZE * SIZE;
    }
    public void print() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }




    int [] getValidCell() {
        int row, col;
        boolean boardPrinted = true;
        do {
            // print the board when user makes  an invalid move
            if (!boardPrinted)
                print();
            System.out.println("It's " + currentPlayer + " turn");
            System.out.println("Enter row and column number you want to play: ");
            System.out.println("Please choose an empty cell from 1 to " + SIZE + " ");
            Scanner sc = new Scanner(System.in);
            row = sc.nextInt() - 1;
            col = sc.nextInt() - 1;

            boardPrinted = false;
        } while(isValidCell(row, col));



        return new int [] {row, col};


    }

    boolean isValidCell(int row, int col) {
        return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] != EMPTY_CELL;
    }
    public boolean checkWin() {
        // Check rows, columns, and diagonals for a win

        for (int i = 0; i < SIZE; i++) {
            // Check row
            if (board[i][0] != EMPTY_CELL && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
                return true;
            }
            // Check column
            if (board[0][i] != EMPTY_CELL && board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
                return true;
            }
        }

        // Check diagonals
        if (board[0][0] != EMPTY_CELL && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
            return true;
        }
        if (board[0][2] != EMPTY_CELL && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
            return true;
        }

        return false;
    }


    public boolean isGameOver() {

        return checkWin() || isBoardFull();
    }

    public void makeMove() {
        int [] cell = getValidCell();
        int row = cell[0];
        int col = cell[1];
        validMoves++;
        board[row][col] = currentPlayer;

        if (!isGameOver()) {
            togglePlayer();
        }
    }




}
public class TicTacToe {

    Board board;

    public TicTacToe(int size) {
        board = new Board(size);

    }

    public void play() {
        while (!board.isGameOver()) {
            board.print();
            board.makeMove();
        }
        board.print();

        System.out.println("Game is over");
        if (!board.isBoardFull()) {
            System.out.print("Winner is " + board.getCurrentPlayer());
        } else {
            System.out.println("Draw");
        }
    }
}
import java.util.Scanner;

public class Board {
    private char [] [] board;
    private final int SIZE;

    private char currentPlayer;

    private int movesMade;

    private static final char PLAYER_X = 'X';
    private static final char PLAYER_O = 'O';
    private static final char EMPTY_CELL = '.';

    private static final int DEFAULT_SIZE = 3;



    public Board() {
        this(DEFAULT_SIZE);
    }
    public Board(int size) {
        this.SIZE = size;
        reset();
    }


    public void reset() {
        board = new char [SIZE][SIZE];
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                board[i][j] = EMPTY_CELL;
            }
        }
        currentPlayer = PLAYER_X;
        movesMade = 0;
    }
    void togglePlayer() {
        currentPlayer = (currentPlayer == PLAYER_X ? PLAYER_O : PLAYER_X);
    }



    public char getCurrentPlayer() {
        return currentPlayer;
    }


    boolean isBoardFull() {
        return movesMade == SIZE * SIZE;
    }
    public void print() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }




    int [] getValidUserInput() {
        int row, col;
        boolean boardPrinted = true;
        do {
            // print the board when user makes  an invalid move
            if (!boardPrinted)
                print();
            System.out.println("It's " + currentPlayer + " turn");
            System.out.println("Enter row and column number you want to play: ");
            System.out.println("Please choose an empty cell from 1 to " + SIZE + " ");
            Scanner sc = new Scanner(System.in);
            row = sc.nextInt() - 1;
            col = sc.nextInt() - 1;

            boardPrinted = false;
        } while(isValidCell(row, col));



        return new int [] {row, col};


    }

    boolean isValidCell(int row, int col) {
        return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] != EMPTY_CELL;
    }
    public boolean checkWin() {
        // row check
        for (int i = 0; i < SIZE; i++) {
            boolean win = true;
            for (int j = 0; j < SIZE; j++) {
                if (board[i][j] != currentPlayer) {
                    win = false;
                }
            }
            if (win) {
                return true;
            }
        }
        // column check
        for (int i = 0; i < SIZE; i++) {
            boolean win = true;
            for (int j = 0; j < SIZE; j++) {
                if (board[j][i] != currentPlayer) {
                    win = false;
                }
            }
            if (win) {
                return true;
            }
        }
        boolean win = true;
        for (int i = 0; i < SIZE; i++) {
            if (board[i][i] != currentPlayer) {
                win = false;
            }
        }
        if (win) return true;
        win = true;
        for (int i = 0; i < SIZE; i++) {
            if (board[i][SIZE - i - 1] != currentPlayer) {
                win = false;
            }
        }

        return win;
    }


    public boolean isGameOver() {

        return checkWin() || isBoardFull();
    }

    public void makeMove() {
        int [] cell = getValidUserInput();
        int row = cell[0];
        int col = cell[1];
        movesMade++;
        board[row][col] = currentPlayer;

        if (!isGameOver()) {
            togglePlayer();
        }
    } 







}
public class TicTacToe {

    Board board;

    public TicTacToe(int size) {
        board = new Board(size);

    }

    public void play() {
        while (!board.isGameOver()) {
            board.print();
            board.makeMove();
        }
        board.print();

        System.out.println("Game is over");
        if (board.checkWin()) {
            System.out.print("Winner is " + board.getCurrentPlayer());
        } else {
            System.out.println("Draw");
        }
    }
}
Became Hot Network Question
Source Link

Java Tic-Tac-Toe Game Implementation - Seeking Feedback and Best Practices

I have recently developed a Java program for a simple Tic-Tac-Toe game and would like to request a code review to improve its quality and learn from experienced developers. Below is the code for my game:

Board.java

import java.util.Scanner;

public class Board {
    private char [] [] board;
    private final int SIZE;

    private char currentPlayer;

    private int validMoves;

    private static final char PLAYER_X = 'X';
    private static final char PLAYER_O = 'O';
    private static final char EMPTY_CELL = '.';

    private static final int DEFAULT_SIZE = 3;



    public Board() {
        this(DEFAULT_SIZE);
    }
    public Board(int size) {
        this.SIZE = size;
        reset();
    }


    public void reset() {
        board = new char [SIZE][SIZE];
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                board[i][j] = EMPTY_CELL;
            }
        }
        currentPlayer = PLAYER_X;
        validMoves = 0;
    }
    void togglePlayer() {
        currentPlayer = (currentPlayer == PLAYER_X ? PLAYER_O : PLAYER_X);
    }



    public char getCurrentPlayer() {
        return currentPlayer;
    }


    boolean isBoardFull() {
        return validMoves == SIZE * SIZE;
    }
    public void print() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }




    int [] getValidCell() {
        int row, col;
        boolean boardPrinted = true;
        do {
            // print the board when user makes  an invalid move
            if (!boardPrinted)
                print();
            System.out.println("It's " + currentPlayer + " turn");
            System.out.println("Enter row and column number you want to play: ");
            System.out.println("Please choose an empty cell from 1 to " + SIZE + " ");
            Scanner sc = new Scanner(System.in);
            row = sc.nextInt() - 1;
            col = sc.nextInt() - 1;

            boardPrinted = false;
        } while(isValidCell(row, col));



        return new int [] {row, col};


    }

    boolean isValidCell(int row, int col) {
        return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] != EMPTY_CELL;
    }
    public boolean checkWin() {
        // Check rows, columns, and diagonals for a win

        for (int i = 0; i < SIZE; i++) {
            // Check row
            if (board[i][0] != EMPTY_CELL && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
                return true;
            }
            // Check column
            if (board[0][i] != EMPTY_CELL && board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
                return true;
            }
        }

        // Check diagonals
        if (board[0][0] != EMPTY_CELL && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
            return true;
        }
        if (board[0][2] != EMPTY_CELL && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
            return true;
        }

        return false;
    }


    public boolean isGameOver() {

        return checkWin() || isBoardFull();
    }

    public void makeMove() {
        int [] cell = getValidCell();
        int row = cell[0];
        int col = cell[1];
        validMoves++;
        board[row][col] = currentPlayer;

        if (!isGameOver()) {
            togglePlayer();
        }
    }




}

TicTacToe.java

public class TicTacToe {

    Board board;

    public TicTacToe(int size) {
        board = new Board(size);

    }

    public void play() {
        while (!board.isGameOver()) {
            board.print();
            board.makeMove();
        }
        board.print();

        System.out.println("Game is over");
        if (!board.isBoardFull()) {
            System.out.print("Winner is " + board.getCurrentPlayer());
        } else {
            System.out.println("Draw");
        }
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        TicTacToe tic = new TicTacToe(3);
        tic.play();
    }
}

Questions:

  1. Are there any potential issues or inefficiencies in the code that I should be aware of?
  2. Are there any best practices I should follow or refactorings I should consider?
  3. Is my code readable and well-documented? Are there places where I can improve comments or method names for clarity?
  4. Have I correctly handled user input and error scenarios?
  5. Any suggestions for improving the overall structure and organization of the code?
  6. Do you notice any possible improvements in terms of code maintainability and extensibility?