html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Water Sort Puzzle</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
#gameContainer {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 10px;
margin: 20px;
}
.tube {
width: 50px;
height: 200px;
background-color: rgba(255, 255, 255, 0.8);
border: 2px solid #333;
border-radius: 0 0 25px 25px;
display: flex;
flex-direction: column-reverse;
overflow: hidden;
cursor: pointer;
}
.segment {
height: 50px;
transition: all 0.3s ease;
}
.selected {
border: 2px solid #ff0;
box-shadow: 0 0 10px rgba(255, 255, 0, 0.5);
}
button {
margin: 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
}
button:hover {
background-color: #45a049;
}
#message {
margin: 20px;
font-size: 20px;
color: #333;
}
</style>
</head>
<body>
<h1>Water Sort Puzzle</h1>
<div id="message">將相同顏色的液體集中在同一個試管中即可獲勝!</div>
<div id="gameContainer"></div>
<div>
<button onclick="restartGame()">重新開始</button>
<button onclick="undo()">撤銷</button>
</div>
<script>
const COLORS = ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#ff00ff'];
const TUBE_CAPACITY = 4;
const TUBE_COUNT = 7; // 5個顏色管 + 2個空管
let tubes = [];
let selectedTube = null;
let moveHistory = [];
function createInitialState() {
let colors = [];
// 每個顏色重複4次
COLORS.forEach(color => {
for (let i = 0; i < TUBE_CAPACITY; i++) {
colors.push(color);
}
});
// 打亂顏色順序
colors = shuffleArray(colors);
// 創建試管並分配顏色
tubes = [];
for (let i = 0; i < TUBE_COUNT; i++) {
if (i < TUBE_COUNT - 2) { // 前5個管子裝顏色
tubes.push(colors.slice(i * TUBE_CAPACITY, (i + 1) * TUBE_CAPACITY));
} else { // 後2個是空管
tubes.push([]);
}
}
}
function shuffleArray(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 renderTubes() {
const container = document.getElementById('gameContainer');
container.innerHTML = '';
tubes.forEach((tube, index) => {
const tubeElement = document.createElement('div');
tubeElement.className = 'tube';
if (selectedTube === index) {
tubeElement.classList.add('selected');
}
// 填充空位
for (let i = tube.length; i < TUBE_CAPACITY; i++) {
const emptySegment = document.createElement('div');
emptySegment.className = 'segment';
tubeElement.appendChild(emptySegment);
}
// 添加顏色段
tube.forEach(color => {
const segment = document.createElement('div');
segment.className = 'segment';
segment.style.backgroundColor = color;
tubeElement.appendChild(segment);
});
tubeElement.onclick = () => handleTubeClick(index);
container.appendChild(tubeElement);
});
}
function handleTubeClick(index) {
if (selectedTube === null) {
if (tubes[index].length > 0) {
selectedTube = index;
}
} else {
if (canPour(selectedTube, index)) {
pourWater(selectedTube, index);
if (checkWin()) {
document.getElementById('message').textContent = '恭喜你獲勝!';
}
}
selectedTube = null;
}
renderTubes();
}
function canPour(from, to) {
if (from === to) return false;
if (tubes[from].length === 0) return false;
if (tubes[to].length >= TUBE_CAPACITY) return false;
const sourceColor = tubes[from][tubes[from].length - 1];
return tubes[to].length === 0 || tubes[to][tubes[to].length - 1] === sourceColor;
}
function pourWater(from, to) {
const sourceColor = tubes[from][tubes[from].length - 1];
let amount = 0;
// 計算可以倒多少水
for (let i = tubes[from].length - 1; i >= 0; i--) {
if (tubes[from][i] === sourceColor && tubes[to].length + amount < TUBE_CAPACITY) {
amount++;
} else {
break;
}
}
// 保存移動歷史
moveHistory.push({
from,
to,
amount,
color: sourceColor
});
// 執行倒水
for (let i = 0; i < amount; i++) {
tubes[from].pop();
tubes[to].push(sourceColor);
}
}
function undo() {
if (moveHistory.length === 0) return;
const lastMove = moveHistory.pop();
for (let i = 0; i < lastMove.amount; i++) {
tubes[lastMove.to].pop();
tubes[lastMove.from].push(lastMove.color);
}
renderTubes();
}
function checkWin() {
return tubes.every(tube => {
if (tube.length === 0) return true;
if (tube.length !== TUBE_CAPACITY) return false;
return tube.every(color => color === tube[0]);
});
}
function restartGame() {
createInitialState();
moveHistory = [];
selectedTube = null;
document.getElementById('message').textContent = '將相同顏色的液體集中在同一個試管中即可獲勝!';
renderTubes();
}
// 初始化遊戲
restartGame();
</script>
</body>
</html>