Java Bootcamp – Workbooks and Challenges
)}

Tic Tac Toe - Part 2

If you made it to part 2, then you were able to achieve this output:

The next step is to let the players take turns on the board:

Inside the workbook, Scanner was declared as a class variable. That's because you'll need to access Scanner from more than one place inside the class.

public class TicTacToe { Scanner scan = new Scanner(System.in); public static void main(String[] args) { //...  }

Task 3: Taking turns

The maximum number of turns is 9. Set up a for loop that runs 9 times and prints the counter.

Each run represents a turn, such that X goes first.

Hint: X plays when the counter is even, whereas O plays when the counter is odd. Set up a condition that alternates between printing Turn: X and Turn: O.

Task 4: Write a function

Write a function that asks the user to choose a spot. As you write the function, call it for each turn.

if (X turn) { Task 4: call askUser(board). } else { Task 4: call askUser(board). }

1. Function name: The name of the function is askUser(). It lets the player choose a spot on the board.

/** * Function name -- askUser <------- * @param board (char[][] board) * @return spot (int[]) * * Inside the function * 1. Asks the user: - pick the row and column: * 2. If the spot is taken, ask the user to choose again. * 3. Return the row and column in an int[] array. * */

2. Parameters: The function takes one parameter: char[][] board.

/** * Function name -- askUser * @param board (char[][] board) <-------- * @return spot (int[]) * * Inside the function * 1. Asks the user: - pick the row and column: * 2. If the spot is taken, ask the user to choose again. * 3. Return the row and column in an int[] array. * */

3. Return value: The function returns the spot. It will be an array that holds two numbers: the row and column.

/** * Function name -- askUser * @param board (char[][] board) * @return spot (int[]) <------- * * Inside the function * 1. Asks the user: - pick the row and column: * 2. If the spot is taken, ask the user to choose again. * 3. Return the row and column in an int[] array. * */

Inside the function:

  • Ask the player to pick a spot on the grid and return it
/** * Function name -- askUser * @param board (char[][] board) * @return spot (int[]) * * Inside the function <--- * 1. Asks the user: - pick a row and column number: <--- * 2. If the spot is taken, ask the user to choose again. * 3. Return the row and column in an int[] array. <--- * */

The user will write their numbers beside each question. Then, store both numbers in the int[] array.

Ignore the second step for now. You can move on to task 5.

/** * Function name -- askUser * @param board (char[][] board) * @return spot (int[]) * * Inside the function * 1. Asks the user: - pick a row and column number: * * ***** Ignore this step for now ***** * 2. If the spot is taken, ask the user to choose again. * ***** Ignore this step for now ***** * 3. Return the row and column in an int[] array. * */

Task 5: Populate the board


The function call returns the player's spot. Use that return value to index the board and populate it. Then, print the board:

Task 4: Revisited


We can't let the player pick a spot that's already taken.

Inside the function:

  • Check if the spot on the board is taken. If so, prompt the user to choose again.
/** * Function name -- askUser * @param board (char[][] board) * @return spot (int[]) * * Inside the function <------ * 1. Asks the user: - pick a row and column number: * 2. If the spot is taken, ask the user to choose again. <---- * 3. Return the row and column in an int[] array. * */

Hint: use a while loop! Final output:

The player tried to pick a spot that was already taken (0 0). The game prompted them to pick another spot.

Good Luck!

Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.