当前位置: 首页 > article >正文

一个十字翻转小游戏

有没有万能公式,我现在只会33 35这种简单的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Toggle Grid Game</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            background-color: #f0f0f0;
        }
        .grid {
            display: grid;
            border: 2px solid red; /* Outer border in red */
            margin-top: 20px;
        }
        .cell {
            width: 30px;
            height: 30px;
            border: 1px solid red; /* Red border for each cell */
            margin: 0;
            padding: 0;
            background-color: white;
            cursor: pointer;
        }
        .cell.black {
            background-color: black;
        }
        .controls {
            margin-bottom: 20px;
        }
        .controls input,
        .controls button {
            margin-right: 10px;
            padding: 5px;
        }
    </style>
</head>
<body>
    <h1>Toggle Grid Game</h1>
    <div class="controls">
        <label for="rows">Rows:</label>
        <input type="number" id="rows" value="3" min="1" max="50">
        <label for="cols">Columns:</label>
        <input type="number" id="cols" value="3" min="1" max="50">
        <button onclick="initializeGrid()">Set Grid</button>
        <button onclick="fillAllBlack()">Fill All Black</button>
    </div>
    <div id="gridContainer"></div>
    <script>
        let rows = 3; // Default number of rows
        let cols = 3; // Default number of columns
        let grid = [];
        let clickCount = 0; // Counter for clicks

        const container = document.getElementById("gridContainer");

        // Function to initialize grid
        function initializeGrid() {
            rows = parseInt(document.getElementById("rows").value, 10);
            cols = parseInt(document.getElementById("cols").value, 10);
            clickCount = 0; // Reset click count

            // Initialize grid with random 0s and 1s
            grid = Array.from({ length: rows }, () => Array.from({ length: cols }, () => Math.random() < 0.5 ? 1 : 0));

            // Set grid layout styles dynamically
            container.style.gridTemplateRows = `repeat(${rows}, 30px)`; // Fixed height for rows
            container.style.gridTemplateColumns = `repeat(${cols}, 30px)`; // Fixed width for columns
            container.classList.add("grid");

            // Render grid
            createGrid();
        }

        // Function to render grid
        function createGrid() {
            container.innerHTML = ""; // Clear existing grid
            grid.forEach((row, rowIndex) => {
                row.forEach((cell, colIndex) => {
                    const button = document.createElement("button");
                    button.className = `cell ${cell ? "black" : ""}`; // Black if 1, white if 0
                    button.onclick = () => toggleCell(rowIndex, colIndex);
                    container.appendChild(button);
                });
            });
            checkWin(); // Check for win condition after rendering
        }

        // Toggle cell and adjacent cells
        function toggleCell(x, y) {
            clickCount++; // Increment click count
            grid[x][y] ^= 1;
            if (x > 0) grid[x - 1][y] ^= 1; // Top
            if (x < rows - 1) grid[x + 1][y] ^= 1; // Bottom
            if (y > 0) grid[x][y - 1] ^= 1; // Left
            if (y < cols - 1) grid[x][y + 1] ^= 1; // Right
            createGrid(); // Re-render the grid
        }

        // Fill all cells with black
        function fillAllBlack() {
            grid = Array.from({ length: rows }, () => Array.from({ length: cols }, () => 1)); // All 1s
            clickCount = 0; // Reset click count
            createGrid(); // Re-render the grid
        }

        // Check if all cells are white
        function checkWin() {
            const allWhite = grid.every(row => row.every(cell => cell === 0)); // Check if all cells are 0
            if (allWhite) {
                setTimeout(() => alert(`Congratulations! You turned all the cells white! 🎉\nYou completed the game in ${clickCount} steps.`), 100);
            }
        }

        // Initial render
        initializeGrid();
    </script>
</body>
</html>


http://www.kler.cn/a/411412.html

相关文章:

  • JSON数据转化为Excel及数据处理分析
  • 充满智慧的埃塞俄比亚狼
  • LeetCode739. 每日温度(2024冬季每日一题 15)
  • Spire.PDF for .NET【页面设置】演示:打开 PDF 时自动显示书签或缩略图
  • 麒麟部署一套NFS服务器,用于创建网络文件系统
  • 替换Nacos的MySQL驱动
  • D80【 python 接口自动化学习】- python基础之HTTP
  • MyBatis框架介绍、部署及使用
  • 在Ubuntu2004中搭建基于ESP-IDF v5.1的ESP32-S3开发环境
  • ES索引模板操作
  • 模拟实现Bash
  • Dubbo 最基础的 RPC 应用(使用 ZooKeeper)
  • C++《模板进阶》
  • Linux 服务器安装 Docker - CentOS 9 (Stream)
  • Qt界面篇:QMessageBox高级用法
  • Java中的多线程
  • YOLOv8实战无人机视角目标检测
  • 网络——浏览器发送一个请求到收到响应经历了哪些步骤
  • react实现模拟chatGPT问答页
  • 春秋云境 CVE 复现
  • UE5 使用SlateViewer模版创建窗口(记录)
  • 人体特定吸收率 (SAR) 分布建模
  • JavaWeb——Maven高级
  • 代码美学:MATLAB制作渐变色
  • 区块链知识体系
  • 人工智能与人类:共创未来的新篇章