Package pdp.scrabble.dictionary

Source Code of pdp.scrabble.dictionary.DictionaryAbstr

package pdp.scrabble.dictionary;

import java.io.BufferedReader;
import java.io.FileReader;

import pdp.scrabble.game.VocLevel;

/***
*
* @author Maxime Oudot
*
*/
public abstract class DictionaryAbstr implements Dictionary
{
  protected VocLevel level = VocLevel.getMax();

  public abstract void addWord(String word, int level);

  public void fromFileToDictionary(String dictionaryPath) throws FileNotFoundException
  {
    FileReader reader = new FileReader(dictionaryPath);
    BufferedReader buffReader = new BufferedReader(reader);

    String line = null;
    int level = null;
    while (buffReader.ready())
    {
      try
      {
        line = buffReader.readLine();
      }
      catch (IOException ex)
      {
        continue;
      }
      String[] lineSplit = line.split(" ");
      String word = lineSplit[0];

      if (lineSplit.length > 1)
      {
        try
        {
          level = Integer.parseInt(lineSplit[1]);
        }
        catch (NumberFormatException e)
        {
          level = VocLevel.getMax().ordinal();
        }
      }
      else
      {
        level = VocLevel.getMax().ordinal();
      }

      this.addWord(word, level);
    }
  }

  public abstract boolean contains(String word);

  public abstract boolean isLastWord(String word);

  public void initWithLanguage(String language)
  {
    try
    {
      this.fromFileToDictionary(PATH+language+EXTENSION);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      System.exit(1);
    }
  }

  public boolean isLast(String word)
  {
    return this.isLastWord(word);
  }

  public VocLevel setVocabulary(VocLevel level)
  {
    VocLevel oldLevel = this.level;
    this.level = level;
    return oldLevel;
  }

}
TOP

Related Classes of pdp.scrabble.dictionary.DictionaryAbstr

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.