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

15 changes: 15 additions & 0 deletions src/main/java/Direction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public enum Direction {
LEFT(-1), NONE(0), RIGHT(1);

private final int direction;

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

public int getDirection() {
return direction;
}


}
30 changes: 30 additions & 0 deletions src/main/java/Index.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
public class Index {
private int index;

private Index(int index){
this.index = index;
}

public static Index from(int index){
if ( !validateIndex(index) ){
throw new IndexOutOfBoundsException("Index out of bounds");
}
return new Index(index);
}

private static boolean validateIndex(int index) {
return index >= 0;
}

public int getNumber(){
return index;
}

public void move(Direction a) {
this.index += a.getDirection();
}

public Index nextIndex() {
return Index.from(this.index + 1);
}
}
8 changes: 0 additions & 8 deletions src/main/java/Ladder.java

This file was deleted.

65 changes: 65 additions & 0 deletions src/main/java/LadderCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.util.Random;

public class LadderCreator {
private Row[] rows;

public LadderCreator(Index rowNum, Index numberOfPerson, boolean isRandom) {
rows = new Row[rowNum.getNumber()];
SetRows(numberOfPerson);
if (isRandom) {
MakeRandomLine(numberOfPerson);
}
}

private void SetRows(Index numberOfPerson) {
for (int i=0; i < rows.length; i++) {
rows[i] = new Row(numberOfPerson);
}
}

private void MakeRandomLine(Index numberOfPerson) {
int numberOfLines = (int) (rows.length * numberOfPerson.getNumber() * 0.3);
Index rowsLength = Index.from(rows.length);

for (int i=0; i < numberOfLines; i++) {
PositionOfLine position = PositionOfLine.randomPosition(rowsLength, numberOfPerson);
PositionOfLine nextPosition = PositionOfLine.of(position.getX(), position.getY().nextIndex());

if (canDrawLine(position) && !LineAlreadyExisting(position.getX(), position.getY().nextIndex())) { drawLine(position); continue;}
i--;
}
}

public void drawLine(PositionOfLine position) {
canDrawLine(position);
Index x = position.getX();
Index y = position.getY();

rows[x.getNumber()].setValue(y, Direction.RIGHT);
rows[x.getNumber()].setValue(y.nextIndex(), Direction.LEFT);
}

public boolean canDrawLine(PositionOfLine position) {
Index x = position.getX();
Index y = position.getY();

if (LineAlreadyExisting(x, y) || validateRangeOfIndex(x, y)) {
return false;
}

return true;
}

public boolean LineAlreadyExisting(Index x, Index y) {
Node node = rows[x.getNumber()].getValue(y);
return node.isLeft() || node.isRight();
}

private boolean validateRangeOfIndex(Index x, Index y) {
return x.getNumber() >= rows.length || y.getNumber() >= rows[0].getPeopleNum() - 1;
}

public Row[] getRow() {
return rows;
}
}
14 changes: 14 additions & 0 deletions src/main/java/LadderGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class LadderGame {
private LadderCreator ladderCreator;
private RunGame runGame;
Comment on lines +2 to +3

Choose a reason for hiding this comment

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

클래스를 final로 선언해서 불변 객체로 만들면 어떨까요?


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

public int run(int startLine) {
runGame = new RunGame(ladderCreator.getRow());

return runGame.run(startLine);
}
}
19 changes: 19 additions & 0 deletions src/main/java/LadderGameFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class LadderGameFactory {

Choose a reason for hiding this comment

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

객체지향을 살릴 수 있는 디자인 패턴중 팩터리 메서드 패턴이란 것이 있습니다!
팩터리 메서드 패턴은 클래스의 인스턴스 생성을 서브 클래스에게 맡기는 것인데요!
지금 이 코드는 메서드가 겹쳐 코드의 중복도가 높은 것 같습니다.

  1. LadderGameFactory를 추상 클래스(abstract class)로 선언합니다.
  2. 추상 클래스 내에 공통으로 게임을 만드는 createGame() 메서드를 선언합니다.
  3. createLadderGame 클래스와 createRandomLadderGame 클래스는 1번 추상 메서드를 확장하여 각각의 인스턴스를 반환합니다.
  4. 구상 클래스(createLadderGame, createRandomLadderGame) 클래스의 createGame() 메서드를 통해 객체를 반환받습니다.

이런식으로 하면 코드를 확장할때도, 객체지향적으로 용이해보이네요!
디자인 패턴 중 팩터리 메서드 패턴에 대해 공부해보시면 좋을 것 같아요!

static LadderGame ladderGame;

public static LadderGame createLadderGame(Index rowNum, Index numberOfPerson) {
LadderCreator ladderCreator = new LadderCreator(rowNum, numberOfPerson, false);
ladderGame = new LadderGame(ladderCreator);

return ladderGame;
}

public static LadderGame createRandomLadderGame(Index rowNum, Index numberOfPerson) {
LadderCreator ladderCreator = new LadderCreator(rowNum, numberOfPerson, true);
ladderGame = new LadderGame(ladderCreator);

return ladderGame;
}


}
31 changes: 31 additions & 0 deletions src/main/java/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class Node {
private Direction direction;

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

public Direction getDirection() {
return direction;
}

public void SetNode(Direction direction) {
this.direction = direction;
}

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

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

public boolean isZero() {
return direction == Direction.NONE;
}

public void printDirectionNumber() {
System.out.print(direction.getDirection());
}
}
39 changes: 39 additions & 0 deletions src/main/java/PositionOfLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.Random;

public class PositionOfLine {
private Index x;
private Index y;

private PositionOfLine(Index x, Index y) {
this.x = x;
this.y = y;
}

public static PositionOfLine of(Index x, Index y) {
return new PositionOfLine(x, y);
}

public static PositionOfLine randomPosition(Index rowsNum, Index colsNum) {
Random rand = new Random();
Index x = Index.from(rand.nextInt(rowsNum.getNumber()));
Index y = Index.from(rand.nextInt(colsNum.getNumber()));

return PositionOfLine.of(x, y);
}

public Index getX() {
return x;
}

public Index getY() {
return y;
}





}


//데이터를 감싸는 객체는 세터 게터가 불가피
Loading