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주차 미션 / 서버 3조 정유혁 #15

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

public class Ladder {

private Row[] rows;

public Ladder(int numberOfRows, int numberOfPerson) {
rows = new Row[numberOfPerson];

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

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

public int run(int position) {

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

return position;
}
}
//package ladder;
//
//public class Ladder {
//
// private Row[] rows;
//
// public Ladder(NaturalNumber numberOfRows, NaturalNumber numberOfPerson) {
// rows = new Row[numberOfPerson.getNum()];
//
// for(int i = 0; i < numberOfRows.getNum(); i++) {
// rows[i] = new Row(numberOfPerson);
// }
// }
//
// public void drawLine(Position row, Position col) {
// rows[row.getPosition()].drawLine(Position.of(col.getPosition()));
// }
//
// public int run(Position position) {
//
// for(int i = 0; i < rows.length; i++) {
// position = rows[i].nextPosition(position);
// }
//
// return position.getPosition();
// }
//}
60 changes: 0 additions & 60 deletions src/main/java/ladder/Row.java

This file was deleted.

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

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

private final int value;

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

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

import ladder.creator.LadderCreator;
import ladder.position.Position;

public class LadderGame {

private final LadderCreator ladderCreator;

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

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

public boolean isLineDrawn(int i, int j) {
return ladderCreator.getRows()[i].isLineDrawn(j);
}
}
18 changes: 18 additions & 0 deletions src/main/java/ladder/core/LadderGameFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ladder.core;

import ladder.creator.CustomLadderCreator;
import ladder.creator.RandomLadderCreator;

public class LadderGameFactory {
private LadderGameFactory() {}
public static LadderGame randomLaddergame(LadderSize ladderSize) {
CustomLadderCreator customLadderCreator = new CustomLadderCreator(ladderSize);
RandomLadderCreator randomLadderCreator = new RandomLadderCreator(customLadderCreator);
return new LadderGame(randomLadderCreator);
}

static LadderGame customLadderGame(LadderSize ladderSize) {
CustomLadderCreator customLadderCreator = new CustomLadderCreator(ladderSize);
return new LadderGame(customLadderCreator);
}
}
37 changes: 37 additions & 0 deletions src/main/java/ladder/core/LadderRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ladder.core;

import ladder.position.LadderPosition;
import ladder.position.Position;

public class LadderRunner {
private Row[] rows;

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

public int run(Position position) {

for(int i = 0; i < rows.length; i++) {
printLadder(LadderPosition.of(Position.of(i), position), "Before");
position = rows[i].nextPosition(position);
printLadder(LadderPosition.of(Position.of(i), position), "After");
}

return position.getPosition();
}

public void printLadder(LadderPosition currentRowPosition, String label) {
System.out.println(label);
String ladder = generate(currentRowPosition);
System.out.println(ladder);
}

public String generate(LadderPosition currentPosition) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < rows.length; i++) {
rows[i].generateRow(sb, Position.of(i), currentPosition);
}
return sb.toString();
}
}
26 changes: 26 additions & 0 deletions src/main/java/ladder/core/LadderSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ladder.core;

public class LadderSize {
private final NaturalNumber height;
private final NaturalNumber width;

private LadderSize(NaturalNumber height, NaturalNumber width) {
this.height = height;
this.width = width;
}

public int getHeight() {
return height.getNum();
}
public int getWidth() {
return width.getNum();
}

public static LadderSize of(NaturalNumber height, NaturalNumber width) {
return new LadderSize(height, width);
}

public int getNumberOfNodes() {
return height.getNum() * width.getNum();
}
}
26 changes: 26 additions & 0 deletions src/main/java/ladder/core/NaturalNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ladder.core;

import ladder.exception.ExceptionMessage;

public class NaturalNumber {
private final int num;

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

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

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

public int getNum(){ //원시값 반환
return num;
}
}
41 changes: 41 additions & 0 deletions src/main/java/ladder/core/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ladder.core;

import ladder.position.Position;

public class Node {
private Direction direction;

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

public Direction getDirection(){
return 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;
}

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

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

public void appendSymbol(StringBuilder sb) {
sb.append(this.direction.getValue());
}
}
Loading