幫我寫一個 water sort 的 web game, 放在單一個 html 裡面, 請根據合理的設定來製作遊戲 (比如說有足夠的 buffer tube 好做交換),…
- User
幫我寫一個 water sort 的 web game, 放在單一個 html 裡面, 請根據合理的設定來製作遊戲 (比如說有足夠的 buffer tube 好做交換), 包含重新開始跟 undo 按鈕, 請盡量完善遊戲內容.
Assistant
好大的挑戰!我會盡量幫你寫一個完善的 Water Sort 遊戲在單一個 HTML 文件裡,使用 HTML、CSS 和 JavaScript。這個遊戲會包含以下功能:
- 遊戲初始化:隨機生成一定數量的試管(tube),每個試管裝有隨機顏色的水(最多4種顏色),並且會有足夠的緩衝試管(buffer tube)來進行交換。
- 玩家互動:點擊試管進行水顏色交換。
- 遊戲目標:將所有試管中的水按照顏色排序,每個試管只包含一種顏色的水。
- 重新開始(Restart)按鈕:重置遊戲,重新生成隨機試管內容。
- 撤銷(Undo)按鈕:返回上一步操作的狀態。
下面是完整的 HTML 代碼,請複製並保存為
.html文件後在瀏覽器中打開即可遊玩:<!DOCTYPE html> <html lang="zh-TW"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Water Sort Puzzle Game</title> <style> body { font-family: Arial, sans-serif; text-align: center; } .tube-container { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin: 20px; } .tube { width: 50px; height: 200px; border: 2px solid #333; border-radius: 5px; background-color: #f0f0f0; display: flex; flex-direction: column-reverse; align-items: center; justify-content: flex-end; cursor: pointer; } .color-block { width: 40px; height: 40px; margin: 1px 0; border-radius: 3px; } button { margin: 10px; padding: 10px 20px; font-size: 16px; } .empty { background-color: #fff; border: 1px dashed #ccc; } </style> </head> <body> <h1>Water Sort Puzzle Game</h1> <div class="tube-container" id="tubeContainer"></div> <button onclick="restartGame()">重新開始 (Restart)</button> <button onclick="undoMove()">撤銷 (Undo)</button> <p id="statusText">移動水塊來排序!</p> <script> // 顏色列表 const COLORS = ['#FF0000', '#0000FF', '#00FF00', '#FFFF00', '#FF00FF', '#00FFFF']; // 遊戲設定 const TUBE_COUNT = 8; // 總試管數 const BUFFER_TUBES = 2; // 緩衝試管數 const TUBE_CAPACITY = 4; // 每個試管容量 let tubes = []; // 當前試管狀態 let moveHistory = []; // 移動歷史記錄 let selectedTube = null; // 已選擇的試管 // 初始化遊戲 function initGame() { tubes = []; moveHistory = []; selectedTube = null; // 生成空試管結構 for (let i = 0; i < TUBE_COUNT; i++) { tubes.push([]); } // 隨機分配顏色到非緩衝試管 const colorTubes = TUBE_COUNT - BUFFER_TUBES; const colorsPerTube = TUBE_CAPACITY; for (let i = 0; i < colorTubes; i++) { for (let j = 0; j < colorsPerTube; j++) { const color = COLORS[Math.floor(Math.random() * COLORS.length)]; tubes[i].push(color); } } // 緩衝試管保持空 for (let i = TUBE_COUNT - BUFFER_TUBES; i < TUBE_COUNT; i++) { tubes[i] = []; } renderTubes(); } // 繪製試管狀態 function renderTubes() { const container = document.getElementById('tubeContainer'); container.innerHTML = ''; tubes.forEach((tube, index) => { const tubeDiv = document.createElement('div'); tubeDiv.className = 'tube'; tubeDiv.onclick = () => selectTube(index); if (tube.length === 0) { const emptyBlock = document.createElement('div'); emptyBlock.className = 'color-block empty'; tubeDiv.appendChild(emptyBlock); } else { tube.forEach(color => { const colorBlock = document.createElement('div'); colorBlock.className = 'color-block'; colorBlock.style.backgroundColor = color; tubeDiv.appendChild(colorBlock); }); } container.appendChild(tubeDiv); }); checkGameStatus(); } // 選擇試管邏輯 function selectTube(index) { if (selectedTube === null) { if (tubes[index].length > 0) { selectedTube = index; highlightTube(index, true); } } else { if (index === selectedTube) { highlightTube(index, false); selectedTube = null; } else { if (moveWater(selectedTube, index)) { moveHistory.push([selectedTube, index]); } highlightTube(selectedTube, false); selectedTube = null; } } } // 高亮選中試管 function highlightTube(index, highlight) { const tubeDiv = document.querySelectorAll('.tube')[index]; tubeDiv.style.border = highlight ? '3px solid #FFA500' : '2px solid #333'; } // 移動水塊邏輯 function moveWater(from, to) { if (tubes[to].length === TUBE_CAPACITY) return false; // 目標試管已滿 const fromColor = tubes[from][tubes[from].length - 1]; if (tubes[to].length === 0 || tubes[to][tubes[to].length - 1] === fromColor) { const water = tubes[from].pop(); tubes[to].push(water); renderTubes(); return true; } return false; } // 檢查遊戲狀態 function checkGameStatus() { let isWin = true; for (let i = 0; i < TUBE_COUNT - BUFFER_TUBES; i++) { const tube = tubes[i]; if (tube.length === 0) continue; if (tube.length !== TUBE_CAPACITY || new Set(tube).size !== 1) { isWin = false; break; } } document.getElementById('statusText').innerText = isWin ? "恭喜!你贏了!" : "移動水塊來排序!"; if (isWin) { alert("恭喜!你贏了!"); } } // 撤銷最後一步 function undoMove() { if (moveHistory.length === 0) { alert("沒有可撤銷的步驟!"); return; } const [from, to] = moveHistory.pop(); const water = tubes[to].pop(); tubes[from].push(water); renderTubes(); } // 重新開始遊戲 function restartGame() { initGame(); } // 啟動遊戲 initGame(); </script> </body> </html>遊戲說明:
- 遊戲目標:將前
TUBE_COUNT - BUFFER_TUBES個試管中的水塊按顏色排序,每個試管只能有一種顏色。 - 點擊試管:第一次點擊選擇來源試管,第二次點擊選擇目標試管。
- 只有非空試管可以被選為來源。
- 水塊只能倒入顏色相同或空試管中。
- 緩衝試管:最後兩個試管(可調整)始終為空,用於臨時存放水塊。
- Undo 功能:點擊 撤銷 可返回上一步操作的狀態。
- 重新開始:隨機生成新的試管顏色分布。
技術亮點:
- 使用
moveHistory數組記錄每一步操作,實現 Undo 功能。 - 動態渲染試管內容,用
border高亮選中試管,提升交互體驗。 - 遊戲勝負條件自動檢測,完成排序後彈窗提示。
你可以調整的參數:
TUBE_COUNT:總試管數。BUFFER_TUBES:緩衝試管數(建議至少 2 個)。TUBE_CAPACITY:每個試管的水塊容量(默認 4)。COLORS:顏色數組,可增加/減少顏色種類。
複製以上代碼保存為
water_sort.html,用瀏覽器打開即可開始玩!如果發現任何問題,歡迎提出。祝你遊戲愉快! 🎉
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.