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조 박성현 #17

Open
wants to merge 1 commit 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
19 changes: 19 additions & 0 deletions src/main/java/ladder/Direction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ladder;

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

private final int direction;

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


// 게임 실행 중 결과 출력을 위해, 값 가져오기
public int getDirection() {
return direction;
}
}
25 changes: 25 additions & 0 deletions src/main/java/ladder/ExceptionMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ladder;

public enum ExceptionMessage {

INVALID_LADDER_POSITION("사다리 위치는 0이상의 정수입니다."),
INVALID_NUMBER_OF_PERSON("참여 인원은 2명 이상이어야 합니다."),

INVALID_NUMBER_OF_ROW("사다리의 높이는 1칸 이상이어야 합니다."),
INVALID_POSITION("유효하지 않은 위치 입니다."),

INVALID_POISTION_START("유효하지 않은 시작 위치 입니다.");

//..

private String message;


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

public String getExceptionMessage(){
return message;
}
}
27 changes: 0 additions & 27 deletions src/main/java/ladder/Ladder.java

This file was deleted.

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

public class LadderCreator {
private Row[] rows;

//
public LadderCreator(NaturalNumber naturalNumberOfRows, NaturalNumber naturalNumberOfPerson) {
rows = new Row[naturalNumberOfRows.getNum()];
for(int i = 0; i < naturalNumberOfRows.getNum(); i++) {
rows[i] = new Row(naturalNumberOfPerson);
}
}


public void drawLine(Position row, Position column) {
rows[row.getValue()].drawLine(column);
}


public int numberOfLine(){
return (int) (rows.length * rows[0].getLength() * 0.3);
}

public Row[] getRows(){
return rows;
}


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

public class LadderGame {
private LadderCreator ladderCreator;

public LadderGame (LadderCreator ladderCreator) {
this.ladderCreator = ladderCreator;
}
public int run(Position position){
LadderRun ladderRun = new LadderRun(ladderCreator.getRows());
return ladderRun.run(position);

}



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

public class LadderGameFactory{

private LadderGame ladderGame;

public LadderGame createRandomLadderGame(LadderCreator ladderCreator){

ladderGame = new LadderGame(ladderCreator);

// 몇개의 선 그을지
int numberOfLine = ladderCreator.numberOfLine();
// 선 랜덤하게 긋기 반복문 & 예외처리





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

public class LadderRun {

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


public int run(Position position){
validationStartPosition(position);
System.out.println("게임 시작");
for(int i = 0; i < rows.length - 1; i++) {
printBefore(Position.of(i), position);
position = rows[i].nextPosition(position);
printAfter(Position.of(i), position);
}

return position.getValue();
}

private void printBefore(Position row, Position position) {
System.out.println("Before");
for(int i = 0; i < rows.length; i++) {
if (i == row.getValue()){
rows[i].printNode(true, position);
} else {
rows[i].printNode(false, position);
}
}
System.out.println();
}

private void printAfter(Position row, Position position) {
System.out.println("After");
for(int i = 0; i < rows.length; i++) {
if (i == row.getValue()){
rows[i].printNode(true, position);
} else {
rows[i].printNode(false, position);
}
}
System.out.println();
}



private void validationStartPosition(Position position) {
if(position.getValue() >= rows.length || position.getValue() < 0){
throw new IllegalArgumentException(ExceptionMessage.INVALID_POISTION_START.getExceptionMessage());
}
}
}
36 changes: 36 additions & 0 deletions src/main/java/ladder/NaturalNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ladder;

public class NaturalNumber {
private final int num;
private NaturalNumber(int num){
this.num = num;
}

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

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


private static void validationNumberOfPerson(int num) {
if(num < 1) {
throw new IllegalArgumentException(ExceptionMessage.INVALID_NUMBER_OF_PERSON.getExceptionMessage());
}
}

private static void validationNumberOfRow(int num) {
if(num < 1) {
throw new IllegalArgumentException(ExceptionMessage.INVALID_NUMBER_OF_ROW.getExceptionMessage());
}
}


public int getNum() {
return num;
}
}
47 changes: 47 additions & 0 deletions src/main/java/ladder/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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();
}
if (isLeft()) {
return position.prev();
}
return position;
}

protected boolean isLeft() {
return this.direction == Direction.LEFT;
}

protected boolean isRight() {
return this.direction == Direction.RIGHT;
}

public void drawLeft() {
direction = Direction.LEFT;
}
public void drawRight() {
direction = Direction.RIGHT;
}


// 게임 실행 중 결과 출력을 위해, 값 가져오기
public int getValue() {
return direction.getDirection();

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

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


// 밑에처럼 사람 수 받아워서 validation하는게 좀 어색함.
// 그렇다고 안받아오면 사람수 넘게 position이 입력됐는지 validation하기 어려움.

static public Position of(int position){

validationPosition(position);
return new Position(position);
}

private static void validationPosition(int position) {
// row 수정후에 다시 수정
if(position < 0 ) {
throw new IllegalArgumentException(ExceptionMessage.INVALID_POSITION.getExceptionMessage());
}
}
public Position prev(){
return new Position(position -1);
}


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


public int getValue(){
return position;
}

}
Loading