-
Notifications
You must be signed in to change notification settings - Fork 26
2주차 미션/ 서버 4조 조규빈 #41
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 2 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.
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package ladder; | ||
|
|
||
| import ladder.creator.*; | ||
|
|
||
| public class LadderGameFactory { | ||
| public static LadderGame createBasicLadderGame(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) { | ||
| LadderCreator creator = new BasicLadderCreator(numberOfRow, numberOfPerson); | ||
| return new LadderGame(creator); | ||
| } | ||
|
|
||
| public static LadderGame createRandomLadderGame(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) { | ||
| LadderCreator creator = new AutoLadderCreator(numberOfRow, numberOfPerson); | ||
| return new LadderGame(creator); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package ladder; | ||
|
|
||
| public class LadderPrinter { | ||
| private final Row[] rows; | ||
| private StringBuilder sb; | ||
|
||
|
|
||
| public LadderPrinter(Row[] rows) { | ||
| this.rows = rows; | ||
| this.sb = new StringBuilder(); | ||
| } | ||
|
|
||
| public void printLadder(Position position, int currentRow, boolean isAfter) { | ||
| sb.setLength(0); // StringBuilder 초기화 | ||
| for (int i = 0; i < rows.length; i++) { | ||
| sb.append(addPositionMarker(rows[i].rowToString(), position, i == currentRow)); | ||
| sb.append("\n"); | ||
| } | ||
|
|
||
| System.out.println(isAfter ? "After" : "Before"); | ||
| System.out.print(sb.toString()); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| private String addPositionMarker(String rowString, Position position, boolean isCurrentRow) { | ||
| String[] parts = rowString.split(" "); | ||
| StringBuilder rowSb = new StringBuilder(); | ||
|
|
||
| for (int i = 0; i < parts.length; i++) { | ||
| if (i == position.getValue() && isCurrentRow) { | ||
| rowSb.append(parts[i].equals("-1") ? "-1*" : parts[i] + "*"); | ||
| } else { | ||
| rowSb.append(parts[i]); | ||
| } | ||
| if (i < parts.length - 1) { | ||
| rowSb.append(" "); | ||
| } | ||
| } | ||
| return rowSb.toString(); | ||
| } | ||
|
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. LadderPrinter 에서 node 의 정보를 확인하고 그리기보다는 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,15 +3,22 @@ | |
| public class LadderRunner { | ||
|
|
||
| private final Row[] rows; | ||
| private final LadderPrinter printer; | ||
|
|
||
| public LadderRunner(Row[] rows) { | ||
| this.rows = rows; | ||
| this.printer = new LadderPrinter(rows); | ||
| } | ||
|
|
||
| public int run(Position position) { | ||
| System.out.println("사다리 게임 시작"); | ||
|
|
||
| for (int i = 0; i < rows.length; i++) { | ||
| printer.printLadder(position, i, false); //Before 출력 | ||
| rows[i].nextPosition(position); | ||
| printer.printLadder(position, i, true); //After 출력 | ||
|
Comment on lines
+17
to
+19
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. 주석처리보다는 Before, After 상수를 갖는 enum 클래스를 만들어보는건 어떤가요?? |
||
| } | ||
|
|
||
| return position.getValue(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package ladder.creator; | ||
|
|
||
| import ladder.GreaterThanOne; | ||
| import ladder.Position; | ||
| import ladder.Row; | ||
| import java.util.Random; | ||
|
|
||
| public class AutoLadderCreator implements LadderCreator { | ||
| private final BasicLadderCreator basicCreator; //조합 사용을 위한 private class | ||
| private int count; | ||
|
||
| private final Random random = new Random(); | ||
|
||
| private final GreaterThanOne numberOfRow; | ||
| private final GreaterThanOne numberOfPerson; | ||
|
||
|
|
||
| public AutoLadderCreator(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) { | ||
| this.basicCreator = new BasicLadderCreator(numberOfRow, numberOfPerson); | ||
| this.count = (int) (numberOfRow.getNumber() * numberOfPerson.getNumber() * 0.3 + 1); | ||
| this.numberOfRow = numberOfRow; | ||
| this.numberOfPerson = numberOfPerson; | ||
| createLadder(); | ||
| } | ||
|
|
||
| private void createLadder() { | ||
| while (count > 0) { | ||
| try { | ||
| int x = random.nextInt(numberOfRow.getNumber()); | ||
| int y = random.nextInt(numberOfPerson.getNumber() - 1); | ||
| drawLine(Position.from(x), Position.from(y)); | ||
|
Comment on lines
+27
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. 랜덤 포지션의 상한값은 잘 지정해주었지만 음수값이 발생할 수 있네요. |
||
| count--; | ||
| } catch (IllegalArgumentException e) {} | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void drawLine(Position row, Position col) { | ||
| basicCreator.drawLine(row, col); | ||
| } | ||
|
|
||
| @Override | ||
| public Row[] getRows() { | ||
| return basicCreator.getRows(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ladder.creator; | ||
|
|
||
| import ladder.GreaterThanOne; | ||
| import ladder.Position; | ||
| import ladder.Row; | ||
|
|
||
| public class BasicLadderCreator implements LadderCreator { | ||
| private final Row[] rows; | ||
|
|
||
| public BasicLadderCreator(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) { | ||
| rows = new Row[numberOfRow.getNumber()]; | ||
|
||
| for (int i = 0; i < numberOfRow.getNumber(); i++) { | ||
| rows[i] = new Row(numberOfPerson); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void drawLine(Position row, Position col) { | ||
| rows[row.getValue()].drawLine(col); | ||
| } | ||
|
|
||
| @Override | ||
| public Row[] getRows() { | ||
| return rows; | ||
| } | ||
| } | ||
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.
GreaterThanOne 인자 두개를 받는 메서드나 클래스가 많은데,
두 인자를 변수로 갖는 사다리 사이즈에 대한 객체가 있으면 관리가 더욱 용이할 것 같아요