A simple web-based Tic Tac Toe game where the player can never win against the computer.
- Open
index.htmlin your web browser - You play as X, and the computer plays as O
- Click on any empty cell to make your move
- Try to win against the computer (spoiler: you can't!)
- Click the "Restart Game" button to start a new game
- Clean, responsive design
- Computer AI that prevents the player from winning
- The computer will either win or force a draw
- Visual feedback for player and computer moves
The computer AI uses a strategy that ensures the player can never win:
- If the computer can win in the next move, it will make that move
- If the player could win in the next move, the computer will block that move
- If the center is available, the computer will take it
- If a corner is available, the computer will take it
- Otherwise, the computer will take any available side
This implementation uses the minimax algorithm principles without the full recursive search, as tic-tac-toe is a simple enough game that can be solved with a few prioritized rules.
- HTML5
- CSS3
- JavaScript (ES6+)
Feel free to use and modify this code for your own projects.
To enable sound when a mark is placed:
- Add two audio files in the project directory:
x.mp3(sound for X)o.mp3(sound for O)
You can use any short click or pop sound for these files. The game will play the corresponding sound each time a mark is placed.