[Fix] ECS

This commit is contained in:
2026-06-07 01:12:10 +07:00
parent 285c11597a
commit 5a58c9031a
16 changed files with 225 additions and 36 deletions
+5 -1
View File
@@ -1,19 +1,23 @@
using System.Collections.Generic;
namespace Minesweeper.Core
{
public readonly struct BoardActionResult
{
public BoardActionResult(bool changed, bool hitMine, bool won, bool invalid)
public BoardActionResult(bool changed, bool hitMine, bool won, bool invalid, IReadOnlyList<BoardCellData> changedCells = null)
{
Changed = changed;
HitMine = hitMine;
Won = won;
Invalid = invalid;
ChangedCells = changedCells;
}
public bool Changed { get; }
public bool HitMine { get; }
public bool Won { get; }
public bool Invalid { get; }
public IReadOnlyList<BoardCellData> ChangedCells { get; }
public static BoardActionResult NoChange => new BoardActionResult(false, false, false, false);
public static BoardActionResult InvalidAction => new BoardActionResult(false, false, false, true);
+29 -4
View File
@@ -7,6 +7,7 @@ namespace Minesweeper.Core
{
private readonly IGameSettingsService settingsService;
private readonly Random random = new Random();
private readonly List<BoardCellData> changedCells = new List<BoardCellData>();
private CellData[,] cells;
public BoardService(IGameSettingsService settingsService)
@@ -19,7 +20,9 @@ namespace Minesweeper.Core
public int MinesCount { get; private set; }
public bool IsGenerated { get; private set; }
public int OpenedSafeCellsCount { get; private set; }
public int FlaggedCellsCount { get; private set; }
public int SafeCellsCount => Width * Height - MinesCount;
public IReadOnlyList<BoardCellData> LastChangedCells => changedCells;
public void InitializeEmptyBoard()
{
@@ -27,7 +30,9 @@ namespace Minesweeper.Core
Height = settingsService.SizeY;
MinesCount = Math.Min(settingsService.MinesCount, Width * Height - 1);
OpenedSafeCellsCount = 0;
FlaggedCellsCount = 0;
IsGenerated = false;
changedCells.Clear();
cells = new CellData[Width, Height];
for (var x = 0; x < Width; x++)
@@ -58,6 +63,7 @@ namespace Minesweeper.Core
public BoardActionResult OpenCell(int x, int y)
{
EnsureInitialized();
changedCells.Clear();
if (!IsGenerated || !IsInside(x, y))
{
@@ -73,7 +79,8 @@ namespace Minesweeper.Core
if (cell.IsMine)
{
cell.IsOpened = true;
return new BoardActionResult(true, true, false, false);
AddChangedCell(cell);
return new BoardActionResult(true, true, false, false, changedCells);
}
if (cell.NeighborMines == 0)
@@ -85,12 +92,13 @@ namespace Minesweeper.Core
OpenSafeCell(cell);
}
return new BoardActionResult(true, false, IsWin(), false);
return new BoardActionResult(true, false, IsWin(), false, changedCells);
}
public BoardActionResult ToggleFlag(int x, int y)
{
EnsureInitialized();
changedCells.Clear();
if (!IsInside(x, y))
{
@@ -103,8 +111,19 @@ namespace Minesweeper.Core
return BoardActionResult.NoChange;
}
cell.IsFlagged = !cell.IsFlagged;
return new BoardActionResult(true, false, false, false);
if (cell.IsFlagged)
{
cell.IsFlagged = false;
FlaggedCellsCount--;
}
else
{
cell.IsFlagged = true;
FlaggedCellsCount++;
}
AddChangedCell(cell);
return new BoardActionResult(true, false, false, false, changedCells);
}
public bool IsInside(int x, int y)
@@ -273,6 +292,12 @@ namespace Minesweeper.Core
cell.IsOpened = true;
OpenedSafeCellsCount++;
AddChangedCell(cell);
}
private void AddChangedCell(CellData cell)
{
changedCells.Add(ToData(cell));
}
private bool IsWin()
+2
View File
@@ -9,7 +9,9 @@ namespace Minesweeper.Core
int MinesCount { get; }
bool IsGenerated { get; }
int OpenedSafeCellsCount { get; }
int FlaggedCellsCount { get; }
int SafeCellsCount { get; }
IReadOnlyList<BoardCellData> LastChangedCells { get; }
void InitializeEmptyBoard();
bool GenerateAfterFirstClick(int safeX, int safeY);