HoangHai-portliofio-projects / Projects / 5.Tictactoe_java / src / TicTacToe.java
TicTacToe.java
Raw
public class TicTacToe {
    protected char[] board;
    protected char userMarker;
    protected char aiMarker;
    protected char winner;
    protected char currentMarker;

    // void setUpBoard() {
    //     for (int i = 0; i < board.length; i++ ) {
    //         board[i] = '-';
    //     }
    // }

    public static void printBoardIndex() {
        for (int i = 0; i < 9; i++) {
            if (i % 3 ==0 && i != 0 ) {
                System.out.println();
                System.out.println("------------");
            }
            System.out.print("| " + (i+1) );
        } 
        System.out.println();
        System.out.println("");
    }
    
    public static void printBoard(char[] board) {
       for (int i = 0; i < 9; i++) {
           if (i % 3 ==0 && i != 0 ) {
               System.out.println();
               System.out.println("------------");
           }
           System.out.print("|" + board[i] + "|");
       } 
       System.out.println("");
    }

    public TicTacToe(char playerToken, char aiMarker){
        this.userMarker = playerToken;
        this.aiMarker = aiMarker;
        this.winner = '-';
        this.board = setUpNewBoard();
        this.currentMarker = this.userMarker;
    }

    public static char[] setUpNewBoard() {
        char[]board = new char[9];
        for (int i = 0; i < board.length; i++) {
            board[i] = '-';
        } 
        return board;
    }

    public boolean playTurn(int spot) {
        boolean isValid = withinRange(spot) && !isSpotTaken(spot);
        if (isValid) {
            board[spot-1] = currentMarker;
            currentMarker = (currentMarker == userMarker) ? aiMarker : userMarker;
        }
        return isValid;
    }

    //Check if ourspot is in range 
    public boolean withinRange(int number) {
        return number > 0 && number < board.length + 1;
    }

    //Check if the spot is taken 
    public boolean isSpotTaken(int number ) {
        return board[number-1] != '-';
    }

    public boolean isThereAWinner () {
        boolean diagonalsAndMiddle = (rightDi() ||leftDi() ||middleRow() || secondCol()) && board[4] != '-';
        boolean topAndFirst = (topRow() || firstCol()) && board[0] != '-';
        boolean bottomAndThird = (bottomRow() || thirdCol()) && board[8] != '-';
        if (diagonalsAndMiddle) {
            this.winner = board[4];
        } else if (topAndFirst) {
            this.winner = board[0];
        } else if(bottomAndThird) {
            this.winner = board[8];
        }
        return diagonalsAndMiddle || topAndFirst || bottomAndThird ;
    }

    public boolean topRow() {
        return board[0] == board[1] && board [1] == board[2];
    }

    public boolean middleRow() {
        return board[3] == board[4] && board[4] == board[5];
    }

    public boolean bottomRow() {
        return board[6] ==board[7] && board[7]== board[8];
    }

    public boolean firstCol() {
        return board[0] == board[3] && board[3] == board[6];
    }

    public boolean secondCol() {
        return board[1] == board[4] && board [4] == board[7];
    }

    public boolean thirdCol() {
        return board[2] == board[5] && board[5] == board[8];
    }

    public boolean rightDi() {
        return board[2] == board[4] && board[4] == board[6];
    }

    public boolean leftDi() {
        return board[0] == board[4] && board[4] == board[8];
    }

    public boolean isTheBoardFilled() {
        for (int i = 0 ; i< board.length; i++) {
            if (board[i] == '-' ) {
                return false;
            }
        }
        return true;
    }

    public String gameOver() {
        boolean didSomeoneWin = isThereAWinner();
        if (didSomeoneWin) {
            return "We have a winner! The winner is " + this.winner + "'s";
        } else if (isTheBoardFilled()) {
            return "Draw! GameOver";
        } else {
            return "notOver";
        }
    }

}