Time To Code: Task IV

Now, let’s build on our project and implement the rest of the game.

We'll cover the following

Task IV: Make the user’s move

Once we have the layout of our game, we need to start playing. We’ll take the players’ moves as input and then update the board accordingly.

Note: We’ll use java.util.Scanner to take the user’s input, but you don’t need to know its details as it is not part of your AP CS A exam.

Let’s discuss this part of the project a little more to better understand how to take user input. On line 13, we create a Scanner object that will help us to take the user input. On line 15 and 17, we take the integer values of row and col from the player. The row and col tell us the position at which our player wants to place their mark. For example, game_board[1][1] means the player has placed their mark, x or o, at the second row and second column of the board. The mark will only be placed if the entered value is valid!

The key points for the validity of the user input are listed below:

  • The valid values for row and col would be any value within the dimensions of our game board.

  • We’ll also check whether the entered values for row and col have already been used by the other player or not.

Our task is to validate the player’s input and then update the game_board accordingly.

Get hands-on with 1400+ tech skills courses.