private static ColorWeight GetMaxColorWeight(ColorSpot moveSpot) { int maxColorIndex = LINQExstensions.MaxIndex(moveSpot.ColorsWeights, x => x.TileNumber); ColorWeight maxColorWeight = moveSpot.ColorsWeights[maxColorIndex]; return(maxColorWeight); }
private static void CleanupEmptyColorWeights(ColorSpot moveSpot) { List <ColorWeight> emptyColorWeights = moveSpot.ColorsWeights.Where(x => x.NeighborsSpotsIds.Count == 0).ToList(); foreach (var emptyColorWeight in emptyColorWeights) { moveSpot.ColorsWeights.Remove(emptyColorWeight); } }
public static byte?ChangeColor(List <ColorSpot> colorSpots) { byte? newColor; ColorSpot moveSpot = Program.StartSpot; ColorWeight maxColorWeight = GetMaxColorWeight(moveSpot); newColor = moveSpot.Color = maxColorWeight.Color; MergeWithMaxColorWeightNeigbors(colorSpots, moveSpot, maxColorWeight); moveSpot.SpotTiles.ForEach(x => x.Color = moveSpot.Color); return(newColor); }
private static void TilesNumbersRecalculate(List <ColorSpot> colorSpots, ColorSpot moveSpot) { foreach (ColorWeight weight in moveSpot.ColorsWeights) { weight.NeighborsSpotsIds = weight.NeighborsSpotsIds.Except(Program.MergedIds).ToList(); int tilesNumber = 0; foreach (int id in weight.NeighborsSpotsIds) { tilesNumber += colorSpots.First(x => x.Id == id).SpotTiles.Count; } weight.TileNumber = tilesNumber; } }
private static List <ColorSpot> SpotGroupsToSpots(List <IGrouping <byte, Tile> > colorSpotsGroups) { List <ColorSpot> spots = new List <ColorSpot>(); for (int i = 0; i < colorSpotsGroups.Count; i++) { IGrouping <byte, Tile> group = colorSpotsGroups[i]; ColorSpot spot = new ColorSpot { Color = group.Key, Id = i, SpotTiles = group.ToList() }; CalculateColorWeights(colorSpotsGroups, i, spot); spots.Add(spot); } return(spots); }
private static void MergeColorWeights(ColorSpot moveSpot, ColorSpot neighbor) { foreach (ColorWeight colorWeight in neighbor.ColorsWeights) { if (moveSpot.ColorsWeights.Any(x => x.Color == colorWeight.Color)) { var moveSpotSameColorWeight = moveSpot.ColorsWeights.First(x => x.Color == colorWeight.Color); MergeColorWeightData(colorWeight, moveSpotSameColorWeight, moveSpot.Id); } else { ColorWeight newColorWeight = new ColorWeight() { Color = colorWeight.Color }; MergeColorWeightData(colorWeight, newColorWeight, moveSpot.Id); moveSpot.ColorsWeights.Add(colorWeight); } } }
private static void MergeWithMaxColorWeightNeigbors(List <ColorSpot> colorSpots, ColorSpot moveSpot, ColorWeight maxColorWeight) { List <ColorSpot> maxColorNeighbors = new List <ColorSpot>(); Program.MergedIds.AddRange(maxColorWeight.NeighborsSpotsIds); foreach (int id in maxColorWeight.NeighborsSpotsIds) { var colorSpot = colorSpots.First(x => x.Id == id); maxColorNeighbors.Add(colorSpot); colorSpots.Remove(colorSpot); } foreach (ColorSpot neighbor in maxColorNeighbors) { MergeColorWeights(moveSpot, neighbor); moveSpot.SpotTiles.AddRange(neighbor.SpotTiles); moveSpot.ColorsWeights.First(x => x.Color == maxColorWeight.Color).NeighborsSpotsIds.Remove(neighbor.Id); } CleanupEmptyColorWeights(moveSpot); TilesNumbersRecalculate(colorSpots, moveSpot); }
private static int BiggestSpotConditions(ColorSpot x) { return(x.SpotTiles.Count + x.ColorsWeights.Max(y => y.TileNumber)); }
private static void CalculateColorWeights(List <IGrouping <byte, Tile> > colorSpotsGroups, int spotIndex, ColorSpot spot) { List <IGrouping <byte, Tile> > neighbors = new List <IGrouping <byte, Tile> >(); var otherSpots = colorSpotsGroups.Where((v, j) => j != spotIndex).ToList(); foreach (Tile tile in spot.SpotTiles) { List <IGrouping <byte, Tile> > tilNeighbours = GetNeighborSpots(tile, otherSpots); neighbors.AddRange(tilNeighbours); } neighbors = neighbors.Distinct().ToList(); var neighborsGroups = neighbors.GroupBy(x => x.Key).ToList(); foreach (var color in neighborsGroups) { GetColorWeight(colorSpotsGroups, spot, color); } }
private static void GetColorWeight(List <IGrouping <byte, Tile> > colorSpotsGroups, ColorSpot spot, IGrouping <byte, IGrouping <byte, Tile> > color) { ColorWeight colorWeight = new ColorWeight { Color = color.Key }; var colorNeighbours = color.ToList(); foreach (IGrouping <byte, Tile> neighbour in colorNeighbours) { colorWeight.NeighborsSpotsIds.Add(colorSpotsGroups.IndexOf(neighbour)); colorWeight.TileNumber += neighbour.Count(); } spot.ColorsWeights.Add(colorWeight); }