Package pdp.scrabble.game

Examples of pdp.scrabble.game.Placement


    @Override
    public Placement find(Player player, AIConfig config,
        List<Letter> required, List<Letter> excluded) {

  Placement bestPlacement = null;
  SearchPlacementImpl hori = null, vert = null;

  // Use multithreading if has enough core (at least 2)
  if (Main_old.CPU_CORES_NUMBER > 1) {
      hori = new SearchPlacementImpl(
        this.game, player, config, required, excluded, false);

      vert = new SearchPlacementImpl(
        this.game, player, config, required, excluded, true);

      hori.start();
      vert.start();

      try {
    hori.join();
      }
      catch (InterruptedException ex) {
    Display.fatal("Search Placement",
            "An error occured during horizontal search");
      }
      try {
    vert.join();
      }
      catch (InterruptedException ex) {
    Display.fatal("Search Placement",
            "An error occured during vertical search");
      }
  }
  // Use singlethreading else (sequential)
  else {
      hori = new SearchPlacementImpl(
        this.game, player, config, required, excluded, false);

      hori.start();
      try {
    hori.join();
      }
      catch (InterruptedException ex) {
    Display.fatal("Search Placement",
            "An error occured during horizontal search");
      }

      vert = new SearchPlacementImpl(
        this.game, player, config, required, excluded, true);

      vert.start();
      try {
    vert.join();
      }
      catch (InterruptedException ex) {
    Display.fatal("Search Placement",
            "An error occured during vertical search");
      }
  }

  List<Placement> best = new ArrayList<Placement>(16);
  int id = 0;

  Iterator<Placement> itrH = hori.getBestPlacements();
  while (itrH.hasNext()) {
      Placement current = itrH.next();
      current.setID(id);
      id++;
      best.add(current);
  }

  Iterator<Placement> itrV = vert.getBestPlacements();
  while (itrV.hasNext()) {
      Placement current = itrV.next();
      current.setID(id);
      id++;
      best.add(current);
  }

  Collections.sort(best);
  List<Placement> allBest = new ArrayList<Placement>(13);

  int len = best.size();
  for (int i = 0; i < len; i++) {
      Placement p = best.get(len - 1 - i);
      if (i < 13) {
    allBest.add(p);
      }
      else {
    p.clearActions();
      }
  }

  // Display list of best placements found
  if (Debug.STATE) {
      Iterator<Placement> itr = allBest.iterator();
      while (itr.hasNext()) {
    Placement p = itr.next();
    System.out.println(p.getScore() + " " + p.getFirstWord());
      }
      System.out.println("-----");
  }

  // Keep best placement found
  Placement bestHoriPlacement = hori.getBestPlacement();
  Placement bestVertPlacement = vert.getBestPlacement();

  if (bestVertPlacement.getScore() > bestHoriPlacement.getScore()) {
      bestPlacement = bestVertPlacement;
      bestHoriPlacement.clearActions();
  }
  else {
      bestPlacement = bestHoriPlacement;
      bestVertPlacement.clearActions();
  }

  bestHoriPlacement = null;
  bestVertPlacement = null;
  hori = null;
View Full Code Here


    }

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

   * @param priority search priority.
   */
  private void startBestPlacementSearch(
      AIConfig config, List<Letter> required, List<Letter> excluded) {

    Placement best = this.player.resolution().suggestPlacement(
        this.player, config, required, excluded);

    if (best != null) {
      Debug.console("help", "best placement", best.toString());
      best.applyActions(this.player, this.game.board());
      best.clearActions();
      this.panel.getButton("Validate").setEnabled(true);
      this.panel.getButton("Cancel").setEnabled(true);
    }
    else {
      Display.information("Help", "Sorry, no placement found !");
View Full Code Here

  }
    }

    @Override
    public void run() {
  Placement current = null;
  if (this.debugActive) {
      this.frame.setVisible(true);
  }

  // Vertical search ?
View Full Code Here

     * @return best placement found.
     */
    private Placement tryAllLocations(Board board, Player player, boolean center,
              boolean vertical, int axis) {

  Placement best = FACTORY.createPlacement();
  int maxWindowSize = this.maximalWindowSize;
  boolean ignore = true;

  // Ensure there is an adjacent old letter on current line
  // Or center is free (first turn)
  int boardMax = HORI_DIM;
  if (vertical) {
      boardMax = VERT_DIM;
  }

  for (int i = 0; i < boardMax; i++) {
      if (vertical) {
    if (this.hitOldCase(i, axis)) {
        ignore = false;
        break;
    }
      }
      else {
    if (this.hitOldCase(axis, i)) {
        ignore = false;
        break;
    }
      }
  }

  // Search best placement for each window size (word length)
  // Ignore empty lines
  if (!ignore || center) {
      for (int window = 2; window <= maxWindowSize; window++) {

    // Window is the number of letter for the word
    // It include both rack + board letter
    // Just the result on board is important
    // Minimum window size is 2, because with one letter,
    // we got a word length of 2
    Placement current = this.tryWindow(
      board, player, center, vertical, axis, window);

    // Update best placement
    if (current != null) {
        best = this.getBestPlacement(best, current, this.priority);
View Full Code Here

     * @return
     */
    private Placement tryWindow(Board board, Player player, boolean center,
        boolean vertical, int axis, int windowSize) {

  Placement best = FACTORY.createPlacement();
  Placement current = null;
  BoardCase boardCase = null;
  int index = 0;
  int boardMax = HORI_DIM;
  if (vertical) {
      boardMax = VERT_DIM;
View Full Code Here

     * @param letterIDStart first letter to take in rack.
     */
    private Placement tryLocation(Board board, StringBuilder location, int len,
          Player player, boolean vertical, int axis) {

  Placement best = FACTORY.createPlacement();
  HashSet<Loc> allowed = new HashSet<Loc>(1);
  Letter letter = null;
  int index = 0;
  int skip = 0;
  int i = 0, j = 0;

  // Find existing letters
  for (i = 0; i < len; i++) {
      j = Tool.getNumericValue(location.charAt(i));
      allowed.add(new Loc(i, j));
  }

  // Apply all possible letter positions from rack
  Iterator<String> rackItr = anagrams.get(len - 1).get();
  while (rackItr.hasNext()) {

      // Skip anagram (depending of the difficulty
      for (skip = 0; skip < this.anagramSkip; skip++) {
    if (rackItr.hasNext()) {
        rackItr.next();
    }
      }
      if (!rackItr.hasNext()) {
    break;
      }

      // Current anagram
      String ana = rackItr.next();
      Placement current = FACTORY.createPlacement();

      // For each available location indexs
      Iterator<Loc> locations = allowed.iterator();
      boolean done = false;
      while (locations.hasNext()) {
    Loc loc = locations.next();
    i = loc.getI();
    j = loc.getJ();
    index = Integer.parseInt(ana.charAt(i) + "");
    letter = player.getRack().getLetter(index);

    // Apply location
    if (vertical) {
        done = this.applyLocation(
          board, j, axis, letter, current, vertical);
    }
    else {
        done = this.applyLocation(
          board, axis, j, letter, current, vertical);
    }

    loc = null;
    if (!done) {
        break;
    }
      }

      locations = null;

      // Check if location found is good
      if (board.validateAI() && done) {
    current.set(board.getFormedWords(), board.getWordPoints(),
          board.getWordLength());

    current.setID(this.placementID);
    this.placementID++;
    this.bestPlacements.add(current);

    best = this.getBestPlacement(best, current, this.priority);

    this.repaint();
      }
      else {
    current.clearActions();
    current = null;
      }

      board.cancel(null);
  }
View Full Code Here

TOP

Related Classes of pdp.scrabble.game.Placement

Copyright © 2018 www.massapicom. 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.