patternjavaMinor
Scrabble algorithm review and performance suggestions
Viewed 0 times
scrabblealgorithmsuggestionsperformanceandreview
Problem
After finishing Project Euler 24, I decided to make a scrabble type of application using /usr/lib/dict as my dictionary what I cat that into a word file. It does take a few seconds, and the dictionary selection isn't that great.
Is there any way I could make it faster, more effective, and with a better dictionary source?
Is there any way I could make it faster, more effective, and with a better dictionary source?
public class Scrabble {
public static ArrayList numbers = new ArrayList();
public static ArrayList numbers2 = new ArrayList() {
};
public static void main(String[] args) {
Scanner dict = null;
try {
dict = new Scanner(new File("words.txt"));
} catch (FileNotFoundException ex) {
}
while (dict.hasNextLine()) {
numbers.add(dict.nextLine());
}
String n = "gojy";//random text here
rearrange("", n);
LinkedHashSet listToSet = new LinkedHashSet(numbers2);
ArrayList listWithoutDuplicates = new ArrayList(listToSet);
for (int i = 0; i < listWithoutDuplicates.size(); i++) {
if (numbers.contains(listWithoutDuplicates.get(i))) {
System.out.println(listWithoutDuplicates.get(i));
}
}
}
public static void rearrange(
String q, String w) {
if (w.length() <= 1) {
String k = q + w;
numbers2.add(k);//full word
numbers2.add(q);//smaller combination to get words with less letters. doesn't work too well
} else {
for (int i = 0; i < w.length(); i++) {
String c = w.substring(0, i)
+ w.substring(i + 1);
rearrange(q
+ w.charAt(i), c);
}
}
}
}Solution
If what you are trying to do is store a huge list of valid words in such a way that you can test whether a given string is a valid word, a Trie works well. See this Stack Overflow question.
Load the wordlist into the Trie when your server starts, and use the same Trie for all games (assuming this is a client server game). It's trickier than the linked thread if you need to include more than the standard 26 letters of the alphabet, but I've done a Unicode Trie for a chat filter before and I could help if you need that.
I don't understand what you are trying to do with the rearrange method. Can you explain?
Load the wordlist into the Trie when your server starts, and use the same Trie for all games (assuming this is a client server game). It's trickier than the linked thread if you need to include more than the standard 26 letters of the alphabet, but I've done a Unicode Trie for a chat filter before and I could help if you need that.
I don't understand what you are trying to do with the rearrange method. Can you explain?
Context
StackExchange Code Review Q#39922, answer score: 7
Revisions (0)
No revisions yet.