Skip to content

[사다리 - 4단계] 정민주 미션 제출합니다. #25

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

Merged
merged 9 commits into from
Nov 26, 2024
Merged
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
37 changes: 28 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
- 사다리의 크기를 입력받아 생성할 수 있다.
### step3
- 사다리를 보여준 후 결과를 출력한다.
### step4
- 사다리 게임에 참여하는 사람에 이름을 최대 5글자까지 부여할 수 있다. 사다리를 출력할 때 사람 이름도 같이 출력한다.
- 사람 이름은 쉼표(,)를 기준으로 구분한다.
- 개인별 이름을 입력하면 개인별 결과를 출력하고, "all"을 입력하면 전체 참여자의 실행 결과를 출력한

**📠 도메인 분석 내용**
- 사용자는 "참여 인원 수"를 입력한다 (네이버엔 구현되어있지만 실행예시엔 해당 없음)
Expand Down Expand Up @@ -47,23 +51,38 @@

## 실행결과
```
사다리의 넓이는 몇 개인가요?
4
참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)
neo,brown,brie,tommy

사다리의 높이는 몇 개인가요?
실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)
꽝,5000,꽝,3000

최대 사다리 높이는 몇 개인가요?
5

실행결과
사다리 결과

neo brown brie tommy
|-----| |-----|
| |-----| |
|-----| | |
| |-----| |
|-----| |-----|

0 -> 0
1 -> 3
2 -> 2
3 -> 1
꽝 5000 꽝 3000

결과를 보고 싶은 사람은?
neo

실행 결과

결과를 보고 싶은 사람은?
all

실행 결과
neo : 꽝
brown : 3000
brie : 꽝
tommy : 5000

```
2 changes: 1 addition & 1 deletion src/main/java/LadderGameApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
public class LadderGameApplication {

public static void main(String[] args) {
final RandomRungsBuilder randomRungsBuilder = new RandomRungsBuilder();
RandomRungsBuilder randomRungsBuilder = new RandomRungsBuilder();

LadderGameController ladderGameController = new LadderGameController(randomRungsBuilder);
ladderGameController.start();
Expand Down
33 changes: 24 additions & 9 deletions src/main/java/controller/LadderGameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import domain.Height;
import domain.Ladder;
import domain.RungsBuilder;
import java.util.List;
import java.util.Map;
import service.LadderService;
import view.InputView;
import view.OutputView;
Expand All @@ -21,24 +23,37 @@ public LadderGameController(RungsBuilder rungsBuilder) {
}

public void start() {
CountOfLine countOfLine = getcountOfLine();
List<String> names = getNames();
List<String> outcomes = getOutcomes();
Height height = getHeight();

Ladder ladder = laddersService.createLadder(countOfLine, height);
outputView.printStatusOfLadders(ladder.getRightRungStatus(), height.value());
outputView.printResult(ladder.getResult());
Ladder ladder = laddersService.createLadder(height, names, outcomes);
outputView.printStatusOfLadders(names, outcomes, ladder.getRightRungStatus(), height.value());
printResult(ladder.getResult());
}

private CountOfLine getcountOfLine() {
outputView.printInputCountOfLineGuide();
final int valueOfCountOfLine = inputView.getUserIntegerInput();
return new CountOfLine(valueOfCountOfLine);
private List<String> getNames() {
outputView.printInputNamesGuide();
return inputView.getStringList();
}

private List<String> getOutcomes() {
outputView.printInputOutcomesGuid();
return inputView.getStringList();
}


private Height getHeight() {
outputView.printInputHeightGuide();
final int valueOfHeight = inputView.getUserIntegerInput();
int valueOfHeight = inputView.getUserIntegerInput();
return new Height(valueOfHeight);
}

private void printResult(Map<String, String> result) {
outputView.printInputTargetName();
String targetName = inputView.getString();
Map<String, String> resultToPrint = laddersService.getResultToPrint(result, targetName);
outputView.printResult(resultToPrint);
}

}
24 changes: 18 additions & 6 deletions src/main/java/domain/Ladder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package domain;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import util.Errors;

Expand Down Expand Up @@ -30,9 +32,9 @@ private void validateLaddersHeight(List<Line> lines) {
}

private void validatePointStatus(List<Line> lines) {
for (int index = 0; index < lines.size()-1; index++) {
for (int index = 0; index < lines.size() - 1; index++) {
Line nowLine = lines.get(index);
Line nextLine = lines.get(index+1);
Line nextLine = lines.get(index + 1);
validateRungInSamePosition(nowLine, nextLine);
}
}
Expand All @@ -44,7 +46,7 @@ private void validateRungInSamePosition(Line nowLine, Line nextLine) {
}

private boolean isRungInSamePosition(Line nowLine, Line nextLine) {
final int maxPosition = nowLine.getHeight();
int maxPosition = nowLine.getHeight();
return IntStream.range(0, maxPosition)
.allMatch(
position -> nowLine.isConnectedToRightLineAt(position) == nextLine.isConnectedToLeftLineAt(position));
Expand All @@ -60,13 +62,15 @@ public int getHeight() {
return this.lines.get(0).getHeight();
}

public List<Integer> getResult() {
List<Integer> result = new ArrayList<>();
public Map<String, String> getResult() {
Map<String, String> result = new LinkedHashMap<>();
int height = this.getHeight();

for (int nowIndex = 0; nowIndex < lines.size(); nowIndex++) {
int finalIndex = calculateTargetIndex(nowIndex, height);
result.add(finalIndex);
String name = getNameOfLine(nowIndex);
String outcome = getOutcomeOfLine(finalIndex);
result.put(name, outcome);
}
return result;
}
Expand All @@ -89,4 +93,12 @@ private int getNextIndex(int currentIndex, int position) {
}
return currentIndex;
}

private String getNameOfLine(int index) {
return lines.get(index).getName();
}

private String getOutcomeOfLine(int index) {
return lines.get(index).getOutcome();
}
}
24 changes: 19 additions & 5 deletions src/main/java/domain/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,34 @@

public class Line {

private final Player player;
private final String outcome;

private final List<Point> points;

private Line(List<Point> points) {
private Line(Player player, String outcome, List<Point> points) {
this.player = player;
this.outcome = outcome;
this.points = points;
}

public static Line of(List<Boolean> leftRungsStatus, List<Boolean> rightRungsStatus) {
public static Line of(Player player, String outcome, List<Boolean> leftRungsStatus, List<Boolean> rightRungsStatus) {
validateHeight(leftRungsStatus, rightRungsStatus);

int maxPosition = leftRungsStatus.size();
List<Point> points = new ArrayList<>();
for (int position = 0; position < maxPosition; position++) {
points.add(new Point(leftRungsStatus.get(position), rightRungsStatus.get(position)));
}
return new Line(points);
return new Line(player, outcome, points);
}

private static void validateHeight(List<Boolean> leftRungsStatus, List<Boolean> rightRungsStatus) {
if (leftRungsStatus.size() != rightRungsStatus.size()) {
throw new IllegalArgumentException(Errors.RUNG_STATUS_LENGTH_MUST_MATCH);
}
}

public List<Boolean> getRightStatus() {
List<Boolean> rightStatus = new ArrayList<>();
for (Point point : points) {
Expand All @@ -40,13 +46,21 @@ public int getHeight() {
return this.points.size();
}

public String getName() {
return player.getName();
}

public String getOutcome() {
return outcome;
}

public boolean isConnectedToLeftLineAt(int position) {
final Point nowPoint = this.points.get(position);
Point nowPoint = this.points.get(position);
return nowPoint.isConnectedToLeftLadder();
}

public boolean isConnectedToRightLineAt(int position) {
final Point nowPoint = this.points.get(position);
Point nowPoint = this.points.get(position);
return nowPoint.isConnectedToRightLadder();
}

Expand Down
25 changes: 25 additions & 0 deletions src/main/java/domain/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package domain;

import util.Errors;

public class Player {

private final int MAX_LENGTH = 5;

private final String name;

public Player(String name) {
validate(name);
this.name = name;
}

public String getName() {
return name;
}

private void validate(String name) {
if (name.length() > MAX_LENGTH) {
throw new IllegalArgumentException(Errors.NAME_IS_TOO_LONG);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/domain/RandomRungsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public RandomRungsBuilder() {
public List<Boolean> buildAndGetRungsStatus(List<Boolean> prevRungsStatus) {
List<Boolean> rungsStatus = new ArrayList<>();
for (Boolean doesPrevLadderHaveRung : prevRungsStatus) {
final boolean nowRungsStatus = generateRungIfAbsent(doesPrevLadderHaveRung);
boolean nowRungsStatus = generateRungIfAbsent(doesPrevLadderHaveRung);
rungsStatus.add(nowRungsStatus);
}
return rungsStatus;
Expand Down
67 changes: 56 additions & 11 deletions src/main/java/service/LadderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import domain.Height;
import domain.Ladder;
import domain.Line;
import domain.Player;
import domain.RungsBuilder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import util.Errors;

public class LadderService {

Expand All @@ -18,17 +22,22 @@ public LadderService(RungsBuilder rungsBuilder) {
this.rungsBuilder = rungsBuilder;
}

public Ladder createLadder(CountOfLine countOfLine, Height height) {
final List<Line> lineCollection = createLineCollection(countOfLine, height);
public Ladder createLadder(Height height, List<String> names, List<String> outcomes) {
final List<Player> players = getPlayers(names);
CountOfLine countOfLine = getcountOfLine(players, outcomes);
List<Line> lineCollection = createLineCollection(countOfLine, height, players, outcomes);
return new Ladder(lineCollection);
}

private List<Line> createLineCollection(CountOfLine countOfLine, Height height) {
final List<Line> lineCollection = new ArrayList<>();
private List<Line> createLineCollection(CountOfLine countOfLine, Height height, List<Player> players,
List<String> outcomes) {
List<Line> lineCollection = new ArrayList<>();

for (int index = 0; index < countOfLine.value(); index++) {
final List<Boolean> prevLineRightStatus = getPrevLineRightStatus(lineCollection, index, height);
final Line nowLine = createNowLine(index, height, countOfLine, prevLineRightStatus);
List<Boolean> prevLineRightStatus = getPrevLineRightStatus(lineCollection, index, height);
Player player = players.get(index);
String outcome = outcomes.get(index);
Line nowLine = createNowLine(index, height, countOfLine, prevLineRightStatus, player, outcome);
lineCollection.add(nowLine);
}
return lineCollection;
Expand All @@ -38,18 +47,18 @@ private List<Boolean> getPrevLineRightStatus(List<Line> lineCollection, int inde
if (index == 0) {
return rungsBuilder.buildTemporaryRungsStatus(height.value());
}
final Line prevLine = lineCollection.get(index - 1);
Line prevLine = lineCollection.get(index - 1);
return prevLine.getRightStatus();
}

private Line createNowLine(int index, Height height, CountOfLine countOfLine,
List<Boolean> nowLineLeftStatus) {
final List<Boolean> nowLineRightStatus = createNowLineRightStatus(index, countOfLine, height,
nowLineLeftStatus);
List<Boolean> nowLineLeftStatus, Player player, String outcome) {
List<Boolean> nowLineRightStatus = createNowLineRightStatus(index, countOfLine, height,
nowLineLeftStatus);
if (index == 0) {
nowLineLeftStatus = createEmptyStatus(height);
}
return Line.of(nowLineLeftStatus, nowLineRightStatus);
return Line.of(player, outcome, nowLineLeftStatus, nowLineRightStatus);
}

private List<Boolean> createNowLineRightStatus(int index, CountOfLine countOfLine, Height height,
Expand All @@ -65,4 +74,40 @@ private List<Boolean> createEmptyStatus(Height height) {
.mapToObj(i -> false)
.collect(Collectors.toList());
}

public Map<String, String> getResultToPrint(Map<String, String> result, String targetName) {
if (isAllMode(targetName)) {
return Collections.unmodifiableMap(result);
}
validateTargetName(result, targetName);
return Map.of(targetName, result.get(targetName));
}

private boolean isAllMode(String targetName) {
return "all".equals(targetName);
}

private void validateTargetName(Map<String, String> result, String targetName) {
if (!result.containsKey(targetName)) {
throw new IllegalArgumentException(Errors.TARGET_NAME_MUST_BE_IN_NAMES);
}
}

private CountOfLine getcountOfLine(List<Player> players, List<String> outcomes) {
validateCountOfLine(players, outcomes);
int valueOfCountOfLine = players.size();
return new CountOfLine(valueOfCountOfLine);
}

private void validateCountOfLine(List<Player> players, List<String> outcomes) {
if (players.size() != outcomes.size()) {
throw new IllegalArgumentException(Errors.PLAYERS_AND_OUTCOMES_SIZE_IS_NOT_SAME);
}
}

private List<Player> getPlayers(List<String> names) {
return names.stream()
.map(Player::new)
.collect(Collectors.toList());
}
}
Loading