From 680de2218b748ce43999ccff874d7b286830020d Mon Sep 17 00:00:00 2001 From: jwoh Date: Tue, 15 Nov 2022 19:28:35 +0900 Subject: [PATCH 1/6] =?UTF-8?q?refect=20:=20print=EC=B1=85=EC=9D=B8=20cont?= =?UTF-8?q?roller=EC=97=90=EC=84=9C=20=EC=A0=9C=EC=99=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder/view/InputView.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/ladder/view/InputView.java b/src/main/java/ladder/view/InputView.java index bc607f83fc..e11266da0f 100644 --- a/src/main/java/ladder/view/InputView.java +++ b/src/main/java/ladder/view/InputView.java @@ -14,10 +14,23 @@ private InputView() { private static final Scanner scanner = new Scanner(System.in); public static String[] getNames() { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); return scanner.next().split(COMMA); } public static int getHeight() { + System.out.println("최대 사다리 높이는 몇 개인가요?"); return scanner.nextInt(); } -} + + public static String[] getResult() { + System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); + return scanner.next().split(COMMA); + } + + public static String getTargetPlayer() + { + System.out.println("결과를 보고 싶은 사람은?"); + return scanner.next(); + } +} \ No newline at end of file From 85ae3f8d4c90fba4eb6d0c5176eec361c6ec0b5e Mon Sep 17 00:00:00 2001 From: jwoh Date: Tue, 15 Nov 2022 19:30:41 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat=20:=20=EA=B2=B0=EA=B3=BC=EA=B0=92?= =?UTF-8?q?=EC=9D=84=20=EC=B0=BE=EC=9D=84=20=EC=88=98=20=EC=9E=88=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=82=AC=EB=8B=A4=EB=A6=AC=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?index=20retrun?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder/domain/Ladder.java | 9 +++++++++ src/main/java/ladder/domain/Line.java | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/ladder/domain/Ladder.java b/src/main/java/ladder/domain/Ladder.java index eac547fc84..bdfda52a8b 100644 --- a/src/main/java/ladder/domain/Ladder.java +++ b/src/main/java/ladder/domain/Ladder.java @@ -20,4 +20,13 @@ public static Ladder of(int userCount, int height) { public List getLines() { return lines; } + + public int move(int userIndex) { + int resultIndex = userIndex; + for (Line line : lines) { + resultIndex = line.move(resultIndex); + } + + return resultIndex; + } } diff --git a/src/main/java/ladder/domain/Line.java b/src/main/java/ladder/domain/Line.java index 9c72405a1c..32529d1d53 100644 --- a/src/main/java/ladder/domain/Line.java +++ b/src/main/java/ladder/domain/Line.java @@ -7,7 +7,7 @@ public class Line { - private static final Random RANDOM = new Random(); + private static final Random RANDOM = new Random(); private final List points = new ArrayList<>(); @@ -36,5 +36,18 @@ private boolean validPoint(boolean currentPoint, boolean beforePoint) { } return currentPoint; } + + public int move(int index) + { + if (index > 0 && points.get(index - 1)) { + return --index; + } + + if (index == points.size()) { + return points.get(index - 1) ? --index : index; + } + + return points.get(index) ? ++index : index; + } } From 0fd9aa35aca9c3941022f7e6e428271709cd401b Mon Sep 17 00:00:00 2001 From: jwoh Date: Tue, 15 Nov 2022 19:31:09 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat=20:=20=EA=B2=B0=EA=B3=BC=EA=B0=92=20?= =?UTF-8?q?=EC=9D=BC=EA=B8=89=EA=B0=9D=EC=B2=B4,=20=EC=9D=BC=EA=B8=89?= =?UTF-8?q?=EC=BB=AC=EB=A0=89=EC=85=98=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder/domain/Result.java | 24 ++++++++++++++++++++ src/main/java/ladder/domain/Results.java | 29 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/main/java/ladder/domain/Result.java create mode 100644 src/main/java/ladder/domain/Results.java diff --git a/src/main/java/ladder/domain/Result.java b/src/main/java/ladder/domain/Result.java new file mode 100644 index 0000000000..1a368a63db --- /dev/null +++ b/src/main/java/ladder/domain/Result.java @@ -0,0 +1,24 @@ +package ladder.domain; + +public class Result +{ + private String result; + + private Result(String result) + { + this.result = result; + } + + public static Result from(String result) { + return new Result(result); + } + + public String getResult() { + return result; + } + + @Override + public String toString() { + return result; + } +} diff --git a/src/main/java/ladder/domain/Results.java b/src/main/java/ladder/domain/Results.java new file mode 100644 index 0000000000..5cf88505a0 --- /dev/null +++ b/src/main/java/ladder/domain/Results.java @@ -0,0 +1,29 @@ +package ladder.domain; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Results +{ + private final List results; + + private Results(List results) + { + this.results = results; + } + + public static Results of(String[] resultArray, int userSize) { + if (userSize != resultArray.length) { + throw new IllegalArgumentException("결과값과 인원수가 다릅니다"); + } + return new Results(Arrays.stream(resultArray) + .map(Result::from) + .collect(Collectors.toList()) + ); + } + + public List getResults() { + return results; + } +} From 01910e93b24a21b6ba1d9248a9811ec104d57826 Mon Sep 17 00:00:00 2001 From: jwoh Date: Tue, 15 Nov 2022 19:31:33 +0900 Subject: [PATCH 4/6] =?UTF-8?q?refect=20:=20=EA=B0=9D=EC=B2=B4=20toString?= =?UTF-8?q?=20=EC=9E=AC=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ladder/domain/User.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/ladder/domain/User.java b/src/main/java/ladder/domain/User.java index 20d2ab9118..203cbaa690 100644 --- a/src/main/java/ladder/domain/User.java +++ b/src/main/java/ladder/domain/User.java @@ -22,4 +22,9 @@ public static User from(String name) { public String getName() { return name; } + + @Override + public String toString() { + return name; + } } From de72b93601214ff1cc6fb40efa2da5fbf90dc009 Mon Sep 17 00:00:00 2001 From: jwoh Date: Tue, 15 Nov 2022 19:31:55 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat=20:=20=EC=B5=9C=EC=A2=85=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=B2=98=EB=A6=AC=EC=97=90=20=EB=B9=84=EC=A6=88?= =?UTF-8?q?=EB=8B=88=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4=EC=97=90=EC=84=9C=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ladder/service/LadderResultService.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/main/java/ladder/service/LadderResultService.java diff --git a/src/main/java/ladder/service/LadderResultService.java b/src/main/java/ladder/service/LadderResultService.java new file mode 100644 index 0000000000..ed9e1172f3 --- /dev/null +++ b/src/main/java/ladder/service/LadderResultService.java @@ -0,0 +1,51 @@ +package ladder.service; + + +import ladder.domain.Ladder; +import ladder.domain.Result; +import ladder.domain.User; + +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class LadderResultService +{ + private static LadderResultService ladderResultService; + + public static LadderResultService getInstance() + { + if (ladderResultService == null) + { + ladderResultService = new LadderResultService(); + } + + return ladderResultService; + } + + public List getLadderResult(String targetPlayer, List userList + , Ladder ladder, List results) { + if (targetPlayer.equals("all")) { + return allLadderResult(userList, ladder, results); + } + return userLadderResult(findUserIndex(targetPlayer, userList), ladder, results); + } + + private List userLadderResult(int userIndex, Ladder ladder, List results) { + return List.of(results.get(ladder.move(userIndex)).getResult()); + } + + private List allLadderResult(List userList, Ladder ladder, List results) { + return IntStream.range(0, userList.size()) + .mapToObj(index -> userList.get(index) + " : " + results.get(ladder.move(index))) + .collect(Collectors.toList()); + } + + private int findUserIndex(String targetPlayer, List userList) { + return IntStream.range(0, userList.size()) + .filter(i -> Objects.equals(userList.get(i).getName(), targetPlayer)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("사다리게임에 참여하지 않은 사람입니다.")); + } +} From 80cdb39c5740ae14f96fee76e7f41ba3bb898c2b Mon Sep 17 00:00:00 2001 From: jwoh Date: Tue, 15 Nov 2022 19:32:21 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat=20:=20=EC=84=9C=EB=B9=84=EC=8A=A4=20?= =?UTF-8?q?=ED=98=B8=EC=B6=9C=20=ED=9B=84=20=EB=82=98=EC=98=A8=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=EA=B0=92=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ladder/controller/LadderController.java | 23 +++++++++++++++---- src/main/java/ladder/view/OutputView.java | 22 ++++++++++++++---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/main/java/ladder/controller/LadderController.java b/src/main/java/ladder/controller/LadderController.java index d185f746d0..4fda2c3d8c 100644 --- a/src/main/java/ladder/controller/LadderController.java +++ b/src/main/java/ladder/controller/LadderController.java @@ -1,28 +1,43 @@ package ladder.controller; import ladder.domain.Ladder; +import ladder.domain.Results; import ladder.domain.Users; +import ladder.service.LadderResultService; import ladder.view.InputView; import ladder.view.OutputView; public class LadderController { private static LadderController ladderController; + private final LadderResultService ladderResultService; + + private LadderController(LadderResultService ladderResultService) + { + this.ladderResultService = ladderResultService; + } + public static LadderController getInstance() { if (ladderController == null) { - ladderController = new LadderController(); + ladderController = new LadderController(LadderResultService.getInstance()); } return ladderController; } public void run() { - System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); Users users = Users.from(InputView.getNames()); - System.out.println("최대 사다리 높이는 몇 개인가요?"); + Results results = Results.of(InputView.getResult(), users.getUserCount()); Ladder ladder = Ladder.of(users.getUserCount(), InputView.getHeight()); - OutputView.printLadder(users.getUserList(), ladder.getLines()); + OutputView.printLadder(users.getUserList(), ladder.getLines(), results.getResults()); + + boolean isEnd = false; + while (!isEnd) { + isEnd = OutputView + .printLadderResult(ladderResultService.getLadderResult(InputView.getTargetPlayer(), users.getUserList() + , ladder, results.getResults())); + } } } diff --git a/src/main/java/ladder/view/OutputView.java b/src/main/java/ladder/view/OutputView.java index 00301f4e4c..a9469db124 100644 --- a/src/main/java/ladder/view/OutputView.java +++ b/src/main/java/ladder/view/OutputView.java @@ -2,6 +2,7 @@ import ladder.domain.Line; +import ladder.domain.Result; import ladder.domain.User; import java.util.List; @@ -17,19 +18,20 @@ public class OutputView { private OutputView() { } - public static void printLadder(List userList, List lines) + public static void printLadder(List userList, List lines, List results) { - System.out.println("실행결과"); + System.out.println("사다리 결과"); printUser(userList); printLine(lines); + printResult(results); } private static void printLine(List lines) { lines.forEach(line -> { - System.out.print("\n" + BLANK.repeat(3)); + System.out.print(BLANK.repeat(3)); printLines(line.getPoints()); - System.out.print(LADDER_SHAPE); + System.out.print(LADDER_SHAPE+"\n"); }); } @@ -56,10 +58,22 @@ private static void printUser(List userList) { .collect(Collectors.joining(BLANK))); } + private static void printResult(List userList) { + System.out.println(userList.stream() + .map(result -> String.format("%4s ", result.getResult())) + .collect(Collectors.joining(BLANK))); + } + private static String outputPersonName(String name) { if (name.length() < LINE_LENGTH_MAX) { return String.format("%4s ", name); } return name; } + + public static boolean printLadderResult(List ladderResult) { + ladderResult.forEach(System.out::println); + + return ladderResult.size() != 1; + } }