package pdp.scrabble.game.impl;
import pdp.scrabble.game.Bag;
import pdp.scrabble.game.Board;
import pdp.scrabble.game.Letter;
import pdp.scrabble.game.LettersProbability;
import pdp.scrabble.game.Player;
import pdp.scrabble.game.Rack;
import pdp.scrabble.utility.Tool;
public class LettersProbabilityImpl implements LettersProbability {
/** Original number for each letters. */
private int[] originals = null;
/** List containing probability result. */
private float[] results = null;
/** Contains number of letter uses for each of them. */
private int[] uses = null;
/** Number of different letters. */
private int numberOfDifferentLetter = 0;
/** Create a new letter probability analysis.
* @param bag bag reference.
*/
public LettersProbabilityImpl(Bag bag) {
this.numberOfDifferentLetter = Bag.GOD_LETTERS.length;
this.originals = new int[this.numberOfDifferentLetter];
this.results = new float[this.numberOfDifferentLetter];
this.uses = new int[this.numberOfDifferentLetter];
for (int i = 0; i < this.numberOfDifferentLetter; i++) {
this.originals[i] = bag.getOriginalNumberOf(Tool.getCharacter(i));
}
}
@Override
public void start(Board board, Player player) {
// Initialise uses occurence
for (int i = 0; i < this.numberOfDifferentLetter; i++) {
this.uses[i] = 0;
}
// Calculate new value considering known used letters
Letter letter = null;
for (int v = 0; v < Board.VERT_DIM; v++) {
for (int h = 0; h < Board.HORI_DIM; h++) {
letter = board.getCase(v, h).getLetter();
if (letter != null) {
this.uses[Tool.getNumericValue(letter.getName())]++;
}
}
}
for (int i = 0; i < Rack.MAX_RACK_LETTERS; i++) {
letter = player.getRack().getLetter(i);
if (letter != null) {
this.uses[Tool.getNumericValue(letter.getName())]++;
}
}
// Calculate final percent
for (int i = 0; i < this.numberOfDifferentLetter; i++) {
float remaining = this.originals[i] - this.uses[i];
this.results[i] = (remaining / Bag.MAXIMUM_LETTERS) * 100.0f;
}
}
@Override
public float get(Character letterName) {
return this.results[Tool.getNumericValue(letterName)];
}
}