Skip to content

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

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

Open
wants to merge 2 commits into
base: joungminju
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
1 change: 0 additions & 1 deletion src/main/java/controller/LadderGameController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package controller;

import domain.CountOfLine;
import domain.Height;
import domain.Ladder;
import domain.RungsBuilder;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/domain/CountOfLine.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package domain;

import java.util.function.Predicate;
import util.Errors;

public record CountOfLine(int value) {

private static final Predicate<Integer> ismpty = value -> value <= 0;

public CountOfLine {
validate(value);
}
Expand All @@ -13,7 +16,7 @@ private void validate(int value) {
}

private void validateIsNotEmpty(int value) {
if (value <= 0) {
if (ismpty.test(value)) {
throw new IllegalArgumentException(Errors.LADDERS_MUST_CONTAINS_LEAST_ONE_LADDER);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/domain/Height.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package domain;

import java.util.function.Predicate;
import util.Errors;

public record Height(int value) {

private static final Predicate<Integer> isNegative = value -> value <= 0;

public Height {
validate(value);
}
Expand All @@ -13,7 +16,7 @@ private void validate(int value) {
}

private void validatePositive(int value) {
if (value <= 0) {
if (isNegative.test(value)) {
throw new IllegalArgumentException(Errors.LADDER_HEIGHT_MUST_BE_POSITIVE);
}
}
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/domain/Ladder.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ private void validateLaddersHeight(List<Line> lines) {
}

private void validatePointStatus(List<Line> lines) {
for (int index = 0; index < lines.size() - 1; index++) {
Line nowLine = lines.get(index);
Line nextLine = lines.get(index + 1);
validateRungInSamePosition(nowLine, nextLine);
}
IntStream.range(0, lines.size() - 1)
.forEach(index -> validateRungInSamePosition(lines.get(index), lines.get(index + 1)));
}

private void validateRungInSamePosition(Line nowLine, Line nextLine) {
Expand All @@ -46,8 +43,7 @@ private void validateRungInSamePosition(Line nowLine, Line nextLine) {
}

private boolean isRungInSamePosition(Line nowLine, Line nextLine) {
int maxPosition = nowLine.getHeight();
return IntStream.range(0, maxPosition)
return IntStream.range(0, nowLine.getHeight())
.allMatch(
position -> nowLine.isConnectedToRightLineAt(position) == nextLine.isConnectedToLeftLineAt(position));
}
Expand All @@ -68,8 +64,8 @@ public Map<String, String> getResult() {

for (int nowIndex = 0; nowIndex < lines.size(); nowIndex++) {
int finalIndex = calculateTargetIndex(nowIndex, height);
String name = getNameOfLine(nowIndex);
String outcome = getOutcomeOfLine(finalIndex);
String name = getNameOfLine(nowIndex);
String outcome = getOutcomeOfLine(finalIndex);
result.put(name, outcome);
}
return result;
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/domain/Line.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package domain;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import util.Errors;

public class Line {
Expand All @@ -17,14 +18,13 @@ private Line(Player player, String outcome, List<Point> points) {
this.points = points;
}

public static Line of(Player player, String outcome, 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)));
}
List<Point> points = IntStream.range(0, leftRungsStatus.size())
.mapToObj(position -> new Point(leftRungsStatus.get(position), rightRungsStatus.get(position)))
.collect(Collectors.toList());
return new Line(player, outcome, points);
}

Expand All @@ -35,11 +35,9 @@ private static void validateHeight(List<Boolean> leftRungsStatus, List<Boolean>
}

public List<Boolean> getRightStatus() {
List<Boolean> rightStatus = new ArrayList<>();
for (Point point : points) {
rightStatus.add(point.isConnectedToRightLadder());
}
return rightStatus;
return points.stream()
.map(Point::isConnectedToRightLadder)
.collect(Collectors.toList());
}

public int getHeight() {
Expand All @@ -55,12 +53,12 @@ public String getOutcome() {
}

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

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

Expand Down
10 changes: 3 additions & 7 deletions src/main/java/domain/RandomRungsBuilder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
Expand All @@ -16,12 +15,9 @@ public RandomRungsBuilder() {

@Override
public List<Boolean> buildAndGetRungsStatus(List<Boolean> prevRungsStatus) {
List<Boolean> rungsStatus = new ArrayList<>();
for (Boolean doesPrevLadderHaveRung : prevRungsStatus) {
boolean nowRungsStatus = generateRungIfAbsent(doesPrevLadderHaveRung);
rungsStatus.add(nowRungsStatus);
}
return rungsStatus;
return prevRungsStatus.stream()
.map(this::generateRungIfAbsent)
.collect(Collectors.toList());
}

private boolean generateRungIfAbsent(Boolean doesPrevLadderHaveRung) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/service/LadderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public LadderService(RungsBuilder rungsBuilder) {
}

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