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
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;
}
}
24 changes: 24 additions & 0 deletions src/main/java/ladder/GreaterThanOne.java
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;
}
}
26 changes: 26 additions & 0 deletions src/main/java/ladder/LadderGame.java
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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/ladder/LadderGameFactory.java
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);
}
}
23 changes: 23 additions & 0 deletions src/main/java/ladder/LadderPosition.java
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;
}
}
30 changes: 30 additions & 0 deletions src/main/java/ladder/LadderRunner.java
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)){
Copy link
Member

Choose a reason for hiding this comment

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

LadderSize 를 기반으로 반복문을 제어하는게 가독성이 좋을 것 같습니다! fori문을 사용하면 인덱스의 변화에 따른 코드 실행이 눈에 더 잘 들어올 것 같아요.

moveTurnPrinting(ladderPosition);
}
Position lastColPosition = ladderPosition.getColPos();
return lastColPosition.getValue();
}

private void moveTurnPrinting(LadderPosition ladderPosition){
Copy link
Member

Choose a reason for hiding this comment

The 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();
}
}
23 changes: 23 additions & 0 deletions src/main/java/ladder/LadderSize.java
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;
}
}
62 changes: 62 additions & 0 deletions src/main/java/ladder/LadderWrapper.java
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;
Copy link
Member

Choose a reason for hiding this comment

The 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; }
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
55 changes: 55 additions & 0 deletions src/main/java/ladder/Node.java
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();
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
54 changes: 54 additions & 0 deletions src/main/java/ladder/Position.java
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;
}
}
Loading