-
Notifications
You must be signed in to change notification settings - Fork 738
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
Step2 - 사다리(생성) #2315
Open
minji-go
wants to merge
15
commits into
next-step:minji-go
Choose a base branch
from
minji-go:step2
base: minji-go
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+451
−18
Open
Step2 - 사다리(생성) #2315
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a0d5531
refactor: @FunctionalInterface 활용
minji-go 0d49e96
docs: step2 요구사항 정의
minji-go 1107b19
feat: 사다리 게임 생성자 검증
minji-go ec8d0fc
feat: 사다리 라인 생성
minji-go a1678ef
feat: 사다리 라인 겹침 제한
minji-go 58927b4
feat: 사다리 타기 참여자 이름 조회
minji-go f56bb41
feat: 입출력 및 로직 제어
minji-go bc583e0
feat: Ladders 객체 생성 및 분리
minji-go f8a2262
feat: Names 객체 생성 및 분리
minji-go 8b01169
feat: Line 객체 생성 및 분리
minji-go bf9ab43
style: intellij code style
minji-go 3eb4aa5
refactor: OutputView Depth 개선
minji-go bf20775
refactor: InputView, LadderFactory 관심사 분리
minji-go 10bee83
refactor: Line equals, hashcode 재정의
minji-go e283d75
refactor: Line.createLine 변경
minji-go File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ladder; | ||
|
||
import ladder.domain.LadderFactory; | ||
import ladder.view.InputView; | ||
import ladder.view.OutputView; | ||
|
||
|
||
public class LadderApplication { | ||
|
||
public static void main(String[] args) { | ||
InputView inputView = new InputView(); | ||
OutputView outputView = new OutputView(); | ||
|
||
LadderFactory ladderFactory = new LadderFactory(inputView.inputNames(), inputView.inputHeight()); | ||
outputView.printNames(ladderFactory.getNames()); | ||
outputView.printLadder(ladderFactory.getLadder()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ladder.domain; | ||
|
||
import java.util.List; | ||
|
||
public class LadderFactory { | ||
private final Names names; | ||
private final Lines lines; | ||
|
||
public LadderFactory(List<String> names, int height) { | ||
this(names, height, new RandomLineGenerator()); | ||
} | ||
|
||
public LadderFactory(List<String> names, int height, LineGenerator lineGenerator) { | ||
this.names = new Names(names); | ||
this.lines = new Lines(height, this.names.connectSize(), lineGenerator); | ||
} | ||
|
||
public List<List<Boolean>> getLadder() { | ||
return lines.getList(); | ||
} | ||
|
||
public List<String> getNames() { | ||
return names.getAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package ladder.domain; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class Line { | ||
private final List<Boolean> line; | ||
|
||
public Line(int size, LineGenerator generator) { | ||
this(createLine(size, generator)); | ||
} | ||
|
||
public Line(boolean... line) { | ||
this(createLine(line)); | ||
} | ||
|
||
public Line(List<Boolean> line) { | ||
this.line = line; | ||
} | ||
|
||
private static List<Boolean> createLine(int size, LineGenerator generator) { | ||
if (size < 1) { | ||
throw new IllegalArgumentException("Line size should be greater than 0"); | ||
} | ||
return createLine(generate(size, generator)); | ||
} | ||
|
||
private static List<Boolean> createLine(boolean[] line) { | ||
return IntStream.range(0, line.length) | ||
.mapToObj(i -> line[i]) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static boolean[] generate(int size, LineGenerator generator) { | ||
boolean[] line = new boolean[size]; | ||
line[0] = generator.isConnected(); | ||
|
||
for (int i = 1; i < size; i++) { | ||
line[i] = (!line[i - 1] && generator.isConnected()); | ||
} | ||
|
||
return line; | ||
} | ||
|
||
public List<Boolean> getList() { | ||
return Collections.unmodifiableList(line); | ||
minji-go marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Line line1 = (Line) o; | ||
return Objects.equals(line, line1.line); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(line); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ladder.domain; | ||
|
||
public interface LineGenerator { | ||
boolean isConnected(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ladder.domain; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class Lines { | ||
private final List<Line> lines; | ||
|
||
public Lines(int height, int width, LineGenerator generator) { | ||
validLadderLength(height); | ||
validLadderLength(width); | ||
minji-go marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.lines = createLadder(height, width, generator); | ||
} | ||
|
||
private void validLadderLength(int value) { | ||
if (value < 1) | ||
throw new IllegalArgumentException("The ladder height should be larger than zero."); | ||
} | ||
|
||
private List<Line> createLadder(int height, int width, LineGenerator generator) { | ||
return IntStream.range(0, height) | ||
.mapToObj(i -> new Line(width, generator)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<List<Boolean>> getList() { | ||
return lines.stream() | ||
.map(Line::getList) | ||
.collect(Collectors.toUnmodifiableList()); | ||
} | ||
Comment on lines
+27
to
+31
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.
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ladder.domain; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class Names { | ||
private final List<String> names; | ||
|
||
public Names(String... names) { | ||
this(Arrays.asList(names)); | ||
} | ||
|
||
public Names(List<String> names) { | ||
this.names = names; | ||
validateNameMaxLength(names); | ||
} | ||
|
||
private void validateNameMaxLength(List<String> names) { | ||
if (names.stream().anyMatch(name -> name.length() > 5)) | ||
throw new IllegalArgumentException("The name should be up to 5 letters."); | ||
} | ||
|
||
public List<String> getAll() { | ||
return Collections.unmodifiableList(names); | ||
} | ||
|
||
public int connectSize() { | ||
return names.size() - 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ladder.domain; | ||
|
||
import java.util.Random; | ||
|
||
public class RandomLineGenerator implements LineGenerator { | ||
public static final Random random = new Random(); | ||
|
||
@Override | ||
public boolean isConnected() { | ||
return random.nextBoolean(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ladder.view; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
private static final Scanner SCANNER = new Scanner(System.in); | ||
|
||
public List<String> inputNames() { | ||
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); | ||
String names = SCANNER.nextLine(); | ||
System.out.println(); | ||
return Arrays.asList(names.split(",")); | ||
} | ||
|
||
public int inputHeight() { | ||
System.out.println("최대 사다리 높이는 몇 개인가요?"); | ||
int height = SCANNER.nextInt(); | ||
SCANNER.nextLine(); | ||
return height; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ladder.view; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class OutputView { | ||
public void printNames(List<String> names) { | ||
System.out.println("실행결과"); | ||
System.out.println(); | ||
|
||
names.stream() | ||
.map(name -> String.format("%5s ", name)) | ||
.forEach(System.out::print); | ||
|
||
System.out.println(); | ||
} | ||
|
||
public void printLadder(List<List<Boolean>> ladders) { | ||
ladders.stream() | ||
.map(this::convertLine) | ||
.forEach(System.out::println); | ||
} | ||
|
||
private String convertLine(List<Boolean> line) { | ||
return line.stream() | ||
.map(this::convertLineElement) | ||
.collect(Collectors.joining("|", " |", "|")); | ||
} | ||
|
||
private String convertLineElement(Boolean connected) { | ||
return Boolean.TRUE.equals(connected) ? "-----" : " "; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
이 부분도
값을 풀어서 제공하고 있네요
혹시 외부에서 사용할 때 디미터 법칙을 지키기 위함일까요?