package com.srs.tetris.bob;

import com.srs.tetris.bob.evaluator.PositionEvaluator;
import com.srs.tetris.game.BitBoard;
import com.srs.tetris.game.Piece;
import java.util.List;

public class MoveSelector {

	private MoveEnumerator moveEnumerator;
	private PositionEvaluator evaluator;

	public MoveSelector(MoveEnumerator moveEnumerator, PositionEvaluator evaluator) {
		this.moveEnumerator = moveEnumerator;
		this.evaluator = evaluator;
	}

	/**
	 * Selects the best move to make in the given position.
	 *
	 * @param position the position for which to find a move.
	 * @param depth the maximum depth to search for next pieces.
	 *
	 * @return the best move.
	 */
	public Move getMove(Position position, int depth) {
		// Enumerate all the current moves.
		List<Move> moves = moveEnumerator.findPossibleMoves(position);

		Move best = null;
		for (Move move : moves) {
			// Calculate the position after this move.
			Position after = position.doMove(move);

			if (depth == 1 || after.getPiece() == null) {
				// This is the bottom of the move tree, so evaluate the position.
				move.setScore(evaluator.evaluate(after));

			} else {
				// We can still go deeper, so find the best move from this position.
				int nextDepth = move.isSwap() ? depth : depth - 1;
				Move nextMove = getMove(after, nextDepth);
				move.setScore(nextMove.getScore());
			}

			// If this move is the best so far, remember it.
			if (best == null || move.getScore().getScore() > best.getScore().getScore()) {
				best = move;
			}
		}

		return best;
	}
}

@@ -77,16 +77,65 @@
 luator;%0A
+import com.srs.tetris.bob.evaluator.ScalarScore;%0A
 import c
@@ -1361,47 +1361,292 @@
 h);%0A
-%09%09%09%09move.setScore(nextMove.getScore());
+%0A%09%09%09%09if (nextMove != null) %7B%0A%09%09%09%09%09// Pass the next move's best score onto this move.%0A%09%09%09%09%09move.setScore(nextMove.getScore());%0A%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09// There were no possible moves from this position, so it's game over!%0A%09%09%09%09%09move.setScore(new ScalarScore(Double.NEGATIVE_INFINITY));%0A%09%09%09%09%7D
 %0A%09%09%09

Handle positions at end game when there are no valid moves