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주차 미션 / 서버 2조 정서현 #18

Open
wants to merge 13 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
27 changes: 0 additions & 27 deletions src/main/java/ladder/Ladder.java

This file was deleted.

60 changes: 0 additions & 60 deletions src/main/java/ladder/Row.java

This file was deleted.

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

import ladder.application.creator.LadderCreator;
import ladder.domain.Position;

public class LadderGame {

private final LadderCreator ladder;

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

public int run(Position position){
LadderRunner ladderRunner = new LadderRunner(ladder.getRows());
return ladderRunner.run(position);
}

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

import ladder.domain.Position;
import ladder.domain.Row;

public class LadderRunner {

private final Row[] rows;

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

// 현재 포지션 입력하면 최종 포지션 출력
public int run(Position position) {

for(int i = 0; i < rows.length; i++) {
position = rows[i].nextPosition(position);
}

return position.getValue();
}
}
23 changes: 23 additions & 0 deletions src/main/java/ladder/application/creator/LadderCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ladder.application.creator;

import ladder.domain.NaturalNumber;
import ladder.domain.Position;
import ladder.domain.Row;

public interface LadderCreator {
// public Row[] rows;
//
// public LadderCreator(NaturalNumber numberOfRows, NaturalNumber numberOfPerson) {
// rows = new Row[numberOfRows.getNum()];
//
// for(int i = 0; i < numberOfRows.getNum(); i++) {
// rows[i] = new Row(numberOfPerson);
// }
// }
//
// public void drawLine(Position row, Position col) {
// rows[row.getValue()].drawLine(col);
// }
//
public Row[] getRows();
}
11 changes: 11 additions & 0 deletions src/main/java/ladder/application/creator/RandomLadderCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ladder.application.creator;

import ladder.domain.Row;

public class RandomLadderCreator implements LadderCreator {

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

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

private final int value;

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

public int getValue() {
return this.value;
}
}
34 changes: 34 additions & 0 deletions src/main/java/ladder/domain/NaturalNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ladder.domain;

import static ladder.exception.ExceptionMessage.INVALID_NATURAL_NUMBER;

public class NaturalNumber {
private final int num;

private NaturalNumber(int num) {
validateNaturalNumber(num);
this.num = num;
}

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

public int getNum() {
// 검증된 num 반환
return num;
}

// 유효성 검증
private void validateNaturalNumber(int num){
// num이 자연수인지 검증
if (!isNaturalNumber((num))) {
throw new IllegalArgumentException(INVALID_NATURAL_NUMBER.getMessage());
}
}

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

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

import ladder.domain.Direction;

public class Node {
private final Direction nodeDirection;

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

public Position move(Position position) {
if(isRight()) {
return position.nextPosition();
}
if(isLeft()){
return position.prevPosition();
}
return position;
}

public boolean isRight() {
return nodeDirection == Direction.RIGHT;
}

public boolean isLeft() {
return nodeDirection == Direction.LEFT;
}

// 정적 팩토리 메소드
public static Node of(Direction direction) {
return new Node(direction);
}
}
48 changes: 48 additions & 0 deletions src/main/java/ladder/domain/Position.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ladder.domain;

import static ladder.exception.ExceptionMessage.INVALID_LADDER_POSITION;

public class Position {
private int position;

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

// 정적 팩토리 메소드
public static Position of(int position){
return new Position(position);
}

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

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

public int getValue(){
return position;
}

public boolean isBiggerThan(int position){
return this.position > position;
}

public boolean isSmallerThan(int position){
return this.position < position;
}

// 유효성 검증
private void validatePosition(int position) {
if(!isPositionValid(position)) {
throw new IllegalArgumentException(INVALID_LADDER_POSITION.getMessage());
}
}

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

import static ladder.exception.ExceptionMessage.*;

public class Row {

// Node들로 이루어진 한 행
private Node[] row;

// 사람 수만큼의 node를 가진 행 생성
public Row(NaturalNumber numberOfPerson) {
row = new Node[numberOfPerson.getNum()];
// 초기화
for (int i = 0; i < numberOfPerson.getNum(); i++) {
row[i] = Node.of(Direction.NONE);
}
}

// 사다리 그리기
public void drawLine(Position lineStartPosition) {
validateDrawLinePosition(lineStartPosition);

Position next = lineStartPosition.nextPosition();
row[lineStartPosition.getValue()] = Node.of(Direction.RIGHT);
row[next.getValue()] = Node.of(Direction.LEFT);
}

// 다음 position으로 이동
public Position nextPosition(Position position) {
validatePosition(position);
Position newPosition = row[position.getValue()].move(position);
// 새로운 위치가 유효한지 검증
validatePosition(newPosition);
return newPosition;
}


// 유효성 검증
private void validateDrawLinePosition(Position position) {
if(isInvalidDrawPosition(position) && isSameLine(position)){
throw new IllegalArgumentException(INVALID_ROW_POSITION.getMessage());
}
}

private boolean isInvalidDrawPosition(Position position){
return position.isBiggerThan(row.length - 2) || position.isSmallerThan(0);
}

// 중복 라인 체크
private boolean isSameLine(Position position){
return (row[position.getValue()].isLeft()
|| row[position.nextPosition().getValue()].isRight()
|| row[position.getValue()].isRight());
}

private void validatePosition(Position position){
if(isInvalidPosition(position)) {
throw new IllegalArgumentException(INVALID_LADDER_POSITION.getMessage());
}
}

private boolean isInvalidPosition(Position position) {
return position.isBiggerThan(row.length - 1) || position.isSmallerThan(0);
}

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

public enum ExceptionMessage {

INVALID_LADDER_POSITION("유효하지 않은 위치입니다."),
INVALID_NUMBER_OF_PERSON("참여 인원은 2명 이상이어야 합니다."),
INVALID_ROW_POSITION("라인 생성이 불가능한 위치입니다."),
INVALID_NATURAL_NUMBER("1 이상의 정수여야 합니다.");

//..

private final String message;

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

public String getMessage() {
return message;
}
}
Loading