-
Notifications
You must be signed in to change notification settings - Fork 50
[4기][3주차] Wordle 과제 제출 - 와제 #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9bd90f4
7a47e5b
eea56a0
f34d966
c0802d7
cc73074
0d6cee9
93933d6
de62b94
a8ef199
4880da9
6ab25b5
f7c4936
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package controller; | ||
|
|
||
| import domain.Colors; | ||
| import domain.TryResult; | ||
| import domain.Words; | ||
| import ui.IOUtils; | ||
| import ui.InputView; | ||
| import ui.ResultView; | ||
|
|
||
| public class WordleController { | ||
|
|
||
| private static final int PLAY_ROUND = 6; | ||
| private static final String WORDS_TXT = "words.txt"; | ||
|
|
||
| public static void main(String[] args) { | ||
| Words words = Words.of(IOUtils.readFromResource(WORDS_TXT)); | ||
|
|
||
| ResultView.startComent(); | ||
|
|
||
| TryResult tryResult = new TryResult(); | ||
| int round = 0; | ||
| while (round++ < PLAY_ROUND && !tryResult.isFinished()) { | ||
| Colors colors = words.matchingAnswer(InputView.inputComment()); | ||
| tryResult.addTry(colors); | ||
| ResultView.results(tryResult); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package domain; | ||
|
|
||
| public enum Color { | ||
| GREY ("⬜"), | ||
| YELLOW ("\uD83D\uDFE8"), | ||
| GREEN ("\uD83D\uDFE9"); | ||
|
|
||
| private String icon; | ||
|
|
||
| Color(String icon) { | ||
| this.icon = icon; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return icon; | ||
| } | ||
| } |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Color들의 집합(게임 1라운드의 결과)을 일급 컬렉션으로 관리해주셨네요 👍🏻👍🏻 💯 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package domain; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public class Colors { | ||
| private static final List<Color> ALL_GREEN = List.of(Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN); | ||
| private final List<Color> colors; | ||
|
|
||
| public Colors(List<Color> colors) { | ||
| this.colors = colors; | ||
| } | ||
|
|
||
| public void add(Color color) { | ||
| colors.add(color); | ||
| } | ||
|
|
||
| public boolean isAllGreen() { | ||
| return ALL_GREEN.equals(colors); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Colors colors1 = (Colors) o; | ||
| return Objects.equals(colors, colors1.colors); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(colors); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class TryResult { | ||
| private List<Colors> results = new ArrayList<>(); | ||
| private boolean finished; | ||
|
|
||
| public void addTry(Colors colors) { | ||
| if (colors.isAllGreen()) { | ||
| finished = true; | ||
| } | ||
| results.add(colors); | ||
| } | ||
|
|
||
| public int count() { | ||
| return results.size(); | ||
| } | ||
|
|
||
| public List<Colors> getResults() { | ||
| return results; | ||
| } | ||
|
|
||
| public boolean isFinished() { | ||
| return finished; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public class Word { | ||
| private final String word; | ||
|
|
||
| public Word(String word) { | ||
| if (word.length() != 5) { | ||
| throw new IllegalArgumentException("단어는 다섯글자로만 입력 가능합니다"); | ||
| } | ||
| this.word = word; | ||
| } | ||
|
|
||
| public Colors compareWith(Word input) { | ||
| List<Color> result = new ArrayList<>(); | ||
| char[] answerArray = word.toCharArray(); | ||
| char[] inputArray = input.word.toCharArray(); | ||
|
|
||
| for (int i = 0; i < answerArray.length; i++) { | ||
| result.add(mapped(answerArray[i], inputArray[i])); | ||
| } | ||
|
|
||
| return new Colors(result); | ||
| } | ||
|
|
||
| private Color mapped(char answer, char input) { | ||
| if (answer == input) { | ||
| return Color.GREEN; | ||
| } | ||
| if (isContains(input)) { | ||
| return Color.YELLOW; | ||
| } | ||
| return Color.GREY; | ||
| } | ||
|
|
||
| private boolean isContains(char c) { | ||
| return this.word.contains(String.valueOf(c)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Word word1 = (Word) o; | ||
| return Objects.equals(word, word1.word); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(word); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package domain; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.Period; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Words { | ||
| private static final LocalDate BASE_DATE_FOR_ANSWER = LocalDate.of(2021, 6, 19); | ||
| private final List<Word> words; | ||
|
|
||
| public Words(List<Word> words) { | ||
| this.words = Collections.unmodifiableList(words); | ||
| } | ||
|
|
||
| public static Words of(List<String> strings) { | ||
| return new Words(strings.stream().map(Word::new).collect(Collectors.toList())); | ||
| } | ||
|
|
||
| public Word answer(LocalDate from) { | ||
| Period period = Period.between(BASE_DATE_FOR_ANSWER, from); | ||
| int range = period.getDays() % words.size(); | ||
| return words.get(range); | ||
| } | ||
|
|
||
| public Colors matchingAnswer(Word input) { | ||
| validate(input); | ||
| Word answer = answer(LocalDate.now()); | ||
| return answer.compareWith(input); | ||
| } | ||
|
|
||
| private void validate(Word input) { | ||
| if (!words.contains(input)) { | ||
| throw new IllegalArgumentException("단어집에 없는 단어를 선택하였습니다."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package ui; | ||
|
|
||
| import domain.Word; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URISyntaxException; | ||
| import java.net.URL; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.util.List; | ||
|
|
||
| public class IOUtils { | ||
|
|
||
| private IOUtils(){} | ||
|
|
||
| public static List<String> readFromResource(String resourceName){ | ||
| URL txtUrl = IOUtils.class.getClassLoader().getResource(resourceName); | ||
| try { | ||
| List<String> words = Files.readAllLines(Paths.get(txtUrl.toURI())); | ||
| return words; | ||
| } catch (IOException e) { | ||
| System.out.println("파일을 읽는도중 문제가 발생했습니다."); | ||
| throw new RuntimeException(); | ||
| } catch (URISyntaxException e) { | ||
| System.out.println("존재하지 않는 경로 입니다."); | ||
| throw new RuntimeException(); | ||
|
Comment on lines
+18
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a: 따로 말씀 안드려도 평소에는 잘 하실거 같아서 a 레벨로 코멘트 남깁니다 😊 |
||
| } | ||
| } | ||
|
|
||
| public static String[] readFromResource2(String resourceName){ | ||
| URL txtUrl = IOUtils.class.getClassLoader().getResource(resourceName); | ||
| try { | ||
| List<String> words = Files.readAllLines(Paths.get(txtUrl.toURI())); | ||
| return words.toArray(new String[words.size()]); | ||
| } catch (IOException e) { | ||
| System.out.println("파일을 읽는도중 문제가 발생했습니다."); | ||
| throw new RuntimeException(); | ||
| } catch (URISyntaxException e) { | ||
| System.out.println("존재하지 않는 경로 입니다."); | ||
| throw new RuntimeException(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package ui; | ||
|
|
||
| import domain.Word; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| private InputView(){} | ||
|
|
||
| public static Word inputComment() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("정답을 입력해 주세요.\n"); | ||
| return new Word(scanner.nextLine()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ui; | ||
|
|
||
| import domain.Colors; | ||
| import domain.TryResult; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class ResultView { | ||
|
akayj820 marked this conversation as resolved.
|
||
|
|
||
| private ResultView(){} | ||
|
|
||
| public static void startComent() { | ||
| System.out.println("WORDLE을 6번 만에 맞춰 보세요.\n시도의 결과는 타일의 색 변화로 나타납니다.\n"); | ||
| } | ||
|
|
||
| public static void results(TryResult tryResult) { | ||
| List<Colors> results = tryResult.getResults(); | ||
| if (tryResult.isFinished()) { | ||
| System.out.println(String.format("%d/6", tryResult.count())); | ||
| } | ||
|
|
||
| for (Colors colors : results) { | ||
| System.out.println(colors); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import domain.Colors; | ||
| import domain.Word; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import ui.IOUtils; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import static domain.Color.*; | ||
|
|
||
| public class WordleTest { | ||
|
akayj820 marked this conversation as resolved.
|
||
| @Test | ||
| void compare() { | ||
| Word answer = new Word("spill"); | ||
| Colors colors = answer.compareWith(new Word("hello")); | ||
| Assertions.assertEquals(new Colors(Arrays.asList(GREY, GREY, YELLOW, GREEN, GREY)), colors); | ||
| } | ||
|
|
||
| @Test | ||
| void readFile() { | ||
| //when | ||
| List<String> words = IOUtils.readFromResource("words.txt"); | ||
|
|
||
| //then | ||
| Assertions.assertEquals(words.get(0), "cigar"); | ||
| } | ||
|
|
||
| @Test | ||
| void sizeCheck() { | ||
| Assertions.assertThrows(IllegalArgumentException.class, () -> { | ||
| new Word("aaaa"); | ||
| }); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.