示例#1
0
            private bool IsAligned(int y, int x, TransformedTile tile)
            {
                var edges = tile.GetEdges().Select(EdgeToNum).ToArray();

                if (y == 0)
                {
                    if (_matchingEdges[edges[0]].Count != 1)
                    {
                        return(false);
                    }
                }
                else
                {
                    var otherTile = _matchingEdges[edges[0]].SingleOrDefault(t => t != tile.Tile.Id);
                    if (_result[y - 1, x].Tile.Id != otherTile || EdgeToNum(_result[y - 1, x].GetEdge(2)) != edges[0])
                    {
                        return(false);
                    }
                }

                if (x == 0)
                {
                    if (_matchingEdges[edges[3]].Count != 1)
                    {
                        return(false);
                    }
                }
                else
                {
                    var otherTile = _matchingEdges[edges[3]].SingleOrDefault(t => t != tile.Tile.Id);
                    if (_result[y, x - 1].Tile.Id != otherTile || EdgeToNum(_result[y, x - 1].GetEdge(1)) != edges[3])
                    {
                        return(false);
                    }
                }

                if (y + 1 == _result.GetLength(0))
                {
                    if (_matchingEdges[edges[2]].Count != 1)
                    {
                        return(false);
                    }
                }

                if (x + 1 == _result.GetLength(1))
                {
                    if (_matchingEdges[edges[1]].Count != 1)
                    {
                        return(false);
                    }
                }

                return(true);
            }
示例#2
0
            private static IReadOnlyDictionary <ushort, IReadOnlySet <long> > FindMatchingEdges(IReadOnlyDictionary <long, Tile> tiles)
            {
                var potentialMatches = new ConcurrentDictionary <ushort, HashSet <long> >();

                foreach (var(_, tile) in tiles)
                {
                    var transformed = new TransformedTile(tile, Flip.None, Rotate.CW0);
                    foreach (var edge in transformed.GetEdges())
                    {
                        potentialMatches.GetOrAdd(EdgeToNum(edge), _ => new HashSet <long>()).Add(tile.Id);
                        potentialMatches.GetOrAdd(EdgeToNum(edge.Reverse().ToList()), _ => new HashSet <long>()).Add(tile.Id);
                    }
                }

                return(potentialMatches.ToDictionary(x => x.Key, x => (IReadOnlySet <long>)x.Value));
            }