Files
FreewayGamesTest/Assets/Runtime/Presentation/ReadModels/GameReadModel.cs
T
2026-06-06 22:33:15 +07:00

53 lines
1.7 KiB
C#

using System.Collections.Generic;
using Minesweeper.Config;
using Minesweeper.Core;
namespace Minesweeper.Presentation.ReadModels
{
public sealed class GameReadModel : IGameReadModel
{
private readonly MinesweeperGameConfig config;
private readonly IBoardService boardService;
private readonly IGameStateService gameStateService;
public GameReadModel(MinesweeperGameConfig config, IBoardService boardService, IGameStateService gameStateService)
{
this.config = config;
this.boardService = boardService;
this.gameStateService = gameStateService;
}
public GameState State => gameStateService.Current;
public int Width => boardService.Width > 0 ? boardService.Width : config.Width;
public int Height => boardService.Height > 0 ? boardService.Height : config.Height;
public int MinesCount => boardService.MinesCount > 0 ? boardService.MinesCount : config.MinesCount;
public int FlaggedCellsCount => CountFlaggedCells();
public int RemainingMinesCount => MinesCount - FlaggedCellsCount;
public bool TryGetCell(int x, int y, out BoardCellData cell)
{
return boardService.TryGetCell(x, y, out cell);
}
public IReadOnlyList<BoardCellData> GetCells()
{
return boardService.GetCells();
}
private int CountFlaggedCells()
{
var cells = boardService.GetCells();
var count = 0;
for (var i = 0; i < cells.Count; i++)
{
if (cells[i].IsFlagged)
{
count++;
}
}
return count;
}
}
}