Skip to content

Commit

Permalink
[#8] Fixed word solutions beeing accepted as solutions sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederik Kammel committed Sep 21, 2016
1 parent 6cbfb11 commit 8a2743c
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/main/java/algorithm/HangmanSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public class HangmanSolver {
* @see Result
*/
public static Result solve(String currentSequence, Language lang) {


if (currentSequence.equals("Hang_an ____er")) {
System.out.println("Stopping...");
}

ResultList resultList = new ResultList();

if (!lang.equals(langOld) || database == null) {
Expand Down Expand Up @@ -76,18 +80,29 @@ public static Result solve(String currentSequence, Language lang) {

// Find the best score
int maxIndex = 0;
int maxIndexCopy;
ResultList maxValues = new ResultList();

for (int i = 0; i < resultList.size(); i++) {
if (resultList.get(i).bestCharScore > resultList.get(maxIndex).bestCharScore && resultList.get(i).resultType==ResultType.letter) {
// new max found
maxIndex = i;
maxValues = new ResultList();
maxValues.add(resultList.get(i));
} else if (resultList.get(i).bestCharScore == resultList.get(maxIndex).bestCharScore) {
// found another letter with the same score so add it to the
// list too
maxValues.add(resultList.get(i));

// find first valid solution
while (resultList.get(maxIndex).resultType != ResultType.letter){
maxIndex++;
}

maxIndexCopy = maxIndex;

for (int i = maxIndexCopy; i < resultList.size(); i++) {
if (resultList.get(i).resultType == ResultType.letter) {
if (resultList.get(i).bestCharScore > resultList.get(maxIndex).bestCharScore) {
// new max found
maxIndex = i;
maxValues = new ResultList();
maxValues.add(resultList.get(i));
} else if (resultList.get(i).bestCharScore == resultList.get(maxIndex).bestCharScore) {
// found another letter with the same score so add it to
// the
// list too
maxValues.add(resultList.get(i));
}
}
}

Expand All @@ -112,14 +127,12 @@ public static Result solve(String currentSequence, Language lang) {
// Get the char with most appearances
Map.Entry<Character, Integer> maxEntry = null;

for (Entry<Character, Integer> entry : charCounts.entrySet())
{
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
{
maxEntry = entry;
}
for (Entry<Character, Integer> entry : charCounts.entrySet()) {
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0) {
maxEntry = entry;
}
}

globalResult = resultList.getFirstResultWithBestChar(maxEntry.getKey());
}

Expand Down

0 comments on commit 8a2743c

Please sign in to comment.