Skip to content
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

[4기][3주차] Wordle 과제 제출 - 와제 #20

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/main/java/controller/WordleController.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package controller;

import domain.Color;
import domain.Colors;
import domain.TryResult;
import domain.Word;
import domain.Words;
import ui.IOUtils;
import ui.InputView;
import ui.ResultView;

import java.time.LocalDate;
import java.util.List;

public class WordleController {

private static final int PLAY_ROUND = 6;
akayj820 marked this conversation as resolved.
Show resolved Hide resolved
private static final String WORDS_TXT = "words.txt";

public static void main(String[] args) {
Words words = new Words(IOUtils.readFromResource("words.txt"));
Words words = Words.of(IOUtils.readFromResource(WORDS_TXT));

ResultView.startComent();

TryResult tryResult = new TryResult();
int round = 0;
while (round++ < PLAY_ROUND && !tryResult.isFinished()) {
List<Color> colors = words.matchingAnswer(InputView.inputComment());
Colors colors = words.matchingAnswer(InputView.inputComment());
tryResult.addTry(colors);
ResultView.results(tryResult);
}
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/domain/Colors.java
Copy link

Choose a reason for hiding this comment

The 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);
}
}
10 changes: 5 additions & 5 deletions src/main/java/domain/TryResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import java.util.List;

public class TryResult {
private static final List<Color> ALL_GREEN = List.of(Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN);
private List<List<Color>> results = new ArrayList<>();
private List<Colors> results = new ArrayList<>();
private boolean finished;

public void addTry (List<Color> colors) {
if (colors.equals(ALL_GREEN)) {
public void addTry(Colors colors) {
if (colors.isAllGreen()) {
finished = true;
}
results.add(colors);
Expand All @@ -18,7 +17,8 @@ public void addTry (List<Color> colors) {
public int count() {
return results.size();
}
public List<List<Color>> getResults() {

public List<Colors> getResults() {
return results;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/domain/Word.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Word(String word) {
this.word = word;
}

public List<Color> compareWith(Word input) {
public Colors compareWith(Word input) {
List<Color> result = new ArrayList<>();
char[] answerArray = word.toCharArray();
char[] inputArray = input.word.toCharArray();
Expand All @@ -23,20 +23,20 @@ public List<Color> compareWith(Word input) {
result.add(mapped(answerArray[i], inputArray[i]));
}

return result;
return new Colors(result);
}

private Color mapped(char answer, char input) {
if (answer == input) {
return Color.GREEN;
}
if (isContains2(input)) {
if (isContains(input)) {
return Color.YELLOW;
}
return Color.GREY;
}

private boolean isContains2(char c) {
private boolean isContains(char c) {
return this.word.contains(String.valueOf(c));
}

Expand Down
23 changes: 8 additions & 15 deletions src/main/java/domain/Words.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,36 @@

import java.time.LocalDate;
import java.time.Period;
import java.util.Arrays;
import java.util.Collection;
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(String... words){
this(Arrays.stream(words)
.map(Word::new)
.collect(Collectors.toList()));
}

public Words(List<Word> words) {
this.words = Collections.unmodifiableList(words);
}

public boolean contains(Word word){
return words.contains(word);
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(LocalDate.of(2021, 6, 19), from);
int range = period.getDays()%words.size();
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 List<Color> matchingAnswer(Word input) {
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)){
if (!words.contains(input)) {
throw new IllegalArgumentException("단어집에 없는 단어를 선택하였습니다.");
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/ui/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ public class IOUtils {

private IOUtils(){}

public static String[] readFromResource(String resourceName){
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a: 따로 말씀 안드려도 평소에는 잘 하실거 같아서 a 레벨로 코멘트 남깁니다 😊
catch문에서 유의미한 Custom Exception으로 변경하여 throw 하는 것도 좋은 방법인 것 같습니다!!

}
}

public static String[] readFromResource2(String resourceName){
URL txtUrl = IOUtils.class.getClassLoader().getResource(resourceName);
try {
List<String> words = Files.readAllLines(Paths.get(txtUrl.toURI()));
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ui/ResultView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ui;

import domain.Color;
import domain.Colors;
import domain.TryResult;

import java.util.List;
Expand All @@ -14,12 +14,12 @@ public static void startComent() {
}

public static void results(TryResult tryResult) {
List<List<Color>> results = tryResult.getResults();
List<Colors> results = tryResult.getResults();
if (tryResult.isFinished()) {
System.out.println(String.format("%d/6", tryResult.count()));
}

for (List colors : results) {
for (Colors colors : results) {
System.out.println(colors);
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/WordleTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import domain.Color;
import domain.Colors;
import domain.Word;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import ui.IOUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;

import static domain.Color.*;

public class WordleTest {
akayj820 marked this conversation as resolved.
Show resolved Hide resolved
@Test
void compare() {
Word answer = new Word("spill");
List<Color> colors = answer.compareWith(new Word("hello"));
Assertions.assertEquals(Arrays.asList(Color.GREY, Color.GREY, Color.YELLOW, Color.GREEN, Color.GREY), colors);
Colors colors = answer.compareWith(new Word("hello"));
Assertions.assertEquals(new Colors(Arrays.asList(GREY, GREY, YELLOW, GREEN, GREY)), colors);
}

@Test
void readFile() throws URISyntaxException, IOException {
void readFile() {
//when
String[] words = IOUtils.readFromResource("words.txt");
List<String> words = IOUtils.readFromResource("words.txt");

//then
Assertions.assertEquals(words[0], "cigar");
Assertions.assertEquals(words.get(0), "cigar");
}

@Test
Expand Down
16 changes: 4 additions & 12 deletions src/test/java/WordsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,15 @@
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;

public class WordsTest {

@Test
void containsWord(){
//given
Words words = new Words("aaaaa", "bbbbb", "ccccc");

//when
Assertions.assertTrue(words.contains(new Word("aaaaa")));
Assertions.assertFalse(words.contains(new Word("ddddd")));
}

@Test
void getArrayLocation(){
//given
Words words = new Words("aaaaa", "bbbbb");
Words words = Words.of(Arrays.asList("aaaaa", "bbbbb"));

//when
Word answers = words.answer(LocalDate.of(2021, 6, 20));
Expand All @@ -31,7 +23,7 @@ void getArrayLocation(){

@Test
void validation() {
Words words = new Words("aaaaa");
Words words = Words.of(Arrays.asList("aaaaa"));
Assertions.assertThrows(IllegalArgumentException.class, () -> {
words.matchingAnswer(new Word("bbbbb"));
});
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/domain/ColorsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package domain;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class ColorsTest {

@DisplayName("Colors equals 테스트")
@Test
void equalsTest() {
List<Color> green = List.of(Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN, Color.GREEN);
Colors colors = new Colors(green);
assertThat(colors.isAllGreen()).isTrue();
}
}