Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

8.9 KB
Raw Blame
<div class="p-4 flex flex-col items-center">
  <div class="mb-4 text-center">
    <h1 class="text-2xl font-bold mb-2">Minesweeper</h1>
    <div class="flex gap-4 justify-center items-center mb-2">
      <div class="text-lg font-mono">🚩 <span id="flag-count">0</span></div>
      <button id="reset-btn" class="px-4 py-2 bg-gray-200 hover:bg-gray-300 rounded">
        😊 New Game
      </button>
      <div class="text-lg font-mono">⏱️ <span id="timer">0</span></div>
    </div>
    <div id="status" class="text-sm font-semibold"></div>
  </div>
  <div id="game-board" class="inline-block border-4 border-gray-400 bg-gray-300"></div>

  <style>
    .cell {
      width: 30px;
      height: 30px;
      border: 2px outset #999;
      background: #c0c0c0;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      font-weight: bold;
      font-size: 16px;
      cursor: pointer;
      user-select: none;
      box-sizing: border-box;
    }
    .cell:hover:not(.revealed):not(.flagged) {
      background: #d0d0d0;
    }
    .cell.revealed {
      border: 1px solid #999;
      background: #e0e0e0;
      cursor: default;
    }
    .cell.flagged {
      background: #c0c0c0;
    }
    .cell.mine {
      background: #ff6b6b;
    }
    .cell.num-1 {
      color: blue;
    }
    .cell.num-2 {
      color: green;
    }
    .cell.num-3 {
      color: red;
    }
    .cell.num-4 {
      color: darkblue;
    }
    .cell.num-5 {
      color: darkred;
    }
    .cell.num-6 {
      color: cyan;
    }
    .cell.num-7 {
      color: black;
    }
    .cell.num-8 {
      color: gray;
    }
  </style>

  <script>
    class Minesweeper {
      constructor(rows, cols, mines) {
        this.rows = rows;
        this.cols = cols;
        this.totalMines = mines;
        this.board = [];
        this.revealed = [];
        this.flagged = [];
        this.gameOver = false;
        this.gameWon = false;
        this.flagCount = 0;
        this.revealedCount = 0;
        this.timer = 0;
        this.timerInterval = null;
        this.firstClick = true;

        this.initBoard();
        this.render();
        window.__gameState = 'playing';
        window.__gameWon = false;
        window.__gameLost = false;
      }

      initBoard() {
        // Create empty board
        for (let r = 0; r < this.rows; r++) {
          this.board[r] = [];
          this.revealed[r] = [];
          this.flagged[r] = [];
          for (let c = 0; c < this.cols; c++) {
            this.board[r][c] = 0;
            this.revealed[r][c] = false;
            this.flagged[r][c] = false;
          }
        }
      }

      placeMines(excludeRow, excludeCol) {
        let placed = 0;
        while (placed < this.totalMines) {
          const r = Math.floor(Math.random() * this.rows);
          const c = Math.floor(Math.random() * this.cols);

          // Don't place mine on first click or if already a mine
          if ((r === excludeRow && c === excludeCol) || this.board[r][c] === -1) {
            continue;
          }

          this.board[r][c] = -1; // -1 represents a mine
          placed++;
        }

        // Calculate numbers
        for (let r = 0; r < this.rows; r++) {
          for (let c = 0; c < this.cols; c++) {
            if (this.board[r][c] !== -1) {
              this.board[r][c] = this.countAdjacentMines(r, c);
            }
          }
        }
      }

      countAdjacentMines(row, col) {
        let count = 0;
        for (let dr = -1; dr <= 1; dr++) {
          for (let dc = -1; dc <= 1; dc++) {
            if (dr === 0 && dc === 0) continue;
            const r = row + dr;
            const c = col + dc;
            if (r >= 0 && r < this.rows && c >= 0 && c < this.cols && this.board[r][c] === -1) {
              count++;
            }
          }
        }
        return count;
      }

      reveal(row, col) {
        if (this.gameOver || this.revealed[row][col] || this.flagged[row][col]) {
          return;
        }

        if (this.firstClick) {
          this.placeMines(row, col);
          this.firstClick = false;
          this.startTimer();
        }

        this.revealed[row][col] = true;
        this.revealedCount++;

        if (this.board[row][col] === -1) {
          this.lose();
          return;
        }

        // Auto-reveal adjacent cells if count is 0
        if (this.board[row][col] === 0) {
          for (let dr = -1; dr <= 1; dr++) {
            for (let dc = -1; dc <= 1; dc++) {
              if (dr === 0 && dc === 0) continue;
              const r = row + dr;
              const c = col + dc;
              if (r >= 0 && r < this.rows && c >= 0 && c < this.cols && !this.revealed[r][c]) {
                this.reveal(r, c);
              }
            }
          }
        }

        this.checkWin();
        this.render();
      }

      toggleFlag(row, col) {
        if (this.gameOver || this.revealed[row][col]) {
          return;
        }

        this.flagged[row][col] = !this.flagged[row][col];
        this.flagCount += this.flagged[row][col] ? 1 : -1;
        this.render();
      }

      checkWin() {
        const nonMines = this.rows * this.cols - this.totalMines;
        if (this.revealedCount === nonMines) {
          this.win();
        }
      }

      win() {
        this.gameOver = true;
        this.gameWon = true;
        window.__gameState = 'won';
        window.__gameWon = true;
        clearInterval(this.timerInterval);
        document.getElementById('status').textContent = 'πŸŽ‰ You Won!';
        document.getElementById('status').className = 'text-sm font-semibold text-green-600';
        document.getElementById('reset-btn').textContent = '😎 New Game';
      }

      lose() {
        this.gameOver = true;
        window.__gameState = 'lost';
        window.__gameLost = true;
        clearInterval(this.timerInterval);

        // Reveal all mines
        for (let r = 0; r < this.rows; r++) {
          for (let c = 0; c < this.cols; c++) {
            if (this.board[r][c] === -1) {
              this.revealed[r][c] = true;
            }
          }
        }

        document.getElementById('status').textContent = 'πŸ’₯ Game Over!';
        document.getElementById('status').className = 'text-sm font-semibold text-red-600';
        document.getElementById('reset-btn').textContent = '😒 New Game';
        this.render();
      }

      startTimer() {
        this.timerInterval = setInterval(() => {
          this.timer++;
          document.getElementById('timer').textContent = this.timer;
        }, 1000);
      }

      render() {
        const board = document.getElementById('game-board');
        board.innerHTML = '';
        board.style.display = 'grid';
        board.style.gridTemplateColumns = `repeat(${this.cols}, 30px)`;
        board.style.gap = '0';

        document.getElementById('flag-count').textContent = `${this.flagCount}/${this.totalMines}`;

        for (let r = 0; r < this.rows; r++) {
          for (let c = 0; c < this.cols; c++) {
            const cell = document.createElement('div');
            cell.className = 'cell';
            cell.dataset.row = r;
            cell.dataset.col = c;
            cell.dataset.instruction = `cell at row ${r + 1} column ${c + 1}`;

            if (this.revealed[r][c]) {
              cell.classList.add('revealed');
              if (this.board[r][c] === -1) {
                cell.textContent = 'πŸ’£';
                cell.classList.add('mine');
              } else if (this.board[r][c] > 0) {
                cell.textContent = this.board[r][c];
                cell.classList.add(`num-${this.board[r][c]}`);
              }
            } else if (this.flagged[r][c]) {
              cell.textContent = '🚩';
              cell.classList.add('flagged');
            }

            cell.addEventListener('click', () => this.reveal(r, c));
            cell.addEventListener('contextmenu', (e) => {
              e.preventDefault();
              this.toggleFlag(r, c);
            });

            board.appendChild(cell);
          }
        }
      }

      reset() {
        clearInterval(this.timerInterval);
        this.board = [];
        this.revealed = [];
        this.flagged = [];
        this.gameOver = false;
        this.gameWon = false;
        this.flagCount = 0;
        this.revealedCount = 0;
        this.timer = 0;
        this.timerInterval = null;
        this.firstClick = true;

        document.getElementById('timer').textContent = '0';
        document.getElementById('status').textContent = '';
        document.getElementById('reset-btn').textContent = '😊 New Game';

        this.initBoard();
        this.render();

        window.__gameState = 'playing';
        window.__gameWon = false;
        window.__gameLost = false;
      }
    }

    // Initialize game with parameters from task config
    window.initGame = function (rows, cols, mines) {
      window.game = new Minesweeper(rows, cols, mines);
      document.getElementById('reset-btn').addEventListener('click', () => window.game.reset());
    };
  </script>
</div>