Playing with AI: defensive play
While the creation of an algorithm to make the computer play perfectly is beyond the scope of this book, we'll see the basics of artificial intelligence making the CPU player at least trying not to let the human player win that easily.
The idea: When it's time to choose the column, don't pick it randomly among all possible columns, but among the columns that can give the highest number of connected discs if played by the opponent. Look at this picture:
The same board configuration, on the left the possible red moves with the number of possible yellow streaks. The third column from the left is the best move, as it ...