-
Notifications
You must be signed in to change notification settings - Fork 742
3단계 - 사다리(게임 실행) #1806
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
base: nice7677
Are you sure you want to change the base?
3단계 - 사다리(게임 실행) #1806
Changes from 10 commits
3487e3d
790dd79
620051c
7472f11
22cce2b
32bf732
fb362d0
de3f109
d4acc2b
7084fb4
606681e
c673459
d79de56
6356d9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,89 @@ | ||
| package nextstep.ladder; | ||
|
|
||
| import nextstep.ladder.domain.Ladder; | ||
| import nextstep.ladder.domain.Line; | ||
| import nextstep.ladder.domain.Player; | ||
| import nextstep.ladder.domain.Result; | ||
| import nextstep.ladder.util.RandomUtil; | ||
| import nextstep.ladder.view.InputView; | ||
| import nextstep.ladder.view.ResultView; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.*; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class LadderGame { | ||
|
|
||
| private static final String NAME_REX_PATTERN = ","; | ||
|
|
||
| private final List<Player> players = new ArrayList<>(); | ||
| private static final String ALL_PLAYER = "all"; | ||
|
|
||
| private Ladder ladder; | ||
|
|
||
| private Result result; | ||
|
|
||
| public static void main(String[] args) { | ||
| LadderGame game = new LadderGame(); | ||
| game.run(); | ||
| } | ||
|
|
||
| public void run() { | ||
|
|
||
| inputPlayers(); | ||
| List<Player> players = new ArrayList<>(); | ||
|
|
||
| saveLadder(new Ladder(inputLadderHeight(), this.players.size())); | ||
| inputPlayers(players, InputView.inputPlayers()); | ||
|
|
||
| ResultView.printResult(this.players, ladder.getLines()); | ||
| result = new Result(InputView.inputResult()); | ||
|
|
||
| } | ||
| saveLadder(new Ladder(InputView.inputLadderHeight())); | ||
| addLadderLines(ladder.getHeight(), players.size()); | ||
|
|
||
| ResultView.printLadderResult(players, ladder.getLines(), result); | ||
|
|
||
| String inputPlayer = InputView.inputPlayer(); | ||
| printPlayerResult(inputPlayer, players); | ||
|
|
||
| private void inputPlayers() { | ||
| addPlayers(InputView.inputPlayers()); | ||
| } | ||
|
|
||
| private void addPlayers(String players) { | ||
| Arrays.stream(players.split(NAME_REX_PATTERN)) | ||
| .map(Player::new) | ||
| .forEach(this.players::add); | ||
| private void inputPlayers(List<Player> players, String inputPlayers) { | ||
| addPlayers(players, inputPlayers); | ||
| } | ||
|
|
||
| private int inputLadderHeight() { | ||
| return InputView.inputLadderHeight(); | ||
| private void addPlayers(List<Player> players, String inputPlayers) { | ||
| Arrays.stream(inputPlayers.split(NAME_REX_PATTERN)) | ||
| .map(Player::new) | ||
| .forEach(players::add); | ||
| } | ||
|
|
||
| private void saveLadder(Ladder ladder) { | ||
| this.ladder = ladder; | ||
| } | ||
|
|
||
| private void addLadderLines(int height, int width) { | ||
|
||
| IntStream.range(0, height) | ||
| .mapToObj(i -> new Line(() -> RandomUtil.generatorPoints(width - 1))) | ||
| .forEach(this::addLine); | ||
| } | ||
|
|
||
| private void addLine(Line line) { | ||
| this.ladder.addLine(line); | ||
| } | ||
|
|
||
| private void printPlayerResult(String inputPlayer, List<Player> players) { | ||
|
|
||
| ResultView.printResultText(); | ||
|
||
|
|
||
| if (ALL_PLAYER.equals(inputPlayer)) { | ||
| IntStream.range(0, players.size()).forEach(index -> printPlayerResultWithName(index, players.get(index))); | ||
| return; | ||
| } | ||
|
|
||
| int point = players.indexOf(new Player(inputPlayer)); | ||
|
||
| ResultView.printPlayerResult(result.getValue(players.get(point).getPlayerResultIndex(point, ladder))); | ||
|
|
||
| } | ||
|
|
||
| private void printPlayerResultWithName(int index, Player player) { | ||
| ResultView.printPlayerResultWithName(player.getName(), result.getValue(player.getPlayerResultIndex(index, ladder))); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,28 @@ | ||
| LadderGame | ||
| 플레이어 추가 | ||
| 사다이 추가 | ||
| 라인 추가 | ||
| # 3단계 - 사다리(게임 실행) | ||
|
|
||
| ## 기능 요구사항 | ||
| - 사다리 실행 결과를 출력해야 한다. | ||
| - 개인별 이름을 입력하면 개인별 결과를 출력하고, "all"을 입력하면 전체 참여자의 실행 결과를 출력한다. | ||
|
|
||
| ## 프로그래밍 요구사항 | ||
| - 자바 8의 스트림과 람다를 적용해 프로그래밍한다. | ||
| - 규칙 6: 모든 엔티티를 작게 유지한다. | ||
| - 규칙 7: 3개 이상의 인스턴스 변수를 가진 클래스를 쓰지 않는다. | ||
|
|
||
| --- | ||
|
|
||
| ### LadderGame | ||
| -[X] 플레이어 추가 | ||
| -[X] 사다리 저장 | ||
| -[X] 플레이어 결과 구하기 | ||
|
|
||
| InputView | ||
| 참여할 사람 이름 입력 | ||
| 최대 사다리 높이 입력 | ||
| -[X] 참여할 사람 이름 입력 | ||
| -[X] 최대 사다리 높이 입력 | ||
| -[X] 실행 결과 입력 | ||
| -[X] 결과를 보고 싶은 사람 입력 | ||
|
|
||
| ResultView | ||
| 플레이어 이름 출력 | ||
| 사다리 출력 | ||
| -[X] 플레이어 이름 출력 | ||
| -[X] 사다리 출력 | ||
| -[X] 실행 결과 출력 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,25 +4,29 @@ | |
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.Supplier; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class Line { | ||
|
|
||
| private final List<Boolean> points; | ||
|
|
||
| public Line(int countOfPerson) { | ||
| List<Boolean> points = new ArrayList<>(); | ||
| IntStream.range(0, countOfPerson - 1) | ||
| .forEach(index -> addPoint(points, index)); | ||
| this.points = points; | ||
| public Line(Supplier<List<Boolean>> supplierPoints) { | ||
| this.points = supplierPoints.get(); | ||
| } | ||
|
|
||
| private void addPoint(List<Boolean> points, int index) { | ||
| if (index == 0 || !points.get(index - 1)) { | ||
| points.add(RandomUtil.generator()); | ||
| return; | ||
| public boolean hasLeftPoint(int currentPlayerPoint) { | ||
|
||
| if (currentPlayerPoint == 0) { | ||
| return false; | ||
| } | ||
| points.add(false); | ||
| return points.get(currentPlayerPoint - 1); | ||
| } | ||
|
|
||
| public boolean hasRightPoint(int currentPlayerPoint) { | ||
| if (points.size() == currentPlayerPoint || points.size() < currentPlayerPoint) { | ||
| return false; | ||
| } | ||
| return points.get(currentPlayerPoint); | ||
| } | ||
|
|
||
| public List<Boolean> getPoints() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| package nextstep.ladder.domain; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Player { | ||
|
|
||
| private static final String NAME_OVER_LENGTH_ERROR_TEXT = "사람에 이름을 최대5글자까지 가능합니다."; | ||
|
|
@@ -17,4 +19,38 @@ public String getName() { | |
| return name; | ||
| } | ||
|
|
||
| public int getPlayerResultIndex(int currentPoint, Ladder ladder) { | ||
|
||
| int point = currentPoint; | ||
| for (int index = 0; index < ladder.getHeight(); index++) { | ||
| point = calculationPoint(index, point, ladder); | ||
| } | ||
| return point; | ||
| } | ||
|
|
||
| private int calculationPoint(int lineIndex, int point, Ladder ladder) { | ||
| Line line = ladder.getLines().get(lineIndex); | ||
| if (line.hasLeftPoint(point)) { | ||
| return point - 1; | ||
| } | ||
| if (line.hasRightPoint(point)) { | ||
| return point + 1; | ||
| } | ||
| return point; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
||
| Player player = (Player) o; | ||
|
|
||
| return Objects.equals(name, player.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return name != null ? name.hashCode() : 0; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package nextstep.ladder.domain; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Result { | ||
|
|
||
| private static final String RESULT_REX_PATTERN = ","; | ||
|
|
||
| private final List<String> values; | ||
|
|
||
| public Result(String values) { | ||
| this.values = Arrays.asList(values.split(RESULT_REX_PATTERN)); | ||
| } | ||
|
|
||
| public String getValue(int index) { | ||
| return values.get(index); | ||
| } | ||
|
|
||
| public List<String> getValues() { | ||
| return Collections.unmodifiableList(values); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,27 @@ | ||
| package nextstep.ladder.util; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class RandomUtil { | ||
|
|
||
| private static final Random random = new Random(); | ||
| public static boolean generator() { | ||
| return random.nextBoolean(); | ||
|
|
||
| public static List<Boolean> generatorPoints(int count) { | ||
|
||
| List<Boolean> points = new ArrayList<>(); | ||
| IntStream.range(0, count) | ||
|
||
| .forEach(index -> addPoint(points, index, random.nextBoolean())); | ||
| return points; | ||
| } | ||
|
|
||
| private static void addPoint(List<Boolean> points, int index, boolean point) { | ||
|
||
| if (index == 0 || !points.get(index - 1)) { | ||
| points.add(point); | ||
| return; | ||
| } | ||
| points.add(false); | ||
| } | ||
|
|
||
| } | ||
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.
ladder,result를 필드로 관리할 필요가 있을까요?run메서드의 지역 변수로 관리해도 괜찮지 않을까요?