function onClick() {
    var isWinner = false;
    if (!gameFinished) {
        // Return if field  aleady set
        if (this.innerHTML !== '') return

        // Two Player Mode
        if (isTwoPlayer) {
            if (currentTurn) {
                boardArray[this.index] = playerOne;
                currentTurn = false;
                currentTurnText.innerText = "It is " + `${player2Name}` + "'s turn (X)";
            } else {
                boardArray[this.index] = playerTwo;
                currentTurn = true;
                currentTurnText.innerText = "It is " + `${player1Name}` + "'s turn (O)";

            }
            // Place marker on the board
            drawOnTo(boardArray);

            // Player 1 Won
            if (checkWin(boardArray, playerOne)) {
                alertText.innerText = `${player1Name}` + ' wins.';
                currentTurnText.style.visibility = "hidden";
                player1Score++;
                player1ScoreText.innerText = `${player1Score}`;
                showAlertMessage();
                gameFinished = true;
                isWinner = true;
            }

            // Player 2 won
            if (checkWin(boardArray, playerTwo)) {
                alertText.innerText = `${player2Name}` + ' wins.';
                currentTurnText.style.visibility = "hidden";
                player2Score++;
                player2ScoreText.innerText = `${player2Score}`;
                showAlertMessage();
                gameFinished = true;
                isWinner = true;
            }

        } else {
            // Human vs AI
            boardArray[this.index] = playerOne;
            if (lowDifficulty){
                // use MiniMax Alpha Beta to get the optimal move
                const availablePositions = getAvailablePositions(boardArray);
                var move= availablePositions[Math.floor(Math.random() * availablePositions.length)]
                // Place marker on the board
                boardArray[move] = playerAi;
                drawOnTo(boardArray);

            } else {
                // use MiniMax Alpha Beta to get the optimal move
                const optimalMove = alphaBetaMiniMax(playerAi, boardArray, -Infinity, Infinity, 0);
                // Place marker on the board
                boardArray[optimalMove.index] = playerAi;
                drawOnTo(boardArray);
            }
            
            // AI won
            if (checkWin(boardArray, playerAi)) {
                alertText.innerText = 'You lose.';
                currentTurnText.style.visibility = "hidden";
                player2Score++;
                player2ScoreText.innerText = `${player2Score}`;
                showAlertMessage();
                gameFinished = true;
                isWinner = true;
            }
            // Human won
            if (checkWin(boardArray, playerOne)) {
                alertText.innerText = 'You have beat the AI.';
                currentTurnText.style.visibility = "hidden";
                player1Score++;
                player1ScoreText.innerText = `${player1Score}`;
                showAlertMessage();
                gameFinished = true;
                isWinner = true;
            }
        }
        // Draw
        if (!boardArray.includes('') && !isWinner) {
            alertText.innerText = 'It is a draw.';
            currentTurnText.style.visibility = "hidden";
            showAlertMessage();
            gameFinished = true;
        }
    }
}
/*
* handles click events for the grid depending on the game mode
*/
handles click events for the grid depending on the game mode
handles click events for the grid depending on the game mode
function onClick() {
    var isWinner = false;
    if (!gameFinished) {
        if (this.innerHTML !== '') return
        if (isTwoPlayer) {
            if (currentTurn) {
                boardArray[this.index] = playerOne;
                currentTurn = false;
                currentTurnText.innerText = "It is " + `${player2Name}` + "'s turn (X)";
            } else {
                boardArray[this.index] = playerTwo;
                currentTurn = true;
                currentTurnText.innerText = "It is " + `${player1Name}` + "'s turn (O)";
            }
            drawOnTo(boardArray);
            if (checkWin(boardArray, playerOne)) {
                alertText.innerText = `${player1Name}` + ' wins.';
                currentTurnText.style.visibility = "hidden";
                player1Score++;
                player1ScoreText.innerText = `${player1Score}`;
                showAlertMessage();
                gameFinished = true;
                isWinner = true;
            }
            if (checkWin(boardArray, playerTwo)) {
                alertText.innerText = `${player2Name}` + ' wins.';
                currentTurnText.style.visibility = "hidden";
                player2Score++;
                player2ScoreText.innerText = `${player2Score}`;
                showAlertMessage();
                gameFinished = true;
                isWinner = true;
            }
        } else {
            boardArray[this.index] = playerOne;
            if (lowDifficulty){
                const availablePositions = getAvailablePositions(boardArray);
                var move= availablePositions[Math.floor(Math.random() * availablePositions.length)]
                boardArray[move] = playerAi;
                drawOnTo(boardArray);
            } else {
                const optimalMove = alphaBetaMiniMax(playerAi, boardArray, -Infinity, Infinity, 0);
                boardArray[optimalMove.index] = playerAi;
                drawOnTo(boardArray);
            }
            if (checkWin(boardArray, playerAi)) {
                alertText.innerText = 'You lose.';
                currentTurnText.style.visibility = "hidden";
                player2Score++;
                player2ScoreText.innerText = `${player2Score}`;
                showAlertMessage();
                gameFinished = true;
                isWinner = true;
            }
            if (checkWin(boardArray, playerOne)) {
                alertText.innerText = 'You have beat the AI.';
                currentTurnText.style.visibility = "hidden";
                player1Score++;
                player1ScoreText.innerText = `${player1Score}`;
                showAlertMessage();
                gameFinished = true;
                isWinner = true;
            }
        }
        if (!boardArray.includes('') && !isWinner) {
            alertText.innerText = 'It is a draw.';
            currentTurnText.style.visibility = "hidden";
            showAlertMessage();
            gameFinished = true;
        }
    }
}
function onClick ( ) { var isWinner = false ; if ( ! gameFinished ) { if ( this . innerHTML !== '' ) return if ( isTwoPlayer ) { if ( currentTurn ) { boardArray [ this . index ] = playerOne ; currentTurn = false ; currentTurnText . innerText = "It is " + ` ${ player2Name } ` + "'s turn (X)" ; } else { boardArray [ this . index ] = playerTwo ; currentTurn = true ; currentTurnText . innerText = "It is " + ` ${ player1Name } ` + "'s turn (O)" ; } drawOnTo ( boardArray ) ; if ( checkWin ( boardArray , playerOne ) ) { alertText . innerText = ` ${ player1Name } ` + ' wins.' ; currentTurnText . style . visibility = "hidden" ; player1Score ++ ; player1ScoreText . innerText = ` ${ player1Score } ` ; showAlertMessage ( ) ; gameFinished = true ; isWinner = true ; } if ( checkWin ( boardArray , playerTwo ) ) { alertText . innerText = ` ${ player2Name } ` + ' wins.' ; currentTurnText . style . visibility = "hidden" ; player2Score ++ ; player2ScoreText . innerText = ` ${ player2Score } ` ; showAlertMessage ( ) ; gameFinished = true ; isWinner = true ; } } else { boardArray [ this . index ] = playerOne ; if ( lowDifficulty ) { const availablePositions = getAvailablePositions ( boardArray ) ; var move = availablePositions [ Math . floor ( Math . random ( ) * availablePositions . length ) ] boardArray [ move ] = playerAi ; drawOnTo ( boardArray ) ; } else { const optimalMove = alphaBetaMiniMax ( playerAi , boardArray , - Infinity , Infinity , 0 ) ; boardArray [ optimalMove . index ] = playerAi ; drawOnTo ( boardArray ) ; } if ( checkWin ( boardArray , playerAi ) ) { alertText . innerText = 'You lose.' ; currentTurnText . style . visibility = "hidden" ; player2Score ++ ; player2ScoreText . innerText = ` ${ player2Score } ` ; showAlertMessage ( ) ; gameFinished = true ; isWinner = true ; } if ( checkWin ( boardArray , playerOne ) ) { alertText . innerText = 'You have beat the AI.' ; currentTurnText . style . visibility = "hidden" ; player1Score ++ ; player1ScoreText . innerText = ` ${ player1Score } ` ; showAlertMessage ( ) ; gameFinished = true ; isWinner = true ; } } if ( ! boardArray . includes ( '' ) && ! isWinner ) { alertText . innerText = 'It is a draw.' ; currentTurnText . style . visibility = "hidden" ; showAlertMessage ( ) ; gameFinished = true ; } } }
handles click events for the grid depending on the game mode
handles click events for the grid depending on the game mode
// Return if field  aleady set // Two Player Mode // Place marker on the board // Player 1 Won // Player 2 won // Human vs AI // use MiniMax Alpha Beta to get the optimal move // Place marker on the board // use MiniMax Alpha Beta to get the optimal move // Place marker on the board // AI won // Human won // Draw