A classic Tic-Tac-Toe game built with vanilla JavaScript, HTML, and CSS. Simple, clean, and fun to play!
- Two-player gameplay - Enter your names and battle it out
- Smart win detection - Automatically detects wins and ties
- Clean UI - Responsive design with smooth interactions
- Modal dialogs - Player name input and winner announcements
- Game reset - Start fresh anytime
- Open
tictactoe.htmlin your browser - Enter both player names in the dialog
- Player 1 (X) goes first, then Player 2 (O)
- Click any empty cell to place your mark
- First to get 3 in a row wins!
- Game automatically detects wins, ties, and prevents invalid moves
This project helped me practice key JavaScript concepts:
- Array methods:
some()for win detection,forEach()for UI updates - Event handling: Click listeners and form submissions
- DOM manipulation: Dynamic class additions/removals
- Module pattern: Separating game logic, display, and flow control
- Data attributes: Using
datasetto store cell positions
โโโ tictactoe.html # Game layout and structure
โโโ style.css # Styling and responsive design
โโโ tictactoe.js # Game logic and interactions
โโโ Gameboard() # Core game state and rules
โโโ DisplayController() # UI updates and visual feedback
โโโ GameFlow() # Turn management and game flow
The code follows a modular approach:
- Gameboard: Handles game state, moves, and win conditions
- DisplayController: Manages visual updates and CSS classes
- GameFlow: Controls turns, player management, and game lifecycle
This separation makes the code maintainable and easy to extend!
- Win Detection: Uses
some()method to efficiently check all 8 winning patterns - Turn Management: Boolean flag to alternate between players
- State Management: Separate game logic from visual representation
- Event Delegation: Single click listener handles all cell interactions
- Clone this repo
- Open
tictactoe.htmlin any modern browser - Start playing!
No build process, no dependencies - just pure vanilla JavaScript fun! ๐
Built as a learning project to practice JavaScript fundamentals and DOM manipulation.