Package pdp.scrabble.utility

Source Code of pdp.scrabble.utility.Anagram

package pdp.scrabble.utility;

import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.TreeSet;
import pdp.scrabble.Game;
import pdp.scrabble.ihm.MainFrame_old;
import pdp.scrabble.multiplayer.Server;

/** Handle anagram generator from a word.
*/
public class Anagram {

    private Game game = null;
    private MainFrame_old mainFrame = null;
    private TreeSet<String> result = null;

    public Anagram(Game game, String str) {
  char[] word, output, tmp = str.toCharArray();
  this.result = new TreeSet<String>();

  this.game = game;
  this.mainFrame = this.game.mainFrame();
  int outputSize = 0;
  int len = tmp.length;

  word = new char[len];
  output = new char[len];
  System.arraycopy(tmp, 0, word, 0, len);

  this.next(word, 0, len, output, outputSize);
    }

    public Anagram(String str) {
  char[] word, output, tmp = str.toCharArray();
  this.result = new TreeSet<String>();

  int outputSize = 0;
  int len = tmp.length;

  word = new char[len];
  output = new char[len];
  System.arraycopy(tmp, 0, word, 0, len);

  this.nextStd(word, 0, len, output, outputSize);
    }

    private void next(char[] word, int n, int lg, char[] output, int outSize) {
  char[] comb;
  int i, j, k, m, outputSize = outSize;
  StringBuilder tmp = null;

  if (lg != 0) {
      comb = new char[lg];
      outputSize++;

      for (i = 0; i < lg; i++) {
    output[n] = word[i];

    tmp = new StringBuilder(output[0]);
    for (m = 0; m < outputSize; m++) {
        tmp.append(output[m]);
    }

    // Anagrams
    if (this.game.dictionary().contains(tmp)) {
        String str = tmp.toString();
        Server server = this.mainFrame.getMultiplayerPanel().getAction().server();
        int value = 0;

        if (server != null) {
      try {
          value = server.getWordValueServer(str);
      }
      catch (RemoteException ex) {
          Display.error("Multiplayer",
            "Unable to reach Server's bag !");
      }
        }
        else {
      value = this.game.bag().getWordValue(str);
        }

        if (value < 10) {
      this.result.add("0" + value + " - " + str);
        }
        else {
      this.result.add(value + " - " + str);
        }
    }

    // Combine the remainder
    k = 0;
    for (j = 0; j < lg; j++) {
        if (j != i) {
      comb[k] = word[j];
      k++;
        }
    }
    // Next using the new combinaison
    this.next(comb, n + 1, lg - 1, output, outputSize);
      }
  }
  else {
      outputSize--;
  }
    }

    private void nextStd(char[] word, int n, int lg, char[] output, int outSize) {
  char[] comb;
  int i, j, k, m, outputSize = outSize;
  StringBuilder tmp = null;

  if (lg != 0) {
      comb = new char[lg];
      outputSize++;

      for (i = 0; i < lg; i++) {
    output[n] = word[i];

    tmp = new StringBuilder(output[0]);
    for (m = 0; m < outputSize; m++) {
        tmp.append(output[m]);
    }

    this.result.add(tmp.toString());

    // Combine the remainder
    k = 0;
    for (j = 0; j < lg; j++) {
        if (j != i) {
      comb[k] = word[j];
      k++;
        }
    }
    // Next using the new combinaison
    this.nextStd(comb, n + 1, lg - 1, output, outputSize);
      }
  }
  else {
      outputSize--;
  }
    }

    public Iterator<String> get() {
  return this.result.iterator();
    }

    public Object[] toArray() {
  return this.result.toArray();
    }

    public void clear() {
  this.result.clear();
    }

    public void show() {
  Iterator<String> itr = this.result.iterator();
  while (itr.hasNext()) {
      System.out.println(itr.next());
  }
    }
}
TOP

Related Classes of pdp.scrabble.utility.Anagram

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.