Beat The Machine

AI-Powered Tic Tac Toe


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.


The code was written in javascript and styled minimally with CSS.




Technology

  • Javascript
  • HTML
  • CSS


Challenge · Designing the Algorithm


Objectives:


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:


1-2 Moves:

The computer must simply place its mark on the board


3 Moves:

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.


4-9 Moves:

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. 

Diagram illustrating the computer's possible moves. There are only 9 possible moves in the whole game.

Code Overview. For more, see the repo linked below:



Final Product