-
Notifications
You must be signed in to change notification settings - Fork 26
2주차 미션/ 서버 2조 임제형 #36
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: main
Are you sure you want to change the base?
Changes from all commits
adb3d07
7c31908
1b840fa
ecb3b54
b7a4b2e
8c02b54
d82a8dd
67b8f56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ladder; | ||
|
|
||
| public enum Direction { | ||
|
|
||
| LEFT(-1), RIGHT(1), NONE(0); | ||
|
|
||
| private final int value; | ||
|
|
||
| Direction(int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return value; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package ladder; | ||
|
|
||
| public enum ExceptionMessage { | ||
|
|
||
| INVALID_LADDER_POSITION("사다리 위치는 1이상 자연수입니다."), | ||
| INVALID_LADDER_NUMBER("사다리의 행과 열은 2 이상이어야 합니다."), | ||
| INVALID_POSITION("유효하지 않은 위치입니다."), | ||
| INVALID_DRAW_POSITION("사다리를 그릴 수 없는 위치입니다."), | ||
| INVALID_NATURAL_NUMBER("자연수가 아닙니다."); | ||
|
|
||
| private final String message; | ||
|
|
||
| ExceptionMessage(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package ladder; | ||
|
|
||
| public class GreaterThanOne { | ||
| private final int number; | ||
|
|
||
| private GreaterThanOne(int number) { | ||
| this.number = number; | ||
| } | ||
|
|
||
| public static GreaterThanOne from(int number){ | ||
| if(!isGreaterThanOne(number)){ | ||
| throw new IllegalArgumentException(ExceptionMessage.INVALID_LADDER_NUMBER.getMessage()); | ||
| } | ||
| return new GreaterThanOne(number); | ||
| } | ||
|
|
||
| private static boolean isGreaterThanOne(int number) { | ||
| return number > 1; | ||
| } | ||
|
|
||
| public int getNumber() { | ||
| return number; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ladder; | ||
|
|
||
| import ladder.creator.LadderCreator; | ||
|
|
||
| public class LadderGame { | ||
|
|
||
| private final LadderCreator ladderCreator; | ||
|
|
||
| private LadderGame(LadderCreator ladderCreator) { | ||
| this.ladderCreator = ladderCreator; | ||
| } | ||
|
|
||
| public static LadderGame from(LadderCreator ladderCreator){ | ||
| return new LadderGame(ladderCreator); | ||
| } | ||
|
|
||
| public int run(Position position) { | ||
| LadderRunner ladderRunner = LadderRunner.from(ladderCreator.getLadderWrapper()); | ||
| ladderRunner.run(position); | ||
| return position.getValue(); | ||
| } | ||
|
|
||
| public LadderCreator getLadderCreator() { | ||
| return ladderCreator; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package ladder; | ||
|
|
||
| import ladder.creator.CustomLadderCreator; | ||
| import ladder.creator.LadderCreator; | ||
| import ladder.creator.RandomLadderCreator; | ||
|
|
||
| public class LadderGameFactory { | ||
| public static LadderGame createRandomLadderGame(LadderSize ladderSize){ | ||
| LadderCreator randomLadderCreator = RandomLadderCreator.from(ladderSize); | ||
| return LadderGame.from(randomLadderCreator); | ||
| } | ||
|
|
||
| public static LadderGame createCustomLadderGame(LadderSize ladderSize){ | ||
| LadderCreator customLadderCreator = CustomLadderCreator.from(ladderSize); | ||
| return LadderGame.from(customLadderCreator); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package ladder; | ||
|
|
||
| public class LadderPosition { | ||
| private Position rowPos; | ||
| private Position colPos; | ||
|
|
||
| private LadderPosition(Position rowPos, Position colPos) { | ||
| this.rowPos = rowPos; | ||
| this.colPos = colPos; | ||
| } | ||
|
|
||
| public static LadderPosition of(Position rowPos, Position colPos){ | ||
| return new LadderPosition(rowPos, colPos); | ||
| } | ||
|
|
||
| public Position getRowPos() { | ||
| return rowPos; | ||
| } | ||
|
|
||
| public Position getColPos() { | ||
| return colPos; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package ladder; | ||
|
|
||
| public class LadderRunner { | ||
| LadderWrapper ladderWrapper; | ||
|
|
||
| private LadderRunner(LadderWrapper ladderWrapper) { | ||
| this.ladderWrapper = ladderWrapper; | ||
| } | ||
|
|
||
| public static LadderRunner from(LadderWrapper ladderWrapper) { | ||
| return new LadderRunner(ladderWrapper); | ||
| } | ||
|
|
||
| public int run(Position position) { | ||
| LadderPosition ladderPosition = LadderPosition.of(Position.from(0), position); | ||
| while(!ladderWrapper.isLadderPositionAtLastRow(ladderPosition)){ | ||
| moveTurnPrinting(ladderPosition); | ||
| } | ||
| Position lastColPosition = ladderPosition.getColPos(); | ||
| return lastColPosition.getValue(); | ||
| } | ||
|
|
||
| private void moveTurnPrinting(LadderPosition ladderPosition){ | ||
|
Member
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. move와 print는 각각의 기능으로 봐야할 것 같아요. 메서드가 여러 일을 하고 있는 것 같습니다! |
||
| ladderWrapper.printRowsWithLabel(ladderPosition, "Before"); | ||
| ladderWrapper.changeLadderPositionHorizontally(ladderPosition); | ||
| ladderWrapper.printRowsWithLabel(ladderPosition, "After"); | ||
| ladderWrapper.changeLadderPositionVertically(ladderPosition); | ||
| System.out.println(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package ladder; | ||
|
|
||
| public class LadderSize { | ||
| private GreaterThanOne numberOfRow; | ||
| private GreaterThanOne numberOfPerson; | ||
|
|
||
| private LadderSize(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson){ | ||
| this.numberOfRow = numberOfRow; | ||
| this.numberOfPerson = numberOfPerson; | ||
| } | ||
|
|
||
| public static LadderSize of(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson){ | ||
| return new LadderSize(numberOfRow, numberOfPerson); | ||
| } | ||
|
|
||
| public GreaterThanOne getNumberOfRow() { | ||
| return numberOfRow; | ||
| } | ||
|
|
||
| public GreaterThanOne getNumberOfPerson() { | ||
| return numberOfPerson; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package ladder; | ||
|
|
||
| public class LadderWrapper { | ||
| private final Row[] rows; | ||
|
|
||
| private LadderWrapper(Row[] rows) { | ||
| this.rows = rows; | ||
| } | ||
|
|
||
| public static LadderWrapper from(Row[] rows){ | ||
| return new LadderWrapper(rows); | ||
| } | ||
|
|
||
| public void changeLadderPositionHorizontally(LadderPosition ladderPosition){ | ||
| Position rowPosition = ladderPosition.getRowPos(); | ||
| rows[rowPosition.getValue()].nextPosition(ladderPosition.getColPos()); | ||
| } | ||
|
|
||
| public void changeLadderPositionVertically(LadderPosition ladderPosition){ | ||
| Position rowPosition = ladderPosition.getRowPos(); | ||
| rowPosition.next(); | ||
| } | ||
|
|
||
| public boolean isLadderPositionAtLastRow(LadderPosition ladderPosition){ | ||
| Position rowPosition = ladderPosition.getRowPos(); | ||
| return rowPosition.getValue() == rows.length; | ||
|
Member
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. getValue로 비교하기보단 Position 내부 메서드로 처리하는게 어떨까요? |
||
| } | ||
|
|
||
| public void printRowsWithLabel(LadderPosition ladderPosition, String label){ | ||
| System.out.println(label); | ||
| printRows(ladderPosition); | ||
| } | ||
|
|
||
| private void printRows(LadderPosition ladderPosition){ | ||
| Position nowRowPosition = ladderPosition.getRowPos(); | ||
| for(int rowIndex=0; rowIndex<rows.length; rowIndex++){ | ||
| if(rowIndex != nowRowPosition.getValue()) { System.out.println(rows[rowIndex].buildRowString()); continue; } | ||
|
Member
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. 반복문과 조건문이 겹쳐서 이중 들여쓰기가 되었습니다! 메서드 추출이 필요할 것 같아요! |
||
| System.out.println(rows[rowIndex].buildStarRowString(ladderPosition.getColPos())); | ||
| } | ||
| } | ||
|
|
||
| public void drawLine(LadderPosition ladderPosition){ | ||
| Position rowPos = ladderPosition.getRowPos(); | ||
| Position colPos = ladderPosition.getColPos(); | ||
| rows[rowPos.getValue()].drawLine(colPos); | ||
| } | ||
|
|
||
| public int getRowsSize(){ | ||
| return rows.length; | ||
| } | ||
|
|
||
| public int getColsSize(){ | ||
| return rows[0].getNodesSize(); | ||
| } | ||
|
|
||
| public int getLadderValue(LadderPosition ladderPosition){ | ||
| Position rowPos = ladderPosition.getRowPos(); | ||
| Position colPos = ladderPosition.getColPos(); | ||
|
|
||
| return rows[rowPos.getValue()].getRowValue(colPos); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package ladder; | ||
|
|
||
| import static ladder.Direction.*; | ||
|
|
||
| public class Node { | ||
|
|
||
| private Direction direction; | ||
|
|
||
| private Node(Direction direction) { | ||
| this.direction = direction; | ||
| } | ||
|
|
||
| public static Node from(Direction direction) { | ||
| return new Node(direction); | ||
| } | ||
|
|
||
| public int getDirectionValue(){ | ||
| return direction.getValue(); | ||
|
Member
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. Node 의 값을 외부로 노출하는 건 캡슐화의 위반이죠. 코드 전반적으로 의미없는 getter가 많이 보이는데 더 신경을 쓰시면 좋을 것 같습니다. |
||
| } | ||
|
|
||
| public void move(Position position) { | ||
| if (isRight()) { | ||
| position.next(); | ||
| return; | ||
| } | ||
| if (isLeft()) { | ||
| position.prev(); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| public void setRightNode() { | ||
| direction = RIGHT; | ||
| } | ||
|
|
||
| public void setLeftNode() { | ||
| direction = LEFT; | ||
| } | ||
|
|
||
| public boolean isAlreadySetDirection() { | ||
| return !isNone(); | ||
| } | ||
|
|
||
| private boolean isRight() { | ||
| return direction == RIGHT; | ||
| } | ||
|
|
||
| private boolean isLeft() { | ||
| return direction == LEFT; | ||
| } | ||
|
|
||
| private boolean isNone() { | ||
| return direction == NONE; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package ladder; | ||
|
|
||
| import static ladder.ExceptionMessage.INVALID_LADDER_POSITION; | ||
|
|
||
| public class Position { | ||
| private int position; | ||
|
|
||
| private Position(int position) { | ||
| this.position = position; | ||
| } | ||
|
|
||
| public static Position from(int position) { | ||
| validatePosition(position); | ||
| return new Position(position); | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return position; | ||
| } | ||
|
|
||
| public void prev() { | ||
| position--; | ||
| } | ||
|
|
||
| public void next() { | ||
| position++; | ||
| } | ||
|
|
||
| public Position prevPosition(){ | ||
| return Position.from(position-1); | ||
| } | ||
|
|
||
| public Position nextPosition(){ | ||
| return Position.from(position+1); | ||
| } | ||
|
|
||
| public boolean isSmallerThan(int position) { | ||
| return this.position < position; | ||
| } | ||
|
|
||
| public boolean isBiggerThan(int position) { | ||
| return this.position > position; | ||
| } | ||
|
|
||
| private static void validatePosition(int position) { | ||
| if (!isPosition(position)) { | ||
| throw new IllegalArgumentException(INVALID_LADDER_POSITION.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private static boolean isPosition(int position) { | ||
| return position >= 0; | ||
| } | ||
| } |
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.
LadderSize 를 기반으로 반복문을 제어하는게 가독성이 좋을 것 같습니다! fori문을 사용하면 인덱스의 변화에 따른 코드 실행이 눈에 더 잘 들어올 것 같아요.