Reputation: 31
I'm playing a TicTacToe game where the user plays the computer. The user is always X and the computer is O. I have my code written to check every row, column, and diagonal to see if the user or computer wins. If neither then it just prints false. I would like the game to print out if the user win Human player won" or if the computer wins "Computer won". I just don't know how to add it to the boolean method.
public static boolean checkWin()
{
//checks for human win
if ("X".equals(board[0][0]) && "X".equals(board[0][1]) && "X".equals(board[0][2]))
return true;
else if ("X".equals(board[1][0]) && "X".equals(board[1][1]) && "X".equals(board[1][2]))
return true;
else if ("X".equals(board[2][0]) && "X".equals(board[2][1]) && "X".equals(board[2][2]))
return true;
else if ("X".equals(board[0][0]) && "X".equals(board[1][0]) && "X".equals(board[2][0]))
return true;
else if ("X".equals(board[0][1]) && "X".equals(board[1][1]) && "X".equals(board[2][1]))
return true;
else if ("X".equals(board[0][2]) && "X".equals(board[1][2]) && "X".equals(board[2][2]))
return true;
else if ("X".equals(board[0][0]) && "X".equals(board[1][1]) && "X".equals(board[2][2]))
return true;
else if ("X".equals(board[0][2]) && "X".equals(board[1][1]) && "X".equals(board[2][0]))
return true;
//checks if computer has won
else if ("O".equals(board[0][0]) && "O".equals(board[0][1]) && "O".equals(board[0][2]))
return true;
else if ("O".equals(board[1][0]) && "O".equals(board[1][1]) && "O".equals(board[1][2]))
return true;
else if ("O".equals(board[2][0]) && "O".equals(board[2][1]) && "O".equals(board[2][2]))
return true;
else if ("O".equals(board[0][0]) && "O".equals(board[1][0]) && "O".equals(board[2][0]))
return true;
else if ("O".equals(board[0][1]) && "O".equals(board[1][1]) && "O".equals(board[2][1]))
return true;
else if ("O".equals(board[0][2]) && "O".equals(board[1][2]) && "O".equals(board[2][2]))
return true;
else if ("O".equals(board[0][0]) && "O".equals(board[1][1]) && "O".equals(board[2][2]))
return true;
else if ("O".equals(board[0][2]) && "O".equals(board[1][1]) && "O".equals(board[2][0]))
return true;
else
//it's a tie
return false;
}
Upvotes: 0
Views: 1737
Reputation: 4448
Although I agree with other answers that say a method should do one thing, if you want to do it that way you can create a class, Result
public class Result{
boolean win;
String message;
}
and return Result instead of boolean
public static Result checkWin()
{
Result result = new Result();
if ("X".equals(board[0][0]) && "X".equals(board[0][1]) && "X".equals(board[0][2])){
result.win = true;
result.messsage = "Computer won";
}
...
return result;
}
Upvotes: 0
Reputation: 79807
After the human's turn, call checkWin
. If it returns true, then print the "human won" message and end the game.
After the computer's turn, call checkWin
. If it returns true, then print the "computer won" message and end the game.
If nine moves have occurred and checkWin
hasn't returned true, the game is a tie.
Upvotes: 1
Reputation: 83517
The general rule of thumb is that each method should do one thing and do it well. In this case, checkWin()
does exactly what its name implies: it checks if the human or computer won. If you add code to print a message, then it should real then it should be called checkWinAndPrintWinner()
. Now it is obvious that you are trying to do two things in the function. Instead, you should write another method named printWinner()
which calls checkWin()
and then prints the appropriate message.
Upvotes: 1