UNPKG

krying

Version:

krying — games hub, AI, movies, and tools

157 lines (150 loc) • 5.7 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Kobran -Connect Four</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: linear-gradient(135deg, #581c87, #1e3a8a, #3730a3); min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 1rem; } .game-card { background: rgba(255, 255, 255, 0.1); border: 1px solid rgba(255, 255, 255, 0.2); border-radius: 0.75rem; padding: 1.5rem; backdrop-filter: blur(10px); } .game-title { color: white; text-align: center; font-size: 1.5rem; font-weight: bold; margin-bottom: 1rem; } .game-board { display: grid; grid-template-columns: repeat(7, 60px); grid-template-rows: repeat(6, 60px); gap: 5px; background: #0066cc; padding: 10px; border-radius: 10px; margin: 1rem auto; } .cell { width: 60px; height: 60px; background: white; border-radius: 50%; cursor: pointer; transition: all 0.3s ease; } .cell.red { background: #ff4444; } .cell.yellow { background: #ffff00; } .cell:hover { transform: scale(1.1); } .status { color: white; text-align: center; margin-bottom: 1rem; font-weight: bold; } .controls { display: flex; justify-content: center; gap: 1rem; } .btn { background: rgba(255, 255, 255, 0.2); border: 1px solid rgba(255, 255, 255, 0.3); color: white; padding: 0.5rem 1rem; border-radius: 0.375rem; cursor: pointer; font-weight: 500; } .btn:hover { background: rgba(255, 255, 255, 0.3); } </style> </head> <body> <div class="game-card"> <h1 class="game-title">Connect Four</h1> <div class="status" id="status">Red Player's Turn</div> <div class="game-board" id="board"></div> <div class="controls"> <button class="btn" onclick="resetGame()">New Game</button> </div> </div> <script> let board = Array(6).fill().map(() => Array(7).fill(0)); let currentPlayer = 1; let gameOver = false; function createBoard() { const boardEl = document.getElementById('board'); boardEl.innerHTML = ''; for (let row = 0; row < 6; row++) { for (let col = 0; col < 7; col++) { const cell = document.createElement('div'); cell.className = 'cell'; cell.onclick = () => dropPiece(col); cell.dataset.row = row; cell.dataset.col = col; boardEl.appendChild(cell); } } } function dropPiece(col) { if (gameOver) return; for (let row = 5; row >= 0; row--) { if (board[row][col] === 0) { board[row][col] = currentPlayer; updateDisplay(); if (checkWin(row, col)) { document.getElementById('status').textContent = `Player ${currentPlayer === 1 ? 'Red' : 'Yellow'} Wins!`; gameOver = true; return; } currentPlayer = currentPlayer === 1 ? 2 : 1; document.getElementById('status').textContent = `${currentPlayer === 1 ? 'Red' : 'Yellow'} Player's Turn`; return; } } } function updateDisplay() { for (let row = 0; row < 6; row++) { for (let col = 0; col < 7; col++) { const cell = document.querySelector(`[data-row="${row}"][data-col="${col}"]`); if (board[row][col] === 1) { cell.className = 'cell red'; } else if (board[row][col] === 2) { cell.className = 'cell yellow'; } } } } function checkWin(row, col) { const directions = [[0,1], [1,0], [1,1], [1,-1]]; for (let [dr, dc] of directions) { let count = 1; for (let dir of [-1, 1]) { let r = row + dir * dr; let c = col + dir * dc; while (r >= 0 && r < 6 && c >= 0 && c < 7 && board[r][c] === currentPlayer) { count++; r += dir * dr; c += dir * dc; } } if (count >= 4) return true; } return false; } function resetGame() { board = Array(6).fill().map(() => Array(7).fill(0)); currentPlayer = 1; gameOver = false; document.getElementById('status').textContent = 'Red Player\'s Turn'; createBoard(); } createBoard(); </script> </body> </html>