4. Check the winner

Workshop Resources

Displaying a Winner

Write a method getWinner(String[] curBoard) that returns the winner in a String with an array input of the current game board.

There are several ways to write this method.

Give it a try in the following Replit first; it will tell you if your method is correctly written! Launch Replit

Ideas/Hints for writing the method getWinner()

  • Concatenate Strings at the 3 positions that consist a horizontal, vertical, or diagonal row.
  • Use equals() to check if each of the concatenated Strings is "XXX" or "OOO" for a winner. (You can even create another method that checks if an input String is "XXX" or "OOO")
  • If all the spots in the array is filled (a for loop can be helpful) and there is no winner, the game ends in a tie.

Calling the Winner Method

In main(), call the method getWinner() after the line of code where you place the player’s move, "X".

Check if the game should continue (when there is no winner or a tie).

If there is a winner or a tie, print out the final board and the game result! And, add the line of code break;, which prompts the program to break out from the while loop!

Repeat Step Above

In main(), repeat the code in the previous step (check winner and print result if needed) after you place the computer’s move, "O".

Close the Scanner Object

Outside of the while loop, add the code sc.close() to close the Scanner object from reading any new inputs.

It is good practice to close the Scanner object if we won’t be getting any more inputs after we break from the while loop!

All Done :)!

You should now have a functioning TicTacToe game! You should be so proud of yourself for finishing this workshop! Good Job 👍!

As an overview, this should be the structure of your code:
Tic Tac Toe: Your Java Project. void main(String args[]) that reads in player inputs, process inputs, and prompt each round of the game or print the winner of the game. void printBoard(String[] curBoard) is a method to print the game board. int getComputerMove(String[] curBoard) randomly returns an available move for the computer. String getWinner(String[] curBoard) returns the winner of the game/TIE or '' if the game should continue.