public class WordMatch {
private String secret;
public WordMatch(String word) {
secret = word;
}
public int scoreGuess(String guess) {
int guessLength = guess.length(); //Will be used to calculate score later
//Count occurrences of substring
int count = 0;
for (int i = 0; i < secret.length() - guessLength + 1; i++) {
if (secret.substring(i, i + guessLength).equals(guess)) {
count++;
}
}
return count * guessLength * guessLength; //Calculate and return score
}
public String findBetterGuess(String guess1, String guess2) {
String returner = "";
int score1 = scoreGuess(guess1); //calculate scores of the two guesses
int score2 = scoreGuess(guess2);
if (score1>score2) { //compare the scores
returner = guess1;
}
else if (score2>score1) {
returner = guess2;
}
else if (score1==score2) { // if the scores are the same, output the alphabetically higher guess
for (int i=0; i<guess1.length(); i++) {
char char1 = guess1.charAt(i);
char char2 = guess2.charAt(i);
if (char1==char2) {
continue;
}
else if (char1>char2) {
returner = guess1;
}
else if (char1<char2) {
returner = guess2;
}
}
}
return returner;
}
}