public static List <TileData> PlaceTilesOnCart(List <Tile> tiles, int cartWidth) { int tilesTotalSquare = tiles.Aggregate(0, (square, tile) => square + tile.Square); const int maxCartRows = 20; int cartMinimumRows = tilesTotalSquare % cartWidth > 0 ? (int)Math.Ceiling((double)tilesTotalSquare / cartWidth) : tilesTotalSquare / cartWidth; bool areTilesArrangedOnGrid; int cartRows = cartMinimumRows; List <TileData> arrangedTiles; do { Grid cartGrid = new Grid(cartWidth, cartRows); List <TileData> gridTiles = tiles.ConvertAll(tile => new TileData { Tile = tile, AvailablePoints = cartGrid.GetAvailablePoints(tile), }); areTilesArrangedOnGrid = ArrangeTilesToFitGrid(cartGrid, gridTiles); arrangedTiles = gridTiles; } while (!areTilesArrangedOnGrid && cartRows++ < maxCartRows); return(arrangedTiles); }
private static bool ArrangeTilesToFitGrid(Grid grid, List <TileData> gridTiles) { foreach (TileData tile in gridTiles) { foreach (GridPoint point in tile.AvailablePoints) { if (grid.CanTileBePlaced(tile.Tile, point)) { grid.PlaceTile(tile.Tile, point); tile.SetPlacedPoint(point); break; } } if (!tile.IsPlaced) { return(false); } } return(true); }
private GridTilingService(Grid grid, DifficultyEnum difficulty) { _grid = grid.Clone(); _difficulty = difficulty; _tiles = AvailableTileTypes.ConvertAll(tile => new TileData { Tile = tile, AvailablePoints = grid.GetAvailablePoints(tile) }); int hardMinimumFreePoints = GetHardMinimumFreePoints(); int normalMinimumFreePointsByPercentage = (int)Math.Round((double)(100 * NormalDifficultyPointsPercentage) / _grid.FreePointsCount); _freePointsByDifficulty = new Difficulties { { DifficultyEnum.Hard, hardMinimumFreePoints }, { DifficultyEnum.Normal, Math.Max(hardMinimumFreePoints + 2, normalMinimumFreePointsByPercentage) } }; Log($"Difficulties: hard - {_freePointsByDifficulty[DifficultyEnum.Hard]}, normal - {_freePointsByDifficulty[DifficultyEnum.Normal]}"); }
public static List <Tile> GetTilesForGrid(Grid grid, DifficultyEnum difficulty) { return((new GridTilingService(grid.Clone(), difficulty)).GetTiles()); }