package player.gamer.statemachine.sadna;

/**
 * Calculate board score
 */
public class ScoreCalculator {

	private WeightUpdater updater;
	
	public ScoreCalculator(WeightUpdater updater) {
		this.updater = updater;
	}
	
	/**
	 * Score is inner product of board attributes vector and weights vector, or infinity if player won, or negative
	 * infinity if opponent won 
	 * @return The score
	 */
	public double calculateScore(Board board) {
		double score = 0;
		
		int[] atrributes = board.calclateAttributeVector();
		double[] weights = updater.getWeights();
		
		// player won
		if (atrributes[Board.FOUR_COINS_INDEX] > 0 || atrributes[Board.IMMEDIATE_THREAT] > 0) {
			return Double.POSITIVE_INFINITY;
		}
		// opponent won
		if (atrributes[Board.FOUR_COINS_INDEX+1] > 0 || atrributes[Board.IMMEDIATE_THREAT+1] > 0) {
			return Double.NEGATIVE_INFINITY;
		}
		
		// inner product
		for (int i = 0; i < weights.length; i++) {
			score += (weights[i] * atrributes[i]);
		}

		return score;
	}
}
