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 과제 제출 - 김민수 #13

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4dd7b76
docs: 코드 컨벤션 가이드 추가
minsoozz Apr 24, 2022
2ef7228
feat: 유저의 인풋을 입력받는 기능 추가
minsoozz Apr 24, 2022
1f15cd5
wip: 게임 매니저 작성
minsoozz Apr 24, 2022
d9acdb3
docs: 기능 분석 문서 추가
minsoozz Apr 24, 2022
ce89938
refactor: UserWord 리팩토링
tongnamuu Apr 24, 2022
faf7b92
docs: 비기능 요구사항 추가
tongnamuu Apr 24, 2022
0c674a3
Merge pull request #2 from minsoozz/pair-1
minsoozz Apr 24, 2022
d38868f
wip: GameManager
tongnamuu Apr 24, 2022
c887e24
Merge pull request #3 from minsoozz/feat/game-manager
tongnamuu Apr 24, 2022
b6645a1
feat: words.txt 파일을 읽어 단어를 반환하는 기능 추가
minsoozz Apr 24, 2022
9e7d23a
refactor: 정답을 상수로 수정
minsoozz Apr 24, 2022
ce3154d
feat: Application main() 추가
minsoozz Apr 24, 2022
9457b4c
feat: Judgement 기능 구현
tongnamuu May 1, 2022
198e500
Merge pull request #4 from minsoozz/feat/judgement
minsoozz May 1, 2022
cdeb7d7
feat: Color code 추가
minsoozz May 9, 2022
1e94fc3
fix: GameManager 완료
minsoozz May 9, 2022
f13a2fd
fix: Printer 완료
minsoozz May 9, 2022
105d441
fix: WordGenerator 완료
minsoozz May 9, 2022
215cb1c
fix: UserInput 완료
minsoozz May 9, 2022
837f053
feat: 결과 출력 기능 추가
minsoozz May 9, 2022
5824ac5
fix: main application 완료
minsoozz May 9, 2022
4d15ea9
refactor: txt 파일 읽는 방법 변경
minsoozz May 14, 2022
1d8c3be
refactor: 이전 결과를 나타내도록 수정
minsoozz May 14, 2022
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
12 changes: 12 additions & 0 deletions src/main/java/wordle/WordleApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package wordle;

import wordle.design.GameManager;
import wordle.impl.GameManagerImpl;

public class WordleApplication {

public static void main(String[] args) {
GameManager gameManager = new GameManagerImpl();
gameManager.start();
}
}
8 changes: 8 additions & 0 deletions src/main/java/wordle/design/GameManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package wordle.design;

public interface GameManager {

void start();

void end();
}
7 changes: 7 additions & 0 deletions src/main/java/wordle/design/Judgement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package wordle.design;

import wordle.vo.JudgeResult;

public interface Judgement {
JudgeResult execute(String answer, String input); // 유저 인풋을 판단
}
7 changes: 7 additions & 0 deletions src/main/java/wordle/design/Printer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package wordle.design;

import wordle.vo.JudgeResult;

public interface Printer {
void execute(JudgeResult result);
}
8 changes: 8 additions & 0 deletions src/main/java/wordle/design/UserInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package wordle.design;

import wordle.vo.UserWord;

public interface UserInput {

UserWord execute();
}
6 changes: 6 additions & 0 deletions src/main/java/wordle/design/WordGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package wordle.design;

public interface WordGenerator {

String execute();
}
48 changes: 48 additions & 0 deletions src/main/java/wordle/impl/GameManagerImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package wordle.impl;

import wordle.design.GameManager;
import wordle.design.Judgement;
import wordle.design.Printer;
import wordle.design.UserInput;
import wordle.design.WordGenerator;
import wordle.vo.JudgeResult;
import wordle.vo.UserWord;

