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

1주차 미션 / 서버 4조 지호준 #9

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/ladder/Direction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ladder;

public enum Direction {
LEFT(-1), RIGHT(1), NONE(0);

private final int value;

Direction(int value) {
this.value = value;
}

public int getValue() {
return value;
}

}
20 changes: 20 additions & 0 deletions src/main/java/ladder/ExceptionMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ladder;

public enum ExceptionMessage {

INVALID_NUMBER_OF_PERSON("게임의 참여자 수는 1명 이상이어야 합니다."),
INVALID_LINE_POSITION("라인 생성이 불가능한 위치입니다."),
INVALID_POSITION("유효하지 않은 위치입니다."),
INVALID_NATURAL_NUMBER("유효하지 않은 자연수입니다.");

private final String message;

ExceptionMessage(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

}
27 changes: 0 additions & 27 deletions src/main/java/ladder/Ladder.java

This file was deleted.

21 changes: 21 additions & 0 deletions src/main/java/ladder/LadderCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ladder;

public class LadderCreator {
private Row[] rows;

public LadderCreator(NaturalNumber numberOfRows, NaturalNumber numberOfPerson) {
rows = new Row[numberOfPerson.getValue()];

for (int i = 0; i < numberOfRows.getValue(); i++) {
rows[i] = new Row(numberOfPerson);
}
}

public void drawLine(int row, int col) {
rows[row].drawLine(Position.of(col));
}

public Row[] getRows() {
return rows;
}
}
14 changes: 14 additions & 0 deletions src/main/java/ladder/LadderGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ladder;

public class LadderGame {
private final LadderCreator ladderCreator;

public LadderGame(LadderCreator ladderCreator){
this.ladderCreator = ladderCreator;
}

public Position run(Position position){
LadderRunner ladderRunner =new LadderRunner(ladderCreator.getRows());
return ladderRunner.run((position));
}
}
17 changes: 17 additions & 0 deletions src/main/java/ladder/LadderRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ladder;

public class LadderRunner {
private Row[] rows;

public LadderRunner(Row[] rows) {
this.rows = rows;
}

public Position run(Position position) {

for (int i = 0; i < rows.length; i++) {
position = rows[i].nextPosition(position);
}
return position;
}
}
33 changes: 33 additions & 0 deletions src/main/java/ladder/NaturalNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ladder;

public class NaturalNumber {

private final int value;

private NaturalNumber(int value) {
validate(value);
this.value = value;
}


public static NaturalNumber of(int value){
return new NaturalNumber(value);
}

public int getValue() {
return value;
}

private void validate(int value){
if(!isNaturalNumber(value)){
throw new IllegalArgumentException(ExceptionMessage.INVALID_NATURAL_NUMBER.getMessage());
}
}

private static boolean isNaturalNumber(int value){
return value >=1;
}
}



33 changes: 33 additions & 0 deletions src/main/java/ladder/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ladder;

public class Node {

private Direction direction;

private Node(Direction direction){
this.direction = direction;
}

public static Node of(Direction direction){
return new Node(direction);
}

public Position move(Position position){
if(isRight()){
return position.next();
}
else if(isLeft()){
return position.prev();
}
return position;
}

public boolean isRight(){
return direction == Direction.RIGHT;
}
public boolean isLeft(){
return direction == Direction.LEFT;
}


}
37 changes: 37 additions & 0 deletions src/main/java/ladder/Position.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ladder;

public class Position {
private int position;

private Position(int position) {
this.position = position;
}

public static Position of(int position) {
validatePosition(position);
return new Position(position);
}

public int getValue() {
return position;
}

public Position prev() {
return Position.of(position - 1);
}

public Position next() {
return Position.of(position + 1);
}

// 유효성 검증
private static void validatePosition(int position) {
if (!isPosition(position)) {
throw new IllegalArgumentException(ExceptionMessage.INVALID_POSITION.getMessage());
}
}

private static boolean isPosition(int position) {
return position >= 0;
}
}
63 changes: 29 additions & 34 deletions src/main/java/ladder/Row.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,51 @@
package ladder;

import static ladder.ExceptionMessage.*;

public class Row {
private int[] row;
private Node[] nodes;

public Row(int numberOfPerson) {
validateNumberOfPerson(numberOfPerson);
row = new int[numberOfPerson];
}
public static final int MINIMUM_NUMBER_OF_PERSON = 1;
public static final int MAXIMUM_INVALID_POSITION_NUMBER = 0;

public void drawLine(int lineStartPosition) {
validateDrawLinePosition(lineStartPosition);
row[lineStartPosition] = 1;
row[lineStartPosition + 1] = -1;
public Row(NaturalNumber numberOfPerson) {
validateNumberOfPerson(numberOfPerson.getValue());
nodes = new Node[numberOfPerson.getValue()];
}

public int nextPosition(int position) {

validatePosition(position);

if (isLeft(position)) {
return position - 1;
}
if (isRight(position)) {
return position + 1;
}

return position;
// 사다리 행에 가로선을 그리는 역할
public void drawLine(Position lineStartPosition) {
validateDrawLinePosition(lineStartPosition);
nodes[lineStartPosition.getValue()] = Node.of(Direction.RIGHT);
nodes[lineStartPosition.getValue() + 1] = Node.of(Direction.LEFT);
}

private boolean isLeft(int position) {
return row[position] == -1;
}
//현재위치 position에서 다음 위치를 계산해서 변환
public Position nextPosition(Position currentPosition) {

private boolean isRight(int position) {
return row[position] == 1;
validatePosition(currentPosition);
Position nextPosition = nodes[currentPosition.getValue()].move(currentPosition);
return nextPosition;
}

private void validateNumberOfPerson(int numberOfPerson) {
if(numberOfPerson < 1) {
throw new IllegalArgumentException("게임의 참여자 수는 1명 이상이어야 합니다.");
if(numberOfPerson < MINIMUM_NUMBER_OF_PERSON) {
throw new IllegalArgumentException(INVALID_NUMBER_OF_PERSON.getMessage());
}
}

private void validateDrawLinePosition(int lineStartPosition) {
if(lineStartPosition < 0 || lineStartPosition >= row.length - 1 || row[lineStartPosition] == -1 || row[lineStartPosition + 1] == 1) {
throw new IllegalArgumentException("라인 생성이 불가능한 위치 입니다.");
private void validateDrawLinePosition(Position lineStartPosition) {
if(lineStartPosition.getValue() < MAXIMUM_INVALID_POSITION_NUMBER ||
lineStartPosition.getValue() >= nodes.length - 1 ||
nodes[lineStartPosition.getValue()].isLeft() ||
nodes[lineStartPosition.getValue() + 1].isRight() ) {
throw new IllegalArgumentException(INVALID_LINE_POSITION.getMessage());
}
}

private void validatePosition(int position) {
if(position >= row.length || position < 0 ) {
throw new IllegalArgumentException("유효하지 않은 위치 입니다.");
private void validatePosition(Position position) {
if(position.getValue() >= nodes.length || position.getValue() < MAXIMUM_INVALID_POSITION_NUMBER ) {
throw new IllegalArgumentException(INVALID_POSITION.getMessage());
}
}

Expand Down
36 changes: 19 additions & 17 deletions src/test/java/ladder/LadderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

import ladder.NaturalNumber;
class LadderTest {

@Test
void 사다리_생성_확인() {
//given
int numberOfRows = 3;
int numberOfPerson = 5;
NaturalNumber numberOfRows = NaturalNumber.of(3);
NaturalNumber numberOfPerson = NaturalNumber.of(5) ;

//when
Ladder ladder = new Ladder(numberOfRows, numberOfPerson);
LadderCreator ladder = new LadderCreator(numberOfRows, numberOfPerson);

//then
assertNotNull(ladder);
Expand All @@ -22,48 +22,50 @@ class LadderTest {
@Test
void 사다리_시작위치_예외_처리() {
//given
int numberOfPerson = 3;
int numberOfRows = 1;
Ladder ladder = new Ladder(numberOfRows, numberOfPerson);
NaturalNumber numberOfPerson = NaturalNumber.of(3);
NaturalNumber numberOfRows = NaturalNumber.of(1);
LadderCreator ladder = new LadderCreator(numberOfRows, numberOfPerson);

//when
int position = 3;
Position position = Position.of(3);

//then
assertThrows(IllegalArgumentException.class, () -> ladder.run(3));
assertThrows(IllegalArgumentException.class, () -> ladder.run(Position.of(3)));
}

@Test
void 사다리_결과_확인() {
//given
int numberOfPerson = 4;
int numberOfRows = 4;
Ladder ladder = new Ladder(numberOfRows, numberOfPerson);
NaturalNumber numberOfPerson = NaturalNumber.of(4);
NaturalNumber numberOfRows =NaturalNumber.of(4) ;
LadderCreator ladder = new LadderCreator(numberOfRows, numberOfPerson);
ladder.drawLine(1,0);
ladder.drawLine(1,2);
ladder.drawLine(2,1);


//when
int position = 0;
int resultPosition = ladder.run(position);
Position position = Position.of(0);
LadderRunner ladderRunner = new LadderRunner(ladder.getRows());
Position resultPosition = ladderRunner.run(position);

//then
assertEquals(2, resultPosition);

//when
position = 1;
position = Position.of(1);
resultPosition = ladder.run(position);
//then
assertEquals(0, resultPosition);

//when
position = 2;
position = Position.of(2);
resultPosition = ladder.run(position);
//then
assertEquals(3, resultPosition);

//when
position = 3;
position = Position.of(3);
resultPosition = ladder.run(position);
//then
assertEquals(1, resultPosition);
Expand Down
Loading