各位開發者大家好!我很高興分享我的最新項目:經典的井字遊戲。這個專案是練習 JavaScript 技能的好方法,特別是在處理遊戲邏輯、DOM 操作和使用者互動方面。無論您是剛開始使用 JavaScript 還是正在尋找有趣的挑戰,這款 Tic-Tac-Toe 遊戲都是磨練您技能的完美選擇。
Tic-Tac-Toe 遊戲 是流行的兩人遊戲的基於網路的實現。該專案展示瞭如何創建互動元素、管理遊戲狀態以及實現簡單的人工智慧邏輯。該遊戲被設計為完全響應式,使其可以在桌面和行動裝置上玩。
以下是專案結構的快速瀏覽:
Tic-Tac-Toe/ ├── index.html ├── styles.css └── script.js
要開始該項目,請按照以下步驟操作:
複製儲存庫:
git clone https://github.com/abhishekgurjar-in/Tic-Tac-Toe.git
開啟專案目錄:
cd Tic-Tac-Toe
運行項目:
index.html 檔案設定 Tic-Tac-Toe 遊戲的結構,包括遊戲板和控制按鈕。這是一個片段:
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
styles.css 檔案設計 Tic-Tac-Toe 遊戲的樣式,包括網格佈局、按鈕和響應式設計。以下是一些關鍵樣式:
* { 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; }
script.js 檔案管理遊戲邏輯,包括處理玩家回合、檢查獲勝者以及重置遊戲。這是一個片段:
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);
您可以在這裡查看 Tic-Tac-Toe 遊戲的現場演示。
建立這款 Tic-Tac-Toe 遊戲是一次愉快的體驗,它讓我能夠練習 JavaScript,尤其是在創建互動式 Web 應用程式方面。我希望這個專案能夠激勵您建立自己的遊戲並探索 JavaScript 的可能性。快樂編碼!
這個專案是我不斷提升 Web 開發技能的一部分,專注於 JavaScript 和 DOM 操作。
以上是建立井字遊戲網站的詳細內容。更多資訊請關注PHP中文網其他相關文章!