Skip to content

Commit

Permalink
fix: 알고리즘이 오동작 하는 문제 수정
Browse files Browse the repository at this point in the history
- 정답 : lurid, 입력 : hello일 때 노란 타일이 두 번 출력되는 문제 수정
  • Loading branch information
SeolYoungKim committed Jun 22, 2024
1 parent 518e295 commit e21443a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/main/java/wordle/model/LetterCounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Letter> filteredLetters = letters.getLetters()
.stream()
Expand All @@ -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;
}
Expand All @@ -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);
}
}
20 changes: 15 additions & 5 deletions src/main/java/wordle/model/TileService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package wordle.model;

import java.util.ArrayList;
import java.util.List;

public class TileService {
Expand Down Expand Up @@ -30,14 +31,23 @@ 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<Letter> lettersForYellowTile = new ArrayList<>();
List<Letter> lettersForGrayTile = new ArrayList<>();
for (Letter letter : sameValueLetters.getLetters()) {
if (letterCounter.canDecreaseCount(letter)) {
lettersForYellowTile.add(letter);
letterCounter.decreaseCount(letter);
} else {
lettersForGrayTile.add(letter);
}
}

tiles.addYellowTile(new Letters(lettersForYellowTile));
tiles.addGrayTile(new Letters(lettersForGrayTile));
}

private void processNoneMatchLetters(Letters answerLetters, Letters inputLetters, Tiles tiles) {
Expand Down

0 comments on commit e21443a

Please sign in to comment.