-
Notifications
You must be signed in to change notification settings - Fork 26
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
2주차 미션 / 서버 1조 김지현 #49
base: main
Are you sure you want to change the base?
Changes from all commits
adb3d07
7c31908
0b34dd5
73cb92a
8a8aa6a
5eb6c3d
779ace3
c5caa08
aff6206
beaf072
d0e6790
8fa2890
9b3a2d9
5ebaf3a
9f87691
0ea3ab6
7954a2b
449a429
3e5115e
79b8c4e
9d969b9
86e5be6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,29 @@ | ||
package ladder; | ||
|
||
public class GreaterThanOne { | ||
|
||
private final int number; | ||
|
||
public GreaterThanOne(int number) { | ||
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. 정적 팩터리 메서드인 from()으로 객체를 반환하니 생성자는 private으로 설정해도 좋아보이네요! |
||
validate(number); | ||
this.number = number; | ||
} | ||
|
||
public int getNumber() { | ||
return number; | ||
} | ||
|
||
public static GreaterThanOne from(int number) { | ||
return new GreaterThanOne(number); | ||
} | ||
|
||
private void validate(int number) { | ||
if (!isGreaterThanOne(number)) { | ||
throw new IllegalArgumentException(ExceptionMessage.INVALID_LADDER_NUMBER.getMessage()); | ||
} | ||
} | ||
|
||
private static boolean isGreaterThanOne(int number) { | ||
return number > 1; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ladder; | ||
|
||
import ladder.creator.LadderCreator; | ||
|
||
public class LadderGame { | ||
|
||
private final LadderCreator ladderCreator; | ||
|
||
public LadderGame(LadderCreator ladderCreator) { | ||
this.ladderCreator = ladderCreator; | ||
} | ||
|
||
public int run(Position position) { | ||
LadderRunner ladderRunner = new LadderRunner(ladderCreator.getRows()); | ||
ladderRunner.run(position); | ||
return position.getValue(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ladder; | ||
|
||
public class LadderRunner { | ||
|
||
private final Row[] rows; | ||
|
||
public LadderRunner(Row[] rows) { | ||
this.rows = rows; | ||
} | ||
|
||
public int run(Position position) { | ||
for (int i = 0; i < rows.length; i++) { | ||
|
||
StringBuilder sbBefore = new StringBuilder("Before\n"); | ||
printLadder(sbBefore, GreaterThanOne.from(rows.length), position, i); | ||
System.out.println(sbBefore); | ||
|
||
rows[i].nextPosition(position); | ||
|
||
StringBuilder sbAfter = new StringBuilder("After\n"); | ||
printLadder(sbAfter, GreaterThanOne.from(rows.length), position, i); | ||
System.out.println(sbAfter); | ||
} | ||
|
||
return position.getValue(); | ||
} | ||
|
||
private void printLadder(StringBuilder sb, GreaterThanOne numberOfRows, Position position, int currentRowIndex) { | ||
for (int rowIndex = 0; rowIndex < rows.length; rowIndex++) { | ||
if (rowIndex == currentRowIndex) { | ||
rows[rowIndex].printCurrentRow(sb, numberOfRows, position); | ||
} else { | ||
rows[rowIndex].printRow(sb, numberOfRows); | ||
} | ||
sb.append("\n"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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 void move(Position position) { | ||
if (isRight()) { | ||
position.prev(); | ||
return; | ||
} | ||
if (isLeft()) { | ||
position.next(); | ||
} | ||
} | ||
|
||
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; | ||
} | ||
|
||
public void printDirection(StringBuilder sb) { | ||
sb.append(direction.getValue()); | ||
//sb.append(' '); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package ladder; | ||
|
||
import static ladder.ExceptionMessage.INVALID_LADDER_POSITION; | ||
|
||
public class Position { | ||
private int position; | ||
|
||
private Position(int position) { | ||
this.position = position; | ||
} | ||
|
||
public int getValue() { | ||
return position; | ||
} | ||
|
||
public static Position from(int position) { | ||
validatePosition(position); | ||
return new Position(position); | ||
} | ||
|
||
public void prev() { | ||
position--; | ||
} | ||
|
||
public Position next() { | ||
position++; | ||
return null; | ||
} | ||
|
||
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package ladder; | ||
|
||
import static ladder.Direction.*; | ||
|
||
public class Row { | ||
public Node[] getNodes() { | ||
return nodes; | ||
} | ||
|
||
private final Node[] nodes; | ||
|
||
public Row(GreaterThanOne numberOfPerson) { | ||
nodes = new Node[numberOfPerson.getNumber()]; | ||
for (int i = 0; i < numberOfPerson.getNumber(); i++) { | ||
nodes[i] = Node.from(NONE); | ||
} | ||
} | ||
|
||
public void drawLine(Position startPosition) { | ||
validateDrawLinePosition(startPosition); | ||
|
||
setDirectionBetweenNextPosition(startPosition); | ||
} | ||
|
||
private void setDirectionBetweenNextPosition(Position position) { | ||
nodes[position.getValue()].setRightNode(); | ||
position.next(); | ||
nodes[position.getValue()].setLeftNode(); | ||
} | ||
|
||
public void printLine(Position position) { | ||
validateDrawLinePosition(position); | ||
|
||
setLineDirection(position, LEFT); | ||
position.next(); | ||
setLineDirection(position, RIGHT); | ||
} | ||
|
||
|
||
private void setLineDirection(Position position, Direction direction) { | ||
nodes[position.getValue()] = Node.from(direction); | ||
} | ||
|
||
public void nextPosition(Position position) { | ||
validatePosition(position); | ||
|
||
nodes[position.getValue()].move(position); | ||
} | ||
|
||
// private void setDirectionBetweenNextPosition(Position position) { | ||
// nodes[position.getValue()].setRightNode(); | ||
// position.next(); | ||
// nodes[position.getValue()].setLeftNode(); | ||
// } | ||
|
||
|
||
private void validatePosition(Position position) { | ||
if (isInvalidPosition(position)) { | ||
throw new IllegalArgumentException(ExceptionMessage.INVALID_POSITION.getMessage()); | ||
} | ||
} | ||
|
||
private void validateDrawLinePosition(Position startPosition) { | ||
validatePosition(startPosition); | ||
if (isLineAtPosition(startPosition) || isLineAtNextPosition(startPosition)) { | ||
throw new IllegalArgumentException(ExceptionMessage.INVALID_DRAW_POSITION.getMessage()); | ||
} | ||
} | ||
|
||
private boolean isInvalidPosition(Position position) { | ||
return position.isBiggerThan(nodes.length - 1); | ||
} | ||
|
||
private boolean isLineAtPosition(Position position) { | ||
return nodes[position.getValue()].isAlreadySetDirection(); | ||
} | ||
|
||
private boolean isLineAtNextPosition(Position position) { | ||
position.next(); | ||
boolean lineAtPosition = isLineAtPosition(position); | ||
position.prev(); | ||
return lineAtPosition; | ||
} | ||
|
||
public void printCurrentRow(StringBuilder sb, GreaterThanOne numberOfRow, Position position) { | ||
for (int i = 0; i < numberOfRow.getNumber(); i++) { | ||
nodes[i].printDirection(sb); | ||
if (position.getValue() == i) { | ||
sb.append('*'); | ||
} | ||
} | ||
} | ||
|
||
public void printRow(StringBuilder sb, GreaterThanOne numberOfRow) { | ||
for (int i = 0; i < numberOfRow.getNumber(); i++) { | ||
nodes[i].printDirection(sb); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ladder.creator; | ||
|
||
import ladder.GreaterThanOne; | ||
import ladder.Position; | ||
import ladder.Row; | ||
|
||
public class LadderCreator { | ||
|
||
private final Row[] rows; | ||
|
||
public LadderCreator(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) { | ||
rows = new Row[numberOfRow.getNumber()]; | ||
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. 로우를 불러 올때 생성자보다 따로 매개변수를 가진 정적 팩터리 메서드를 생성하여 불러오는게 좋겠네요! |
||
for (int i = 0; i < numberOfRow.getNumber(); i++) { | ||
rows[i] = new Row(numberOfPerson); | ||
} | ||
} | ||
|
||
public void drawLine(Position row, Position col) { | ||
rows[row.getValue()].printLine(col); | ||
} | ||
|
||
public Row[] getRows() { | ||
return rows; | ||
} | ||
} |
This file was deleted.
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.
GreaterThanOne 클래스로 빼신 이유가 있을까요?