public class GameManagerImpl implements GameManager {

private static final String OPENING_PHRASE = "WORDLE을 6번 만에 맞춰 보세요.\n시도의 결과는 타일의 색 변화로 나타납니다.";
private static final String CLOSING_PHRASE = "정답입니다 짝짝짝";
private static final int PROGRESS_COUNT = 6;
Comment on lines +15 to +17

Choose a reason for hiding this comment

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

상수로 깔끔하게 잘 빼셨네요🎉!


private UserInput userInput;
private WordGenerator wordGenerator;
private Judgement judgement;
private Printer printer;

public GameManagerImpl() {
this.userInput = new UserInputImpl();
this.wordGenerator = new WordGeneratorImpl();
this.judgement = new JudgementImpl();
this.printer = new PrinterImpl();
}

@Override
public void start() {
final String answer = wordGenerator.execute();
System.out.println(OPENING_PHRASE);
for (int i = 0; i < PROGRESS_COUNT; i++) {
UserWord userWord = userInput.execute();
JudgeResult judgeResult = judgement.execute(answer, userWord.getValue());
printer.execute(judgeResult);
Copy link

@david-learner david-learner May 10, 2022

Choose a reason for hiding this comment

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

이전 결과도 함께 나타내야하는데, 이전 결과를 보관하는 곳이 있으면 어떨까요?

Copy link
Author

Choose a reason for hiding this comment

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

기능 구현이 누락된 것 같습니다. 감사합니다!

if (judgeResult.isEndAble()) {
end();
return;
}
}
}

@Override
public void end() {
System.out.println(CLOSING_PHRASE);
}
}
65 changes: 65 additions & 0 deletions src/main/java/wordle/impl/JudgementImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package wordle.impl;

import wordle.design.Judgement;
import wordle.vo.Color;
import wordle.vo.JudgeResult;

/**
* 판별 결과는 흰색의 타일이 세 가지 색(초록색/노란색/회색) 중 하나로 바뀌면서 표현된다. 맞는 글자는 초록색, 위치가 틀리면 노란색, 없으면 회색 두 개의 동일한 문자를 입력하고 그중 하나가 회색으로 표시되면
* 해당 문자 중 하나만 최종 단어에 나타난다.
*/
public class JudgementImpl implements Judgement {
@Override
public JudgeResult execute(String answer, String input) {
boolean[] check = new boolean[input.length()];
JudgeResult result = new JudgeResult();

getGreenResult(answer, input, check, result);
getYellowResult(answer, input, check, result);
getGrayResult(check, result);
Comment on lines +17 to +19

Choose a reason for hiding this comment

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

정답 비교 과정을 3개의 메서드로 깔끔하게 잘 분리하셨네요!👍

return result;
}

private void getGreenResult(String ans, String input, boolean[] check, JudgeResult judgeResult) {
for (int i = 0; i < input.length(); ++i) {
checkGreen(ans.charAt(i), input.charAt(i), check, judgeResult, i);
}
}

private void checkGreen(char input, char ans, boolean[] check, JudgeResult result, int idx) {
if (ans == input) {
check[idx] = true;
result.setColor(idx, Color.GREEN);
}
}

private void getYellowResult(String ans, String input, boolean[] check, JudgeResult judgeResult) {
for (int i = 0; i < input.length(); ++i) {
for (int j = 0; j < ans.length(); ++j) {
if (!check[j] && input.charAt(i) == ans.charAt(j)) {
check[j] = true;
judgeResult.setColor(i, Color.YELLOW);
break;
}
}
}
}

private void getGrayResult(boolean[] check, JudgeResult judgeResult) {
for(int i=0;i<check.length;++i) {
if(!check[i]) {
judgeResult.setColor(i, Color.GRAY);
}
}
}


/*
1. Green 인 것 부터 찾는다. -> index, color 모두 맞아야함
2. Yellow 인 것을 찾는다. -> index 는 틀려야되고 color 는 같아야함
3. 나머진 Gray 로 처리한다.
단 답에는 존재하지만 유저 인풋에 두 개인 문자가 있을 수 있으므로
check 배열을 사용해야 할 것 같다.
결과는 Green Gray or Yellow Gray 가 되야한다.
*/
}
11 changes: 11 additions & 0 deletions src/main/java/wordle/impl/PrinterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package wordle.impl;

import wordle.design.Printer;
import wordle.vo.JudgeResult;

