Move Modules

Learn how to take input from the player (user/computer).

What we have

We have our board to play the game.

Requirements of this module

We need input from the player (user/computer) to place a marker on the board. Our game has two modes, so we will need input from the user and the computer.

Let’s break down the process into two parts:

  • User move

  • Computer move

In the next few lessons, we will learn how to get input from the user and computer.

There are two modes (1 and 2) and two players (X and O). The computer move is needed when it’s a “one player” mode and the current player is “X”. In the other case, we need to input the move from the user. We should perform the following steps for this task:

  • Write code for the function getMove().
  • Receive three parameters: board, mode, and currentPlayer.
  • Declare a variable to store the new move of the player.
  • Check whether it is a computer move or a user move.
  • In case of computer move:
    • Call the function getComputerMove(board) and store the resulting move.
    • Display a message on screen showing “Computer chose: ” and the move.
  • In case of user move:
    • Call the function getPlayerMove(currentPlayer, board) and store the move.
    • Return the move stored in a local variable.

Note: We should not worry about the details of subordinate functions (getComputerMove(board) and getPlayerMove(currentPlayer, board)) at this stage.

Try it yourself

All the functions are already defined. You just have to implement the given function and execute the code.

Get hands-on with 1400+ tech skills courses.