-
Notifications
You must be signed in to change notification settings - Fork 6
유태식 - 사다리 게임 #4
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
base: 유태식
Are you sure you want to change the base?
The head ref may contain hidden characters: "\uC720\uD0DC\uC2DD"
유태식 - 사다리 게임 #4
Changes from all commits
1e8dfa4
745093f
6ba8dd9
9422068
09e5724
93fe5e8
d260eb2
34a673b
5c91317
ed98290
16ca1e4
4ce42af
02dc9de
12ba062
a092efe
c975af2
a538a99
dc460ca
37a54e6
895cced
737fd27
05a7d11
ca27cae
a436477
51c66b0
fa9783b
e34411d
1c66463
cf3b3b1
8911a2c
8bc1632
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| .idea/** | ||
| .gradle/** | ||
| build/ | ||
| build/** | ||
| out/** |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,34 @@ | ||
| # java-ladder 게임 | ||
| # 사다리 게임 | ||
| ## 기능 구현 목록 | ||
| ### 참여자 이름 입력 | ||
| - [x] 여러 사람의 이름은 쉼표(,)를 기준으로 구분 | ||
| - [x] 참여자의 이름은 최대 5글자 | ||
| - [x] 앞 뒤 공백을 제거 | ||
| - [x] 중복된 이름이 있는 경우 예외 처리 | ||
| - [ ] 이름이 공백일 수 없음 | ||
|
|
||
| ### 실행결과 입력 | ||
| - [x] 여러 결과는 쉼표(,)를 기준으로 구분 | ||
| - [x] 결과는 최대 5글자 | ||
| - [x] 앞 뒤 공백을 제거 | ||
| - [ ] 결과가 공백일 수 없음 | ||
| - [ ] 참여자 수와 결과의 수가 같아야 함 | ||
|
|
||
| ### 사다리 높이 입력 | ||
| - [x] 사다리 높이는 1 이상이여야함 | ||
|
|
||
| ### 사다리 생성 | ||
| - [x] 사다리의 라인이 겹치지 않도록 사다리를 생성 | ||
| e.g. |-----|-----| 모양과 같이 가로 라인이 겹치는 경우 어느 방향으로 이동할 | ||
| 지 결정할 수 없다. | ||
|
|
||
| ### 사다리 출력 | ||
| - [x] 사다리 위에 사람 이름을 출력 | ||
| - [x] 사람 이름을 5자 기준으로 출력 | ||
| - [x] 사다리 폭 또한 사람 이름을 기준으로 함 | ||
| - [ ] 사다리 아래에 결과를 출력 | ||
| - [ ] 결과를 5자 기준으로 출력 | ||
|
|
||
| ### 사다리게임 결과 출력 | ||
| - [ ] 'all'을 입력시 전체참여자의 실행결과를 출력 | ||
| - [ ] 개인의 이름을 입력시 해당하는 참여자의 실행결과를 출력 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| rootProject.name = 'java-racingcar' | ||
| rootProject.name = 'java-ladder' | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package laddergame; | ||
|
|
||
| import laddergame.controller.LadderGameController; | ||
| import laddergame.view.InputView; | ||
| import laddergame.view.OutputView; | ||
|
|
||
| public class LadderGame { | ||
|
|
||
| public static void main(String[] args) { | ||
| LadderGameController ladderGameController = new LadderGameController(new InputView(), new OutputView()); | ||
| ladderGameController.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package laddergame.controller; | ||
|
|
||
| import laddergame.domain.ladder.Ladder; | ||
| import laddergame.domain.ladder.strategy.RandomLadderCreationStrategy; | ||
| import laddergame.domain.ladderheight.LadderHeight; | ||
| import laddergame.domain.player.Players; | ||
| import laddergame.domain.result.Results; | ||
| import laddergame.service.LadderGameService; | ||
| import laddergame.view.InputView; | ||
| import laddergame.view.OutputView; | ||
|
|
||
| public class LadderGameController { | ||
|
|
||
| private final InputView inputView; | ||
| private final OutputView outputView; | ||
| private final LadderGameService ladderGameService = new LadderGameService(); | ||
|
|
||
| public LadderGameController(InputView inputView, OutputView outputView) { | ||
| this.inputView = inputView; | ||
| this.outputView = outputView; | ||
| } | ||
|
|
||
| public void run() { | ||
| String playerNames = inputView.getPlayerNamesFromConsole(); | ||
| Players players = ladderGameService.createPlayers(playerNames); | ||
|
|
||
| String gameResults = inputView.getGameResultFromConsole(); | ||
| Results results = ladderGameService.createResults(gameResults, players.size()); | ||
|
|
||
| int ladderHeightInput = inputView.getLadderHeightFromConsole(); | ||
| LadderHeight ladderHeight = ladderGameService.createLadderHeight(ladderHeightInput); | ||
|
|
||
| Ladder ladder = ladderGameService.createLadder(players, ladderHeight, new RandomLadderCreationStrategy()); | ||
|
|
||
| //LadderGame ladderGame = ladderGameService.createLadderGame(players, results, ladder); | ||
|
|
||
| outputView.printResult(players, ladder); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package laddergame.domain.ladder; | ||
|
|
||
| import laddergame.domain.ladder.strategy.LadderCreationStrategy; | ||
| import laddergame.domain.ladderheight.LadderHeight; | ||
| import laddergame.domain.player.Players; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Ladder { | ||
|
|
||
| private final List<LadderLine> lines; | ||
|
|
||
| private Ladder(final List<LadderLine> lines) { | ||
| this.lines = lines; | ||
| } | ||
|
|
||
| public static Ladder with(final Players players, final LadderHeight ladderHeight, LadderCreationStrategy strategy) { | ||
| List<LadderLine> lines = new ArrayList<>(); | ||
| int width = players.size(); | ||
| int height = ladderHeight.getHeight(); | ||
|
|
||
| for (int i = 0; i < height; i++) { | ||
| LadderLine line = LadderLine.with(width, strategy); | ||
| lines.add(line); | ||
| } | ||
|
|
||
| return new Ladder(lines); | ||
| } | ||
|
|
||
| public List<LadderLine> getLines() { | ||
| return lines; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package laddergame.domain.ladder; | ||
|
|
||
| import laddergame.domain.ladder.strategy.LadderCreationStrategy; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public class LadderLine { | ||
|
|
||
| public static final String POINT= "|"; | ||
| public static final String CONNECTION= "-----"; | ||
| public static final String NON_CONNECTION= " "; | ||
|
|
||
| private final List<Point> points; | ||
|
|
||
| public LadderLine(final List<Point> points) { | ||
| this.points = points; | ||
| } | ||
|
|
||
| public static LadderLine with(final int width, LadderCreationStrategy strategy) { | ||
| List<Point> points = new ArrayList<>(); | ||
| Point previousPoint = null; | ||
|
|
||
| for (int i = 0; i < width; i++) { | ||
| int remain = width - i; | ||
| Point point = generatePoint(previousPoint, remain, strategy); | ||
| points.add(point); | ||
| previousPoint = point; | ||
| } | ||
|
|
||
| return new LadderLine(points); | ||
| } | ||
|
|
||
| private static Point generatePoint(Point point, int remain, LadderCreationStrategy strategy) { | ||
| if (isFirstPoint(point)) { | ||
| return Point.generateFirstPoint(strategy); | ||
| } | ||
|
|
||
| if (isLastPoint(remain)) { | ||
| return Point.generateLastPoint(point); | ||
| } | ||
|
|
||
| return point.decideNextPoint(strategy); | ||
| } | ||
|
|
||
| private static boolean isFirstPoint(Point point) { | ||
| return point == null; | ||
| } | ||
|
|
||
| private static boolean isLastPoint(int remain) { | ||
| return remain == 1; | ||
| } | ||
|
|
||
| public int size() { | ||
| return points.size(); | ||
| } | ||
|
|
||
| public String lineToString() { | ||
| List<Point> points = getPoints(); | ||
|
|
||
| String result = " "; | ||
| for (Point point : points) { | ||
| String connectionString = point.connectionToString(); | ||
| result += (POINT + connectionString); | ||
| } | ||
| return result; | ||
| } | ||
|
Comment on lines
+59
to
+68
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. 저도 옛날에 비슷하게 구현했던 적이 있었는데요, |
||
|
|
||
| public List<Point> getPoints() { | ||
| return points; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| LadderLine that = (LadderLine) o; | ||
| return Objects.equals(points, that.points); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(points); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "LadderLine{" + | ||
| "points=" + points + | ||
| '}'; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package laddergame.domain.ladder; | ||
|
|
||
| import laddergame.domain.ladder.strategy.LadderCreationStrategy; | ||
|
|
||
| public enum Point { | ||
|
|
||
| LEFT(true, false), | ||
| RIGHT(false, true), | ||
| NONE(false, false); | ||
|
Comment on lines
+5
to
+9
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 boolean leftConnection; | ||
| private boolean rightConnection; | ||
|
|
||
| Point(boolean leftConnection, boolean rightConnection) { | ||
| this.leftConnection = leftConnection; | ||
| this.rightConnection = rightConnection; | ||
| } | ||
|
|
||
| public static Point generateFirstPoint(LadderCreationStrategy strategy) { | ||
| return generatePoint(false, strategy.isConnectable()); | ||
| } | ||
|
|
||
| public static Point generateLastPoint(Point point) { | ||
| if (point.hasRightConnection()) { | ||
| return generatePoint(true, false); | ||
| } | ||
| return generatePoint(false, false); | ||
|
Comment on lines
+24
to
+27
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. return point.hasRightConnection() ? LEFT : NONE;요렇게 하면 훨씬 깔끔하지 않을까요?ㅎㅎ |
||
| } | ||
|
|
||
| public Point decideNextPoint(LadderCreationStrategy strategy) { | ||
| if (rightConnection) { | ||
| return generatePoint(true, false ); | ||
| } | ||
| return generatePoint(false, strategy.isConnectable()); | ||
| } | ||
|
|
||
| private static Point generatePoint(boolean leftConnection, boolean rightConnection) { | ||
| if (leftConnection && rightConnection) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| if (leftConnection) { | ||
| return LEFT; | ||
| } | ||
|
|
||
| if (rightConnection) { | ||
| return RIGHT; | ||
| } | ||
|
|
||
| return NONE; | ||
| } | ||
|
|
||
| public boolean hasRightConnection() { | ||
| return rightConnection; | ||
| } | ||
|
|
||
| public String connectionToString() { | ||
| if (hasRightConnection()) { | ||
| return LadderLine.CONNECTION; | ||
| } | ||
| return LadderLine.NON_CONNECTION; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package laddergame.domain.ladder.strategy; | ||
|
|
||
| public interface LadderCreationStrategy { | ||
|
|
||
| boolean isConnectable(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package laddergame.domain.ladder.strategy; | ||
|
|
||
| public class ManualLadderCreationStrategy implements LadderCreationStrategy { | ||
|
|
||
| @Override | ||
| public boolean isConnectable() { | ||
| return true; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package laddergame.domain.ladder.strategy; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class RandomLadderCreationStrategy implements LadderCreationStrategy{ | ||
|
|
||
| @Override | ||
| public boolean isConnectable() { | ||
| return new Random().nextBoolean(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package laddergame.domain.ladderheight; | ||
|
|
||
| public class LadderHeight { | ||
|
|
||
| private static final int LADDER_HEIGHT_MIN_LENGTH = 1; | ||
|
|
||
| private final int height; | ||
|
|
||
| public LadderHeight(int height) { | ||
| if (!isLongerThanMinLength(height)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| this.height = height; | ||
| } | ||
|
|
||
| private boolean isLongerThanMinLength(int height) { | ||
| return height >= LADDER_HEIGHT_MIN_LENGTH; | ||
| } | ||
|
|
||
| public int getHeight() { | ||
| return height; | ||
| } | ||
| } |
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.
저번 모임때도 전체적으로 이야기가 잠깐 나왔던거같은데, null 초기화는 굉장히 위험합니다.
NPE(NullPointerException)가 나올 수 있는 가능성은 원천 배제하는 것이 좋습니다!
정말 필요한 상황이라면 어쩔 수 없지만, 정말 필요한 상황이 정말 있을지는 의문이네요