Package pdp.scrabble.game.impl

Source Code of pdp.scrabble.game.impl.PlacementImpl

package pdp.scrabble.game.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import pdp.scrabble.game.Bag;
import pdp.scrabble.game.Board;
import pdp.scrabble.game.Letter;
import pdp.scrabble.game.Location;
import pdp.scrabble.game.Placement;
import pdp.scrabble.game.Player;

public class PlacementImpl implements Placement {

    /** List of player actions. */
    private List<PlayerPlacement> actions = null;

    /** Created word from board changes. */
    private HashSet<String> words = null;

    /** Best score cumulating all word made score. */
    private int score = 0;

    /** Best length cumulating all word made length. */
    private int length = 0;

    /** Number of joker used. */
    private int jokersNumber = 0;

    /** Number of reliquat letters. */
    private int reliquatNum = 0;

    /** First formed word. */
    private String firstWord = null;

    /** Placement id. */
    private int id = 0;

    public PlacementImpl() {
  this.actions = new ArrayList<PlayerPlacement>(1);
  this.words = new HashSet<String>(1);
  this.score = -1;
  this.length = -1;
  this.jokersNumber = 0;
  this.reliquatNum = 0;
    }

    @Override
    public void addAction(Location location, Letter letter) {
  this.actions.add(new PlayerPlacement(location, letter));
  if (letter.getName() == Bag.JOKER) {
      this.jokersNumber++;
  }
    }

    @Override
    public void clearActions() {
  this.actions.clear();
  this.length = -1;
  this.jokersNumber = 0;
  this.reliquatNum = 0;
    }

    @Override
    public void applyActions(Player player, Board board) {
  Iterator<PlayerPlacement> itr = this.actions.iterator();
  while (itr.hasNext()) {
      PlayerPlacement action = itr.next();
      board.setCaseLetter(
        action.getV(), action.getH(), action.getLetter(), true);

      player.getRack().removeLetter(action.getLetter());
  }
    }

    @Override
    public void set(List<String> words, int score, int length) {
  this.words.clear();
  Iterator<String> itr = words.iterator();
  boolean first = false;
  while (itr.hasNext()) {
      if (!first) {
    this.firstWord = itr.next();
    this.words.add(this.firstWord);
    first = true;
      }
      else {
    this.words.add(itr.next());
      }
  }
  this.score = score;
  this.length = length;
    }

    @Override
    public Iterator<String> getWords() {
  return this.words.iterator();
    }

    @Override
    public int getScore() {
  return this.score;
    }

    @Override
    public int getLength() {
  return this.length;
    }

    @Override
    public int getJokersNumber() {
  return this.jokersNumber;
    }

    @Override
    public String getFirstWord() {
  return this.firstWord;
    }

    @Override
    public void setReliquatNum(int num) {
  this.reliquatNum = num;
    }

    @Override
    public int getReliquatNum() {
  return this.reliquatNum;
    }

    @Override
    public void setID(int id) {
  this.id = id;
    }

    @Override
    public int getID() {
  return this.id;
    }

    @Override
    public List<Letter> getLetters() {
  List<Letter> letters = new ArrayList<Letter>(1);
  Iterator<PlayerPlacement> itr = this.actions.iterator();
  while (itr.hasNext()) {
      letters.add(itr.next().getLetter());
  }
  return letters;
    }

    @Override
    public int compareTo(Placement p) {
  if (this.getScore() == p.getScore()) {
      return 0;
  }
  else if (this.getScore() > p.getScore()) {
      return 1;
  }
  else {
      return -1;
  }
    }

    @Override
    public boolean equals(Object o) {
  if (o instanceof Placement) {
      Placement p = (Placement) o;
      return (this.getID() == p.getID());
  }
  else {
      return false;
  }
    }

    @Override
    public int hashCode() {
  int hash = 5;
  hash = 19 * hash + this.score;
  hash = 19 * hash + this.length;
  hash = 19 * hash + this.jokersNumber;
  hash = 19 * hash + this.reliquatNum;
  hash = 19 * hash + (this.firstWord != null ? this.firstWord.hashCode() : 0);
  hash = 19 * hash + this.id;
  return hash;
    }

    @Override
    public String toString() {
  StringBuilder str = new StringBuilder("list:");
  Iterator<String> itr = this.getWords();
  while (itr.hasNext()) {
      str.append(" ").append(itr.next());
  }
  str.append(" | score = ").append(this.getScore());
  return str.toString();
    }
}
TOP

Related Classes of pdp.scrabble.game.impl.PlacementImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.