reizorwins
reizorwins

Reputation: 13

JavaFX: Why doesn't my MinMax algorithm make a move when it starts a TicTacToe game?

I have a working MinMax algorithm, tested in other situations, the JavaFX implementation doesn't work when the computer (MinMax) has to make the first move, I have to click first.

There is a bug in the code, because when the computer (player2) starts, it doesn't want to make a move first, then when I click for it, then the game goes on normally and the algorithm works correctly, when I start as player1 it's ok.

        pane.setOnMouseClicked(event -> {
            if (currentPlayer.equals(player1)) {
                if (label.getText().isEmpty() && !isEndOfGame) {
                    label.setText(currentPlayer.getSymbol());
                    changePlayerTurn();
                    verifyResultOfTheDuel();

                    if (mode.equals(SINGLE_PLAYER) && !isEndOfGame && currentPlayer.equals(player2)) {
                        makeComputerMove(difficulty);
                        changePlayerTurn();
                        verifyResultOfTheDuel();
                    }
                }
            } else if (currentPlayer.equals(player2)) {
                if (label.getText().isEmpty() && !isEndOfGame) {
                    if (mode.equals(SINGLE_PLAYER)) {
                        makeComputerMove(difficulty);
                        changePlayerTurn();
                        verifyResultOfTheDuel();
                    } else if (mode.equals(MULTI_PLAYER)) {
                        label.setText(currentPlayer.getSymbol());
                        changePlayerTurn();
                        verifyResultOfTheDuel();
                    }
                }
            }
        });

Upvotes: 0

Views: 58

Answers (1)

Baffler
Baffler

Reputation: 11

It is not a bug. In your case, you have to click first because the code within your OnMouseClicked event is only executed when pane is clicked.

In order for the "computer" to make its move when it starts, you should make the check outside of the OnMouseClicked event.

Upvotes: 1

Related Questions