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/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.

6 changes: 6 additions & 0 deletions .idea/kotlinc.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.

5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm'
}

group = 'kuit.com'
Expand All @@ -15,8 +16,12 @@ dependencies {

// Assertj 라이브러리 추가
testImplementation("org.assertj:assertj-core:3.26.3")
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(17)
}
8 changes: 8 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
pluginManagement {
plugins {
id 'org.jetbrains.kotlin.jvm' version '2.0.10'
}
}
plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0'
}
rootProject.name = 'kuit-ladder'

17 changes: 0 additions & 17 deletions src/main/java/ladder/LadderRunner.java

This file was deleted.

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

public class LadderSize {
private final GreaterThanOne numberOfPerson;
private final GreaterThanOne numberOfRow;

public LadderSize(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) {
this.numberOfPerson = numberOfPerson;
this.numberOfRow = numberOfRow;
}

public int calLineSize() {
return (int)(numberOfPerson.getNumber()*numberOfRow.getNumber()*0.3);
Copy link
Member

Choose a reason for hiding this comment

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

랜덤으로 그릴 사다리 라인 값을 정하는 로직이 LadderSize의 책임 중 하나인지 고민해 보시면 좋을 것 같습니다!

}

public GreaterThanOne getNumberOfPerson() {
return numberOfPerson;
}

public GreaterThanOne getNumberOfRow() {
return numberOfRow;
}
}
4 changes: 4 additions & 0 deletions src/main/java/ladder/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public void move(Position position) {
}
}

public void printNode(StringBuilder sb) {
sb.append(direction.getValue());
Copy link
Member

Choose a reason for hiding this comment

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

node의 값을 외부로 노출하지 않고 값을 append 하는 로직을 잘 작성해 주셨네요. 디미터 법칙을 잘 이해하신 것 같습니다!

}

public void setRightNode() {
direction = RIGHT;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/ladder/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ private static void validatePosition(int position) {
private static boolean isPosition(int position) {
return position >= 0;
}

public boolean isSamePosition(int i) {
Copy link
Member

Choose a reason for hiding this comment

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

equals 메서드를 오버라이드해서 구현하면 의미 전달이 더 잘 될 것 같습니다!

return this.position == i;
}
}
11 changes: 11 additions & 0 deletions src/main/java/ladder/PositionPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ladder;

public class PositionPair {
private final int col;
private final int row;
Copy link
Member

Choose a reason for hiding this comment

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

PositionPair 라면 인스턴스 변수로 int 대신 Position 클래스를 갖도록 구현해 보는건 어떠신가요?


public PositionPair(int row, int col) {
this.col = col;
this.row = row;
}
}
21 changes: 21 additions & 0 deletions src/main/java/ladder/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,25 @@ private boolean isLineAtNextPosition(Position position) {
return lineAtPosition;
}

public void printRow(StringBuilder sb) {
for (int i = 0; i < nodes.length; i++) {
nodes[i].printNode(sb);

Choose a reason for hiding this comment

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

노드 출력은 Node가 맡는다는 생각 좋은 것 같아요

sb.append(" ");
}
sb.append("\n");
}

public void printStarRow(Position position, StringBuilder sb) {
Copy link
Member

Choose a reason for hiding this comment

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

printRow와 printStarRow는 로직이 매우 비슷해 보입니다. printRow에서 position과 비교하여 *을 append하는 로직만을 메서드로 따로 분리하면 가독성이나 클라이언트 코드 입장에서도 어떻게 코드를 다뤄야 할지 명확할 것 같습니다.

for (int i = 0; i < nodes.length; i++) {
nodes[i].printNode(sb);

if (position.isSamePosition(i)) {

Choose a reason for hiding this comment

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

값 비교를 메소드로 작성하니 가독성도 늘고, 객체 내부에 어떤 데이터가 있는지 자세히 몰라도 되네요! 객체지향적인 코드의 장점이 보이죠?

sb.append("*");
}

sb.append(" ");
}
sb.append("\n");
}

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

public enum TimePeriod {

Choose a reason for hiding this comment

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

enum을 통해서 올바른 매개변수의 값만 사용할 수 있게 한 점 좋아요!

BEFORE("Before"), AFTER("After");
Copy link
Member

Choose a reason for hiding this comment

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

상수를 이넘으로 처리한 것이 인상깊네요!


private final String timePeriod;

TimePeriod(String timePeriod) {
this.timePeriod = timePeriod;
}

public String getTimePeriod() {
return timePeriod;
}
}
68 changes: 68 additions & 0 deletions src/main/java/ladder/creator/AutoLadderCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ladder.creator;

import ladder.*;

import java.util.HashSet;
import java.util.Random;

public class AutoLadderCreator implements LadderCreator {

private final Row[] rows;

public AutoLadderCreator(LadderSize ladderSize) {
GreaterThanOne numberOfRow = ladderSize.getNumberOfRow();
GreaterThanOne numberOfPerson = ladderSize.getNumberOfPerson();

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

drawAutoRandomLine(ladderSize, numberOfPerson, numberOfRow);

Choose a reason for hiding this comment

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

생성자에서 랜덤 사디리 그리기를 수행하는 점 괜찮은데요!

}

private void drawAutoRandomLine(LadderSize ladderSize, GreaterThanOne numberOfPerson, GreaterThanOne numberOfRow) {
//생성해야 하는 line 개수
int lineSize = ladderSize.calLineSize();

HashSet<PositionPair> set = new HashSet<>();
Copy link
Member

Choose a reason for hiding this comment

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

Set 같은 컬렉션에 들어가는 객체는 equals, hashCode 메서드가 구현되어 있어야 제대로 된 중복 검증이 가능합니다!

Random random = new Random();

int count = 0;

while (count < lineSize) {
//0~numberOfPerson-2 사이의 난수 생성 => 왼쪽에만 생성되기 때문에 2를 뺌!
int col = random.nextInt(numberOfPerson.getNumber()-1);

//0~numberOfRow-1 사이의 난수 생성
int row = random.nextInt(numberOfRow.getNumber());
PositionPair pair = new PositionPair(row, col);

//중복 확인
if(set.contains(pair)) {
Copy link
Member

Choose a reason for hiding this comment

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

반복문 조건식으로 count를 사용하는 것이 아니라 set.size() 를 사용했다면, 중복처리와 반복문 조건식 핸들링을 한 번에 할 수 있습니다!

continue;
}
Comment on lines +42 to +44

Choose a reason for hiding this comment

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

혹시 디버깅하면서 중복확인이 되는지 확인해보셨나요? 제가 찾아보니까 HashSet의 contains()는 두 객체의 주소가 같은지 비교하기 때문에 중복 확인이 안 될 수도 있을 것 같습니다


try{
this.drawLine(Position.from(row), Position.from(col));
set.add(pair);
count++;
} catch (IllegalArgumentException e) {
continue;
}


}
}

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

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

}
19 changes: 3 additions & 16 deletions src/main/java/ladder/creator/LadderCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,9 @@
import ladder.Position;
import ladder.Row;

public class LadderCreator {
public interface LadderCreator {

private final Row[] rows;
void drawLine(Position row, Position col);

public LadderCreator(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) {
rows = new Row[numberOfRow.getNumber()];
for (int i = 0; i < numberOfRow.getNumber(); i++) {
rows[i] = new Row(numberOfPerson);
}
}

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

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