Gomoku game (human vs. computer)

In the last lesson, we discussed the Gomoku human vs. computer approach. In this lesson, we'll implement this approach while explaining the solution in detail. The following reiterates the requirements to convert the Gomoku human vs. human approach to the human vs. computer approach:

  1. The number of players is restricted to two; this is done to ensure that we have one human and one computer player to simplify the algorithm.

  2. The computer can randomly place its symbol in any valid board position.

  3. The computer should perform its turn by keeping in mind the following two points:

    1. The computer must prioritize placing its symbol where it has a winning chance.

    2. If that's not the case, then the computer must prioritize placing its symbol where it can block the opponent's win.

Implementation walkthrough

Restricting the number of players to two

Since we are restricting the number of players to two, there's no need for the NOP variable. Hence, the implementation of the init() and turnChange() functions have to be altered because both use the NOP variable.

Updated version of the init() function

In the previous version, the init() function asked for the number of players from the user and stored the value inside the NOP variable. Based on this variable, it asked the user for the names and symbols of said players. Since we'll only have one player in this version, all of this is unnecessary. The code snippet below highlights the changes made to the init() function:

Get hands-on with 1200+ tech skills courses.