-
Notifications
You must be signed in to change notification settings - Fork 22
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
base: dradnats1012
Are you sure you want to change the base?
Changes from all commits
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,10 @@ | ||
package ladder; | ||
|
||
import ladder.controller.LadderController; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
LadderController controller = new LadderController(); | ||
controller.play(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ladder.controller; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import ladder.domain.Ladder; | ||
import ladder.domain.LadderGame; | ||
import ladder.domain.RandomLadderGenerator; | ||
import ladder.domain.Result; | ||
import ladder.domain.ResultMap; | ||
import ladder.view.InputView; | ||
import ladder.view.OutputView; | ||
|
||
public class LadderController { | ||
|
||
public void play() { | ||
List<String> names = InputView.getNames(); | ||
List<String> results = InputView.getResult(); | ||
int height = InputView.getHeight(); | ||
|
||
Ladder ladder = new Ladder(new RandomLadderGenerator(names.size(), height)); | ||
OutputView.printLadder(ladder.getLadder(), names, results); | ||
|
||
LadderGame ladderGame = new LadderGame(ladder, names.size()); | ||
ladderGame.move(); | ||
|
||
String resultTarget = InputView.wantResult(); | ||
Result result = new Result(new ResultMap(names, results, ladderGame.getResult()), resultTarget); | ||
|
||
if (result.getResult() instanceof Map) { | ||
OutputView.printResult((Map<String, String>)result.getResult()); | ||
} | ||
if (result.getResult() instanceof String) { | ||
OutputView.printResult((String)result.getResult()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ladder.domain; | ||
|
||
import java.util.List; | ||
|
||
public class Ladder { | ||
|
||
private List<Line> ladder; | ||
|
||
public Ladder(LadderGenerator ladderGenerator) { | ||
this.ladder = ladderGenerator.generateLadder(); | ||
} | ||
|
||
public List<Line> getLadder() { | ||
return ladder; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package ladder.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LadderGame { | ||
|
||
private final Ladder ladder; | ||
private final int width; | ||
private final List<Integer> result = new ArrayList<>(); | ||
|
||
public LadderGame(Ladder ladder, int width) { | ||
this.ladder = ladder; | ||
this.width = width; | ||
} | ||
|
||
public void move() { | ||
for (int i = 0; i < width; i++) { | ||
moveHeight(i); | ||
} | ||
} | ||
|
||
private void moveHeight(int position) { | ||
for (int i = 0; i < ladder.getLadder().size(); i++) { | ||
position = moveLine(ladder.getLadder().get(i), position); | ||
} | ||
result.add(position); | ||
} | ||
|
||
private Integer moveLine(Line line, int position) { | ||
if (position == 0) { | ||
if (line.getLine().get(position).equals(true)) { | ||
return ++position; | ||
} | ||
return position; | ||
} | ||
if (position == line.getWidth() - 1) { | ||
if (line.getLine().get(position - 1).equals(true)) { | ||
return --position; | ||
} | ||
return position; | ||
} | ||
if (line.getLine().get(position).equals(true)) { | ||
return ++position; | ||
} | ||
if (line.getLine().get(position - 1).equals(true)) { | ||
return --position; | ||
} | ||
|
||
return position; | ||
} | ||
|
||
public List<Integer> getResult() { | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ladder.domain; | ||
|
||
import java.util.List; | ||
|
||
public interface LadderGenerator { | ||
|
||
List<Line> generateLadder(); | ||
} | ||
Comment on lines
+5
to
+8
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. generator를 따로 분리 하시는 것을 선호 하게 된 이유나 많은 생각들이 있으셨을 것 같은데 어떤 생각들이 있으셨는지 궁금합니다..! 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,44 @@ | ||
package ladder.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class Line { | ||
|
||
private final Random random = new Random(); | ||
private final List<Boolean> points = new ArrayList<>(); | ||
private final int width; | ||
|
||
public Line(int width) { | ||
this.width = width; | ||
generateLine(); | ||
} | ||
|
||
private void generateLine() { | ||
while (checkEmpty()) { | ||
for (int i = 0; i < width - 1; i++) { | ||
points.add(random.nextBoolean()); | ||
checkLeft(i); | ||
} | ||
} | ||
} | ||
Comment on lines
+18
to
+25
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. ladder는 generator를 따로 만들고 line은 클래스 내에서 generate하는 이유가 혹시 따로 있으실까요?? 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. 아 이건 1단계 미션 힌트에 Line을 따로 구현해보라고 써있어서 그렇게 한번 해봤습니다! |
||
|
||
private void checkLeft(int i) { | ||
if (i > 0 && points.get(i - 1).equals(true) && points.get(i).equals(true)) { | ||
points.set(i, !points.get(i)); | ||
} | ||
} | ||
|
||
private Boolean checkEmpty() { | ||
return points.stream().allMatch(point -> !point); | ||
} | ||
|
||
public List<Boolean> getLine() { | ||
return points; | ||
} | ||
|
||
public int getWidth() { | ||
return width; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ladder.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RandomLadderGenerator implements LadderGenerator { | ||
|
||
private final List<Line> lines = new ArrayList<>(); | ||
private final int width; | ||
private final int height; | ||
|
||
public RandomLadderGenerator(int width, int height) { | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
public List<Line> generateLadder() { | ||
for (int i = 0; i < height; i++) { | ||
lines.add(new Line(width)); | ||
} | ||
|
||
return lines; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package ladder.domain; | ||
|
||
import java.util.Objects; | ||
|
||
public class Result { | ||
private final ResultMap resultMap; | ||
private final String resultTarget; | ||
|
||
public Result(ResultMap resultMap, String resultTarget) { | ||
this.resultMap = resultMap; | ||
this.resultTarget = resultTarget; | ||
} | ||
|
||
public Object getResult() { | ||
if (resultTarget.equals("all")) { | ||
return resultMap.getResultMap(); | ||
} | ||
return resultMap.getResultMap().get(resultTarget); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ladder.domain; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ResultMap { | ||
private final Map<String, String> resultMap = new LinkedHashMap<>(); | ||
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. 저는 linkedhashmap을 사용할 생각은 해보지 못했는데 재밌게 잘 구현하셨네요👍👍 |
||
private final List<String> names; | ||
private final List<String> results; | ||
private final List<Integer> resultSeq; | ||
|
||
public ResultMap(List<String> names, List<String> results, List<Integer> resultSeq) { | ||
this.names = names; | ||
this.results = results; | ||
this.resultSeq = resultSeq; | ||
makeResultMap(); | ||
} | ||
|
||
private void makeResultMap() { | ||
for (int i = 0; i < names.size(); i++) { | ||
resultMap.put(names.get(i), results.get(resultSeq.get(i))); | ||
} | ||
} | ||
|
||
public Map<String, String> getResultMap() { | ||
return resultMap; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ladder.message; | ||
|
||
public enum ConsoleMessage { | ||
RESULT("실행결과"), | ||
LADDER_RESULT("사다리 결과"), | ||
LADDER_WIDTH("사다리의 넓이는 몇 개인가요?"), | ||
LADDER_HEIGHT("사다리의 높이는 몇 개인가요?"), | ||
INPUT_NAME("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"), | ||
INPUT_RESULT("실행 결과를 입력하세요. (결과는 쉽표(,)로 구분하세요)"), | ||
RESULT_TARGET("결과를 보고 싶은 사람은?"); | ||
Comment on lines
+3
to
+10
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. console 메시지를 이렇게 enum으로 분리하시는 것도 선호하시는 것 같아요..! 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. 그냥 뭔가 코드에 한글이 들어가니까 이질감이 들어서 아예 메시지 열거형을 하나 만들어서 관리하면 어떨까 라는 생각에 만들어 봤습니다! |
||
|
||
private final String message; | ||
|
||
ConsoleMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ladder.view; | ||
|
||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
import ladder.message.ConsoleMessage; | ||
|
||
public class InputView { | ||
|
||
private final static Scanner scanner = new Scanner(System.in); | ||
|
||
public static Integer getHeight() { | ||
System.out.println(); | ||
System.out.println(ConsoleMessage.LADDER_HEIGHT.getMessage()); | ||
|
||
return scanner.nextInt(); | ||
} | ||
|
||
public static List<String> getNames() { | ||
System.out.println(ConsoleMessage.INPUT_NAME.getMessage()); | ||
|
||
return List.of(scanner.nextLine().split(",")); | ||
} | ||
|
||
public static List<String> getResult() { | ||
System.out.println(); | ||
System.out.println(ConsoleMessage.INPUT_RESULT.getMessage()); | ||
|
||
return List.of(scanner.nextLine().split(",")); | ||
} | ||
|
||
public static String wantResult() { | ||
System.out.println(); | ||
System.out.println(ConsoleMessage.RESULT_TARGET.getMessage()); | ||
|
||
return scanner.next(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package ladder.view; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import ladder.domain.Line; | ||
import ladder.message.ConsoleMessage; | ||
|
||
public class OutputView { | ||
|
||
public static void printLadder(List<Line> ladder, List<String> names, List<String> results) { | ||
System.out.println(ConsoleMessage.LADDER_RESULT.getMessage() + "\n"); | ||
|
||
names.forEach(name -> System.out.print(name + " ")); | ||
System.out.println(); | ||
|
||
ladder.forEach(line -> printLine(line)); | ||
|
||
results.forEach(result -> System.out.print(result + " ")); | ||
System.out.println(); | ||
} | ||
|
||
private static void printLine(Line line) { | ||
System.out.print("|"); | ||
for (int i = 0; i < line.getWidth() - 1; i++) { | ||
if (line.getLine().get(i)) { | ||
System.out.print("-----"); | ||
} else { | ||
System.out.print(" "); | ||
} | ||
System.out.print("|"); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public static void printResult(String result) { | ||
System.out.println(); | ||
System.out.println(ConsoleMessage.RESULT.getMessage()); | ||
System.out.println(result); | ||
} | ||
|
||
public static void printResult(Map<String, String> resultMap) { | ||
System.out.println(); | ||
System.out.println(ConsoleMessage.RESULT.getMessage()); | ||
resultMap.forEach((key, value) -> System.out.println(key + " : " + 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.
instanceof 연산자 사용하신 것도 재밌네요
솔직히 잘 사용 안해서 잊고 있던 연산자 인데 재밌게 잘 구현하신 것 같습니다
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.
메서드 오버로딩으로 하려고 하다가 타협점을 찾은게 instanceof입니다 ㅠㅠ
개인적으로 이부분에서 약간 지저분해진것 같아요ㅠㅠㅠㅠ