๐ฎ I Built a Web-Based Tic Tac Toe Game from Scratch!
A full dev journey โ from concept to publishing
๐ก Why I Chose Tic Tac Toe
Tic Tac Toe is a simple yet classic game that almost everyone has played at some point. I wanted to challenge myself by building something complete โ from scratch โ and also make it beautiful and responsive for the web. Since itโs a logic-based game with a clean UI, I thought it would be the perfect project to sharpen my skills in HTML, CSS, and JavaScript.
๐๏ธ Folder Structure
Before I started coding, I decided how to organize my files. I wanted to keep everything clean and modular, so I created a folder called:
pgsql
Copy
Edit
TicTacToe_Game/
โโโ index.html
โโโ style.css
โโโ script.js
โโโ README.md
This helped me stay focused and maintain clarity throughout the development.
๐งฑ Phase 1 โ Setting Up the HTML Structure
I started by creating a basic 3x3 grid in my index.html. Each cell is a div with a class name like cell and a unique data-index attribute to help me track positions in JavaScript. I also added a heading, a small turn indicator (to show whoโs playing), and a Restart button.
*Hereโs what I added:
*
- A game title (Tic Tac Toe)
- A 3x3 board with 9 clickable cells
- A turn display
- A restart button
- Later, I styled all of these in CSS to give them life.
๐จ Phase 2 โ Styling with CSS
The game needed to look clean and responsive, so I spent time writing good CSS. I centered the board, made it responsive for mobile and desktop, and added modern design features like:
- Rounded corners
- Subtle box shadows
- Hover effects for cells
- Color coding: green for X, blue for O, yellow highlight for winning cells
- I also added a Google Font to make it look cleaner and more elegant.
โ๏ธ Phase 3 โ Game Logic with JavaScript
This was the most important and fun part. In script.js, I created all the logic to:
- Switch turns between X and O
- Track cell states using an array
- Detect a win using a predefined list of winning combinations
- Detect a draw when the board is full
- Highlight winning cells
- Restart the game without refreshing the page
- I used functions like:
handleClick() โ to handle cell clicks
checkWinner() โ to check for winning conditions
checkDraw() โ to detect a draw
resetGame() โ to restart the game
Each time a move is made, the board updates, the current turn switches, and the game checks if itโs over.
## GITHUB+SORUCE CODE
= [https://github.com/Ajitabh11/Tic_Tac_Toe_game]
๐ Adding Replay Feature
After the game ends (win or draw), I wanted the player to restart without reloading the page. The โRestart Gameโ button resets everything:
- Clears the board
- Resets the turn to X
- Hides the win/draw message
- Makes all cells clickable again
- This made the game loop feel complete.
๐ฑ Responsive Design
I made sure the layout works well on both desktop and mobile devices by:
- Using flexbox for centering
- Adding a meta viewport tag
- Setting width in percentages
- Using media queries (optional)
- Now the game looks good even on small screens!
๐ฆ Preparing for Deployment
Once the game was ready, I added a simple README.md that explains:
- What the game does
- How the code is structured
- How to run it locally
- How to publish it online (using GitHub Pages / Netlify)
- Then I published the whole folder to GitHub and deployed it on GitHub Pages!
๐** What I Learned**
- While building this project, I learned:
- How to plan and structure a project folder
- How to handle logic and UI updates in JavaScript
- How to style using clean and modern CSS
- How to make projects mobile-friendly
- How to deploy a project online
It was a great learning experience, and now I have a complete game I can proudly share with others!
๐ผ๏ธ** Screenshots of My Game**
(You can insert images here)
- Game board UI
- Highlighted win condition
- Responsive layout on mobile
- Restart flow
[Prompts i Used]
Create a complete, responsive, and visually appealing web-based Tic Tac Toe game using only HTML, CSS, and JavaScript, and place all code inside a single folder named TicTacToe_Game. The game should feature a 3x3 grid for two-player interaction (Player X and Player O), allow alternating turns, display the current player's move, and visually highlight the winning combination when a player wins. It must also detect draw conditions and show appropriate result messages such as 'X Wins', 'O Wins', or 'Itโs a Draw!'. Include a clean and modern user interface with rounded clickable cells, subtle hover effects, light shadows, and color-coded markers (green for X, blue for O, yellow highlight for winning tiles). Add a styled 'Restart Game' button to reset the board and replay without reloading the page. The layout should be centered, responsive on both desktop and mobile devices using a meta viewport tag, and optionally use a Google Font for better aesthetics. Organize the project into three files: index.html, style.css, and script.js, with well-commented, modular JavaScript code and clean semantic HTML structure. Make sure the code is beginner-friendly, includes no external libraries or frameworks, and is fully ready to be published directly on GitHub Pages, Netlify, or Vercel. Also include a basic README.md file with game description, file structure, and instructions on how to run or deploy the game."
๐ท PHASE 1: Core Logic
Goal: Build the main engine of the game.
Create a main.py file.
Define a 3x3 board using a list or 2D array.
Alternate turns between two players โ Player X and Player O.
Accept valid input positions (1โ9 or row-column format).
Check after every move for win conditions (rows, columns, diagonals).
Check for a draw if all positions are filled without a winner.
Display result messages: "Player X wins!", "Player O wins!", or "Itโs a draw!".
Ensure inputs are validated (no overwriting positions or invalid values).
๐ฉ PHASE 2: UI, Sound, and Replay System
Goal: Add polish to the game with basic visuals and replay ability.
Display the board after every move in a formatted 3x3 grid.
Use color coding for the board: green for X, blue for O (using ANSI colors).
Add ASCII-style borders or visual enhancements for the board.
Play sounds (placeholder: x_move.wav, o_move.wav, win.wav, draw.wav) from the assets/sounds/ folder.
At the end of the game, prompt: โDo you want to play again? (Y/N)โ
Reset the board and loop the game if the user wants to replay.
Organize code into functions: print_board(), check_winner(), get_input(), play_game()
Add a simple README.md with usage instructions and file structure.
๐ฌ Final Words
This project may look small, but it taught me a lot. If you're learning web development, I highly recommend trying something like this. Tic Tac Toe is simple, but building it yourself helps you learn DOM manipulation, game logic, event handling, and UI design โ all in one project.
I'll soon be moving on to build another game โ Hangman โ and Iโll share that dev vlog too. Stay tuned!
Top comments (0)