Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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.

4 changes: 3 additions & 1 deletion .idea/gradle.xml

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.

15 changes: 11 additions & 4 deletions src/main/java/ladder/LadderGame.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package ladder;

import ladder.creator.LadderCreator;
import ladder.creator.LadderCreatorIF;

public class LadderGame {

private final LadderCreator ladderCreator;
//private final LadderCreator ladderCreator;
private final LadderCreatorIF ladderCreator;

Choose a reason for hiding this comment

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

LadderCreatorIF 클래스를 만드신 이유가 있을까요?


public LadderGame(LadderCreator ladderCreator) {

public LadderGame(LadderCreatorIF ladderCreator) {
this.ladderCreator = ladderCreator;
}

public int run(Position position) {
public static LadderGame of(LadderCreator ladderCreator) {
return new LadderGame(ladderCreator);
}

public int run(LadderPosition position) {
LadderRunner ladderRunner = new LadderRunner(ladderCreator.getRows());
ladderRunner.run(position);
return position.getValue();
return position.getX();
}
}
46 changes: 46 additions & 0 deletions src/main/java/ladder/LadderPosition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ladder;

import static ladder.ExceptionMessage.INVALID_LADDER_POSITION;

public class LadderPosition {
private int x, y;

public LadderPosition(int x, int y) {

Choose a reason for hiding this comment

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

정적 팩터리 of로 반환하니 private으로 만들어 무분별한 생성자 접근을 막는게 좋아보여요!

this.x = x;
this.y = y;
}
public static LadderPosition of(int x, int y) {
validatePosition(x, y);
return new LadderPosition(x, y);
}
private static void validatePosition(int x, int y) {
if (x < 0 || y < 0) {
throw new IllegalArgumentException(ExceptionMessage.INVALID_LADDER_POSITION.getMessage());
}
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public boolean isCurrentPosition(int x, int y) {
return this.x == x && this.y == y;
}

@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
35 changes: 33 additions & 2 deletions src/main/java/ladder/LadderRunner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ladder;

import static javax.swing.RowFilter.ComparisonType.AFTER;

public class LadderRunner {

private final Row[] rows;
Expand All @@ -8,10 +10,39 @@ public LadderRunner(Row[] rows) {
this.rows = rows;
}

public int run(Position position) {
public int run(LadderPosition position) {
for (int i = 0; i < rows.length; i++) {

printLadder(position, "BEFORE");

Choose a reason for hiding this comment

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

BEFORE를 String으로 주는것보다, enum값을 추가하여 넣어주는게 나을것 같네요!


// 현재 행(Row)에서 좌우로 이동
rows[i].nextPosition(position);

// 사다리 진행 후 상태 출력 (AFTER)
printLadder(position, "AFTER");

// 행을 한 칸 아래로 이동
PositionMover.moveDown(position);
}
return position.getX();
}
//사다리 출력 메소드
private void printLadder(LadderPosition currentPosition, String state) {
//Row[] rows = ladderCreator.getRows();
System.out.println(state + ": 현재 위치는 (" + currentPosition.getX() + ", " + currentPosition.getY() + ") 입니다.");

for (int y = 0; y < rows.length; y++) {
StringBuilder rowStr = new StringBuilder();
for (int x = 0; x < rows[y].getNodes().length; x++) {
//현재 위치 *으로 표시
if (currentPosition.isCurrentPosition(x, y)) {
rowStr.append("* ");
} else {
rowStr.append(rows[y].getNodes()[x].toString().trim()).append(" ");
}
}
System.out.println(rowStr.toString().trim());

Choose a reason for hiding this comment

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

System.out.print보다 logback을 사용하여 로그 형태로 출력하는게 나을것 같네요!

}
return position.getValue();
System.out.println();
}
}
28 changes: 28 additions & 0 deletions src/main/java/ladder/LadderSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ladder;

import static ladder.ExceptionMessage.INVALID_LADDER_NUMBER;

public class LadderSize {
private final GreaterThanOne numberOfRows;
private final GreaterThanOne numberOfPersons;

public LadderSize(int numberOfRows, int numberOfPersons) {

Choose a reason for hiding this comment

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

이 생성자도 private으로 만들고, from을 통해 초기화한 후 객체를 반환하는게 어떨까요?

validate(numberOfRows, numberOfPersons);
this.numberOfRows = new GreaterThanOne(numberOfRows);
this.numberOfPersons = new GreaterThanOne (numberOfPersons);
}

public int getNumberOfRows() {

Choose a reason for hiding this comment

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

int로 반환하는것보다 GreaterThanOne 클래스로 반환하는것도 좋아보입니다!

return numberOfRows.getNumber();
}

public int getNumberOfPersons() {
return numberOfPersons.getNumber();
}
private void validate(int numberOfRows, int numberOfPersons) {
if (numberOfRows < 2 || numberOfPersons < 2) {

Choose a reason for hiding this comment

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

경계값을 그냥 2로 설정하는것보다 따로 상수로 빼서 관리하는것도 좋아보입니다!

throw new IllegalArgumentException(INVALID_LADDER_NUMBER.getMessage());
}
}

}
51 changes: 17 additions & 34 deletions src/main/java/ladder/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,30 @@
public class Node {

private Direction direction;

private Node(Direction direction) {
this.direction = direction;
public Node() {
this.direction = Direction.NONE;
}

public static Node from(Direction direction) {
return new Node(direction);
public void setDirection(Direction direction) {
this.direction = direction;
}

public void move(Position position) {
if (isRight()) {
position.next();
return;
public void move(LadderPosition position) {
switch (direction) {
case RIGHT:
position.setX(position.getX() + 1);
break;
case LEFT:
position.setX(position.getX() - 1);
break;
default:
break;
}
if (isLeft()) {
position.prev();
return;
}
}

public void setRightNode() {
direction = RIGHT;
}

public void setLeftNode() {
direction = LEFT;
public String toString() {
return direction == Direction.NONE ? "0 " : (direction == Direction.RIGHT ? "1 " : "-1 ");
}

public boolean isAlreadySetDirection() {
return !isNone();
}

private boolean isRight() {
return direction == RIGHT;
return direction != Direction.NONE;
}

private boolean isLeft() {
return direction == LEFT;
}

private boolean isNone() {
return direction == NONE;
}
}
46 changes: 0 additions & 46 deletions src/main/java/ladder/Position.java

This file was deleted.

23 changes: 23 additions & 0 deletions src/main/java/ladder/PositionMover.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ladder;

public class PositionMover {
public static void moveLeft(LadderPosition position) {
position.setX(position.getX() - 1);
}

public static void moveRight(LadderPosition position) {
position.setX(position.getX() + 1);
}

public static void moveDown(LadderPosition position) {
position.setY(position.getY() + 1);
}

public static boolean isSmallerThan(LadderPosition position, int x) {
return position.getX() < x;
}

public static boolean isBiggerThan(LadderPosition position, int x) {
return position.getX() > x;
}
}
Loading