Skip to content
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

Open
wants to merge 13 commits into
base: 1week-completed
Choose a base branch
from
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# KUIT4_Server-Ladder
# KUIT4_Server-ladder.LadderGame

## 미션 제출 방법
1. 해당 프로젝트를 fork 한다.
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/Ladder.java

This file was deleted.

26 changes: 26 additions & 0 deletions src/main/java/ladder/LadderGame.java
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);
}

}

30 changes: 30 additions & 0 deletions src/main/java/ladder/Line.java
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;
}
}
25 changes: 25 additions & 0 deletions src/main/java/ladder/Node.java
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;
}

}
44 changes: 44 additions & 0 deletions src/main/java/ladder/Player.java
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();
}
}
90 changes: 90 additions & 0 deletions src/main/java/ladder/Row.java
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());
}
}
Comment on lines +39 to +53
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if문 조건으로 있는 position 안의 값에 대한 검증, node 안의 값에 대한 검증 로직은 각 클래스에 있는게 더 좋아보여요!



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;
}
}

}
15 changes: 15 additions & 0 deletions src/main/java/ladder/constants/Direction.java
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;
}
}
Loading