-
Notifications
You must be signed in to change notification settings - Fork 26
2주차 미션 / 서버 2조 정윤아 #47
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
d7bc7c2
169aad1
f556053
2aedd9e
bd7f8f2
e7de07f
b7d97db
bf6148a
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public enum Direction { | ||
| LEFT(-1), NONE(0), RIGHT(1); | ||
|
|
||
| private final int direction; | ||
|
|
||
| Direction(int direction) { | ||
| this.direction = direction; | ||
| } | ||
|
|
||
| public int getDirection() { | ||
| return direction; | ||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| public class Index { | ||
| private int index; | ||
|
|
||
| private Index(int index){ | ||
| this.index = index; | ||
| } | ||
|
|
||
| public static Index from(int index){ | ||
| if ( !validateIndex(index) ){ | ||
| throw new IndexOutOfBoundsException("Index out of bounds"); | ||
| } | ||
| return new Index(index); | ||
| } | ||
|
|
||
| private static boolean validateIndex(int index) { | ||
| return index >= 0; | ||
| } | ||
|
|
||
| public int getNumber(){ | ||
| return index; | ||
| } | ||
|
|
||
| public void move(Direction a) { | ||
| this.index += a.getDirection(); | ||
| } | ||
|
|
||
| public Index nextIndex() { | ||
| return Index.from(this.index + 1); | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import java.util.Random; | ||
|
|
||
| public class LadderCreator { | ||
| private Row[] rows; | ||
|
|
||
| public LadderCreator(Index rowNum, Index numberOfPerson, boolean isRandom) { | ||
| rows = new Row[rowNum.getNumber()]; | ||
| SetRows(numberOfPerson); | ||
| if (isRandom) { | ||
| MakeRandomLine(numberOfPerson); | ||
| } | ||
| } | ||
|
|
||
| private void SetRows(Index numberOfPerson) { | ||
| for (int i=0; i < rows.length; i++) { | ||
| rows[i] = new Row(numberOfPerson); | ||
| } | ||
| } | ||
|
|
||
| private void MakeRandomLine(Index numberOfPerson) { | ||
| int numberOfLines = (int) (rows.length * numberOfPerson.getNumber() * 0.3); | ||
| Index rowsLength = Index.from(rows.length); | ||
|
|
||
| for (int i=0; i < numberOfLines; i++) { | ||
| PositionOfLine position = PositionOfLine.randomPosition(rowsLength, numberOfPerson); | ||
| PositionOfLine nextPosition = PositionOfLine.of(position.getX(), position.getY().nextIndex()); | ||
|
|
||
| if (canDrawLine(position) && !LineAlreadyExisting(position.getX(), position.getY().nextIndex())) { drawLine(position); continue;} | ||
| i--; | ||
| } | ||
| } | ||
|
|
||
| public void drawLine(PositionOfLine position) { | ||
| canDrawLine(position); | ||
| Index x = position.getX(); | ||
| Index y = position.getY(); | ||
|
|
||
| rows[x.getNumber()].setValue(y, Direction.RIGHT); | ||
| rows[x.getNumber()].setValue(y.nextIndex(), Direction.LEFT); | ||
| } | ||
|
|
||
| public boolean canDrawLine(PositionOfLine position) { | ||
| Index x = position.getX(); | ||
| Index y = position.getY(); | ||
|
|
||
| if (LineAlreadyExisting(x, y) || validateRangeOfIndex(x, y)) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public boolean LineAlreadyExisting(Index x, Index y) { | ||
| Node node = rows[x.getNumber()].getValue(y); | ||
| return node.isLeft() || node.isRight(); | ||
| } | ||
|
|
||
| private boolean validateRangeOfIndex(Index x, Index y) { | ||
| return x.getNumber() >= rows.length || y.getNumber() >= rows[0].getPeopleNum() - 1; | ||
| } | ||
|
|
||
| public Row[] getRow() { | ||
| return rows; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| public class LadderGame { | ||
| private LadderCreator ladderCreator; | ||
| private RunGame runGame; | ||
|
|
||
| public LadderGame(LadderCreator ladderCreator) { | ||
| this.ladderCreator = ladderCreator; | ||
| } | ||
|
|
||
| public int run(int startLine) { | ||
| runGame = new RunGame(ladderCreator.getRow()); | ||
|
|
||
| return runGame.run(startLine); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| public class LadderGameFactory { | ||
|
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. 객체지향을 살릴 수 있는 디자인 패턴중 팩터리 메서드 패턴이란 것이 있습니다!
이런식으로 하면 코드를 확장할때도, 객체지향적으로 용이해보이네요! |
||
| static LadderGame ladderGame; | ||
|
|
||
| public static LadderGame createLadderGame(Index rowNum, Index numberOfPerson) { | ||
| LadderCreator ladderCreator = new LadderCreator(rowNum, numberOfPerson, false); | ||
| ladderGame = new LadderGame(ladderCreator); | ||
|
|
||
| return ladderGame; | ||
| } | ||
|
|
||
| public static LadderGame createRandomLadderGame(Index rowNum, Index numberOfPerson) { | ||
| LadderCreator ladderCreator = new LadderCreator(rowNum, numberOfPerson, true); | ||
| ladderGame = new LadderGame(ladderCreator); | ||
|
|
||
| return ladderGame; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| public class Node { | ||
| private Direction direction; | ||
|
|
||
| public Node(Direction direction) { | ||
| this.direction = direction; | ||
| } | ||
|
|
||
| public Direction getDirection() { | ||
| return direction; | ||
| } | ||
|
|
||
| public void SetNode(Direction direction) { | ||
| this.direction = direction; | ||
| } | ||
|
|
||
| public boolean isLeft() { | ||
| return direction == Direction.LEFT; | ||
| } | ||
|
|
||
| public boolean isRight() { | ||
| return direction == Direction.RIGHT; | ||
| } | ||
|
|
||
| public boolean isZero() { | ||
| return direction == Direction.NONE; | ||
| } | ||
|
|
||
| public void printDirectionNumber() { | ||
| System.out.print(direction.getDirection()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import java.util.Random; | ||
|
|
||
| public class PositionOfLine { | ||
| private Index x; | ||
| private Index y; | ||
|
|
||
| private PositionOfLine(Index x, Index y) { | ||
| this.x = x; | ||
| this.y = y; | ||
| } | ||
|
|
||
| public static PositionOfLine of(Index x, Index y) { | ||
| return new PositionOfLine(x, y); | ||
| } | ||
|
|
||
| public static PositionOfLine randomPosition(Index rowsNum, Index colsNum) { | ||
| Random rand = new Random(); | ||
| Index x = Index.from(rand.nextInt(rowsNum.getNumber())); | ||
| Index y = Index.from(rand.nextInt(colsNum.getNumber())); | ||
|
|
||
| return PositionOfLine.of(x, y); | ||
| } | ||
|
|
||
| public Index getX() { | ||
| return x; | ||
| } | ||
|
|
||
| public Index getY() { | ||
| return y; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| //데이터를 감싸는 객체는 세터 게터가 불가피 |
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.
클래스를 final로 선언해서 불변 객체로 만들면 어떨까요?