## 2日目

``````[info]   Moving to the left the current piece should
[info]     + change the blocks in the view
[info]     x as long as it doesn't hit the wall.
[error]  List((0,0), (-1,17), (0,17), (1,17), (0,18)) does not contain (2,17), (1,18) and must not contain '(-1,17)' is equal to '(0,17)', '(0,18)' is equal to '(2,17)' in order (StageSpec.scala:22)
``````

### 検証

まず現行の `moveBy` の実装をみてみよう:

``````  private[this] def moveBy(delta: (Double, Double)): this.type = {
val moved = currentPiece.moveBy(delta)
currentPiece = moved
this
}
``````

`moved` を検証して `moved.current`内の全てのブロックが範囲内に収まってるかをチェックするだけでいい。Scala コレクションライブラリ にある `forall` メソッドが正にこの用途にあっている。Scala 逆引きレシピだと、「118: List の要素が条件を満たすか調べたい」が参考になる。`if` 文をループさせるようなことはここでは必要ない:

``````  private[this] def moveBy(delta: (Double, Double)): this.type = {
validate(
currentPiece.moveBy(delta),
currentPiece = moved
}
this
}
private[this] def validate(p: Piece, bs: Seq[Block]): Option[(Piece, Seq[Block])] =
if (p.current map {_.pos} forall inBounds) Some(p, bs)
else None
private[this] def inBounds(pos: (Int, Int)): Boolean =
(pos._1 >= 0) && (pos._1 < size._1) && (pos._2 >= 0) && (pos._2 < size._2)
``````

これでテストはパスするはずだ:

``````[info] Moving to the left the current piece should
[info] + change the blocks in the view,
[info] + as long as it doesn't hit the wall
``````