[Add] Playful interaction with cells

This commit is contained in:
2026-06-06 21:34:35 +07:00
parent 1a6f8901a2
commit 1483964eaf
5 changed files with 195 additions and 5 deletions
+112 -1
View File
@@ -23,6 +23,8 @@ namespace Minesweeper.Core
public int Height { get; private set; }
public int MinesCount { get; private set; }
public bool IsGenerated { get; private set; }
public int OpenedSafeCellsCount { get; private set; }
public int SafeCellsCount => Width * Height - MinesCount;
public void InitializeEmptyBoard()
{
@@ -31,6 +33,7 @@ namespace Minesweeper.Core
Width = width;
Height = height;
MinesCount = minesCount;
OpenedSafeCellsCount = 0;
IsGenerated = false;
cells = new CellData[Width, Height];
@@ -55,11 +58,62 @@ namespace Minesweeper.Core
PlaceMines(safeX, safeY);
CalculateNeighborMines();
cells[safeX, safeY].IsOpened = true;
IsGenerated = true;
return true;
}
public BoardActionResult OpenCell(int x, int y)
{
EnsureInitialized();
if (!IsGenerated || !IsInside(x, y))
{
return BoardActionResult.InvalidAction;
}
var cell = cells[x, y];
if (cell.IsOpened || cell.IsFlagged)
{
return BoardActionResult.NoChange;
}
if (cell.IsMine)
{
cell.IsOpened = true;
return new BoardActionResult(true, true, false, false);
}
if (cell.NeighborMines == 0)
{
RevealEmptyArea(x, y);
}
else
{
OpenSafeCell(cell);
}
return new BoardActionResult(true, false, IsWin(), false);
}
public BoardActionResult ToggleFlag(int x, int y)
{
EnsureInitialized();
if (!IsInside(x, y))
{
return BoardActionResult.InvalidAction;
}
var cell = cells[x, y];
if (cell.IsOpened)
{
return BoardActionResult.NoChange;
}
cell.IsFlagged = !cell.IsFlagged;
return new BoardActionResult(true, false, false, false);
}
public bool IsInside(int x, int y)
{
return cells != null && x >= 0 && y >= 0 && x < Width && y < Height;
@@ -190,6 +244,63 @@ namespace Minesweeper.Core
return count;
}
private void RevealEmptyArea(int startX, int startY)
{
var visited = new bool[Width, Height];
var queue = new Queue<CellData>();
queue.Enqueue(cells[startX, startY]);
while (queue.Count > 0)
{
var cell = queue.Dequeue();
if (visited[cell.X, cell.Y] || cell.IsMine || cell.IsFlagged)
{
continue;
}
visited[cell.X, cell.Y] = true;
OpenSafeCell(cell);
if (cell.NeighborMines != 0)
{
continue;
}
for (var x = cell.X - 1; x <= cell.X + 1; x++)
{
for (var y = cell.Y - 1; y <= cell.Y + 1; y++)
{
if ((x == cell.X && y == cell.Y) || !IsInside(x, y))
{
continue;
}
var neighbor = cells[x, y];
if (!visited[x, y] && !neighbor.IsMine && !neighbor.IsFlagged && !neighbor.IsOpened)
{
queue.Enqueue(neighbor);
}
}
}
}
}
private void OpenSafeCell(CellData cell)
{
if (cell.IsOpened || cell.IsMine)
{
return;
}
cell.IsOpened = true;
OpenedSafeCellsCount++;
}
private bool IsWin()
{
return OpenedSafeCellsCount >= SafeCellsCount;
}
private int ToIndex(int x, int y)
{
return y * Width + x;