lundi 30 novembre 2015

If statement not processing - Java

I'm writing a tic tac toe problem. The tic tac toe class works fine, but the first if statement in my Main class isn't processing. The TicTacToe class isn't what's wrong, because when Player 1 wins the oNew.checkWinner(1) returns true. (I checked by placing System.out.println(oNew.checkWinner(1)); right before the if statement). But then it doesn't pass into the if statement to break the while loop and just continues on. It does the same with the first else if too.

The odd part is it works just fine for the 2nd if statement and 2nd else if statement.

public static void main(String[] args) {

        TicTacToe oNew = new TicTacToe();
        String champ = "";
        boolean hasWon = false;

        oNew.getMove();
        while(hasWon == false){
            oNew.setMove(1);
            oNew.getMove();
            if (oNew.checkWinner(1) == true){
                champ = "Player 1 wins.";
                hasWon = true;
            }
            else if(oNew.checkCatsGame() == true){
                champ = "Draw";
                hasWon = true;
            }
            oNew.setMove(2);
            oNew.getMove();
            if (oNew.checkWinner(2) == true){
                champ = "Player 2 wins.";
                hasWon = true;
            }
            else if(oNew.checkCatsGame() == true){
                champ = "Draw";
                hasWon = true;
            }
        }
        System.out.println(champ);

    }

Aucun commentaire:

Enregistrer un commentaire