Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1e8dfa4
Update .gitignore
deocksookim Jan 11, 2020
745093f
사다리게임 입력 받기 기능 구현
taesikyoo Jan 16, 2020
6ba8dd9
이름 입력 시 길이 제한 기능 구현
taesikyoo Jan 17, 2020
9422068
사다리 구현 (1)
taesikyoo Jan 17, 2020
09e5724
refactor: package 구조를 mvc로 변경
taesikyoo Jan 26, 2020
93fe5e8
refactor: InputScanner의 코드를 InputView로 추출
taesikyoo Jan 26, 2020
d260eb2
refactor: LadderGame의 코드를 LadderGameController로 추출
taesikyoo Jan 26, 2020
34a673b
feat: LadderGameService와 LadderHeight 객체 추가
taesikyoo Jan 26, 2020
5c91317
doc: 기능 구현 목록 추가
taesikyoo Jan 26, 2020
ed98290
feat: 게임 참여자 이름을 받아서 Players 객체를 생성
taesikyoo Jan 27, 2020
16ca1e4
feat: 사다리 높이를 받아서 LadderHeight 객체를 생성
taesikyoo Jan 27, 2020
4ce42af
refactor: Ladder 객체에 팩토리 메서드 추가
taesikyoo Jan 27, 2020
02dc9de
refactor: LadderLine과 Point 객체 수정
taesikyoo Jan 30, 2020
12ba062
feat: LadderCreationStrategy 인터페이스 추가
taesikyoo Jan 30, 2020
a092efe
feat: 사다리 생성 기능 구현
taesikyoo Jan 30, 2020
c975af2
test: PointTest 추가
taesikyoo Jan 30, 2020
a538a99
test: LadderLineTest 추가
taesikyoo Jan 30, 2020
dc460ca
doc: 기능구현목록에 '이름이 공백일 수 없음' 추가
taesikyoo Jan 31, 2020
37a54e6
feat: 도메인 객체에 getter 추가
taesikyoo Jan 31, 2020
895cced
refactor: Player와 Point의 일부 메서드 접근자 수정
taesikyoo Jan 31, 2020
737fd27
fix: LadderHeight 객체 생성자의 길이검사 에러 수정
taesikyoo Jan 31, 2020
05a7d11
feat: OutputView에서 사다리를 출력하는 기능 구현
taesikyoo Jan 31, 2020
ca27cae
feat: OutputView에서 사다리를 출력하는 기능 구현(2)
taesikyoo Jan 31, 2020
a436477
refactor : OutputView의 일부 메서드를 Players로 이주
taesikyoo Jan 31, 2020
51c66b0
refactor: OutputView의 일부 메서드를 LadderLine과 Point로 이주
taesikyoo Jan 31, 2020
fa9783b
doc: 기능구현목록에 구현할 기능들 추가
taesikyoo Jan 31, 2020
e34411d
feat: InputView에서 게임 결과를 받아오는 로직 추가
taesikyoo Jan 31, 2020
1c66463
style: Ladder 패키지 안의 Strategy 패키지의 이름을 strategy로 변경
taesikyoo Jan 31, 2020
cf3b3b1
feat: Results 객체 추가
taesikyoo Jan 31, 2020
8911a2c
feat: Result 객체 추가
taesikyoo Jan 31, 2020
8bc1632
feat: Result의 개수와 Player의 수가 같은지 체크하는 로직 추가
taesikyoo Jan 31, 2020
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/**
.gradle/**
build/
build/**
out/**
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
# java-ladder 게임
# 사다리 게임
## 기능 구현 목록
### 참여자 이름 입력
- [x] 여러 사람의 이름은 쉼표(,)를 기준으로 구분
- [x] 참여자의 이름은 최대 5글자
- [x] 앞 뒤 공백을 제거
- [x] 중복된 이름이 있는 경우 예외 처리
- [ ] 이름이 공백일 수 없음

### 실행결과 입력
- [x] 여러 결과는 쉼표(,)를 기준으로 구분
- [x] 결과는 최대 5글자
- [x] 앞 뒤 공백을 제거
- [ ] 결과가 공백일 수 없음
- [ ] 참여자 수와 결과의 수가 같아야 함

### 사다리 높이 입력
- [x] 사다리 높이는 1 이상이여야함

### 사다리 생성
- [x] 사다리의 라인이 겹치지 않도록 사다리를 생성
e.g. |-----|-----|  모양과 같이 가로 라인이 겹치는 경우 어느 방향으로 이동할
지 결정할 수 없다.

### 사다리 출력
- [x] 사다리 위에 사람 이름을 출력
- [x] 사람 이름을 5자 기준으로 출력
- [x] 사다리 폭 또한 사람 이름을 기준으로 함
- [ ] 사다리 아래에 결과를 출력
- [ ] 결과를 5자 기준으로 출력

### 사다리게임 결과 출력
- [ ] 'all'을 입력시 전체참여자의 실행결과를 출력
- [ ] 개인의 이름을 입력시 해당하는 참여자의 실행결과를 출력
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testCompile "org.assertj:assertj-core:3.14.0"
compile 'org.apache.commons:commons-lang3:3.9'
}
6 changes: 3 additions & 3 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ echo location of your Java installation.
goto fail

:init
@rem Get command-line arguments, handling Windows variants
@rem Get command-ladderLine arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
@rem Slurp the command ladderLine arguments.
set CMD_LINE_ARGS=
set _SKIP=2

Expand All @@ -61,7 +61,7 @@ if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line
@rem Setup the command ladderLine

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rootProject.name = 'java-racingcar'
rootProject.name = 'java-ladder'

Empty file removed src/main/java/empty.txt
Empty file.
13 changes: 13 additions & 0 deletions src/main/java/laddergame/LadderGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package laddergame;

import laddergame.controller.LadderGameController;
import laddergame.view.InputView;
import laddergame.view.OutputView;

public class LadderGame {

public static void main(String[] args) {
LadderGameController ladderGameController = new LadderGameController(new InputView(), new OutputView());
ladderGameController.run();
}
}
39 changes: 39 additions & 0 deletions src/main/java/laddergame/controller/LadderGameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package laddergame.controller;

import laddergame.domain.ladder.Ladder;
import laddergame.domain.ladder.strategy.RandomLadderCreationStrategy;
import laddergame.domain.ladderheight.LadderHeight;
import laddergame.domain.player.Players;
import laddergame.domain.result.Results;
import laddergame.service.LadderGameService;
import laddergame.view.InputView;
import laddergame.view.OutputView;

public class LadderGameController {

private final InputView inputView;
private final OutputView outputView;
private final LadderGameService ladderGameService = new LadderGameService();

public LadderGameController(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}

public void run() {
String playerNames = inputView.getPlayerNamesFromConsole();
Players players = ladderGameService.createPlayers(playerNames);

String gameResults = inputView.getGameResultFromConsole();
Results results = ladderGameService.createResults(gameResults, players.size());

int ladderHeightInput = inputView.getLadderHeightFromConsole();
LadderHeight ladderHeight = ladderGameService.createLadderHeight(ladderHeightInput);

Ladder ladder = ladderGameService.createLadder(players, ladderHeight, new RandomLadderCreationStrategy());

//LadderGame ladderGame = ladderGameService.createLadderGame(players, results, ladder);

outputView.printResult(players, ladder);
}
}
34 changes: 34 additions & 0 deletions src/main/java/laddergame/domain/ladder/Ladder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package laddergame.domain.ladder;

import laddergame.domain.ladder.strategy.LadderCreationStrategy;
import laddergame.domain.ladderheight.LadderHeight;
import laddergame.domain.player.Players;

import java.util.ArrayList;
import java.util.List;

public class Ladder {

private final List<LadderLine> lines;

private Ladder(final List<LadderLine> lines) {
this.lines = lines;
}

public static Ladder with(final Players players, final LadderHeight ladderHeight, LadderCreationStrategy strategy) {
List<LadderLine> lines = new ArrayList<>();
int width = players.size();
int height = ladderHeight.getHeight();

for (int i = 0; i < height; i++) {
LadderLine line = LadderLine.with(width, strategy);
lines.add(line);
}

return new Ladder(lines);
}

public List<LadderLine> getLines() {
return lines;
}
}
93 changes: 93 additions & 0 deletions src/main/java/laddergame/domain/ladder/LadderLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package laddergame.domain.ladder;

import laddergame.domain.ladder.strategy.LadderCreationStrategy;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class LadderLine {

public static final String POINT= "|";
public static final String CONNECTION= "-----";
public static final String NON_CONNECTION= " ";

private final List<Point> points;

public LadderLine(final List<Point> points) {
this.points = points;
}

public static LadderLine with(final int width, LadderCreationStrategy strategy) {
List<Point> points = new ArrayList<>();
Point previousPoint = null;
Copy link

Choose a reason for hiding this comment

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

저번 모임때도 전체적으로 이야기가 잠깐 나왔던거같은데, null 초기화는 굉장히 위험합니다.
NPE(NullPointerException)가 나올 수 있는 가능성은 원천 배제하는 것이 좋습니다!
정말 필요한 상황이라면 어쩔 수 없지만, 정말 필요한 상황이 정말 있을지는 의문이네요


for (int i = 0; i < width; i++) {
int remain = width - i;
Point point = generatePoint(previousPoint, remain, strategy);
points.add(point);
previousPoint = point;
}

return new LadderLine(points);
}

private static Point generatePoint(Point point, int remain, LadderCreationStrategy strategy) {
if (isFirstPoint(point)) {
return Point.generateFirstPoint(strategy);
}

if (isLastPoint(remain)) {
return Point.generateLastPoint(point);
}

return point.decideNextPoint(strategy);
}

private static boolean isFirstPoint(Point point) {
return point == null;
}

private static boolean isLastPoint(int remain) {
return remain == 1;
}

public int size() {
return points.size();
}

public String lineToString() {
List<Point> points = getPoints();

String result = " ";
for (Point point : points) {
String connectionString = point.connectionToString();
result += (POINT + connectionString);
}
return result;
}
Comment on lines +59 to +68
Copy link

Choose a reason for hiding this comment

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

저도 옛날에 비슷하게 구현했던 적이 있었는데요,
한번 더 생각해보면 사다리의 출력(Output)이 어떻게 보일 것인지는 사다리의 책임이 아니라는 생각을 해볼 수 있습니다.
지금 코드를 기반으로 콘솔출력이 아니라 웹에다 표현하려고 하면 어떻게 될까요? :)


public List<Point> getPoints() {
return points;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LadderLine that = (LadderLine) o;
return Objects.equals(points, that.points);
}

@Override
public int hashCode() {
return Objects.hash(points);
}

@Override
public String toString() {
return "LadderLine{" +
"points=" + points +
'}';
}
}
63 changes: 63 additions & 0 deletions src/main/java/laddergame/domain/ladder/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package laddergame.domain.ladder;

import laddergame.domain.ladder.strategy.LadderCreationStrategy;

public enum Point {

LEFT(true, false),
RIGHT(false, true),
NONE(false, false);
Comment on lines +5 to +9
Copy link

Choose a reason for hiding this comment

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

👍


private boolean leftConnection;
private boolean rightConnection;

Point(boolean leftConnection, boolean rightConnection) {
this.leftConnection = leftConnection;
this.rightConnection = rightConnection;
}

public static Point generateFirstPoint(LadderCreationStrategy strategy) {
return generatePoint(false, strategy.isConnectable());
}

public static Point generateLastPoint(Point point) {
if (point.hasRightConnection()) {
return generatePoint(true, false);
}
return generatePoint(false, false);
Comment on lines +24 to +27
Copy link

Choose a reason for hiding this comment

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

return point.hasRightConnection() ? LEFT : NONE;

요렇게 하면 훨씬 깔끔하지 않을까요?ㅎㅎ
다른 메소드에도 generatePoint() 를 거치지 않고도 간단히 할 수 있는 부분들이 있네요!

}

public Point decideNextPoint(LadderCreationStrategy strategy) {
if (rightConnection) {
return generatePoint(true, false );
}
return generatePoint(false, strategy.isConnectable());
}

private static Point generatePoint(boolean leftConnection, boolean rightConnection) {
if (leftConnection && rightConnection) {
throw new IllegalArgumentException();
}

if (leftConnection) {
return LEFT;
}

if (rightConnection) {
return RIGHT;
}

return NONE;
}

public boolean hasRightConnection() {
return rightConnection;
}

public String connectionToString() {
if (hasRightConnection()) {
return LadderLine.CONNECTION;
}
return LadderLine.NON_CONNECTION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package laddergame.domain.ladder.strategy;

public interface LadderCreationStrategy {

boolean isConnectable();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package laddergame.domain.ladder.strategy;

public class ManualLadderCreationStrategy implements LadderCreationStrategy {

@Override
public boolean isConnectable() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package laddergame.domain.ladder.strategy;

import java.util.Random;

public class RandomLadderCreationStrategy implements LadderCreationStrategy{

@Override
public boolean isConnectable() {
return new Random().nextBoolean();
}
}
24 changes: 24 additions & 0 deletions src/main/java/laddergame/domain/ladderheight/LadderHeight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package laddergame.domain.ladderheight;

public class LadderHeight {

private static final int LADDER_HEIGHT_MIN_LENGTH = 1;

private final int height;

public LadderHeight(int height) {
if (!isLongerThanMinLength(height)) {
throw new IllegalArgumentException();
}

this.height = height;
}

private boolean isLongerThanMinLength(int height) {
return height >= LADDER_HEIGHT_MIN_LENGTH;
}

public int getHeight() {
return height;
}
}
Loading