Reputation: 189
I'm programming a tic tac toe game in java and I'm not sure if there is a much faster/ easier way to check for a winner.
if ( boardArray[0][0] == boardArray[0][1] && boardArray[0][0] == boardArray[0][2]) {
System.out.println("The wimmer is: " + boardArray[0][0]);
} else if ( boardArray[1][0] == boardArray[1][1] && boardArray[1][0] == boardArray[1][2]) {
System.out.println("The wimmer is: " + boardArray[1][0]);
} else if ( boardArray[2][0] == boardArray[2][1] && boardArray[2][0] == boardArray[2][2]) {
System.out.println("The wimmer is: " + boardArray[2][0]);
} else if ( boardArray[0][0] == boardArray[1][0] && boardArray[0][0] == boardArray[2][0]) {
System.out.println("The wimmer is: " + boardArray[0][0]);
}else if ( boardArray[0][1] == boardArray[1][1] && boardArray[0][1] == boardArray[2][1]) {
System.out.println("The wimmer is: " + boardArray[0][1]);
} else if ( boardArray[0][2] == boardArray[1][2] && boardArray[0][2] == boardArray[2][2]) {
System.out.println("The wimmer is: " + boardArray[0][2]);
} else if ( boardArray[0][0] == boardArray[1][1] && boardArray[0][0] == boardArray[2][2]) {
System.out.println("The wimmer is: " + boardArray[0][0]);
} else if ( boardArray[0][2] == boardArray[1][1] && boardArray[0][2] == boardArray[2][0]) {
System.out.println("The wimmer is: " + boardArray[1][1]);
} else {
System.out.println("ITS A DRAW");
}
I'm aware that I could use a for loop to loop through the different indexes but this only reduces the lines of code by about 2/3 so doesn't make a huge difference
Upvotes: 1
Views: 641
Reputation: 17454
I'm aware that I could use a for loop to loop through the different indexes but this only reduces the lines of code by about 2/3 so doesn't make a huge difference.
You can go ahead to use loops to improve your code because it not only cut down the number of lines of code, it also gives you a ton of benefits. It improves your:
Maintainability
To edit the code, you just have to edit it once instead of editing it n times, where n is the number of iterations it supposed to go through.
Scalability
This may not be a good example, but imagine you want to change the board to 4 x 4. With loops, you probably just need to update the board size. All other codes can almost be left untouched.
Readability
One instance of the code will be easier to read than multiple repeated lines.
Reduce chances for typo errors
Lesser code, lesser chance for typographical errors.
Makes debugging easier
Error finding and debugging will actually be easier due to improved readability.
Upvotes: 2
Reputation: 45
You do not have to go through if else statement for the first 6 parts. You can use loop for it. It will make your code compact.
public static void check(int[][] boardArray) {
for (int i = 0; i < 3; i++) {
if (boardArray[i][0] == boardArray[i][1] && boardArray[i][0] == boardArray[i][2]) {
System.out.println("The winner is: " + boardArray[i][0]);
return;
} else if (boardArray[0][i] == boardArray[1][i] && boardArray[0][i] == boardArray[2][i]) {
System.out.printf("The winner is: " + boardArray[0][i]);
return;
}
}
if (boardArray[0][0] == boardArray[1][1] && boardArray[0][0] == boardArray[2][2]) {
System.out.println("The winner is: " + boardArray[0][0]);
} else if (boardArray[0][2] == boardArray[1][1] && boardArray[0][2] == boardArray[2][0]) {
System.out.println("The winner is: " + boardArray[1][1]);
} else {
System.out.println("ITS A DRAW");
}
}
Upvotes: 0