cleanup code

This commit is contained in:
2026-05-08 20:47:09 +02:00
parent 07d35a49a3
commit 2e813962c9
9 changed files with 387 additions and 355 deletions

View File

@@ -34,15 +34,29 @@ public sealed record GridPosition(int X, int Y)
{
public IEnumerable<GridPosition> Neighbors()
{
yield return new GridPosition(X - 1, Y);
yield return new GridPosition(X + 1, Y);
yield return new GridPosition(X, Y - 1);
yield return new GridPosition(X, Y + 1);
yield return new(X - 1, Y);
yield return new(X + 1, Y);
yield return new(X, Y - 1);
yield return new(X, Y + 1);
}
}
public sealed record HazardState
{
public HazardState Clamp()
{
return this with
{
Heat = Rules.Clamp(Heat),
Smoke = Rules.Clamp(Smoke),
FuelVapor = Rules.Clamp(FuelVapor),
LiquidFuel = Rules.Clamp(LiquidFuel),
CoolantPooling = Rules.Clamp(CoolantPooling),
ElectricalCharge = Rules.Clamp(ElectricalCharge),
Stability = Rules.Clamp(Stability)
};
}
public int Heat { get; init; }
public int Smoke { get; init; }
public int FuelVapor { get; init; }
@@ -51,17 +65,6 @@ public sealed record HazardState
public int ElectricalCharge { get; init; }
public int Stability { get; init; } = 10;
public bool Fire { get; init; }
public HazardState Clamp() => this with
{
Heat = Rules.Clamp(Heat),
Smoke = Rules.Clamp(Smoke),
FuelVapor = Rules.Clamp(FuelVapor),
LiquidFuel = Rules.Clamp(LiquidFuel),
CoolantPooling = Rules.Clamp(CoolantPooling),
ElectricalCharge = Rules.Clamp(ElectricalCharge),
Stability = Rules.Clamp(Stability)
};
}
public sealed record CellState
@@ -98,40 +101,24 @@ public sealed record Forecast(FailureKind Kind, GridPosition? Position, int Turn
public sealed record LevelState
{
public string Name { get; init; } = "New Reactor";
public int Width { get; init; } = 16;
public int Height { get; init; } = 12;
public CellState[] Cells { get; init; } = CreateCells(16, 12);
public GridPosition Robot { get; init; } = new(1, 1);
public GlobalState Global { get; init; } = new();
public IReadOnlyList<Forecast> Forecasts { get; init; } = Array.Empty<Forecast>();
public static LevelState Create(string name, int width, int height)
{
if (width < 4 || height < 4)
{
throw new ArgumentOutOfRangeException(nameof(width), "Levels must be at least 4x4.");
}
var cells = CreateCells(width, height);
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
if (x == 0 || y == 0 || x == width - 1 || y == height - 1)
{
cells[y * width + x] = cells[y * width + x] with { Kind = CellKind.Wall };
}
}
}
for (var x = 0; x < width; x++)
if (x == 0 || y == 0 || x == width - 1 || y == height - 1)
cells[y * width + x] = cells[y * width + x] with { Kind = CellKind.Wall };
return new LevelState
return new()
{
Name = name,
Width = width,
Height = height,
Cells = cells,
Robot = new GridPosition(1, 1)
Robot = new(1, 1)
};
}
@@ -149,26 +136,40 @@ public sealed record LevelState
return this with { Cells = cells };
}
public bool InBounds(GridPosition position) =>
position.X >= 0 && position.Y >= 0 && position.X < Width && position.Y < Height;
public bool InBounds(GridPosition position)
{
return position.X >= 0 && position.Y >= 0 && position.X < Width && position.Y < Height;
}
public int Index(GridPosition position) => position.Y * Width + position.X;
public int Index(GridPosition position)
{
return position.Y * Width + position.X;
}
private void EnsureInBounds(GridPosition position)
{
if (!InBounds(position))
{
throw new ArgumentOutOfRangeException(nameof(position), $"Position {position.X},{position.Y} is outside {Width}x{Height}.");
}
}
private static CellState[] CreateCells(int width, int height) =>
Enumerable.Range(0, width * height)
.Select(_ => new CellState())
.ToArray();
private static CellState[] CreateCells(int width, int height)
{
return Enumerable.Range(0, width * height).Select(_ => new CellState()).ToArray();
}
public string Name { get; init; } = "New Reactor";
public int Width { get; init; } = 16;
public int Height { get; init; } = 12;
public CellState[] Cells { get; init; } = CreateCells(16, 12);
public GridPosition Robot { get; init; } = new(1, 1);
public GlobalState Global { get; init; } = new();
public IReadOnlyList<Forecast> Forecasts { get; init; } = Array.Empty<Forecast>();
}
internal static class Rules
{
public static int Clamp(int value) => Math.Clamp(value, 0, 10);
}
public static int Clamp(int value)
{
return Math.Clamp(value, 0, 10);
}
}