diff --git a/src/main/java/wordle/model/LetterCounter.java b/src/main/java/wordle/model/LetterCounter.java index a24f39c4..21dea2dc 100644 --- a/src/main/java/wordle/model/LetterCounter.java +++ b/src/main/java/wordle/model/LetterCounter.java @@ -23,6 +23,11 @@ public void decreaseCount(Letters letters) { } } + public void decreaseCount(Letter letter) { + char value = letter.getValue(); + letterCountMap.put(value, letterCountMap.get(value) - 1); + } + public Letters filterCanDecreaseCount(Letters letters) { List filteredLetters = letters.getLetters() .stream() @@ -32,7 +37,7 @@ public Letters filterCanDecreaseCount(Letters letters) { return new Letters(filteredLetters); } - private boolean canDecreaseCount(Letter letter) { + public boolean canDecreaseCount(Letter letter) { char value = letter.getValue(); return letterCountMap.containsKey(value) && letterCountMap.get(value) > 0; } @@ -46,7 +51,7 @@ public Letters filterCanNotDecreaseCount(Letters letters) { return new Letters(filteredLetters); } - private boolean canNotDecreaseCount(Letter letter) { + public boolean canNotDecreaseCount(Letter letter) { return !canDecreaseCount(letter); } } diff --git a/src/main/java/wordle/model/TileService.java b/src/main/java/wordle/model/TileService.java index b99a30fc..7420e992 100644 --- a/src/main/java/wordle/model/TileService.java +++ b/src/main/java/wordle/model/TileService.java @@ -1,5 +1,6 @@ package wordle.model; +import java.util.ArrayList; import java.util.List; public class TileService { @@ -30,14 +31,24 @@ private void processSamePositionAndValueLetters(Letters answerLetters, Letters i tiles.addGreenTile(samePositionAndValueLetters); } + // TODO: 알고리즘에 오류가 있어 급한대로 정상 동작하게 변경하였음. 다시 잘 생각해보자..... private void processSameValueLetters(Letters answerLetters, Letters inputLetters, LetterCounter letterCounter, Tiles tiles) { Letters sameValueLetters = inputLetters.findSameValueLetters(answerLetters); - Letters sameValueLettersForGrayTile = letterCounter.filterCanNotDecreaseCount(sameValueLetters); - tiles.addGrayTile(sameValueLettersForGrayTile); - Letters sameValueLettersForYellowTile = letterCounter.filterCanDecreaseCount(sameValueLetters); - letterCounter.decreaseCount(sameValueLettersForYellowTile); - tiles.addYellowTile(sameValueLettersForYellowTile); + List lettersForYellowTile = new ArrayList<>(); + List lettersForGrayTile = new ArrayList<>(); + for (Letter letter : sameValueLetters.getLetters()) { + if (letterCounter.canDecreaseCount(letter)) { + lettersForYellowTile.add(letter); + letterCounter.decreaseCount(letter); + continue; + } + + lettersForGrayTile.add(letter); + } + + tiles.addYellowTile(new Letters(lettersForYellowTile)); + tiles.addGrayTile(new Letters(lettersForGrayTile)); } private void processNoneMatchLetters(Letters answerLetters, Letters inputLetters, Tiles tiles) {