-
Notifications
You must be signed in to change notification settings - Fork 53
[사다리] 최보현 미션 제출합니다. #64
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 사다리 - 함수형 프로그래밍 | ||
|
||
### [1단계 - 사다리 출력] | ||
|
||
**도메인 요구사항** | ||
|
||
1. 사다리 정보를 담는 도메인 객체가 있어야 한다. | ||
2. 사다리는 높이와 너비가 고정되어 있다. | ||
3. 사다리 내의 위치(좌표)를 표현하는 도메인 객체가 있어야 한다. | ||
4. 가로 라인이 겹치는 경우 (|-----|-----|) 이동이 불가하다. | ||
|
||
**게임 진행 방식** | ||
|
||
1. 사다리는 위에서 아래로 한 행씩 이동한다. | ||
2. 왼쪽과 오른쪽 중 연결선이 먼저 확인된 곳으로 이동한다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import controller.LadderController; | ||
|
||
public class App { | ||
public static void main(String[] args) { | ||
LadderController controller = new LadderController(); | ||
controller.runLadder(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package controller; | ||
|
||
import domain.Ladder; | ||
import domain.Size; | ||
import domain.service.LadderService; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
public class LadderController { | ||
|
||
private final OutputView outputView = new OutputView(); | ||
|
||
public void runLadder() { | ||
|
||
int width = InputView.enterLadderWidth(); | ||
int height = InputView.enterLadderHeight(); | ||
|
||
Size size = new Size(height, width); | ||
LadderService ladderService = new LadderService(size); | ||
|
||
Ladder ladder = ladderService.createLadder(); | ||
outputView.printLadder(ladder); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package domain; | ||
|
||
import java.util.List; | ||
|
||
public class Ladder { | ||
private final List<Line> lines; | ||
private final int width; | ||
|
||
public Ladder(List<Line> lines, int width) { | ||
this.lines = lines; | ||
this.width = width; | ||
} | ||
|
||
public List<Line> getLines() { | ||
return List.copyOf(lines); | ||
} | ||
|
||
public int getWidth() { | ||
return width; | ||
} | ||
|
||
public int getHeight() { | ||
return lines.size(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package domain; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. domain 패키지 내에, model 또는 service 패키지로 분리된 클래스들도 있고, 따로 분리되지 않은 클래스들(ex. Line)도 있는데요. |
||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Line { | ||
|
||
private final List<Boolean> connection; | ||
|
||
public Line(List<Boolean> connection) { | ||
this.connection = new ArrayList<Boolean>(connection); | ||
} | ||
|
||
public boolean hasConnection(int index) { | ||
return connection.get(index); | ||
} | ||
|
||
public int getSize() { | ||
return connection.size(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package domain; | ||
|
||
public class Size { | ||
|
||
private final int height; | ||
private final int width; | ||
private static final String LADDER_SIZE_INVALID_MESSAGE = "높이와 넓이는 모두 1 이상이어야 합니다."; | ||
Comment on lines
+3
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. height와 width를 관리하는 Size 객체를 만들어주셨군요👍 멤버변수와 상수의 선언 순서를 신경써주시면 좋을 것 같네요! |
||
|
||
public Size(int height, int width) { | ||
validate(height, width); | ||
this.height = height; | ||
this.width = width; | ||
} | ||
|
||
private void validate(int height, int width) { | ||
if (height < 1 || width < 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 매직넘버는 상수로 관리해보면 좋을 것 같아요! |
||
throw new IllegalArgumentException(LADDER_SIZE_INVALID_MESSAGE); | ||
} | ||
} | ||
|
||
public int getHeight() { | ||
return height; | ||
} | ||
|
||
public int getWidth() { | ||
return width; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package domain.model; | ||
|
||
public enum LadderSymbol { | ||
VERTICAL("|"), | ||
HORIZONTAL("-----"), | ||
EMPTY(" "); | ||
|
||
private final String symbol; | ||
|
||
LadderSymbol(String symbol) { | ||
this.symbol = symbol; | ||
} | ||
|
||
public String getSymbol() { | ||
return symbol; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class LadderGenerator { | ||
|
||
private final Random random = new Random(); | ||
|
||
public List<Line> generateLadder(int height, int width) { | ||
List<Line> ladderLine = new ArrayList<>(); | ||
for (int i = 0; i < height; i++) { | ||
ladderLine.add(generateLine(width)); | ||
} | ||
return ladderLine; | ||
} | ||
|
||
private Line generateLine(int width) { | ||
List<Boolean> connections = new ArrayList<>(); | ||
|
||
for (int i = 0; i < width - 1; i++) { | ||
boolean canConnect = !hasPreviousConnection(connections, i); | ||
boolean connection = canConnect && random.nextBoolean(); | ||
connections.add(connection); | ||
} | ||
|
||
return new Line(connections); | ||
} | ||
|
||
private boolean hasPreviousConnection(List<Boolean> connections, int currentIndex) { | ||
if (currentIndex == 0) { | ||
return false; | ||
} | ||
return connections.get(currentIndex - 1); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package domain.service; | ||
|
||
import domain.Ladder; | ||
import domain.LadderGenerator; | ||
import domain.Line; | ||
import domain.Size; | ||
import java.util.List; | ||
|
||
public class LadderService { | ||
private final LadderGenerator generator; | ||
private final Size size; | ||
|
||
public LadderService(Size size) { | ||
this.generator = new LadderGenerator(); | ||
this.size = size; | ||
} | ||
Comment on lines
+9
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ladder를 생성하는 LadderService를 만들어주셨군요! 추가적으로 LadderGenerator, Size를 파라미터로 받는 생성자를 만들고, |
||
|
||
public Ladder createLadder() { | ||
int height = size.getHeight(); | ||
int width = size.getWidth(); | ||
List<Line> lines = generator.generateLadder(height, width); | ||
|
||
return new Ladder(lines, width); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package view; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
|
||
private static Scanner scanner = new Scanner(System.in); | ||
private static final String INPUT_WIDTH_MESSAGE = "사다리의 넓이는 몇 개인가요?"; | ||
private static final String INPUT_HEIGHT_MESSAGE = "사다리의 높이는 몇 개인가요?"; | ||
|
||
public static int enterLadderWidth() { | ||
System.out.println(INPUT_WIDTH_MESSAGE); | ||
return Integer.parseInt(scanner.nextLine()); | ||
} | ||
|
||
public static int enterLadderHeight() { | ||
System.out.println(INPUT_HEIGHT_MESSAGE); | ||
return Integer.parseInt(scanner.nextLine()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package view; | ||
|
||
import domain.Ladder; | ||
import domain.Line; | ||
import domain.model.LadderSymbol; | ||
|
||
public class OutputView { | ||
|
||
public static final String PRINT_RESULT_MESSAGE = "\n실행결과\n"; | ||
|
||
public void printLadder(Ladder ladder) { | ||
|
||
System.out.println(PRINT_RESULT_MESSAGE); | ||
|
||
for (Line line : ladder.getLines()) { | ||
printLadderLine(line, ladder.getWidth()); | ||
} | ||
} | ||
|
||
private void printLadderLine(Line line, int width) { | ||
StringBuilder result = new StringBuilder(); | ||
|
||
for (int i = 0; i < width; i++) { | ||
result.append(LadderSymbol.VERTICAL.getSymbol()); | ||
appendHorizontalSymbol(result, line, i, width); | ||
} | ||
System.out.println(result.toString()); | ||
} | ||
|
||
private void appendHorizontalSymbol(StringBuilder result, Line line, int index, int width) { | ||
if (isLastColumn(index, width)) { | ||
return; | ||
} | ||
|
||
if (line.hasConnection(index)) { | ||
result.append(LadderSymbol.HORIZONTAL.getSymbol()); | ||
return; | ||
} | ||
|
||
result.append(LadderSymbol.EMPTY.getSymbol()); | ||
} | ||
|
||
private boolean isLastColumn(int index, int width) { | ||
return index == width - 1; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import domain.Line; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LineTest { | ||
|
||
@Nested | ||
@DisplayName("hasConnection 동작 테스트") | ||
class HasConnectionTest { | ||
|
||
List<Boolean> input = Arrays.asList(true, false, true); | ||
Line line = new Line(input); | ||
|
||
@Test | ||
void hasConnection_가로선이있으면_true를반환한다() { | ||
|
||
assertTrue(line.hasConnection(0)); | ||
} | ||
|
||
@Test | ||
void hasConnection_가로선이없으면_false를반환한다() { | ||
|
||
assertFalse(line.hasConnection(1)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Size를 객체로 관리하고 있으니, Size 자체를 필드로 갖게해보면 어떨까요?
지금과 같은 구조라면 Ladder의 사이즈에 대한 검증을 추가적으로 해야할걸로 보여요.