/
ValidMoves.scala
43 lines (36 loc) · 1.26 KB
/
ValidMoves.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.github.tetrisanalyzer.move
import com.github.tetrisanalyzer.piecemove.PieceMove
import com.github.tetrisanalyzer.board.Board
object ValidMoves {
def apply(board: Board) = {
new ValidMoves(board)
}
}
class ValidMoves(board: Board) {
private var validMoves = List.empty[PieceMove]
private val visitedMoves = Array.fill(board.height, board.width) { 0 }
private def markAsVisited(move: Move) { visitedMoves(move.y)(move.x) |= 1 << move.rotation }
private def isUnvisited(move: Move): Boolean = {
try {
(visitedMoves(move.y)(move.x) & (1 << move.rotation)) == 0
} catch {
case e: IndexOutOfBoundsException => false
}
}
/**
* The incoming pieceMove is at the starting position of the board, and
* links to all possible moves on an empty board. Because the board is probably not empty,
* every possible move needs to be checked (and returned by this method).
*/
def pieceMoves(pieceMove: PieceMove): List[PieceMove] = {
if (isUnvisited(pieceMove.move)) {
markAsVisited(pieceMove.move)
pieceMove.freeAsideAndRotateMoves.foreach(move => pieceMoves(move))
if (pieceMove.canMoveDown)
pieceMoves(pieceMove.down)
else
validMoves = pieceMove :: validMoves
}
validMoves
}
}