-
Notifications
You must be signed in to change notification settings - Fork 26
2주차 미션/ 서버 4조 이정연 #35
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: 1week-completed
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ladder; | ||
|
|
||
| import ladder.creator.LadderAutoCreator; | ||
| import ladder.creator.LadderCreatorImpl; | ||
|
|
||
|
|
||
| public class LadderGameFactory { | ||
|
|
||
| public static LadderGame createLadderGame(GreaterThanOne row, GreaterThanOne numberOfPerson) { | ||
| return new LadderGame(new LadderCreatorImpl(row, numberOfPerson)); | ||
| } | ||
|
|
||
| public static LadderGame createRandomLadderGame(GreaterThanOne row, GreaterThanOne numberOfPerson) { | ||
| return new LadderGame(new LadderAutoCreator(row, numberOfPerson)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package ladder; | ||
|
|
||
| public class LadderPosition { | ||
| Position x; | ||
| Position y; | ||
|
|
||
| public LadderPosition(Position x, Position y) { | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
|
|
||
| public Position getX() { | ||
| return x; | ||
| } | ||
|
|
||
| public Position getY() { | ||
| return y; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,8 +10,29 @@ public LadderRunner(Row[] rows) { | |
|
|
||
| public int run(Position position) { | ||
| for (int i = 0; i < rows.length; i++) { | ||
| LadderPosition ladderPosition = new LadderPosition(Position.from(i), position); | ||
|
|
||
| System.out.println("Before"); | ||
| printLadder(ladderPosition); | ||
|
|
||
| rows[i].nextPosition(position); | ||
|
|
||
| System.out.println("After"); | ||
| printLadder(ladderPosition); | ||
|
|
||
| } | ||
| return position.getValue(); | ||
| } | ||
|
|
||
| private void printLadder(LadderPosition ladderPosition){ | ||
| StringBuilder sb = new StringBuilder(); | ||
| for(int i = 0; i < rows.length; i++){ | ||
| if( i == ladderPosition.getX().getValue()){ | ||
|
Member
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. LadderPosition에서 getX로 값을 가져와서 비교하는건 객체 지향적이지 못하다는 생각이 듭니다! |
||
| sb.append(rows[i].convertPositionRowToString(ladderPosition.getY())).append("\n"); | ||
| continue; | ||
| } | ||
| sb.append(rows[i].convertRowtoString()).append("\n"); | ||
| } | ||
| System.out.println(sb); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package ladder; | ||
|
|
||
| public class LadderSize { | ||
|
Member
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. LadderSize 클래스가 사용되고 있지 않은 것 같은데 기분탓일까요..? |
||
| GreaterThanOne numberOfRow; | ||
| GreaterThanOne numberOfPerson; | ||
|
|
||
| public LadderSize(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) { | ||
| this.numberOfRow = numberOfRow; | ||
| this.numberOfPerson = numberOfPerson; | ||
| } | ||
|
|
||
| public GreaterThanOne getNumberOfRow() { | ||
| return numberOfRow; | ||
| } | ||
|
|
||
| public GreaterThanOne getNumberOfPerson() { | ||
| return numberOfPerson; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ public void move(Position position) { | |
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public void setRightNode() { | ||
| direction = RIGHT; | ||
| } | ||
|
|
@@ -48,4 +50,8 @@ private boolean isLeft() { | |
| private boolean isNone() { | ||
| return direction == NONE; | ||
| } | ||
|
|
||
| public Direction getDirection() { | ||
| return direction; | ||
|
Member
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. 노드의 값을 외부로 노출시키지 않고 노드 내부에서 처리할 수 있도록 바꾸는게 좋을 것 같습니다! |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,9 +12,32 @@ public Row(GreaterThanOne numberOfPerson) { | |
| } | ||
| } | ||
|
|
||
| public int getNodeLength(){ | ||
| return nodes.length; | ||
| } | ||
| public StringBuilder convertRowtoString(){ | ||
| StringBuilder sb = new StringBuilder(); | ||
| for(int i = 0 ; i < nodes.length ; i++){ | ||
| sb.append(nodes[i].getDirection().getValue()).append(" "); | ||
| } | ||
| return sb; | ||
| } | ||
|
|
||
| public StringBuilder convertPositionRowToString(Position y){ | ||
|
Member
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. convertRowtoString() 와 convertPositionRowToString() 는 중복되는 부분이 있는 것 같습니다. 하나의 메서드를 사용하되, *을 append 하는 부분은 메서드로 따로 추출하여 사용하면 해당 코드를 사용하는 입장에서도 사용 방법에 대해 명확히 이해할 수 있을 것 같습니다. |
||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| for (int i = 0; i < nodes.length; i++) { | ||
| if(i == y.getValue()) { | ||
| sb.append(nodes[i].getDirection().getValue()).append("* "); | ||
| continue; | ||
| } | ||
| sb.append(nodes[i].getDirection().getValue()).append(" "); | ||
| } | ||
| return sb; | ||
| } | ||
|
|
||
| public void drawLine(Position startPosition) { | ||
| validateDrawLinePosition(startPosition); | ||
|
|
||
| setDirectionBetweenNextPosition(startPosition); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package ladder.creator; | ||
|
|
||
| import ladder.GreaterThanOne; | ||
| import ladder.LadderPosition; | ||
| import ladder.Position; | ||
| import ladder.Row; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Random; | ||
| import java.util.Set; | ||
|
|
||
| public class LadderAutoCreator implements LadderCreator { | ||
| private Row[] rows; | ||
|
|
||
| public LadderAutoCreator(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) { | ||
| rows = new Row[numberOfRow.getNumber()]; | ||
| for (int i = 0; i < numberOfRow.getNumber(); i++) { | ||
| rows[i] = new Row(numberOfPerson); | ||
| } | ||
| } | ||
| public Row[] getRows() {return rows;} | ||
|
|
||
| public void drawLine(Position row, Position col){ | ||
| Set<LadderPosition> lines = new HashSet<>(); | ||
|
Member
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. Set 같은 컬렉션에 객체를 저장하여 활용하려면 LadderPosition 에서 equals, hashCode 메서드를 오버라이드해야 중복 검증을 제대로 할 수 있습니다. |
||
|
|
||
| int numberOfLines = (int)(rows.length * rows[0].getNodeLength() * 0.3) - 1; | ||
|
Member
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. LadderSize 클래스를 활용해서 해당 객체를 구성하면 코드를 직관적으로 만들 수 있지 않을까요? |
||
|
|
||
| while(lines.size() < numberOfLines){ | ||
| row = getRandomRow(); | ||
| col = getRandomCol(); | ||
| LadderPosition ladderPosition = new LadderPosition(row, col); | ||
| if(!canDrawLine(row,col)){ | ||
| continue; | ||
| } | ||
| lines.add(ladderPosition); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| private Position getRandomCol() { | ||
| Random r = new Random(); | ||
| Position col; | ||
| col = Position.from(r.nextInt(rows[0].getNodeLength() - 1)); | ||
| return col; | ||
| } | ||
|
|
||
| private Position getRandomRow() { | ||
| Random r = new Random(); | ||
| Position row; | ||
| row = Position.from(r.nextInt(rows.length - 1)); | ||
| return row; | ||
| } | ||
|
|
||
| private boolean canDrawLine(Position row, Position col) { | ||
| try { | ||
| rows[row.getValue()].drawLine(col); | ||
| } catch (IllegalArgumentException e) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,12 @@ | ||
| package ladder.creator; | ||
|
|
||
| import ladder.GreaterThanOne; | ||
| import ladder.Position; | ||
| import ladder.Row; | ||
|
|
||
| public class LadderCreator { | ||
| public interface LadderCreator { | ||
|
|
||
| private final Row[] rows; | ||
| void drawLine(Position row, Position col); | ||
|
|
||
| public LadderCreator(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) { | ||
| rows = new Row[numberOfRow.getNumber()]; | ||
| for (int i = 0; i < numberOfRow.getNumber(); i++) { | ||
| rows[i] = new Row(numberOfPerson); | ||
| } | ||
| } | ||
| Row[] getRows(); | ||
|
|
||
| public void drawLine(Position row, Position col) { | ||
| rows[row.getValue()].drawLine(col); | ||
| } | ||
|
|
||
| public Row[] getRows() { | ||
| return rows; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package ladder.creator; | ||
|
|
||
| import ladder.GreaterThanOne; | ||
| import ladder.Position; | ||
| import ladder.Row; | ||
|
|
||
| public class LadderCreatorImpl implements LadderCreator { | ||
| private Row[] rows; | ||
|
|
||
| public LadderCreatorImpl(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) { | ||
| rows = new Row[numberOfRow.getNumber()]; | ||
| for (int i = 0; i < numberOfRow.getNumber(); i++) { | ||
| rows[i] = new Row(numberOfPerson); | ||
| } | ||
| } | ||
|
|
||
| public void drawLine(Position row, Position col) { | ||
| rows[row.getValue()].drawLine(col); | ||
| } | ||
|
|
||
| public Row[] getRows() { | ||
| return rows; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package ladder; | ||
|
|
||
| import ladder.creator.LadderAutoCreator; | ||
| import ladder.creator.LadderCreator; | ||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| public class RandomLadderTest { | ||
| GreaterThanOne numberOfPerson; | ||
| GreaterThanOne row; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| numberOfPerson = GreaterThanOne.from(4); | ||
| row = GreaterThanOne.from(3); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void 사다리_자동_생성_결과_확인(){ | ||
| //given | ||
|
|
||
| LadderGame ladderGame = LadderGameFactory.createRandomLadderGame(numberOfPerson,row); | ||
| LadderCreator ladderCreator = ladderGame.getLadderCreator(); | ||
| ladderCreator.drawLine(Position.from(0),Position.from(0)); | ||
|
|
||
|
|
||
| //when | ||
| ladderGame.run(Position.from(0)); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| void 사다리_일반_생성_결과_확인(){ | ||
| //given | ||
|
|
||
| LadderGame ladderGame = LadderGameFactory.createLadderGame(numberOfPerson,row); | ||
| LadderCreator ladderCreator = ladderGame.getLadderCreator(); | ||
| ladderCreator.drawLine(Position.from(0),Position.from(0)); | ||
|
|
||
| //when | ||
| ladderGame.run(Position.from(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.
Before와 After 로직을 enum으로 분리해 보시는게 어떨까요?