Beat The Machine
An algorithm that can defeat, or match, a human opponent in Tic-Tac-Toe
The algorithm illustrates how Tic-Tac-Toe can be conceptualized as a series of sequential moves that can be programmed in a predictable manner.
Challenge · Designing the Algorithm
The algorithm 1) must make moves based on an opponent's most recent move, and 2) must identify when to stop blocking opponent's moves if the opportunity to win presents itself.
To achieve this, the algorithm's next move depends on the numbers of moves already made in the game by both players. Assuming the human opponent goes first:
The computer must simply place its mark on the board
The human opponent will already have two marks on the board and a potential win coming up. The algorithm at this point determines if the human opponent has two marks lined up to win and blocks that place on the board.
Now that the computer has two marks on the board, it can check if they are lined up to win. If so, it will prioritize this move to win. Otherwise, it will simply block the opponent's winning move.