Skip to content
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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
adb3d07
feat : add basic code and test
hamhyeongju Sep 17, 2024
7c31908
move : move domain to ladder package
hamhyeongju Sep 19, 2024
0b34dd5
refactor : use Direction enum class instead of 1, -1, 0
hamhyeongju Sep 19, 2024
73cb92a
refactor : apply Direction
hamhyeongju Sep 19, 2024
8a8aa6a
refactor : static import Direction
hamhyeongju Sep 19, 2024
5eb6c3d
refactor : add GreaterThanOne for valid ladder constructor argument
hamhyeongju Sep 19, 2024
779ace3
refactor : apply GreaterThanOne
hamhyeongju Sep 19, 2024
c5caa08
refactor : add ExceptionMessage enum
hamhyeongju Sep 19, 2024
aff6206
refactor : apply ExceptionMessage
hamhyeongju Sep 19, 2024
beaf072
refactor : add Position
hamhyeongju Sep 20, 2024
d0e6790
refactor : apply Position
hamhyeongju Sep 20, 2024
8fa2890
refactor : add Node class for wrap int array value
hamhyeongju Sep 20, 2024
9b3a2d9
refactor : apply Node
hamhyeongju Sep 20, 2024
5ebaf3a
test : add test case for Node
hamhyeongju Sep 20, 2024
9f87691
refactor : extract LadderCreator class from Ladder
hamhyeongju Sep 20, 2024
0ea3ab6
refactor : extract LadderRunner class from Ladder
hamhyeongju Sep 20, 2024
7954a2b
refactor : apply LadderCreator and LadderRunner
hamhyeongju Sep 20, 2024
449a429
rename : from Ladder
hamhyeongju Sep 20, 2024
3e5115e
refactor : apply LadderCreator and LadderRunner
hamhyeongju Sep 20, 2024
79b8c4e
update : optimize code
hamhyeongju Sep 20, 2024
9d969b9
요구사항2 기능 구현 완료
jyun-KIM Sep 27, 2024
86e5be6
요구사항2 기능 구현 완료
jyun-KIM Sep 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions src/main/java/Ladder.java

This file was deleted.

16 changes: 16 additions & 0 deletions src/main/java/ladder/Direction.java
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;
}
}
20 changes: 20 additions & 0 deletions src/main/java/ladder/ExceptionMessage.java
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;
}
}
29 changes: 29 additions & 0 deletions src/main/java/ladder/GreaterThanOne.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ladder;

public class GreaterThanOne {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GreaterThanOne 클래스로 빼신 이유가 있을까요?


private final int number;

public GreaterThanOne(int number) {

Choose a reason for hiding this comment

The 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;
}
}
18 changes: 18 additions & 0 deletions src/main/java/ladder/LadderGame.java
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();
}
}
38 changes: 38 additions & 0 deletions src/main/java/ladder/LadderRunner.java
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");
}
}
}
56 changes: 56 additions & 0 deletions src/main/java/ladder/Node.java
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(' ');
}

}
47 changes: 47 additions & 0 deletions src/main/java/ladder/Position.java
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;
}
}
99 changes: 99 additions & 0 deletions src/main/java/ladder/Row.java
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);
}
}
}
25 changes: 25 additions & 0 deletions src/main/java/ladder/creator/LadderCreator.java
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()];

Choose a reason for hiding this comment

The 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;
}
}
5 changes: 0 additions & 5 deletions src/test/java/LadderTest.java

This file was deleted.

Loading