-
Notifications
You must be signed in to change notification settings - Fork 26
2주차 미션 / 서버 4조 최재현 #40
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
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.
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,18 +1,18 @@ | ||
| package ladder; | ||
|
|
||
| import ladder.creator.LadderCreator; | ||
| import ladder.creator.LadderCreatorIF; | ||
|
|
||
| public class LadderGame { | ||
|
|
||
| private final LadderCreator ladderCreator; | ||
| private final LadderCreatorIF ladderCreatorIF; | ||
|
|
||
| public LadderGame(LadderCreator ladderCreator) { | ||
| this.ladderCreator = ladderCreator; | ||
| public LadderGame(LadderCreatorIF ladderCreatorIF) { | ||
| this.ladderCreatorIF = ladderCreatorIF; | ||
| } | ||
|
|
||
| public int run(Position position) { | ||
| LadderRunner ladderRunner = new LadderRunner(ladderCreator.getRows()); | ||
| ladderRunner.run(position); | ||
| return position.getValue(); | ||
| LadderPosition ladderPosition = new LadderPosition(0, position.getValue()); | ||
| LadderRunner ladderRunner = new LadderRunner(ladderCreatorIF.getRows()); | ||
| return ladderRunner.run(ladderPosition); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package ladder; | ||
|
|
||
| import ladder.creator.LadderCreator; | ||
| import ladder.creator.RandomLadderCreator; | ||
|
|
||
| public class LadderGameFactory { | ||
| public static LadderGame createLadderGame(LadderSize ladderSize) { | ||
| return new LadderGame(new LadderCreator(ladderSize)); | ||
| } | ||
|
|
||
| public static LadderGame createRandomLadderGame(LadderSize ladderSize) { | ||
| return new LadderGame(new RandomLadderCreator(ladderSize)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package ladder; | ||
|
|
||
| public class LadderPosition { | ||
|
|
||
| private Position x; | ||
| private Position y; | ||
|
|
||
| public LadderPosition(Position x, Position y) { | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
|
|
||
| public LadderPosition(int x, int y) { | ||
| this.x = Position.from(x); | ||
| this.y = Position.from(y); | ||
| } | ||
|
|
||
| public Position getX() { | ||
| return x; | ||
| } | ||
|
|
||
| public Position getY() { | ||
| return y; | ||
| } | ||
|
|
||
| public int getValueX() { | ||
| return x.getValue(); | ||
| } | ||
|
|
||
| public int getValueY() { | ||
| return y.getValue(); | ||
| } | ||
|
|
||
| public Boolean isNthRow(int n) { | ||
| return x.getValue() == n; | ||
| } | ||
|
|
||
| public Boolean isNthNode(int n) { | ||
| return y.getValue() == n; | ||
| } | ||
|
|
||
| public void goLeft() { | ||
| y.prev(); | ||
| } | ||
|
|
||
| public void goRight() { | ||
| y.next(); | ||
| } | ||
|
|
||
| public void goDown() { | ||
| x.next(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,10 +8,49 @@ public LadderRunner(Row[] rows) { | |
| this.rows = rows; | ||
| } | ||
|
|
||
| public int run(Position position) { | ||
| public int run(LadderPosition ladderPosition) { | ||
| for (int i = 0; i < rows.length; i++) { | ||
| rows[i].nextPosition(position); | ||
| printLadder("Before", ladderPosition); | ||
| rows[i].nextPosition(ladderPosition); | ||
| printLadder("After", ladderPosition); | ||
| ladderPosition.goDown(); | ||
| } | ||
| return position.getValue(); | ||
| return ladderPosition.getValueY(); | ||
| } | ||
|
|
||
| private void printLadder(String title, LadderPosition ladderPosition) { | ||
| String printString = title | ||
| + '\n' | ||
| + getLadderString(ladderPosition); | ||
| System.out.println(printString); | ||
| } | ||
|
|
||
| private String getLadderString(LadderPosition ladderPosition) { | ||
| StringBuilder ladderStringBuilder = new StringBuilder(); | ||
|
|
||
| for (int i = 0; i < rows.length; i++) { | ||
| ladderStringBuilder.append(getNthRowString(i, ladderPosition)) | ||
| .append('\n'); | ||
| } | ||
| return ladderStringBuilder.toString(); | ||
| } | ||
|
|
||
| private String getNthRowString(int n, LadderPosition ladderPosition) { | ||
| String rowString = rows[n].getRowString(); | ||
| if (ladderPosition.isNthRow(n)) { | ||
| rowString = insertAsterisk(rowString, ladderPosition); | ||
| } | ||
| return rowString; | ||
| } | ||
|
|
||
| private String insertAsterisk(String rowString, LadderPosition ladderPosition) { | ||
| String[] nodeStrings = rowString.split(" "); | ||
| StringBuilder stringBuilder = new StringBuilder(); | ||
|
|
||
| for (int i = 0; i < nodeStrings.length; i++) { | ||
| stringBuilder.append(nodeStrings[i]) | ||
| .append(ladderPosition.isNthNode(i) ? "* " : " "); | ||
| } | ||
| return stringBuilder.toString(); | ||
| } | ||
|
Comment on lines
+28
to
55
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. Runner 에서 Row, Node 의 변수 정보를 getter 로 가져와 그리기보다는, |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package ladder; | ||
|
|
||
| public class LadderSize { | ||
| private GreaterThanOne numberOfRow; | ||
| private GreaterThanOne numberOfPerson; | ||
|
|
||
| public LadderSize(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) { | ||
| this.numberOfRow = numberOfRow; | ||
| this.numberOfPerson = numberOfPerson; | ||
| } | ||
|
|
||
| public GreaterThanOne getNumberOfRow() { | ||
| return numberOfRow; | ||
| } | ||
|
|
||
| public GreaterThanOne getNumberOfPerson() { | ||
| return numberOfPerson; | ||
| } | ||
|
|
||
| public int getNumberOfRowValue() { | ||
| return numberOfRow.getNumber(); | ||
| } | ||
|
|
||
| public int getNumberOfPersonValue() { | ||
| return numberOfPerson.getNumber(); | ||
| } | ||
|
|
||
| public int getMaxLineCount() { | ||
| return (int)(numberOfRow.getNumber() * numberOfPerson.getNumber() * 0.3); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,18 +18,24 @@ public void drawLine(Position startPosition) { | |
| setDirectionBetweenNextPosition(startPosition); | ||
| } | ||
|
|
||
| public void nextPosition(Position position) { | ||
| validatePosition(position); | ||
| public void nextPosition(LadderPosition ladderPosition) { | ||
| validateLadderPosition(ladderPosition); | ||
|
|
||
| nodes[position.getValue()].move(position); | ||
| nodes[ladderPosition.getValueY()].move(ladderPosition); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| private void setDirectionBetweenNextPosition(Position position) { | ||
| nodes[position.getValue()].setRightNode(); | ||
| position.next(); | ||
| nodes[position.getValue()].setLeftNode(); | ||
| } | ||
|
|
||
| private void validateLadderPosition(LadderPosition ladderPosition) { | ||
| validatePosition(ladderPosition.getY()); | ||
| } | ||
|
Comment on lines
+35
to
+37
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. 위에 언급했듯이 인자로 Position 을 받으면 해당 메서드는 지워도 좋을 것 같습니다! |
||
|
|
||
| private void validatePosition(Position position) { | ||
| if (isInvalidPosition(position) ) { | ||
| throw new IllegalArgumentException(ExceptionMessage.INVALID_POSITION.getMessage()); | ||
|
|
@@ -58,4 +64,14 @@ private boolean isLineAtNextPosition(Position position) { | |
| return lineAtPosition; | ||
| } | ||
|
|
||
| public String getRowString() { | ||
| StringBuilder rowStringBuilder = new StringBuilder(); | ||
|
|
||
| for (Node node : nodes) { | ||
| rowStringBuilder.append(node.toString()) | ||
| .append(" "); | ||
|
|
||
| } | ||
| return rowStringBuilder.toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,30 @@ | ||
| package ladder.creator; | ||
|
|
||
| import ladder.GreaterThanOne; | ||
| import ladder.Position; | ||
| import ladder.Row; | ||
| import ladder.*; | ||
|
|
||
| public class LadderCreator { | ||
|
|
||
| public class LadderCreator implements LadderCreatorIF { | ||
|
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.
|
||
|
|
||
| private final Row[] rows; | ||
|
|
||
| public LadderCreator(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) { | ||
| rows = new Row[numberOfRow.getNumber()]; | ||
| for (int i = 0; i < numberOfRow.getNumber(); i++) { | ||
| rows[i] = new Row(numberOfPerson); | ||
| public LadderCreator(LadderSize ladderSize) { | ||
| int numberOfRow = ladderSize.getNumberOfRowValue(); | ||
|
|
||
| rows = new Row[numberOfRow]; | ||
| for (int i = 0; i < numberOfRow; i++) { | ||
| rows[i] = new Row(ladderSize.getNumberOfPerson()); | ||
| } | ||
| } | ||
|
|
||
| public void drawLine(Position row, Position col) { | ||
| @Override | ||
| public void drawLine(LadderPosition ladderPosition) { | ||
| Position row = ladderPosition.getX(); | ||
| Position col = ladderPosition.getY(); | ||
|
|
||
| rows[row.getValue()].drawLine(col); | ||
| } | ||
|
|
||
| @Override | ||
| public Row[] getRows() { | ||
| return rows; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package ladder.creator; | ||
|
|
||
| import ladder.LadderPosition; | ||
| import ladder.Position; | ||
| import ladder.Row; | ||
|
|
||
| public interface LadderCreatorIF { | ||
|
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. interface 클래스에 |
||
| public abstract void drawLine(LadderPosition ladderPosition); | ||
|
|
||
| public abstract Row[] getRows(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package ladder.creator; | ||
|
|
||
| import ladder.LadderPosition; | ||
| import ladder.LadderSize; | ||
| import ladder.Row; | ||
|
|
||
| import java.util.HashSet; | ||
|
|
||
| public class RandomLadderCreator implements LadderCreatorIF{ | ||
|
|
||
| private final LadderCreator ladderCreator; | ||
| private final LadderSize ladderSize; | ||
|
|
||
| public RandomLadderCreator(LadderSize ladderSize) { | ||
| ladderCreator = new LadderCreator(ladderSize); | ||
| this.ladderSize = ladderSize; | ||
|
Comment on lines
+14
to
+16
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. LadderCreator 를 생성해서 사용할거면 매개변수로 LadderCreator 를 받아도 좋을 것 같습니다! |
||
| initLadder(); | ||
| } | ||
|
|
||
| private void initLadder() { | ||
| HashSet<LadderPosition> linePositionSet = new HashSet<>(); | ||
|
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. 참조변수인 객체는 |
||
| int maxLineCount = ladderSize.getMaxLineCount(); | ||
| int maxRowNumber = ladderSize.getNumberOfRowValue(); | ||
| int maxNodeNumber = ladderSize.getNumberOfPersonValue() - 1; | ||
|
|
||
| while (linePositionSet.size() < maxLineCount) { | ||
| int x = (int)(Math.random() * maxRowNumber); | ||
| int y = (int)(Math.random() * maxNodeNumber); | ||
| LadderPosition ladderPosition = new LadderPosition(x, y); | ||
|
Comment on lines
+22
to
+29
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. row, node, person, x, y 등 의미는 같은데 이름만 다른 변수가 코드의 직관성을 떨어뜨리는 것 같습니다! |
||
|
|
||
| try { | ||
| drawLine(ladderPosition); | ||
| linePositionSet.add(ladderPosition); | ||
| } catch (IllegalArgumentException ignored) { | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void drawLine(LadderPosition ladderPosition) { | ||
| ladderCreator.drawLine(ladderPosition); | ||
| } | ||
|
|
||
| @Override | ||
| public Row[] getRows() { | ||
| return ladderCreator.getRows(); | ||
| } | ||
| } | ||
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 클래스를 만들어 사용하면 하드코딩을 피하고, 구현코드나 테스트코드 등에서 안전하게 재사용할 수 있을 것 같아요.rows[i].nextPosition() 함수에는 y 좌표만
Position객체로 넘겨주면 내부 메서드가 더 깔끔해질 것 같아요.