Click here to Skip to main content
15,743,429 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it possible to simplify this hasWinner() method, I want to check if some array index not empty, then check if there already arrays that have same value, the method work to check if the tic tac toe has a winner yet or not
Java
Player currentPlayer;
private final Player[] boardOwned = new Player[9];

public boolean hasWinner() {
    return (boardOwned[0] != null && boardOwned[0] == boardOwned[1] && boardOwned[0] == boardOwned[2])
            || (boardOwned[3] != null && boardOwned[3] == boardOwned[4] && boardOwned[3] == boardOwned[5])
            || (boardOwned[6] != null && boardOwned[6] == boardOwned[7] && boardOwned[6] == boardOwned[8])
            || (boardOwned[0] != null && boardOwned[0] == boardOwned[3] && boardOwned[0] == boardOwned[6])
            || (boardOwned[1] != null && boardOwned[1] == boardOwned[4] && boardOwned[1] == boardOwned[7])
            || (boardOwned[2] != null && boardOwned[2] == boardOwned[5] && boardOwned[2] == boardOwned[8])
            || (boardOwned[0] != null && boardOwned[0] == boardOwned[4] && boardOwned[0] == boardOwned[8])
            || (boardOwned[2] != null && boardOwned[2] == boardOwned[4] && boardOwned[2] == boardOwned[6]);
}

public boolean boardFilledUp() {
    for (Player player : boardOwned) {
        if (player == null) {
            return false;
        }
    }
    return true;
}


What I have tried:

I tried extracting it to three methods and using loop for, but its repetitive, and I dont yet to found working code

Java
public boolean hasWinner() {
    return checkHorizontal() || checkVertical() || checkDiagonal()
}
Posted
Updated 7-Jun-20 0:44am

The second version is much cleaner and easier to read: and the three methods are a lot cleaner to write:
Java
public boolean checkHorizontal() {
   for (int j = 1; j < 8; j += 3) {
      if (boardOwned[j] != null && boardOwned[j] == boardOwned[j - 1] && boardOwned[j] == boardOwned[j + 1]) {
         return true;
         }
      }
   return false;
   }
Similar method for Vertical, easy method for the two diagonals.
 
Share this answer
 
v2
Comments
raigen 7-Jun-20 5:03am    
Thanks OriginalGriff, but I still lost why inside the if there are three boardOwned[j] == boardOwned[j - 1]?
OriginalGriff 7-Jun-20 5:31am    
Overzealous copy'n'paste due to a lack of coffee ... fixed now.
raigen 7-Jun-20 6:40am    
High five, Thank you OriginalGriff!
This is the cleaner code, thanks to OriginalGriff explanation, well for diagonal, I think manual index check is better than looping diagonally since 0,4,8 and 2,4,6 have different increment
Java
Player currentPlayer;
private final Player[] boardOwned = new Player[9];

public boolean hasWinner() {
    return checkHorizontal() || checkVertical() || checkDiagonal();
}

private boolean checkVertical() {
    for (int j = 6; j < 9; j += 1) {
        if (boardOwned[j] != null && boardOwned[j] == boardOwned[j - 3] &&
                boardOwned[j] == boardOwned[j - 6])
        {
            return true;
        }
    }
    return false;
}

public boolean checkHorizontal() {
    for (int j = 1; j < 8; j += 3) {
        if (boardOwned[j] != null && boardOwned[j] == boardOwned[j-1] && boardOwned[j] == boardOwned[j+1]) {
            return true;
        }
    }
    return false;
}

private boolean checkDiagonal(){
    return (boardOwned[0] != null && boardOwned[0] == boardOwned[4] && boardOwned[0] == boardOwned[8])
            || (boardOwned[2] != null && boardOwned[2] == boardOwned[4] && boardOwned[2] == boardOwned[6]);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900