public class Game {
    private TicTacToe board;
    private Player player1;
    private Player player2;
    private Player currentPlayer;

    public Game() {
        board = new TicTacToe();
        player1 = new Player('X');
        player2 = new Player('O');
        currentPlayer = player1;
    }

    public void switchPlayer() {
        if (currentPlayer == player1) {
            currentPlayer = player2;
        } else {
            currentPlayer = player1;
        }
    }

    public boolean makeMove(int row, int col) {
        if (board.getGameBoard()[row][col] == ' ') {
            board.getGameBoard()[row][col] = currentPlayer.getSymbol();
            return true;
        }
        return false;
    }

    public void play() {
        Scanner scanner = new Scanner(System.in);
        int movesMade = 0;
        boolean isGameOver = false;
    
        while (!isGameOver && movesMade < 9) {
            board.printGameBoard();
            System.out.println("Player " + currentPlayer.getSymbol() + ", it's your turn.");
            System.out.print("Enter row (0, 1, 2) and column (0, 1, 2) separated by a space: ");
    
            try {
                int row = scanner.nextInt() * 2;
                int col = scanner.nextInt() * 2;
    
                // Check for out-of-bounds
                if (row < 0 || row > 4 || col < 0 || col > 4) {
                    System.out.println("Invalid move. Row and column should be between 0 and 2. Try again.");
                    continue;
                }
    
                if (makeMove(row, col)) {
                    movesMade++;
                    if (checkWin(row, col)) {
                        board.printGameBoard();
                        System.out.println("Player " + currentPlayer.getSymbol() + " wins!");
                        isGameOver = true;
                    } else if (movesMade == 9) {
                        board.printGameBoard();
                        System.out.println("It's a draw!");
                        isGameOver = true;
                    } else {
                        switchPlayer();
                    }
                } else {
                    System.out.println("Invalid move. Cell already occupied. Try again.");
                }
            } catch (InputMismatchException e) {
                System.out.println("Invalid input format. Please enter two integers separated by a space.");
                scanner.nextLine();  // Clear the invalid input
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Invalid move. Row and column should be between 0 and 2. Try again.");
            }
        }
        scanner.close();
    }    

    private boolean checkWin(int lastMoveRow, int lastMoveCol) {
        char currentPlayerSymbol = currentPlayer.getSymbol();
        
        // Check the row
        boolean win = true;
        for (int i = 0; i < 5; i+=2) {
            if (board.getGameBoard()[lastMoveRow][i] != currentPlayerSymbol) {
                win = false;
                break;
            }
        }
        if (win) return true;
        
        // Check the column
        win = true;
        for (int i = 0; i < 5; i+=2) {
            if (board.getGameBoard()[i][lastMoveCol] != currentPlayerSymbol) {
                win = false;
                break;
            }
        }
        if (win) return true;
        
        // Check the diagonals, but only if the last move was on a diagonal
        if (lastMoveRow == lastMoveCol || lastMoveRow + lastMoveCol == 4) {
            // Check the top-left to bottom-right diagonal
            win = true;
            for (int i = 0; i < 5; i+=2) {
                if (board.getGameBoard()[i][i] != currentPlayerSymbol) {
                    win = false;
                    break;
                }
            }
            if (win) return true;
            
            // Check the top-right to bottom-left diagonal
            win = true;
            for (int i = 0; i < 5; i+=2) {
                if (board.getGameBoard()[i][4 - i] != currentPlayerSymbol) {
                    win = false;
                    break;
                }
            }
            if (win) return true;
        }
        
        return false;
    }    
}

public class TicTacToe {
    private char[][] gameBoard;

    // Constructor
    public TicTacToe() {
        this.gameBoard = new char[5][5];
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (i % 2 == 1) {
                    this.gameBoard[i][j] = '-';
                } else if (j % 2 == 1) {
                    this.gameBoard[i][j] = '|';
                } else {
                    this.gameBoard[i][j] = ' ';
                }
            }
        }
    }

    // Getter for the game board
    public char[][] getGameBoard() {
        return this.gameBoard;
    }

    // Method to print the game board
    public void printGameBoard() {
        System.out.println();
        for(char[] row : gameBoard) {
            for(char c : row) {
                System.out.print(c);
            }
            System.out.println();
        }
    }

    // Main method
    public static void main(String[] args) {
        Game game = new Game();
        game.play();
    }
}
TicTacToe.main(null);
 | | 
-----
 | | 
-----
 | | 
Player X, it's your turn.
Enter row (0, 1, 2) and column (0, 1, 2) separated by a space: Invalid input format. Please enter two integers separated by a space.

 | | 
-----
 | | 
-----
 | | 
Player X, it's your turn.
Enter row (0, 1, 2) and column (0, 1, 2) separated by a space: