[Add] Menu with configs and size fix

This commit is contained in:
2026-06-06 23:48:55 +07:00
parent 7104089c00
commit a9767c5301
29 changed files with 453 additions and 153 deletions
+6 -27
View File
@@ -1,22 +1,17 @@
using System;
using System.Collections.Generic;
using Minesweeper.Config;
namespace Minesweeper.Core
{
public sealed class BoardService : IBoardService
{
private const int DefaultWidth = 9;
private const int DefaultHeight = 9;
private const int DefaultMinesCount = 10;
private readonly MinesweeperGameConfig config;
private readonly IGameSettingsService settingsService;
private readonly Random random = new Random();
private CellData[,] cells;
public BoardService(MinesweeperGameConfig config)
public BoardService(IGameSettingsService settingsService)
{
this.config = config;
this.settingsService = settingsService;
}
public int Width { get; private set; }
@@ -28,11 +23,9 @@ namespace Minesweeper.Core
public void InitializeEmptyBoard()
{
ResolveConfig(out var width, out var height, out var minesCount);
Width = width;
Height = height;
MinesCount = minesCount;
Width = settingsService.SizeX;
Height = settingsService.SizeY;
MinesCount = Math.Min(settingsService.MinesCount, Width * Height - 1);
OpenedSafeCellsCount = 0;
IsGenerated = false;
cells = new CellData[Width, Height];
@@ -147,20 +140,6 @@ namespace Minesweeper.Core
return result;
}
private void ResolveConfig(out int width, out int height, out int minesCount)
{
width = config.Width;
height = config.Height;
minesCount = config.MinesCount;
if (width <= 0 || height <= 0 || minesCount <= 0 || minesCount >= width * height)
{
width = DefaultWidth;
height = DefaultHeight;
minesCount = DefaultMinesCount;
}
}
private void EnsureInitialized()
{
if (cells == null)
@@ -0,0 +1,81 @@
using Minesweeper.Config;
using UnityEngine;
namespace Minesweeper.Core
{
public sealed class GameSettingsService : IGameSettingsService
{
private const int MinimumMinesCount = 1;
private readonly MinesweeperGameConfig config;
private readonly IGameSettingsStorage storage;
private GameSettingsValue current;
public GameSettingsService(MinesweeperGameConfig config, IGameSettingsStorage storage)
{
this.config = config;
this.storage = storage;
current = LoadInitialSettings();
}
public int SizeX => current.SizeX;
public int SizeY => current.SizeY;
public int MinesCount => current.MinesCount;
public GameSettingsValue Current => current;
public GameSettingsValue Clamp(GameSettingsValue value)
{
var sizeX = Mathf.Clamp(value.SizeX, config.MinSizeX, config.MaxSizeX);
var sizeY = Mathf.Clamp(value.SizeY, config.MinSizeY, config.MaxSizeY);
EnsureAtLeastTwoCells(ref sizeX, ref sizeY);
var mines = Mathf.Clamp(value.MinesCount, MinimumMinesCount, GetMaxMines(sizeX, sizeY));
return new GameSettingsValue(sizeX, sizeY, mines);
}
public bool ApplyAndSaveIfChanged(GameSettingsValue value)
{
var clamped = Clamp(value);
if (current.Equals(clamped))
{
return false;
}
current = clamped;
storage.Save(current);
return true;
}
public int GetMaxMines(int sizeX, int sizeY)
{
return Mathf.Max(MinimumMinesCount, sizeX * sizeY - 1);
}
private GameSettingsValue LoadInitialSettings()
{
if (storage.TryLoad(out var saved))
{
return Clamp(saved);
}
return Clamp(new GameSettingsValue(config.MinSizeX, config.MinSizeY, MinimumMinesCount));
}
private void EnsureAtLeastTwoCells(ref int sizeX, ref int sizeY)
{
if (sizeX * sizeY > 1)
{
return;
}
if (sizeX < config.MaxSizeX)
{
sizeX++;
}
else if (sizeY < config.MaxSizeY)
{
sizeY++;
}
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 38b7be3d055b5c34bbba9b36c0e11fc6
+21
View File
@@ -0,0 +1,21 @@
namespace Minesweeper.Core
{
public readonly struct GameSettingsValue
{
public GameSettingsValue(int sizeX, int sizeY, int minesCount)
{
SizeX = sizeX;
SizeY = sizeY;
MinesCount = minesCount;
}
public int SizeX { get; }
public int SizeY { get; }
public int MinesCount { get; }
public bool Equals(GameSettingsValue other)
{
return SizeX == other.SizeX && SizeY == other.SizeY && MinesCount == other.MinesCount;
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5a48645a1753ceb4ba0401ec44d70070
@@ -0,0 +1,14 @@
namespace Minesweeper.Core
{
public interface IGameSettingsService
{
int SizeX { get; }
int SizeY { get; }
int MinesCount { get; }
GameSettingsValue Current { get; }
GameSettingsValue Clamp(GameSettingsValue value);
bool ApplyAndSaveIfChanged(GameSettingsValue value);
int GetMaxMines(int sizeX, int sizeY);
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3dcff3a940f664b4b96722f28e46636d
@@ -0,0 +1,8 @@
namespace Minesweeper.Core
{
public interface IGameSettingsStorage
{
bool TryLoad(out GameSettingsValue value);
void Save(GameSettingsValue value);
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 54d04c5398018bc418e1a2528c559d0a