-
Notifications
You must be signed in to change notification settings - Fork 26
2주차 미션 / 서버 2조 황정안 #50
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
base: main
Are you sure you want to change the base?
Changes from all commits
adb3d07
7c31908
1b840fa
38feeda
869d21d
53eda87
c8438ea
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,76 @@ | ||
| public class Ladder { | ||
| Validation validation; | ||
| 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
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. 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; | ||
| } | ||
| } | ||
| 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
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. 이 검증 메서드를 따로 빼는건 어떨까요? |
||
| this.naturalNumber = naturalNumber; | ||
|
|
||
| } | ||
| public int getNaturalNumber() { | ||
| return naturalNumber; | ||
| } | ||
| } | ||
| 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(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } |
| 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; | ||
| } | ||
|
|
||
| } |
| 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); | ||
| } | ||
|
|
||
|
|
||
| } |
| 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; | ||
| } | ||
|
|
||
| } | ||
|
|
| 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; | ||
| } | ||
|
|
||
| } |
| 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; | ||
| } | ||
| } |
| 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; | ||
| } | ||
|
|
||
| } |
| 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; | ||
| } | ||
| } |
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.
Validation은 private으로 접근 제어를 안하신 이유가 궁금하네요!