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
2 changes: 1 addition & 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.

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

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

74 changes: 71 additions & 3 deletions src/main/java/Ladder.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,76 @@
public class Ladder {
Validation validation;
Copy link

Choose a reason for hiding this comment

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

Validation은 private으로 접근 제어를 안하신 이유가 궁금하네요!

private final int numberOfPerson;
private final int row;
private int[][] rows;
private final int RIGHT=1;
private final int LEFT=-1;
Comment on lines +6 to +7
Copy link

Choose a reason for hiding this comment

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

enum으로 따로 빼시면서 관리하는게 어떨까요?


private final int[][] rows;
//high 이 Y크기
public Ladder(NaturalNumber row, NaturalNumber numberOfPerson){
this.numberOfPerson=numberOfPerson.getNaturalNumber();
this.row=row.getNaturalNumber();
rows = new int[row.getNaturalNumber()][numberOfPerson.getNaturalNumber()];

public Ladder(int row, int numberOfPerson) {
rows = new int[row][numberOfPerson];
this.validation=new Validation(rows,row.getNaturalNumber(),numberOfPerson.getNaturalNumber());
}

public void drawLine(NaturalNumber x,NaturalNumber y) {
int coordinateX=x.getNaturalNumber();
int coordinateY=y.getNaturalNumber();
validation.rangeValidation(coordinateX,coordinateY);
//줄 그어져 있나 체크 해야함
validation.lineExistence(coordinateX,coordinateY);

//만약 조건 충족시
rows[coordinateX-1][coordinateY-1]=RIGHT;
rows[coordinateX-1][coordinateY]=LEFT;
}



public int run(NaturalNumber position) {
//배열은 0부터 시작이나 받는건 자연수 범위로 생각
int userPosition=position.getNaturalNumber();
validation.positionRange(userPosition);

for(int i=0;i<row;i++){
if (rows[i][userPosition-1] == 0) { continue; }
userPosition += rows[i][userPosition-1];
}
return userPosition;
}


//
// private void lineExistence(int x,int y) {
// if(coordinate[x-1][y-1]!=0 && coordinate[x-1][y]!=0){
// throw new IllegalArgumentException();
// }
// }
//
// private void positionRange(int userPosition) {
// if(userPosition>numberOfPerson){
// throw new IllegalArgumentException();
// }
// }
//
// private void rangeValidation(int x,int y) {
// if((y>row||x>numberOfPerson)){
// throw new IllegalArgumentException();
// }
// }



public int checkLine(int x,int y){
return rows[x-1][y-1];
}

public int checkRow(){
return rows.length;
}
public int checkNumberOfPerson(){
return rows[0].length;
}
}
18 changes: 18 additions & 0 deletions src/main/java/NaturalNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class NaturalNumber {
private int naturalNumber;
//alt+insert 하면 생성자 쉽게 가능
public NaturalNumber(int naturalNumber) {
if(naturalNumber<0){
try {
throw new IllegalAccessException("Invalid deposit amount");
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
Comment on lines +5 to +11
Copy link

Choose a reason for hiding this comment

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

이 검증 메서드를 따로 빼는건 어떨까요?

this.naturalNumber = naturalNumber;

}
public int getNaturalNumber() {
return naturalNumber;
}
}
30 changes: 30 additions & 0 deletions src/main/java/Validation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
public class Validation {
private int[][] rows;
int row;
int numberOfPerson;

public Validation(int[][] coordinate,int row, int numberOfPerson){
this.rows=coordinate;
this.row=row;
this.numberOfPerson=numberOfPerson;
}
public void lineExistence(int x,int y) {
if(rows[x-1][y-1]!=0 && rows[x-1][y]!=0){
throw new IllegalArgumentException();
}
}

public void positionRange(int userPosition) {
if(userPosition>numberOfPerson){
throw new IllegalArgumentException();
}
}

public void rangeValidation(int x,int y) {
if((y>row||x>numberOfPerson)){
throw new IllegalArgumentException();
}
}


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

import ladder.enumclass.ExceptionMessage;

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;
}

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

import ladder.creator.LadderCreator;
import ladder.creator.LadderUserCreator;

public class LadderGame {

private final LadderCreator ladderCreator;


// Interface로 넘겨서 하는 방법 좋은...?
public LadderGame(LadderCreator ladderCreator) {
this.ladderCreator = ladderCreator;
}
public int run(Position position) {
LadderRunner ladderRunner = new LadderRunner(ladderCreator.getRows());
return ladderRunner.run(position);
}


}
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 row;
private Position col;

private LadderPosition(Position row, Position col) {
this.row = row;
this.col = col;
}
public static LadderPosition from(Position row, Position col) {
return new LadderPosition(row, col);
}
public Position getRow() {
return row;
}
public Position getCol() {
return col;
}

}

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

import static ladder.enumclass.State.AFTER;
import static ladder.enumclass.State.BEFORE;

public class LadderRunner {

private final Row[] rows;

public LadderRunner(Row[] rows) {
this.rows = rows;
}
//before after enum
//random 따로 사다리 그리기 따로
//interface 밑에 추상 밑에 구형클래스 은근 많다 참고하기

public int run(Position position) {
for(int j=0;j<rows.length;j++){
// rows[j].validatePosition(position);

System.out.println(BEFORE.getState());

printLadder(LadderPosition.from(Position.from(j),position));

System.out.println(AFTER.getState());
rows[j].nextPosition(position);
printLadder(LadderPosition.from(Position.from(j),position));
}
return position.getPosition();
}

private void printLadder(LadderPosition getCoordinate){
for (int i = 0; i < rows.length; i++) {
System.out.println(checkStarLine(getCoordinate,i));
}
}
private String checkStarLine(LadderPosition getCoordinate,int i){
String ladder;

if(getCoordinate.getRow().getPosition()==i){
ladder =rows[i].printStarLine(getCoordinate);
}else{
ladder=rows[i].printLine();
}
return ladder;
}

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

public class LadderSize {

private GreaterThanOne numberOfPerson;
private GreaterThanOne numberOfRow;

public LadderSize(GreaterThanOne numberOfPerson, GreaterThanOne numberOfRow) {
this.numberOfPerson = numberOfPerson;
this.numberOfRow = numberOfRow;
}
public static LadderSize from(GreaterThanOne numberOfPerson, GreaterThanOne numberOfRow) {
return new LadderSize(numberOfPerson, numberOfRow);
}
public GreaterThanOne getNumberOfPerson(){
return numberOfPerson;
}

public GreaterThanOne getNumberOfRow(){
return numberOfRow;
}
}
52 changes: 52 additions & 0 deletions src/main/java/ladder/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ladder;

import ladder.enumclass.Direction;

import static ladder.enumclass.Direction.*;
import static ladder.enumclass.Direction.LEFT;

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 setRightNodes(Position position){
direction= RIGHT;
}
public void setLeftNodes(Position position){
direction= LEFT;
}
public void move(Position position) {
switch (this.direction) {
case RIGHT:
position.next();
break;
case LEFT:
position.prev();
break;
default:
break;
}
}


public boolean isAlreadySetDirection(){
return isNone();
}
public boolean isRight(){
return direction==RIGHT;
}

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


private boolean isNone() {
return direction == NONE;
}

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

import ladder.enumclass.ExceptionMessage;

public class Position {
private int position;

private Position(int position) {
this.position = position;
}
public int getPosition(){
return position;
}
public void next(){
position++;
}

public void prev(){
position--;
}

public static Position from(int position){
validatePosition(position);
return new Position(position);
}
public boolean isBiggerThan(int position){
return this.position > position;
}

public boolean isSmallerThan(int position){
return this.position < position;
}

private static void validatePosition(int position) {
if(!isPosition(position)){
throw new IllegalArgumentException(ExceptionMessage.INVALID_LADDER_POSITION.getMessage());
}
}

private static boolean isPosition(int position) {
return position >= 0;
}
}
Loading