chatgpt-4o-latest-20250326· OpenAI
LMArena Human Preference 140K
幫我寫一個 water sort 的 web game, 放在單一個 html 裡面, 請根據合理的設定來製作遊戲 (比如說有足夠的 buffer tube 好做交換),…
- User
幫我寫一個 water sort 的 web game, 放在單一個 html 裡面, 請根據合理的設定來製作遊戲 (比如說有足夠的 buffer tube 好做交換), 請盡量完善遊戲內容.
- Assistant
好的,這是一個 Water Sort Puzzle 的完整網頁遊戲,包含以下功能:
✨ 遊戲特性:
- 顏色排序邏輯遊戲:將相同顏色液體倒到同一試管中。
- 試管滿 / 空規則正確處理。
- 有足夠 buffer tubes 提供換位操作。
- 完成條件檢查。
- 基本的 UI 和互動邏輯。
- 一頁 HTML,無需額外資源。
✅ 規則簡述:
- 點擊選取來源試管,再點擊目標試管來倒液體。
- 只能將相同顏色的液體倒到相同顏色或空的液體之上。
- 每次只能倒最多和頂部相同顏色的部分。
- 不能倒進滿了的試管。
- 完成條件:所有試管要不是空的,要不只含有一種顏色。
🧪 完整 HTML 代碼如下:
<!DOCTYPE html> <html lang="zh-Hant"> <head> <meta charset="UTF-8"> <title>Water Sort Puzzle 遊戲</title> <style> body { background: #f0f0f0; font-family: sans-serif; text-align: center; padding: 20px; } h1 { color: #333; } #game { display: flex; flex-wrap: wrap; justify-content: center; margin-top: 20px; gap: 20px; } .tube { width: 60px; height: 200px; border: 2px solid #555; border-radius: 10px; background: #fff; display: flex; flex-direction: column-reverse; justify-content: flex-start; align-items: center; cursor: pointer; } .color { width: 90%; height: 25%; border-radius: 5px; box-sizing: border-box; margin: 1px 0; } .selected { border: 4px solid #007bff; } button { padding: 10px 20px; margin: 20px; font-size: 16px; } #message { font-size: 20px; color: green; margin-top: 10px; } </style> </head> <body> <h1>💧 Water Sort Puzzle</h1> <button onclick="initGame()">重新開始</button> <div id="game"></div> <div id="message"></div> <script> const COLORS = ['#FF6663', '#FEB144', '#FDFD97', '#9EE09E', '#9EC1CF', '#CC99C9']; // 6 種顏色 const LAYERS = 4; // 每管液體層數 const FILLED_TUBES = COLORS.length; // 填滿的試管數(每種顏色出現 4 次) const EXTRA_TUBES = 2; // buffer tubes const TOTAL_TUBES = FILLED_TUBES + EXTRA_TUBES; let tubes = []; let selectedTubeIndex = null; function shuffle(array) { for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } return array; } function initGame() { // 初始化所有顏色 let colorsPool = []; for (let color of COLORS) { for (let i = 0; i < LAYERS; i++) { colorsPool.push(color); } } shuffle(colorsPool); // 分配顏色給試管 tubes = []; for (let i = 0; i < FILLED_TUBES; i++) { tubes.push(colorsPool.splice(0, LAYERS)); } // 加入空試管 for (let i = 0; i < EXTRA_TUBES; i++) { tubes.push([]); } selectedTubeIndex = null; renderGame(); document.getElementById('message').textContent = ''; } function renderGame() { const gameDiv = document.getElementById('game'); gameDiv.innerHTML = ''; tubes.forEach((tube, i) => { const tubeDiv = document.createElement('div'); tubeDiv.className = 'tube'; tubeDiv.dataset.index = i; if (i === selectedTubeIndex) { tubeDiv.classList.add('selected'); } for (let j = 0; j < tube.length; j++) { const colorDiv = document.createElement('div'); colorDiv.className = 'color'; colorDiv.style.background = tube[j]; tubeDiv.appendChild(colorDiv); } // 補空層確保視覺上高度一致 for (let j = tube.length; j < LAYERS; j++) { const spacer = document.createElement('div'); spacer.className = 'color'; spacer.style.visibility = 'hidden'; tubeDiv.appendChild(spacer); } tubeDiv.addEventListener('click', () => handleTubeClick(i)); gameDiv.appendChild(tubeDiv); }); } function handleTubeClick(i) { if (selectedTubeIndex === null) { selectedTubeIndex = i; } else if (selectedTubeIndex === i) { selectedTubeIndex = null; } else { pour(selectedTubeIndex, i); selectedTubeIndex = null; } renderGame(); } function pour(from, to) { if (from === to) return; const source = tubes[from]; const target = tubes[to]; if (source.length === 0 || target.length === LAYERS) return; const movingColor = source[source.length - 1]; // 確認目標試管可倒入 if (target.length > 0 && target[target.length - 1] !== movingColor) return; if (target.length >= LAYERS) return; // 計算要倒幾層 let moveCount = 0; for (let i = source.length - 1; i >= 0; i--) { if (source[i] === movingColor) moveCount++; else break; } const spaceLeft = LAYERS - target.length; const pourCount = Math.min(moveCount, spaceLeft); for (let i = 0; i < pourCount; i++) { target.push(source.pop()); } checkWin(); } function checkWin() { const allGood = tubes.every(tube => { return tube.length === 0 || (tube.length === LAYERS && new Set(tube).size === 1); }); if (allGood) { document.getElementById('message').textContent = '🎉 恭喜你完成了!'; } } // 自動啟動 initGame(); </script> </body> </html>
📱 使用方式:
- 將上述代碼貼入任何
.html檔案。 - 用你的瀏覽器打開它,即可遊玩。
你可以進一步加入:
- Undo 步驟。
- 不同關卡配置。
- 記錄時間、步數。
- 漂亮動畫。
如果你希望我加入更進階功能,例如 儲存進度、關卡系統或音效,可以告訴我,我可以協助你擴展遊戲功能!🎮🌈
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.