From e21443a4f7277391244fc98fb20eb4972ee18355 Mon Sep 17 00:00:00 2001 From: SeolYoungKim Date: Sun, 23 Jun 2024 01:14:41 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=95=8C=EA=B3=A0=EB=A6=AC=EC=A6=98?= =?UTF-8?q?=EC=9D=B4=20=EC=98=A4=EB=8F=99=EC=9E=91=20=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 정답 : lurid, 입력 : hello일 때 노란 타일이 두 번 출력되는 문제 수정 --- src/main/java/wordle/model/LetterCounter.java | 9 +++++++-- src/main/java/wordle/model/TileService.java | 20 ++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) 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..d2ac67ea 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,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 lettersForYellowTile = new ArrayList<>(); + List 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) {