タスク: 2D 配列内の特定の位置について、半径内にある周囲の位置のリストを生成します。
例えば:
input: (1, 1)
radius: 1
output: ( (0, 0), (1, 0), (2, 0),
(0, 1), (2, 1),
(0, 2), (1, 2), (2, 2) ).
のようなものを書きました
def getPositions(x:Int, y:Int, r:Int) = {
for(radius <- 1 to r) yield {
List(
for (dx <- -radius to radius) yield Pair(x + dx, y - radius),
for (dx <- -radius to radius) yield Pair(x + dx, y + radius),
for (dy <- -radius to radius) yield Pair(x + radius, y + dy),
for (dy <- -radius to radius) yield Pair(x - radius, y + dy)
)
}
}
このコードでは、getPositions はポイントのシーケンスではなく、ポイントのシーケンスの Tuple4 のシーケンスを返します。コードにリストされている4つのジェネレーターを「連結」するにはどうすればよいですか? または、私の仕事のためのより簡潔な解決策はありますか? (私はscalaにかなり慣れていません)。
PS 実際には私のスタークラフトボット用です。