package fr.upem.sortgame;

import java.util.List;

/**
 * Compute score for sortgame
 *
 * @author chilowi at u-pem.fr
 */
public class ScoreComputer {
	public static int maxScore(int itemNumber) {
		return itemNumber * (itemNumber - 1) / 2; // number of pairs
	}

	public static <T> int computeKendallTauDistance(List<T> playerProposition, List<T> sortedList) {
		int d = 0;
		int[] positions = new int[playerProposition.size()];
		for (int i = 0; i < positions.length; i++)
			positions[i] = sortedList.indexOf(playerProposition.get(i)); // assuming that the item is present in sortedList
		for (int i = 0; i < positions.length; i++)
			for (int j = i + 1; j < positions.length; j++)
				if (positions[i] > positions[j]) d++; // bad order for the couple
		return d;
	}

	public static <T> int computeScore(List<T> playerProposition, List<T> sortedList) {
		return maxScore(playerProposition.size()) - computeKendallTauDistance(playerProposition, sortedList);
	}
}