File size: 19,287 Bytes
70c07a8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ball Sort Puzzle</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Arial', sans-serif;
}
body {
background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: white;
overflow: hidden;
}
.game-container {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
max-width: 800px;
padding: 20px;
position: relative;
}
h1 {
margin-bottom: 20px;
font-size: 2.5rem;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
text-align: center;
}
.tubes-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 20px;
width: 100%;
margin: 30px 0;
}
.tube {
width: 60px;
height: 200px;
background-color: rgba(255, 255, 255, 0.1);
border-radius: 5px 5px 0 0;
position: relative;
cursor: pointer;
border: 2px solid rgba(255, 255, 255, 0.3);
display: flex;
flex-direction: column-reverse;
align-items: center;
transition: transform 0.2s, box-shadow 0.2s;
margin-bottom: 40px;
}
.tube::after {
content: '';
position: absolute;
bottom: -20px;
left: -2px;
right: -2px;
height: 20px;
background-color: rgba(255, 255, 255, 0.1);
border-radius: 0 0 5px 5px;
border: 2px solid rgba(255, 255, 255, 0.3);
}
.tube:hover {
transform: translateY(-5px);
}
.tube.selected {
transform: scale(1.05);
box-shadow: 0 0 15px rgba(255, 255, 255, 0.7);
}
.tube.valid-move {
animation: pulse 1.5s infinite;
box-shadow: 0 0 15px rgba(0, 255, 0, 0.7);
}
@keyframes pulse {
0% { box-shadow: 0 0 10px rgba(0, 255, 0, 0.7); }
50% { box-shadow: 0 0 20px rgba(0, 255, 0, 0.9); }
100% { box-shadow: 0 0 10px rgba(0, 255, 0, 0.7); }
}
.ball {
width: 50px;
height: 50px;
border-radius: 50%;
margin-top: 5px;
transition: all 0.3s ease;
position: relative;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
color: rgba(0, 0, 0, 0.5);
box-shadow: inset -5px -5px 10px rgba(0, 0, 0, 0.2);
}
.ball::after {
content: '';
position: absolute;
top: 10px;
left: 10px;
width: 15px;
height: 15px;
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.4);
}
.controls {
display: flex;
gap: 20px;
margin-top: 20px;
}
button {
padding: 10px 20px;
font-size: 1rem;
background-color: rgba(255, 255, 255, 0.2);
color: white;
border: 2px solid white;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s;
text-transform: uppercase;
letter-spacing: 1px;
font-weight: bold;
}
button:hover {
background-color: rgba(255, 255, 255, 0.4);
transform: translateY(-2px);
}
.level-info {
margin-top: 20px;
font-size: 1.2rem;
text-align: center;
}
.message {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 30px 50px;
border-radius: 10px;
font-size: 2rem;
display: none;
z-index: 100;
text-align: center;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}
.message button {
margin-top: 20px;
display: block;
width: 100%;
}
.move-counter {
font-size: 1.2rem;
margin-bottom: 20px;
background-color: rgba(0, 0, 0, 0.3);
padding: 10px 20px;
border-radius: 5px;
}
.hint-container {
position: absolute;
top: 20px;
right: 20px;
display: flex;
flex-direction: column;
gap: 10px;
}
.hint-count {
font-size: 0.9rem;
background-color: rgba(0, 0, 0, 0.3);
padding: 5px 10px;
border-radius: 5px;
text-align: center;
}
@media (max-width: 600px) {
.tube {
width: 50px;
height: 180px;
}
.ball {
width: 45px;
height: 45px;
}
h1 {
font-size: 1.8rem;
}
.hint-container {
position: static;
margin-bottom: 10px;
}
}
.floating-balls {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: -1;
}
.floating-ball {
position: absolute;
border-radius: 50%;
opacity: 0.3;
animation: float 15s infinite linear;
}
@keyframes float {
0% { transform: translate(0, 0) rotate(0deg); }
25% { transform: translate(100px, 50px) rotate(90deg); }
50% { transform: translate(200px, -50px) rotate(180deg); }
75% { transform: translate(100px, 50px) rotate(270deg); }
100% { transform: translate(0, 0) rotate(360deg); }
}
</style>
</head>
<body>
<div class="floating-balls" id="floatingBalls"></div>
<div class="game-container">
<div class="hint-container">
<button id="btnHint">Hint</button>
<div class="hint-count">Hints left: <span id="hintCount">3</span></div>
</div>
<h1>Ball Sort Puzzle</h1>
<div class="move-counter">Moves: <span id="moves">0</span></div>
<div class="tubes-container" id="tubes"></div>
<div class="level-info">Level: <span id="level">1</span></div>
<div class="controls">
<button id="btnNewGame">New Game</button>
<button id="btnUndo">Undo</button>
</div>
</div>
<div class="message" id="winMessage">
Level Complete!
<button id="btnNextLevel">Next Level</button>
</div>
<script>
// Game state
const gameState = {
tubes: [],
selectedTube: null,
moves: 0,
level: 1,
moveHistory: [],
hintsUsed: 0,
maxHints: 3
};
// Colors
const colors = [
'#FF5252', '#FF4081', '#E040FB', '#7C4DFF',
'#536DFE', '#448AFF', '#40C4FF', '#18FFFF',
'#64FFDA', '#69F0AE', '#B2FF59', '#EEFF41',
'#FFFF00', '#FFD740', '#FFAB40', '#FF6E40'
];
// DOM elements
const tubesContainer = document.getElementById('tubes');
const movesDisplay = document.getElementById('moves');
const levelDisplay = document.getElementById('level');
const btnNewGame = document.getElementById('btnNewGame');
const btnUndo = document.getElementById('btnUndo');
const btnHint = document.getElementById('btnHint');
const hintCountDisplay = document.getElementById('hintCount');
const winMessage = document.getElementById('winMessage');
const btnNextLevel = document.getElementById('btnNextLevel');
const floatingBalls = document.getElementById('floatingBalls');
// Initialize floating background balls
function createFloatingBalls() {
floatingBalls.innerHTML = '';
for (let i = 0; i < 20; i++) {
const ball = document.createElement('div');
ball.className = 'floating-ball';
const size = Math.random() * 100 + 50;
ball.style.width = `${size}px`;
ball.style.height = `${size}px`;
ball.style.background = colors[Math.floor(Math.random() * colors.length)];
ball.style.left = `${Math.random() * 100}%`;
ball.style.top = `${Math.random() * 100}%`;
ball.style.animationDuration = `${Math.random() * 20 + 10}s`;
floatingBalls.appendChild(ball);
}
}
// Initialize game
function initGame() {
gameState.tubes = [];
gameState.selectedTube = null;
gameState.moves = 0;
gameState.moveHistory = [];
gameState.hintsUsed = 0;
movesDisplay.textContent = gameState.moves;
levelDisplay.textContent = gameState.level;
hintCountDisplay.textContent = gameState.maxHints - gameState.hintsUsed;
tubesContainer.innerHTML = '';
const tubeCount = Math.min(3 + gameState.level, 8);
const colorCount = Math.min(tubeCount - 2, colors.length);
// Create color set
const selectedColors = colors.slice(0, colorCount);
const allBalls = [];
// Create 4 balls for each color
selectedColors.forEach(color => {
for (let i = 0; i < 4; i++) {
allBalls.push(color);
}
});
// Shuffle balls
shuffleArray(allBalls);
// Distribute balls to tubes (first tubeCount - 2 tubes)
const ballsPerTube = allBalls.length / (tubeCount - 2);
for (let i = 0; i < tubeCount - 2; i++) {
const tubeBalls = [];
for (let j = 0; j < ballsPerTube; j++) {
tubeBalls.push(allBalls[i * ballsPerTube + j]);
}
gameState.tubes.push(tubeBalls);
}
// Add empty tubes (2)
for (let i = 0; i < 2; i++) {
gameState.tubes.push([]);
}
renderTubes();
}
// Helper function to shuffle an array
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]];
}
}
// Render tubes and balls
function renderTubes() {
tubesContainer.innerHTML = '';
gameState.tubes.forEach((tubeBalls, index) => {
const tubeElement = document.createElement('div');
tubeElement.className = 'tube';
if (gameState.selectedTube === index) {
tubeElement.classList.add('selected');
}
tubeElement.addEventListener('click', () => handleTubeClick(index));
tubeBalls.forEach(ballColor => {
const ballElement = document.createElement('div');
ballElement.className = 'ball';
ballElement.style.backgroundColor = ballColor;
tubeElement.appendChild(ballElement);
});
tubesContainer.appendChild(tubeElement);
});
}
// Show hint by highlighting valid move targets
function showHint() {
// Check if hints are available
if (gameState.hintsUsed >= gameState.maxHints) {
alert(`No hints left! You've used all ${gameState.maxHints} hints.`);
return;
}
gameState.hintsUsed++;
hintCountDisplay.textContent = gameState.maxHints - gameState.hintsUsed;
// First look for tubes where we can make progress (destination tubes)
for (let i = 0; i < gameState.tubes.length; i++) {
const tube = gameState.tubes[i];
if (tube.length > 0) {
const topColor = tube[tube.length - 1];
let count = 0;
// Count how many balls of same color are stacked
for (let j = tube.length - 1; j >= 0; j--) {
if (tube[j] === topColor) {
count++;
} else {
break;
}
}
// Find potential destination tubes
const potentialTargets = [];
for (let j = 0; j < gameState.tubes.length; j++) {
if (i === j) continue;
const targetTube = gameState.tubes[j];
// Empty tube is always valid
if (targetTube.length === 0) {
potentialTargets.push(j);
}
// Tube with same color top ball and space available
else if (targetTube[targetTube.length - 1] === topColor &&
(targetTube.length + count) <= 4) {
potentialTargets.push(j);
}
}
// If we found valid targets, highlight them
if (potentialTargets.length > 0) {
// Highlight source tube
gameState.selectedTube = i;
renderTubes();
// Highlight target tubes
const tubeElements = document.querySelectorAll('.tube');
potentialTargets.forEach(targetIndex => {
tubeElements[targetIndex].classList.add('valid-move');
});
// Remove highlights after 3 seconds
setTimeout(() => {
gameState.selectedTube = null;
renderTubes();
const validMoves = document.querySelectorAll('.valid-move');
validMoves.forEach(tube => {
tube.classList.remove('valid-move');
});
}, 3000);
return; // Show just one hint at a time
}
}
}
}
// Handle tube click
function handleTubeClick(tubeIndex) {
if (gameState.selectedTube === null) {
// Select a tube if it has balls
if (gameState.tubes[tubeIndex].length > 0) {
gameState.selectedTube = tubeIndex;
renderTubes();
}
} else {
// Try to move ball from selected tube to clicked tube
if (gameState.selectedTube === tubeIndex) {
// Deselect if same tube clicked
gameState.selectedTube = null;
renderTubes();
} else {
moveBall(gameState.selectedTube, tubeIndex);
}
}
}
// Animate moving the top ball from one tube to another
function animateBallMove(fromTubeIndex, toTubeIndex, ballColor) {
// Get source tube element and its last ball element
const fromTubeElement = tubesContainer.children[fromTubeIndex];
const ballElement = fromTubeElement.lastElementChild;
if (!ballElement) return;
// Get destination tube element
const toTubeElement = tubesContainer.children[toTubeIndex];
// Get bounding rectangles for start and target positions
const fromRect = ballElement.getBoundingClientRect();
const toRect = toTubeElement.getBoundingClientRect();
// Determine target position.
// We use the number of balls already in the destination to offset vertically.
const targetBallIndex = gameState.tubes[toTubeIndex].length;
const ballHeight = ballElement.offsetHeight;
// Adjust target position: center the ball horizontally and stack from the bottom
const targetX = toRect.left + toRect.width / 2 - fromRect.width / 2;
const targetY = toRect.bottom - (targetBallIndex + 1) * (ballHeight + 5);
// Create a clone of the ball element for animation
const clone = ballElement.cloneNode(true);
clone.style.position = 'absolute';
clone.style.left = fromRect.left + 'px';
clone.style.top = fromRect.top + 'px';
clone.style.margin = '0';
clone.style.transition = 'all 0.5s ease-in-out';
document.body.appendChild(clone);
// Hide the original ball temporarily
ballElement.style.visibility = 'hidden';
// Start animation on next frame
requestAnimationFrame(() => {
clone.style.left = targetX + 'px';
clone.style.top = targetY + 'px';
});
// When animation completes, update the game state and re-render
clone.addEventListener('transitionend', () => {
clone.remove();
// Determine how many balls of the same color to move (if they are stacked)
let ballsToMove = 0;
const fromTube = gameState.tubes[fromTubeIndex];
for (let i = fromTube.length - 1; i >= 0; i--) {
if (fromTube[i] === ballColor && (gameState.tubes[toTubeIndex].length + ballsToMove) < 4) {
ballsToMove++;
} else {
break;
}
}
// Save current state for undo
const prevState = JSON.parse(JSON.stringify(gameState.tubes));
gameState.moveHistory.push(prevState);
// Move the balls in game state
const movedBalls = gameState.tubes[fromTubeIndex].splice(fromTube.length - ballsToMove, ballsToMove);
gameState.tubes[toTubeIndex].push(...movedBalls);
gameState.moves++;
movesDisplay.textContent = gameState.moves;
gameState.selectedTube = null;
renderTubes();
// Check win condition after state update
if (checkWin()) {
showWinMessage();
}
});
}
// Move ball from one tube to another (with animation)
function moveBall(fromTubeIndex, toTubeIndex) {
const fromTube = gameState.tubes[fromTubeIndex];
const toTube = gameState.tubes[toTubeIndex];
if (fromTube.length === 0) {
gameState.selectedTube = null;
renderTubes();
return;
}
const topBallColor = fromTube[fromTube.length - 1];
// Move is valid if destination is empty OR has the same color and is not full
if (toTube.length === 0 || (toTube[toTube.length - 1] === topBallColor && toTube.length < 4)) {
// Start the animation; state will update once the animation completes
animateBallMove(fromTubeIndex, toTubeIndex, topBallColor);
} else {
gameState.selectedTube = null;
renderTubes();
}
}
// Check if all tubes are complete (all balls of same color or empty)
function checkWin() {
return gameState.tubes.every(tube => {
if (tube.length === 0) return true;
if (tube.length < 4) return false;
const firstColor = tube[0];
return tube.every(ball => ball === firstColor);
});
}
// Show win message
function showWinMessage() {
winMessage.style.display = 'block';
}
// Undo last move
function undoMove() {
if (gameState.moveHistory.length > 0) {
gameState.tubes = gameState.moveHistory.pop();
gameState.moves++;
movesDisplay.textContent = gameState.moves;
gameState.selectedTube = null;
renderTubes();
}
}
// Next level
function nextLevel() {
gameState.level++;
winMessage.style.display = 'none';
initGame();
}
// Event listeners
btnNewGame.addEventListener('click', initGame);
btnUndo.addEventListener('click', undoMove);
btnHint.addEventListener('click', showHint);
btnNextLevel.addEventListener('click', nextLevel);
// Initialize
createFloatingBalls();
initGame();
// Add keyboard controls
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
gameState.selectedTube = null;
renderTubes();
} else if (e.key === 'z' && (e.ctrlKey || e.metaKey)) {
undoMove();
} else if (e.key === 'h') {
showHint();
}
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <a href="https://enzostvs-deepsite.hf.space" style="color: #fff;" target="_blank" >DeepSite</a> <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;"></p></body>
</html> |