フラッド フィルに基づいてアルゴリズムを作成し、ドロップしたばかりのタイルに接触しているすべてのタイルを収集します。プレイを有効にするには、そのうちの 1 つが中央のタイルと交差する必要があります。
アルゴリズムは、ドロップした各タイルから開始し、周囲の各タイルをチェックします。タイルが特定の方向に存在する場合は、それを Set に追加し、この新しいタイルに接触しているタイルごとに同じことを再帰的に行います。タイルが存在する場合は、関数を終了します。再生した文字からすべての方向にタイルがなくなると、再帰は終了します。
func getFilledSquare(c: Coordinate) -> Square? {
return squares
|> { s in filter(s) { $0.c == c && $0.tile != nil } }
|> { s in first(s) }
}
func getAdjacentFilledSquares(c: Coordinate?, vertically v: Bool, horizontally h: Bool, original: Square, inout output: Set<Square>) {
// We may hit the original square several times in different directions, so we allow it through multiple times
if let coord = c, sq = getFilledSquare(coord) where sq == original || !output.contains(sq) {
output.insert(sq)
if h {
getAdjacentFilledSquares(coord.next(.Horizontal, d: 1, b: self), vertically: v, horizontally: h, original: original, output: &output)
getAdjacentFilledSquares(coord.next(.Horizontal, d: -1, b: self), vertically: v, horizontally: h, original: original, output: &output)
}
if v {
getAdjacentFilledSquares(coord.next(.Vertical, d: 1, b: self), vertically: v, horizontally: h, original: original, output: &output)
getAdjacentFilledSquares(coord.next(.Vertical, d: -1, b: self), vertically: v, horizontally: h, original: original, output: &output)
}
}
}
これはオープン ソースであり、メソッドは getAdjacentFilledSquares と呼ばれます (私が知っている少し冗長です)。私のリポジトリはここにあります: https://github.com/ChrisAU/Locution?files=1