init
This commit is contained in:
67
in_class/cse123-inclass11/ScrabbleHelperv2/Scrabble.java
Normal file
67
in_class/cse123-inclass11/ScrabbleHelperv2/Scrabble.java
Normal file
@@ -0,0 +1,67 @@
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class Scrabble {
|
||||
public static void main(String[] args) {
|
||||
Set<String> dictionary = loadDictionary();
|
||||
List<Character> letters = readLetters();
|
||||
|
||||
String best = findBestWord(letters, dictionary);
|
||||
System.out.println(best + " (" + scoreWord(best) + ")");
|
||||
}
|
||||
|
||||
public static String findBestWord(List<Character> letters, Set<String> dict) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
// HELPER METHODS
|
||||
|
||||
// does not check if word is legal
|
||||
private static int scoreWord(String word) {
|
||||
// A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
|
||||
int[] values = {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};
|
||||
|
||||
int score = 0;
|
||||
word = word.toUpperCase();
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char ch = word.charAt(i);
|
||||
score += values[ch - 'A'];
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
private static List<Character> readLetters() {
|
||||
List<Character> result = new ArrayList<>();
|
||||
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter letters separated by whitespace: ");
|
||||
Scanner letters = new Scanner(input.nextLine());
|
||||
while (letters.hasNext()) {
|
||||
String letter = letters.next();
|
||||
if (letter.length() > 1) {
|
||||
System.out.println(" Skipping " + letter + " - not one letter");
|
||||
} else {
|
||||
result.add(letter.toUpperCase().charAt(0));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Set<String> loadDictionary() {
|
||||
try {
|
||||
Scanner input = new Scanner(new File("dictionary.txt"));
|
||||
Set<String> dict = new HashSet<>();
|
||||
|
||||
while (input.hasNextLine()) {
|
||||
dict.add(input.nextLine());
|
||||
}
|
||||
|
||||
return dict;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
178691
in_class/cse123-inclass11/ScrabbleHelperv2/dictionary.txt
Normal file
178691
in_class/cse123-inclass11/ScrabbleHelperv2/dictionary.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user