-
Notifications
You must be signed in to change notification settings - Fork 50
[사다리 1-4단계] 천희정 미션 제출합니다. #53
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
heejung72
wants to merge
11
commits into
next-step:heejung72
Choose a base branch
from
heejung72:step5
base: heejung72
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.
+776
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d07f90b
feat : 사다리 출력
heejung72 2830505
feat : 사다리 생성
heejung72 cab11e3
feat : 사다리 타기
heejung72 44ad3a4
feat : 게임 실행
heejung72 0c2fd5d
feat : 코드 스타일 정리
heejung72 4ad745b
feat : 게임실행
heejung72 66a7a62
Merge branch 'step4' into step5
heejung72 29da7a7
feat : test case 추가
heejung72 5de368a
리팩토링 1
heejung72 a824d28
refactor : 리팩터링
heejung72 0d59ad1
refactor: 리팩토링 2
heejung72 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 hidden or 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,6 @@ | ||
public class Application { | ||
public static void main(String[] args) { | ||
LadderApplication app = new LadderApplication(); | ||
app.run(); | ||
} | ||
} |
This file contains hidden or 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,28 @@ | ||
import controller.LadderController; | ||
import view.InputView; | ||
import view.OutputView; | ||
import service.LadderService; | ||
|
||
public class LadderApplication { | ||
public void run() { | ||
InputView inputView = new InputView(); | ||
OutputView outputView = new OutputView(); | ||
LadderService ladderService = new LadderService(); | ||
LadderController controller = new LadderController(inputView, outputView, ladderService); | ||
|
||
controller.start(); | ||
processQueries(controller); | ||
|
||
// 종료 | ||
System.exit(0); | ||
} | ||
|
||
private void processQueries(LadderController controller) { | ||
while (shouldContinue(controller)) { | ||
} | ||
} | ||
|
||
private boolean shouldContinue(LadderController controller) { | ||
return controller.processQuery(); | ||
} | ||
} |
This file contains hidden or 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,102 @@ | ||
package controller; | ||
|
||
import domain.Height; | ||
import domain.Result; | ||
import domain.LadderGame; | ||
import domain.Ladder; | ||
import domain.Line; | ||
import service.LadderService; | ||
import domain.Name; | ||
import domain.Names; | ||
import domain.Results; | ||
import view.InputView; | ||
import view.OutputView; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class LadderController { | ||
private final InputView inputView; | ||
private final OutputView outputView; | ||
private final LadderService ladderService; | ||
private LadderGame ladderGame; | ||
|
||
public LadderController(InputView inputView, OutputView outputView, LadderService ladderService) { | ||
this.inputView = inputView; | ||
this.outputView = outputView; | ||
this.ladderService = ladderService; | ||
} | ||
|
||
public void start() { | ||
try { | ||
String namesInput = inputView.readNames(); | ||
String resultsInput = inputView.readResults(); | ||
int heightInput = inputView.readHeight(); | ||
|
||
Names names = ladderService.createNames(namesInput); | ||
Results results = ladderService.createResults(resultsInput); | ||
Height height = ladderService.createHeight(heightInput); | ||
|
||
this.ladderGame = ladderService.createLadderGame(names, results, height); | ||
|
||
// domain 객체 -> OutputView | ||
outputView.printLadder( | ||
convertNamesToStrings(names), | ||
convertLadderToLists(ladderGame.getLadder()), | ||
convertResultsToStrings(results) | ||
); | ||
} catch (IllegalArgumentException e) { | ||
outputView.printError(e.getMessage()); | ||
start(); | ||
} | ||
} | ||
|
||
public boolean processQuery() { | ||
try { | ||
String target = inputView.readNameForResult(); | ||
|
||
if (ladderService.isAllQuery(target)) { | ||
Map<Name, Result> allResults = ladderGame.playAll(); | ||
Map<String, String> convertedResults = convertAllResultsToMap(allResults); | ||
outputView.printAllResults(convertedResults); | ||
return false; | ||
} | ||
|
||
Name targetName = ladderService.createName(target); | ||
Result result = ladderGame.play(targetName); | ||
|
||
outputView.printResult(result.getValue()); | ||
return true; | ||
|
||
} catch (IllegalArgumentException e) { | ||
outputView.printError(e.getMessage()); | ||
return true; | ||
} | ||
} | ||
|
||
private List<String> convertNamesToStrings(Names names) { | ||
return names.getValues().stream() | ||
.map(Name::getValue) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private List<List<Boolean>> convertLadderToLists(Ladder ladder) { | ||
return ladder.getLines().stream() | ||
.map(Line::getSteps) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private List<String> convertResultsToStrings(Results results) { | ||
return results.getValues().stream() | ||
.map(Result::getValue) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private Map<String, String> convertAllResultsToMap(Map<Name, Result> gameResults) { | ||
return gameResults.entrySet().stream() | ||
.collect(Collectors.toMap( | ||
entry -> entry.getKey().getValue(), | ||
entry -> entry.getValue().getValue() | ||
)); | ||
} | ||
} |
This file contains hidden or 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,16 @@ | ||
package domain; | ||
|
||
public class Height { | ||
private final int value; | ||
|
||
public Height(int value) { | ||
if (value <= 0) { | ||
throw new IllegalArgumentException("Height는 0보다 커야한다"); | ||
} | ||
this.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
} |
This file contains hidden or 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,27 @@ | ||
package domain; | ||
|
||
import java.util.List; | ||
|
||
public class Ladder { | ||
private final List<Line> lines; | ||
|
||
public Ladder(List<Line> lines) { | ||
if (lines == null || lines.isEmpty()) { | ||
throw new IllegalArgumentException("사다리는 최소 한 줄 이상이어야 합니다."); | ||
} | ||
|
||
this.lines = List.copyOf(lines); | ||
} | ||
|
||
public int traverse(int startPosition) { | ||
int position = startPosition; | ||
for (Line line : lines) { | ||
position = line.move(position); | ||
} | ||
return position; | ||
} | ||
|
||
public List<Line> getLines() { | ||
return lines; | ||
} | ||
} |
This file contains hidden or 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,34 @@ | ||
package domain; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class LadderGame { | ||
private final Names names; | ||
private final Results results; | ||
private final Ladder ladder; | ||
|
||
public LadderGame(Names names, Results results, Ladder ladder) { | ||
this.names = names; | ||
this.results = results; | ||
this.ladder = ladder; | ||
} | ||
|
||
public Result play(Name name) { | ||
int index = names.indexOf(name); | ||
if (index == -1) { | ||
throw new IllegalArgumentException("존재하지 않는 참가자입니다: " + name.getValue()); | ||
} | ||
int resultIndex = ladder.traverse(index); | ||
return results.get(resultIndex); | ||
} | ||
|
||
public Map<Name, Result> playAll() { | ||
return names.getValues().stream() | ||
.collect(Collectors.toMap(n -> n, this::play)); | ||
} | ||
|
||
public Ladder getLadder() { | ||
return ladder; | ||
} | ||
} |
This file contains hidden or 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,14 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LadderGenerator { | ||
public static Ladder generate(int countOfPlayers, Height height) { | ||
List<Line> lines = new ArrayList<>(); | ||
for (int i = 0; i < height.getValue(); i++) { | ||
lines.add(Line.generate(countOfPlayers)); | ||
} | ||
return new Ladder(lines); | ||
} | ||
} |
This file contains hidden or 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,55 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class Line { | ||
private final List<Boolean> steps; | ||
private static final Random RANDOM = new Random(); | ||
|
||
public Line(List<Boolean> steps) { | ||
this.steps = List.copyOf(steps); | ||
} | ||
|
||
public static Line generate(int width) { | ||
List<Boolean> steps = new ArrayList<>(); | ||
for (int index = 0; index < width - 1; index++) { | ||
steps.add(nextStep(index, steps)); | ||
} | ||
return new Line(steps); | ||
} | ||
|
||
private static boolean nextStep(int index, List<Boolean> steps) { | ||
if (hasLeftStep(index, steps)) { | ||
return false; | ||
} | ||
return RANDOM.nextBoolean(); | ||
} | ||
|
||
private static boolean hasLeftStep(int index, List<Boolean> steps) { | ||
return index > 0 && steps.get(index - 1); | ||
} | ||
|
||
public int move(int position) { | ||
if (canMoveLeft(position)) { | ||
return position - 1; | ||
} | ||
if (canMoveRight(position)) { | ||
return position + 1; | ||
} | ||
return position; | ||
} | ||
|
||
private boolean canMoveLeft(int position) { | ||
return position > 0 && steps.get(position - 1); | ||
} | ||
|
||
private boolean canMoveRight(int position) { | ||
return position < steps.size() && steps.get(position); | ||
} | ||
|
||
public List<Boolean> getSteps() { | ||
return steps; | ||
} | ||
} |
This file contains hidden or 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,35 @@ | ||
package domain; | ||
|
||
public class Name { | ||
private final String value; | ||
|
||
public Name(String value) { | ||
validate(value); | ||
this.value = value; | ||
} | ||
|
||
private void validate(String value) { | ||
if (value == null || value.trim().isEmpty()) { | ||
throw new IllegalArgumentException("이름은 비어있을 수 없습니다."); | ||
} | ||
if (value.length() > 5) { | ||
throw new IllegalArgumentException("이름은 5글자 이하여야 합니다."); | ||
} | ||
if (value.equals("all")) { | ||
throw new IllegalArgumentException("\"all\"은 참가자 이름으로 사용할 수 없습니다."); | ||
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. escape 👍 |
||
} | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public boolean equals(Object obj) { | ||
if (!(obj instanceof Name)) return false; | ||
return value.equals(((Name) obj).value); | ||
} | ||
|
||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
} |
This file contains hidden or 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,50 @@ | ||
package domain; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Names { | ||
private final List<Name> values; | ||
|
||
private Names(List<Name> values) { | ||
validateDuplicates(values); | ||
this.values = values; | ||
} | ||
|
||
public static Names from(String input) { | ||
if (input == null || input.trim().isEmpty()) { | ||
throw new IllegalArgumentException("참가자 이름을 입력해주세요."); | ||
} | ||
|
||
List<Name> names = Arrays.stream(input.split(",")) | ||
.map(String::trim) | ||
.map(Name::new) | ||
.collect(Collectors.toList()); | ||
|
||
if (names.isEmpty()) { | ||
throw new IllegalArgumentException("유효한 참가자 이름을 입력해주세요."); | ||
} | ||
|
||
return new Names(names); | ||
} | ||
|
||
private void validateDuplicates(List<Name> names) { | ||
long uniqueCount = names.stream().distinct().count(); | ||
if (uniqueCount != names.size()) { | ||
throw new IllegalArgumentException("중복된 참가자 이름이 있습니다."); | ||
} | ||
} | ||
|
||
public int size() { | ||
return values.size(); | ||
} | ||
|
||
public List<Name> getValues() { | ||
return values; | ||
} | ||
|
||
public int indexOf(Name name) { | ||
return values.indexOf(name); | ||
} | ||
} |
This file contains hidden or 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,22 @@ | ||
package domain; | ||
|
||
public class Result { | ||
private final String value; | ||
|
||
public Result(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public boolean equals(Object obj) { | ||
if (!(obj instanceof Result)) return false; | ||
return value.equals(((Result) obj).value); | ||
} | ||
|
||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
} |
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.
여기를 보니까 결국 height가 굳이 클래스일 필요가 없는 것이 증명되긴 했군요