-
Notifications
You must be signed in to change notification settings - Fork 43
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 과제 제출 - 분수 #25
base: main
Are you sure you want to change the base?
Changes from 21 commits
99b1f3a
055067c
5b2d7a1
62c685f
407bd21
01c5f7a
e523243
f52a5f0
70f41fa
cb1dc6b
96c91f7
d54e1b7
efba747
d6125e7
9954847
bfe943a
cac009b
13f908c
c8b37fa
5c5daeb
1fc9412
59476e5
09d3cb1
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,9 @@ | ||
import controller.GameController; | ||
import service.GameService; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
GameController gameController = new GameController(new GameService()); | ||
gameController.start(); | ||
} | ||
} |
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. FileConfig 클래스에서 file 관련 설정을 관리하셨네요. 이렇게 하면 추후 파일관련 내용이 추가되더라도 응집도 높은 코드를 작성할 수 있겠네요.😃 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package config; | ||
|
||
public class FileConfig { | ||
|
||
public final static String FILE_PATH = "src/main/resources/words.txt"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package controller; | ||
|
||
import domain.Answer; | ||
import dto.GameHistory; | ||
import service.GameService; | ||
|
||
import java.util.List; | ||
|
||
public class GameController { | ||
|
||
private final GameService gameService; | ||
|
||
public GameController(GameService gameService) { | ||
this.gameService = gameService; | ||
} | ||
|
||
public void start() { | ||
Answer answer = gameService.init(); | ||
List<GameHistory> gameHistories = gameService.startGame(answer); | ||
gameService.endGame(gameHistories); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package domain; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
public class Answer { | ||
|
||
private final Word answer; | ||
|
||
private boolean isSuccess = false; | ||
|
||
public Answer(Word answer) { | ||
this.answer = answer; | ||
} | ||
|
||
public List<Tile> compare(Word answer) { | ||
Map<Letter, Long> letterMap = getLetterMap(); | ||
|
||
List<Tile> result = new ArrayList<>(); | ||
|
||
for (int i = 0; i < Word.WORD_LENGTH; i++) { | ||
Long count = letterMap.get(answer.getWord().get(i)); | ||
|
||
Tile tile = getTile(count, this.answer.getWord().get(i), answer.getWord().get(i)); | ||
result.add(tile); | ||
|
||
letterMap.put(answer.getWord().get(i), letterMap.getOrDefault(answer.getWord().get(i), 0L) - 1); | ||
} | ||
endGame(result); | ||
|
||
Comment on lines
+22
to
+30
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. 중복되는 값을 변수 선언하여 재사용을 할 수 있을것 같습니다. 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. 음 너무 길어져서 10줄이 넘어서 ㅋㅋㅋ 재선언 안하긴 했습니다. |
||
return result; | ||
} | ||
|
||
private Map<Letter, Long> getLetterMap() { | ||
return this.answer.getWord() | ||
.stream() | ||
.collect(Collectors.groupingBy(c -> c, Collectors.counting())); | ||
} | ||
|
||
private void endGame(List<Tile> result) { | ||
int count = Collections.frequency(result, Tile.GREEN); | ||
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. 이런 Collections 메서드가 있는줄 처음 알았네요. 덕분에 배워갑니다!❤ |
||
|
||
if (count == Word.WORD_LENGTH) { | ||
this.isSuccess = true; | ||
} | ||
} | ||
|
||
private Tile getTile(Long count, Letter answerLetter, Letter letter) { | ||
if (count == null || count <= 0) { | ||
return Tile.GRAY; | ||
} | ||
if (answerLetter.equals(letter)) { | ||
return Tile.GREEN; | ||
} | ||
return Tile.YELLOW; | ||
} | ||
|
||
public boolean isSuccess() { | ||
return isSuccess; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Answer that = (Answer) o; | ||
return Objects.equals(answer, that.answer); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(answer); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Answer{" + | ||
"answer=" + answer + | ||
'}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package domain; | ||
|
||
import java.util.Objects; | ||
|
||
public class Letter { | ||
private final String letter; | ||
|
||
public Letter(Character letter) { | ||
if (this.isNotAlphabet(letter)) { | ||
throw new IllegalArgumentException(letter + "는 알파벳이 아닙니다."); | ||
} | ||
|
||
this.letter = String.valueOf(letter); | ||
} | ||
|
||
private boolean isNotAlphabet(Character letter) { | ||
return !(letter >= 'A' && letter <= 'Z') && !(letter >= 'a' && letter <= 'z'); | ||
} | ||
|
||
public String getLetter() { | ||
return letter; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Letter letter1 = (Letter) o; | ||
return Objects.equals(letter, letter1.letter); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(letter); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Letter{" + | ||
"letter='" + letter + '\'' + | ||
'}'; | ||
} | ||
} |
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. enum을 활용해서 각각의 종류에 맞는 이모지를 할당하셨세요. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package domain; | ||
|
||
public enum Tile { | ||
GREEN("\uD83D\uDFE9"), YELLOW("\uD83D\uDFE8"), GRAY("⬜"); | ||
|
||
private final String tile; | ||
|
||
Tile(String tile) { | ||
this.tile = tile; | ||
} | ||
|
||
public String getTile() { | ||
return tile; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package domain; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class Word { | ||
public static final int WORD_LENGTH = 5; | ||
|
||
private final List<Letter> word; | ||
|
||
public Word(String word) { | ||
if (this.isNotMatchWord(word)) { | ||
throw new IllegalArgumentException(word + "는 5글자의 알파벳이 아닙니다."); | ||
} | ||
|
||
this.word = word.chars().mapToObj(c -> (char) c) | ||
.map(Letter::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private boolean isNotMatchWord(String word){ | ||
return word.length() != WORD_LENGTH; | ||
} | ||
|
||
public List<Letter> getWord() { | ||
return word; | ||
} | ||
|
||
@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); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Word{" + | ||
"word=" + word + | ||
'}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package domain; | ||
|
||
import java.time.LocalDate; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Words { | ||
public static final LocalDate DEFAULT_DATE = LocalDate.of(2021, 6, 19); | ||
private final List<Word> words; | ||
|
||
public Words(List<String> words) { | ||
this.words = convert(words); | ||
} | ||
|
||
private List<Word> convert(List<String> words) { | ||
if (words.size() == 0) { | ||
throw new IllegalArgumentException("파일이 비어 있습니다."); | ||
} | ||
|
||
return words.stream().map(Word::new).collect(Collectors.toList()); | ||
} | ||
|
||
public Answer getAnswer(LocalDate now) { | ||
long betweenDay = ChronoUnit.DAYS.between(DEFAULT_DATE, now); | ||
|
||
Word answer = this.words.get((int) (betweenDay % words.size())); | ||
return new Answer(answer); | ||
} | ||
|
||
public List<Word> getWords() { | ||
return words; | ||
} | ||
} |
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. 일급컬렉션을 활용하셨네요!😀 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dto; | ||
|
||
import domain.Tile; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class GameHistory { | ||
|
||
private final List<Tile> gameHistory; | ||
|
||
public GameHistory(List<Tile> gameHistory) { | ||
this.gameHistory = gameHistory; | ||
} | ||
|
||
public String getGameResult() { | ||
return gameHistory.stream().map(Tile::getTile) | ||
.collect(Collectors.joining()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package service; | ||
|
||
import config.FileConfig; | ||
import domain.Answer; | ||
import domain.Tile; | ||
import domain.Word; | ||
import domain.Words; | ||
import dto.GameHistory; | ||
import support.WordsGenerator; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class GameService { | ||
|
||
public Answer init() { | ||
InputView.inputStartGame(); | ||
Words words = new Words(WordsGenerator.read(FileConfig.FILE_PATH)); | ||
return words.getAnswer(LocalDate.now()); | ||
} | ||
|
||
public List<GameHistory> startGame(Answer answer) { | ||
List<GameHistory> gameHistories = new ArrayList<>(); | ||
|
||
int count = 0; | ||
while (count < InputView.GAME_TOTAL_ROUND && !answer.isSuccess()) { | ||
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. 해당 조건을 판별하는 메서드를 분리해보는건 어떨까요? 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. 동감합니다 :) |
||
Word inputWord = new Word(InputView.inputWord()); | ||
List<Tile> tiles = answer.compare(inputWord); | ||
gameHistories.add(new GameHistory(tiles)); | ||
print(answer, gameHistories); | ||
count++; | ||
} | ||
|
||
return gameHistories; | ||
} | ||
|
||
private void print(Answer answer, List<GameHistory> gameHistories) { | ||
if (!answer.isSuccess()) { | ||
OutputView.outputTiles(gameHistories); | ||
} | ||
} | ||
|
||
public void endGame(List<GameHistory> gameHistories) { | ||
OutputView.outputEndGame(gameHistories); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package support; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
public class WordsGenerator { | ||
|
||
public static List<String> read(String filePath) { | ||
try { | ||
return Files.readAllLines(Paths.get(filePath)); | ||
} catch (IOException e) { | ||
throw new RuntimeException("파일을 읽을 수 없습니다.", e); | ||
} | ||
Comment on lines
+11
to
+15
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. 파일이 없는 경우에 발생하는 NoSuchFileException과 같이 구체적인 예외를 작성해보는건 어떨까요? 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. 음 이번 프로젝트에는 있는 예외를 사용하려고 했습니다. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Controller, Service 네이밍을 통해서 MVC 패턴을 적용했다는 의도가 한번에 느껴지네요.👍