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

[3주차] Wordle 과제 제출 - 하현 #19

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
99b1f3a
docs: 기능 요구 사항, 유비쿼터스 언어 정리
anwjrrp33 Mar 26, 2023
055067c
feat: Words 생성기 구현
anwjrrp33 Mar 26, 2023
5b2d7a1
feat: Words 구현
anwjrrp33 Mar 26, 2023
62c685f
docs: 기능 요구 사항 수정
Mar 27, 2023
407bd21
refactor: 메서드, 테스트코드 리팩터링
Mar 27, 2023
01c5f7a
feat: word 구현
Mar 27, 2023
e523243
feat: 일급 컬렉션 구현
Mar 27, 2023
f52a5f0
docs: 기능 요구사항 수정
Mar 27, 2023
70f41fa
test: word 테스트 코드 작성
anwjrrp33 Mar 28, 2023
cb1dc6b
feat: Words 정답을 가져오는 기능 구현
anwjrrp33 Mar 28, 2023
96c91f7
feat: 정답 생성
anwjrrp33 Mar 28, 2023
d54e1b7
feat: 답안과 정답을 비교하는 기능 구현
anwjrrp33 Mar 28, 2023
efba747
docs: 기능 요구사항 수정
anwjrrp33 Mar 28, 2023
d6125e7
refactor: 이름변경 (CorrectAnswer -> Answer)
Mar 30, 2023
9954847
feat: Letter 생성
Mar 30, 2023
bfe943a
refactor: Answer, Word 에서 Letter 를 사용하도록 변경
Mar 30, 2023
cac009b
docs: 기능 요구사항 수정
Mar 30, 2023
13f908c
refactor: 타일 비교 리팩터링
anwjrrp33 Mar 31, 2023
c8b37fa
feat: 입출력 기능 구현
anwjrrp33 Mar 31, 2023
5c5daeb
feat: 기능 조립
anwjrrp33 Mar 31, 2023
1fc9412
refactor: 기능 리팩터링
anwjrrp33 Mar 31, 2023
154b289
refactor: while 조건문 메서드로 변경
anwjrrp33 Apr 15, 2023
0133dd7
refactor: 타일 계산 메소드 위치 변경 및 테스트 코드 작성
anwjrrp33 Apr 15, 2023
a724947
refactor: 워드 검증 로직 분리
anwjrrp33 Apr 15, 2023
4c1357f
refactor: WordsGenerator 생성자 접근제한자를 통해서 외부 생성 막기
anwjrrp33 Apr 15, 2023
072dd46
refactor: 불필요한 주석 제거
anwjrrp33 Apr 15, 2023
4b03b1b
refactor: 워드 테스트 접근 제한자 변경
anwjrrp33 Apr 15, 2023
91199da
refactor: 워드 테스트 파라미터 케이스 추가
anwjrrp33 Apr 15, 2023
0464ddb
feat: 입력된 답안은 words.txt에 존재하는 단어만 생성 가능한 기능 구현
anwjrrp33 Apr 15, 2023
5087c85
docs: 기능 요구사항 수정
anwjrrp33 Apr 15, 2023
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
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
# 미션 - 워들
## 기능 요구 사항
* Letter
* [x] Letter 를 생성한다.
* [x] 알파벳만 입력 가능하다.
* [x] 알파벳이 아니면 에러를 발생한다.
* Words Generator
* [X] `words.txt`에서 Words 를 읽어온다.
* Word
* [x] Word 를 생성한다.
* [x] 5글자의 알파벳만 받는다.
* [x] 5글자가 아니면 에러를 발생한다.
* [x] 알파벳이 아니면 에러를 발생한다.
* Words
* [x] Words 를 생성한다.
* [x] 주어진 Words 가 비어있으면 에러를 발생한다.
* [x] 주어진 Words 에 Word 가 없다면 에러를 발생한다.
* [x] 정답을 가져온다.
* [x] ((현재 날짜 - 2021년 6월 19일) % 배열의 크기) 번째의 단어를 가져온다.
* 정답
* [x] 입력된 답안을 존재하는 Word 인지 비교한다.
* 답안
* [x] 사용자가 워드를 입력한다.
* [x] 글자는 5글자까지 입력 가능하다.
* [x] 사용자가 정답을 6번까지 입력할 수 있다.
* [x] 입력된 답안은 `words.txt`에 존재하는 단어여야 한다.
* 출력
* [x] 타일로 결과를 표시한다.
* [x] 위치와 글자가 맞으면 초록색
* [x] 위치가 틀리고, 글자가 맞으면 노란색
* [x] 위치와 글자가 틀리면 회색
* [x] 두 개의 동일한 문자를 입력하고 그중 하나가 회색으로 표시되면 해당 문자 중 하나만 최종 단어에 나타난다.

## 유비쿼터스 언어
* Word
* 5글자의 영문
* Words
* `words.txt`의 리스트

## 🔍 진행 방식

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/Application.java
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();
}
}
6 changes: 6 additions & 0 deletions src/main/java/config/FileConfig.java
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";
}
24 changes: 24 additions & 0 deletions src/main/java/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package controller;

import domain.Answer;
import domain.Words;
import dto.GameHistory;
import service.GameService;

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

public class GameController {

private final GameService gameService;

public GameController(GameService gameService) {
this.gameService = gameService;
}

public void start() {
Words words = gameService.init();
List<GameHistory> gameHistories = gameService.startGame(words);
gameService.endGame(gameHistories);
}
}
71 changes: 71 additions & 0 deletions src/main/java/domain/Answer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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 = 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);

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);

if (count == Word.WORD_LENGTH) {
this.isSuccess = true;
}
}

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 +
'}';
}
}
43 changes: 43 additions & 0 deletions src/main/java/domain/Letter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package domain;

import java.util.Objects;

public class Letter {

Choose a reason for hiding this comment

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

문자열 자체를 클래스로 만든게 너무 좋네요 👍

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 + '\'' +
'}';
}
}
25 changes: 25 additions & 0 deletions src/main/java/domain/Tile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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;
}

public static 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;
}
}
53 changes: 53 additions & 0 deletions src/main/java/domain/Word.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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) {
validate(word);

this.word = word.chars().mapToObj(c -> (char) c)
.map(Letter::new)
.collect(Collectors.toList());
}

public void validate(String word) {
if (this.isNotMatchWord(word)) {
throw new IllegalArgumentException(word + "는 5글자의 알파벳이 아닙니다.");
}
}

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 +
'}';
}
}
46 changes: 46 additions & 0 deletions src/main/java/domain/Words.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 Word getWord(String word) {
Word inputWord = new Word(word);
validateExist(inputWord);
return inputWord;
}

private void validateExist(Word word) {
if (!words.contains(word)) {
throw new IllegalArgumentException("존재하지 않는 단어입니다.");
}
}

public List<Word> getWords() {
return words;
}
}
20 changes: 20 additions & 0 deletions src/main/java/dto/GameHistory.java
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());
}
}
Loading