-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordUtilities.java
More file actions
140 lines (126 loc) · 3.83 KB
/
WordUtilities.java
File metadata and controls
140 lines (126 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import java.util.Scanner;
/**
* Finds all the words that can be formed with a list of letters,
* then finds the word with the highest Scrabble score.
*
*/
public class WordUtilities
{
private static Scanner infile;
private static String infileName = "wordlist.txt";
private static int wordCount = 1;
private static int currentTopCombo = 0;
private static String currentTopString = "";
private static String [] word = new String [90948];
public static void main (String [] args)
{
String input = getInput();
findAllWords(input);
printWords();
// Score table in alphabetic order according to Scrabble
int [] scoretable = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
String best = bestWord(word,scoretable);
System.out.println("HELLO\n\n\n" + best + "\n\n\n");
}
/**
* Enter a list of 3 to 12 letters. It also checks
* all letters to insure they fall between 'a' and 'z'.
*
* @return A string of the letters
*/
public static String getInput ( )
{
String userInput = Prompt.getString("Please enter a list of letters, from 3 to 12 letters long, without spaces ");
return userInput;
}
/**
* Find all words that can be formed by a list of letters.
*
* @param letters String list of letters
* @return An array of strings with all words found.
*/
public static String [] findAllWords (String letters)
{
infile = OpenFile.openToRead(infileName);
int cat = 1;
while(infile.hasNext()){
String currentWord = infile.next();
if(wordMatch(currentWord, letters)){
word[cat] = currentWord;
cat++;
}
}
return word;
}
/**
* Determines if a word can be formed by a list of letters.
*
* @param temp The word to be tested.
* @param letters A string of the list of letter
* @return True if word can be formed, false otherwise
*/
public static boolean wordMatch (String temp, String letters)
{
for(int i=0;i<temp.length();i++){
int ind = letters.indexOf(temp.charAt(i));
if(ind == -1)
return false;
letters = letters.substring(0,ind) + letters.substring(ind+1);
}
return true;
}
/**
* Print the words found to the screen.
*
* @param word The string array containing the words.
*/
public static void printWords ()
{
int length = 0;
for(int arrayCount = 1; word[arrayCount] != null; arrayCount++){
System.out.printf("%s\t", word[arrayCount]);
if(arrayCount%5 == 0){
System.out.println();
}
}
}
/**
* Finds the highest scoring word according to Scrabble rules.
*
* @param word An array of words to check.
* @param scoretable An array of 26 integer scores in letter order.
* @return The word with the highest score.
*/
public static String bestWord (String [] word, int [] scoretable)
{
for(wordCount = 0; wordCount<word.length; wordCount++){
int right = getScore(word[wordCount], scoretable);
System.out.println(right);
if(right > currentTopCombo) {
currentTopCombo = right;
currentTopString = word[wordCount];
}
}
return currentTopString;
}
/**
* Calculates the score of a word according to Scrabble rules.
*
* @param word The word to score
* @param scoretable The array of 26 scores for alphabet.
* @return The integer score of the word.
*/
public static int getScore (String word, int [] scoretable)
{
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
int determineWordTotal = 0;
for(int letterCount = 0; letterCount < word.length(); letterCount ++){
for(int scoreCount = 0; scoreCount < 26; scoreCount++){
if(alphabet[scoreCount] == word.charAt(letterCount)){
determineWordTotal+= scoretable[scoreCount];
}
}
}
return determineWordTotal;
}
}