public class PrinterImpl implements Printer {
@Override
public void execute(JudgeResult result) {
System.out.println(result);
}
}
36 changes: 36 additions & 0 deletions src/main/java/wordle/impl/UserInputImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package wordle.impl;

import java.util.Scanner;
import wordle.design.UserInput;
import wordle.vo.UserWord;

public class UserInputImpl implements UserInput {

private static final String INSTRUCTION = "정답을 입력해주세요.";
private Scanner sc;

public UserInputImpl() {
this.sc = new Scanner(System.in);
}

@Override
public UserWord execute() {
instructor();
UserWord result = input();
return result;
}

private void instructor() {
System.out.println(INSTRUCTION);
}

private UserWord input() {
try {
String userInput = sc.next();
return UserWord.of(userInput);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
51 changes: 51 additions & 0 deletions src/main/java/wordle/impl/WordGeneratorImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package wordle.impl;

import java.io.BufferedReader;
import java.io.FileReader;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import wordle.design.WordGenerator;

public class WordGeneratorImpl implements WordGenerator {

private static final List<String> WORDS = readFile();
private static final String REFERENCE_DATE = "2021-06-19";

@Override
public String execute() {
return WORDS.get(findIndex());
}

private static List<String> readFile() {

BufferedReader bufferedReader = null;
List<String> listOfLines = new ArrayList<>();

try {
bufferedReader = new BufferedReader(new FileReader("src/main/resources/words.txt"));

String line = bufferedReader.readLine();

while (line != null) {
listOfLines.add(line);
line = bufferedReader.readLine();
}
Copy link

@david-learner david-learner May 10, 2022

Choose a reason for hiding this comment

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

파일의 라인을 읽어와서 List으로 받으실 때 Files.lines() 같은 것도 있으니 찾아보시면 좋을 것 같습니다:)


} catch (Exception e) {
e.printStackTrace();
}
return listOfLines;
}

private int findIndex() {
LocalDate now = LocalDate.now();
LocalDate referenceDay = LocalDate.parse(REFERENCE_DATE, DateTimeFormatter.ISO_LOCAL_DATE);
Duration diff = Duration.between(referenceDay.atStartOfDay(), now.atStartOfDay());
long diffDays = diff.toDays();

return (int) (diffDays % WORDS.size());
}
}
17 changes: 17 additions & 0 deletions src/main/java/wordle/vo/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package wordle.vo;

public enum Color {

Choose a reason for hiding this comment

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

타일색상에 대한 책임을 enum class로 잘 도출하셨네요!👍

GREEN("\uD83D\uDFE9"),
YELLOW("\uD83D\uDFE8"),
GRAY("\u2B1C");

Color(String code) {
this.code = code;
}

private final String code;

public String getCode() {
return code;
}
}
36 changes: 36 additions & 0 deletions src/main/java/wordle/vo/JudgeResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package wordle.vo;

import java.util.Arrays;

public class JudgeResult {

Color[] result;

Choose a reason for hiding this comment

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

접근제어자를 default로 해두신 이유가 있으실까요?


public JudgeResult() {
this.result = new Color[5];
}

public Color[] getResult() {
return result;
}

public void setColor(int idx, Color color) {
this.result[idx] = color;
}

public boolean isEndAble() {
for (int i = 0; i < result.length; ++i) {
if (!result[i].equals(Color.GREEN)) {
return false;
}
}
return true;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Arrays.stream(result).forEach(color -> sb.append(color.getCode()));
return sb.toString();
}
}
24 changes: 24 additions & 0 deletions src/main/java/wordle/vo/UserWord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package wordle.vo;

public class UserWord {
private String value;

private UserWord(String value) {
this.value = value;
}

public static UserWord of(String value) {
validate(value);
return new UserWord(value);
}

private static void validate(String value) throws IllegalArgumentException {
if(value.length()!=5) {
Copy link

@david-learner david-learner May 10, 2022

Choose a reason for hiding this comment

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

단어의 길이는 하드코딩보다는 상수로 빼는 걸 고려해보시면 어떨까요?

throw new IllegalArgumentException();
}
}

public String getValue() {
return value;
}
}
Loading