Skip to content

Commit

Permalink
[#14]: small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
3k-dome committed May 9, 2023
1 parent c115bb6 commit fc86ce0
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions catan-lib/Helpers/ProductionPhase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public static IEnumerable<ITerrainTile> GetProducingTiles(Dictionary<TileCoordin
.Where(tile => tile.Production.Roll == roll);
}

public static IEnumerable<ISettlement> GetSettlements(Dictionary<VertexCoordinate, ISettlement> vertexStore, ITerrainTile tile)
public static IEnumerable<ISettlement> GetPlacedSettlements(Dictionary<VertexCoordinate, ISettlement> vertexStore, ITerrainTile tile)
{
return tile.Coordinate.Vertices()
.Select(vertexCoordinate => vertexStore[vertexCoordinate])
.Where(settlement => settlement.IsSettlement || settlement.IsCity);
.Where(settlement => settlement.Belongs is not null);
}

public static IEnumerable<ResourceType> GetTotalYield(Dictionary<VertexCoordinate, ISettlement> vertexStore, IEnumerable<ITerrainTile> tiles)
Expand All @@ -28,7 +28,7 @@ public static IEnumerable<ResourceType> GetTotalYield(Dictionary<VertexCoordinat
foreach (ITerrainTile tile in tiles)
{
ResourceType resource = TerrainResources.Resources[tile.Terrain];
IEnumerable<ISettlement> settlements = GetSettlements(vertexStore, tile);
IEnumerable<ISettlement> settlements = GetPlacedSettlements(vertexStore, tile);
IEnumerable<ResourceType> tileProduction = settlements
.SelectMany(settlement => settlement.IsSettlement ? new[] { resource } : new[] { resource, resource });

Expand All @@ -43,7 +43,7 @@ public static void DistributeResources(Dictionary<VertexCoordinate, ISettlement>
foreach (ITerrainTile tile in tiles)
{
ResourceType resource = TerrainResources.Resources[tile.Terrain];
IEnumerable<ISettlement> settlements = GetSettlements(vertexStore, tile);
IEnumerable<ISettlement> settlements = GetPlacedSettlements(vertexStore, tile);

foreach (ISettlement settlement in settlements)
{
Expand Down Expand Up @@ -76,13 +76,11 @@ public static void GainResources(IBank bank, ISettlement settlement, IEnumerable

public static int GetPlayerCount(Dictionary<VertexCoordinate, ISettlement> vertexStore, IEnumerable<ITerrainTile> tiles)
{
return tiles.SelectMany(tile => tile.Coordinate.Vertices())
.Select(vertexCoordinate => vertexStore[vertexCoordinate])
.Where(settlement => settlement.IsSettlement || settlement.IsCity)
return tiles.SelectMany(tile => GetPlacedSettlements(vertexStore, tile))
.Select(settlement =>
{
Debug.Assert(settlement.Belongs != null);
return settlement.Belongs;
Debug.Assert(settlement.Belongs is not null);
return settlement.Belongs.Number;
})
.Distinct()
.Count();
Expand All @@ -93,7 +91,7 @@ public static void TryDistributeResources(Dictionary<VertexCoordinate, ISettleme
foreach (ITerrainTile tile in tiles)
{
ResourceType resource = TerrainResources.Resources[tile.Terrain];
IEnumerable<ISettlement> settlements = GetSettlements(vertexStore, tile);
IEnumerable<ISettlement> settlements = GetPlacedSettlements(vertexStore, tile);

foreach (ISettlement settlement in settlements)
{
Expand Down

0 comments on commit fc86ce0

Please sign in to comment.