Home > Article > Web Front-end > Build a Tic Tac Toe Game Website
Hello, fellow developers! I'm excited to share my latest project: a classic Tic-Tac-Toe Game. This project is a great way to practice your JavaScript skills, particularly in handling game logic, DOM manipulation, and user interactions. Whether you're just getting started with JavaScript or looking for a fun challenge, this Tic-Tac-Toe game is perfect for honing your skills.
The Tic-Tac-Toe Game is a web-based implementation of the popular two-player game. The project showcases how to create interactive elements, manage game state, and implement simple AI logic. The game is designed to be fully responsive, making it playable on both desktop and mobile devices.
Here's a quick look at the project structure:
Tic-Tac-Toe/ ├── index.html ├── styles.css └── script.js
To get started with the project, follow these steps:
Clone the repository:
git clone https://github.com/abhishekgurjar-in/Tic-Tac-Toe.git
Open the project directory:
cd Tic-Tac-Toe
Run the project:
The index.html file sets up the structure of the Tic-Tac-Toe game, including the game board and control buttons. Here’s a snippet:
8b05045a5be5764f313ed5b9168a17e6 49099650ebdc5f3125501fa170048923 93f0f5c25f18dab9d176bd4f6de5d30e 7c8d9f814bcad6a1d7abe4eda5f773e5 26faf3d1af674280d03ba217d87e9421 b2386ffb911b14667cb8f0f91ea547a7Tic-Tac-Toe Game6e916e0f7d1e588d4f442bf645aedb2f af75c476cdb7e6c074ca6da9b40841de 90392ec4442ad9ff612213ec639da4832cacc6d41bbb37262a98f745aa00fbf0 9c3bca370b5104690d9ef395f2c5f8d1 6c04bd5ca3fcae76e30b72ad730ca86d d39cfd0366094787321899c5020bc378 6d4a5230d2661bb62bed1c8aaf8c64e0Winner94b3e26ee717c64999d7867364b1b4a3 8e523cc4a74074b4a599f8aab4bcbad4New Game65281c5ac262bf6d81768915a4a77ac0 16b28748ea4df4d9c2150843fecfba68 61b85035edf2b42260fdb5632dc5728a 4a249f0d628e2318394fd9b75b4636b1Tic Tac Toe473f0a7621bec819994bb5020d29372a 4883ec0eb33c31828b7c767c806e14c7 3a1385250c1ecad69875af6138783c80 a008380eccad648f043a77a135bd17ac65281c5ac262bf6d81768915a4a77ac0 a008380eccad648f043a77a135bd17ac65281c5ac262bf6d81768915a4a77ac0 a008380eccad648f043a77a135bd17ac65281c5ac262bf6d81768915a4a77ac0 a008380eccad648f043a77a135bd17ac65281c5ac262bf6d81768915a4a77ac0 a008380eccad648f043a77a135bd17ac65281c5ac262bf6d81768915a4a77ac0 a008380eccad648f043a77a135bd17ac65281c5ac262bf6d81768915a4a77ac0 a008380eccad648f043a77a135bd17ac65281c5ac262bf6d81768915a4a77ac0 a008380eccad648f043a77a135bd17ac65281c5ac262bf6d81768915a4a77ac0 a008380eccad648f043a77a135bd17ac65281c5ac262bf6d81768915a4a77ac0 16b28748ea4df4d9c2150843fecfba68 16b28748ea4df4d9c2150843fecfba68 ebf4b789043d9122effbf39b4551717cReset Game65281c5ac262bf6d81768915a4a77ac0 07ebbc2b3735b6cb0330715d08d2749e ffd6ba4147bda351239915f463e46e38 e388a4556c0f65e1904146cc1a846beeMade with ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3 16b28748ea4df4d9c2150843fecfba68 36cc49f0c466276486e50c850b7e4956 73a6ac4ed44ffec12cee46588e518a5e
The styles.css file styles the Tic-Tac-Toe game, including the grid layout, buttons, and responsive design. Here are some key styles:
* { margin: 0; padding: 0; } body { background-color: #548687; text-align: center; } .container { height: 70vh; display: flex; justify-content: center; align-items: center; } .game { height: 60vmin; width: 60vmin; display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 1.5vmin; } .box { height: 18vmin; width: 18vmin; border-radius: 1rem; border: none; box-shadow: 0 0 1rem rgba(0, 0, 0, 0.3); font-size: 8vmin; color: #b0413e; background-color: #ffffc7; } #reset-btn { padding: 1rem; font-size: 1.25rem; background-color: #191913; color: #fff; border-radius: 1rem; border: none; } #new-btn { padding: 1rem; font-size: 1.25rem; background-color: #191913; color: #fff; border-radius: 1rem; border: none; } #msg { color: #ffffc7; font-size: 5vmin; } .msg-container { height: 100vmin; display: flex; justify-content: center; align-items: center; flex-direction: column; gap: 4rem; } .hide { display: none; } .footer { margin: 50px; text-align: center; color: white; }
The script.js file manages the game logic, including handling player turns, checking for a winner, and resetting the game. Here’s a snippet:
let boxes = document.querySelectorAll(".box"); let resetBtn = document.querySelector("#reset-btn"); let newGameBtn = document.querySelector("#new-btn"); let msgContainer = document.querySelector(".msg-container"); let msg = document.querySelector("#msg"); let turnO = true; //playerX, playerO let count = 0; //To Track Draw const winPatterns = [ [0, 1, 2], [0, 3, 6], [0, 4, 8], [1, 4, 7], [2, 5, 8], [2, 4, 6], [3, 4, 5], [6, 7, 8], ]; const resetGame = () => { turnO = true; count = 0; enableBoxes(); msgContainer.classList.add("hide"); }; boxes.forEach((box) => { box.addEventListener("click", () => { if (turnO) { //playerO box.innerText = "O"; turnO = false; } else { //playerX box.innerText = "X"; turnO = true; } box.disabled = true; count++; let isWinner = checkWinner(); if (count === 9 && !isWinner) { gameDraw(); } }); }); const gameDraw = () => { msg.innerText = `Game was a Draw.`; msgContainer.classList.remove("hide"); disableBoxes(); }; const disableBoxes = () => { for (let box of boxes) { box.disabled = true; } }; const enableBoxes = () => { for (let box of boxes) { box.disabled = false; box.innerText = ""; } }; const showWinner = (winner) => { msg.innerText = `Congratulations, Winner is ${winner}`; msgContainer.classList.remove("hide"); disableBoxes(); }; const checkWinner = () => { for (let pattern of winPatterns) { let pos1Val = boxes[pattern[0]].innerText; let pos2Val = boxes[pattern[1]].innerText; let pos3Val = boxes[pattern[2]].innerText; if (pos1Val != "" && pos2Val != "" && pos3Val != "") { if (pos1Val === pos2Val && pos2Val === pos3Val) { showWinner(pos1Val); return true; } } } }; newGameBtn.addEventListener("click", resetGame); resetBtn.addEventListener("click", resetGame);
You can check out the live demo of the Tic-Tac-Toe Game here.
Building this Tic-Tac-Toe game was an enjoyable experience that allowed me to practice JavaScript, especially in creating interactive web applications. I hope this project inspires you to build your own games and explore the possibilities with JavaScript. Happy coding!
This project was developed as part of my ongoing journey to improve my web development skills, with a focus on JavaScript and DOM manipulation.
The above is the detailed content of Build a Tic Tac Toe Game Website. For more information, please follow other related articles on the PHP Chinese website!