-
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
[3주차] Wordle 과제 제출 - 김민수 #13
base: main
Are you sure you want to change the base?
Changes from 21 commits
4dd7b76
2ef7228
1f15cd5
d9acdb3
ce89938
faf7b92
0c674a3
d38868f
c887e24
b6645a1
9e7d23a
ce3154d
9457b4c
198e500
cdeb7d7
1e94fc3
f13a2fd
105d441
215cb1c
837f053
5824ac5
4d15ea9
1d8c3be
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,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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package wordle.design; | ||
|
||
public interface GameManager { | ||
|
||
void start(); | ||
|
||
void end(); | ||
} |
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); // 유저 인풋을 판단 | ||
} |
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); | ||
} |
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(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package wordle.design; | ||
|
||
public interface WordGenerator { | ||
|
||
String execute(); | ||
} |
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; | ||
|
||
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); | ||
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. 기능 구현이 누락된 것 같습니다. 감사합니다! |
||
if (judgeResult.isEndAble()) { | ||
end(); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void end() { | ||
System.out.println(CLOSING_PHRASE); | ||
} | ||
} |
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
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. 정답 비교 과정을 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 가 되야한다. | ||
*/ | ||
} |
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); | ||
} | ||
} |
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; | ||
} | ||
} | ||
} |
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(); | ||
} | ||
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. 파일의 라인을 읽어와서 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()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package wordle.vo; | ||
|
||
public enum Color { | ||
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 class로 잘 도출하셨네요!👍 |
||
GREEN("\uD83D\uDFE9"), | ||
YELLOW("\uD83D\uDFE8"), | ||
GRAY("\u2B1C"); | ||
|
||
Color(String code) { | ||
this.code = code; | ||
} | ||
|
||
private final String code; | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
} |
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; | ||
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. 접근제어자를 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(); | ||
} | ||
} |
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) { | ||
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. 단어의 길이는 하드코딩보다는 상수로 빼는 걸 고려해보시면 어떨까요? |
||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
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.
상수로 깔끔하게 잘 빼셨네요🎉!