You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

扫雷游戏空白格子可展开但数字格子无法展开问题求助

Fixing Numbered Cells Not Revealing in Minesweeper's revealCells Method

Hey Julio, I see exactly what's going on here—your current code only handles revealing blank cells (mineCount = 0) and completely skips over numbered cells. Let's break this down and fix it:

The Root Problem

Looking at your conditional logic:

else if(cell.getMineCount() == 0 && !(cell.isRevealed())){
    cell.reveal();
    // recursive calls here
}

This code only triggers when the cell is blank and not revealed. Numbered cells (where mineCount > 0) never get their reveal() method called, which is why they stay hidden when clicked.

The Fixed Code

Here's how to adjust your method to handle both blank and numbered cells correctly:

public void revealCells(int row, int column) {
    // First, check if we're out of bounds—exit immediately if so
    if (row < 0 || row >= cells.length || column < 0 || column >= cells[0].length) {
        return;
    }
    
    Cell cell = cells[row][column];
    
    // Exit if it's a mine or already revealed (prevents redundant work/loops)
    if (cell instanceof MineCell || cell.isRevealed()) {
        return;
    }
    
    // Reveal the current cell—this works for both numbered and blank cells!
    cell.reveal();
    
    // Only recursively reveal neighbors if this is a blank cell (mineCount = 0)
    if (cell.getMineCount() == 0) {
        revealCells(row-1, column-1);
        revealCells(row-1, column);
        revealCells(row-1, column+1);
        revealCells(row, column-1);
        revealCells(row, column+1);
        revealCells(row+1, column-1);
        revealCells(row+1, column);
        revealCells(row+1, column+1);
    }
}

Key Changes Explained

  1. Early Exit for Already Revealed Cells: Added a check for cell.isRevealed() to avoid re-processing cells we've already shown.
  2. Unified Reveal Step: Moved cell.reveal() outside the conditional—now every valid non-mine cell gets revealed immediately when this method runs on it.
  3. Recursion Only for Blanks: We still only recurse to neighboring cells if the current cell is blank (since numbered cells shouldn't trigger a chain reveal).

This way, when you click a numbered cell, it will reveal itself, and when you click a blank cell, it will reveal itself and all connected blank/numbered neighbors just like standard Minesweeper.

内容的提问来源于stack exchange,提问作者Julio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:52:59