Hi, ich versuche ein bestimmtes Pattern auf Enum Werte zu matchen, und wollte Fragen ob es schöner geht als wie ich es aktuell löse..
Ein Tile hat 4 Nachbarn und danach richtet sich das eigene Aussehen des Tiles. Die Enum Werte zeigen an welche Seiten verbunden sind (nicht null). Habt ihr eine Idee wie man das schöner lösen kann ohne 16 Ifs zu schreiben? (Gibt ja 16 Möglichkeiten).
C#-Quellcode
- private static readonly Dictionary<string, TileLayout> _layouts;
- /// <summary>
- /// Initializes the TileLayoutPattern class
- /// </summary>
- static TileLayoutPattern()
- {
- _layouts = new Dictionary<string, TileLayout>
- {
- {"0000", TileLayout.Single},
- {"1111", TileLayout.TopRightBottomLeft},
- {"1110", TileLayout.TopRightBottom},
- {"0111", TileLayout.RightBottomLeft},
- {"1011", TileLayout.TopBottomLeft},
- {"1101", TileLayout.TopRightLeft},
- {"0101", TileLayout.RightLeft},
- {"1010", TileLayout.TopBottom},
- {"0110", TileLayout.RightBottom},
- {"0011", TileLayout.BottomLeft},
- {"1100", TileLayout.TopRight},
- {"1001", TileLayout.TopLeft},
- {"0010", TileLayout.Bottom},
- {"1000", TileLayout.Top},
- {"0010", TileLayout.Left},
- {"0100", TileLayout.Right}
- };
- }
- /// <summary>
- /// Gets the tile layout
- /// </summary>
- /// <param name="neighbours">The neighbour tiles</param>
- /// <returns>Returns the tile layout</returns>
- public static TileLayout GetTileLayout(Tile[] neighbours)
- {
- var sb = new StringBuilder();
- for (int i = 0; i < 4; i++)
- {
- sb.Append(neighbours[i] != null ? "1" : "0");
- }
- return _layouts[sb.ToString()];
- }
Ein Tile hat 4 Nachbarn und danach richtet sich das eigene Aussehen des Tiles. Die Enum Werte zeigen an welche Seiten verbunden sind (nicht null). Habt ihr eine Idee wie man das schöner lösen kann ohne 16 Ifs zu schreiben? (Gibt ja 16 Möglichkeiten).