-
Notifications
You must be signed in to change notification settings - Fork 26
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
2주차 미션 / 서버 1조 문승우 #38
base: 1week-completed
Are you sure you want to change the base?
Changes from all commits
7b6351f
a70cb9b
4b12f0c
bcda82c
9236b57
16ad4d3
77fb337
18222c8
2128e79
fcdfa3e
8e6897b
72af15c
0b77363
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,4 +1,4 @@ | ||
# KUIT4_Server-Ladder | ||
# KUIT4_Server-ladder.LadderGame | ||
|
||
## 미션 제출 방법 | ||
1. 해당 프로젝트를 fork 한다. | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ladder; | ||
|
||
import ladder.creator.LadderCreator; | ||
import ladder.runner.LadderRunner; | ||
import ladder.wrapper.ArrayIndex; | ||
|
||
public class LadderGame { | ||
|
||
private final LadderCreator ladderCreator; | ||
|
||
|
||
public LadderGame(LadderCreator ladderCreator) { | ||
this.ladderCreator = ladderCreator; | ||
} | ||
|
||
public void drawLine(Line line){ | ||
ladderCreator.drawLine(line); | ||
} | ||
|
||
public int run (ArrayIndex startingLadderIndex) { | ||
LadderRunner ladderRunner = new LadderRunner(ladderCreator.getRows()); | ||
return ladderRunner.run(startingLadderIndex); | ||
} | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ladder; | ||
|
||
import ladder.constants.Direction; | ||
import ladder.wrapper.unsignedInt; | ||
|
||
public class Line { | ||
private unsignedInt row; | ||
private unsignedInt col; | ||
private Direction direction; | ||
|
||
private Line(int row, int col, Direction direction) { | ||
this.row = unsignedInt.from(row); | ||
this.col = unsignedInt.from(col); | ||
this.direction = direction; | ||
} | ||
|
||
public static Line of(int row, int column, Direction direction) { | ||
return new Line(row ,column ,direction); | ||
} | ||
|
||
public int getRow() { return this.row.getValue(); } | ||
|
||
public int getColumn() { | ||
return this.col.getValue(); | ||
} | ||
|
||
public Direction getDirection() { | ||
return this.direction; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ladder; | ||
|
||
import ladder.constants.nodeState; | ||
|
||
public class Node { | ||
|
||
private nodeState state; | ||
|
||
private Node(nodeState state) { | ||
this.state = state; | ||
} | ||
|
||
public static Node from(nodeState state){ | ||
return new Node(state); | ||
} | ||
|
||
public void setLine(nodeState state){ | ||
this.state = state; | ||
} | ||
|
||
public nodeState getState() { | ||
return state; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ladder; | ||
|
||
import ladder.wrapper.ArrayIndex; | ||
import ladder.wrapper.unsignedInt; | ||
|
||
public class Player { | ||
|
||
unsignedInt currentDepth; | ||
ArrayIndex currentLadderIndex; | ||
|
||
public int getCurrentDepth() { | ||
return currentDepth.getValue(); | ||
} | ||
|
||
private Player(ArrayIndex currentLadderIndex) { | ||
this.currentDepth = currentDepth.from(0); | ||
this.currentLadderIndex = currentLadderIndex; | ||
} | ||
|
||
public static Player startingFrom(ArrayIndex currentLadderIndex) { | ||
return new Player(currentLadderIndex); | ||
} | ||
|
||
public boolean isAtBottom(int rowLength) { | ||
return currentDepth.getValue() == rowLength; | ||
} | ||
|
||
public void moveToBottom() { | ||
currentDepth.increment(); | ||
} | ||
|
||
public void moveIfThereIsLineIn(Row row) { | ||
//오른쪽에 있을 경우 | ||
if(row.hasRightLine(currentLadderIndex)) { | ||
currentLadderIndex.increment(); | ||
} else if (row.hasLeftLine(currentLadderIndex)){ | ||
currentLadderIndex.decrement(); | ||
} | ||
} | ||
|
||
public int getCurrentLadderIndex() { | ||
return currentLadderIndex.getIndex(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package ladder; | ||
|
||
import ladder.constants.Direction; | ||
import ladder.constants.ExceptionMessage; | ||
import ladder.constants.nodeState; | ||
import ladder.wrapper.ArrayIndex; | ||
import ladder.wrapper.unsignedInt; | ||
|
||
public class Row { | ||
private Node[] nodes; | ||
|
||
public Row(int column) { | ||
nodes = new Node[column]; | ||
for (int i = 0; i < column; i++) { | ||
nodes[i] = Node.from(nodeState.empty); | ||
} | ||
} | ||
|
||
public int LineLength() { | ||
return nodes.length; | ||
} | ||
|
||
public nodeState currentState(int index) { | ||
return nodes[index].getState(); | ||
} | ||
|
||
public void drawLineAt(unsignedInt position, Direction direction) { | ||
vaildateLine(position, direction); | ||
|
||
if (isLineToLeft(direction)) { | ||
drawLeftLine(position); | ||
} | ||
|
||
if (isLineToRight(direction)) { | ||
drawRightLine(position); | ||
} | ||
} | ||
|
||
private void vaildateLine(unsignedInt position, Direction direction) { | ||
|
||
if(position.getValue() < 0 || position.getValue() >= LineLength()) { | ||
throw new IllegalArgumentException(ExceptionMessage.INVALID_DRAW_POSITION.getMessage()); | ||
} | ||
|
||
int lineEndingAt = position.getValue() + direction.getValue(); | ||
if(lineEndingAt < 0 || lineEndingAt >= LineLength()) { | ||
throw new IllegalArgumentException(ExceptionMessage.INVALID_DRAW_POSITION.getMessage()); | ||
} | ||
|
||
if((nodes[position.getValue()].getState() != nodeState.empty) || (nodes[lineEndingAt].getState() != nodeState.empty)) { | ||
throw new IllegalArgumentException(ExceptionMessage.INVALID_DRAW_POSITION.getMessage()); | ||
} | ||
} | ||
|
||
|
||
private static boolean isLineToLeft(Direction direction) { | ||
return direction == Direction.LEFT; | ||
} | ||
|
||
private static boolean isLineToRight(Direction direction) { | ||
return direction == Direction.RIGHT; | ||
} | ||
|
||
private void drawLeftLine(unsignedInt position) { | ||
nodes[position.getValue()].setLine(nodeState.RIGHT); | ||
nodes[position.getValue() - 1].setLine(nodeState.LEFT); | ||
} | ||
|
||
private void drawRightLine(unsignedInt position) { | ||
nodes[position.getValue()].setLine(nodeState.LEFT); | ||
nodes[position.getValue() + 1].setLine(nodeState.RIGHT); | ||
} | ||
|
||
public boolean hasRightLine(ArrayIndex currentLadderIndex){ | ||
if(currentLadderIndex.getIndex() == (nodes.length - 1)){ | ||
return false; | ||
} else { | ||
return currentState(currentLadderIndex.getIndex()+1) == nodeState.RIGHT; | ||
} | ||
} | ||
|
||
public boolean hasLeftLine(ArrayIndex currentLadderIndex){ | ||
if(currentLadderIndex.getIndex() == 0){ | ||
return false; | ||
} else { | ||
return currentState(currentLadderIndex.getIndex()-1) == nodeState.LEFT; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ladder.constants; | ||
|
||
public enum Direction { | ||
LEFT(-1) , RIGHT(1); | ||
|
||
private int value; | ||
|
||
private Direction(int value) { | ||
this.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
} |
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.
if문 조건으로 있는 position 안의 값에 대한 검증, node 안의 값에 대한 검증 로직은 각 클래스에 있는게 더 좋아보여요!