Skip to content
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

3단계 - 사다리(게임 실행) #1638

Open
wants to merge 6 commits into
base: jwoo-o
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
23 changes: 19 additions & 4 deletions src/main/java/ladder/controller/LadderController.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
package ladder.controller;

import ladder.domain.Ladder;
import ladder.domain.Results;
import ladder.domain.Users;
import ladder.service.LadderResultService;
import ladder.view.InputView;
import ladder.view.OutputView;

public class LadderController {

private static LadderController ladderController;
private final LadderResultService ladderResultService;

private LadderController(LadderResultService ladderResultService)
{
this.ladderResultService = ladderResultService;
}

public static LadderController getInstance() {
if (ladderController == null) {
ladderController = new LadderController();
ladderController = new LadderController(LadderResultService.getInstance());
}

return ladderController;
}

public void run() {
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)");
Users users = Users.from(InputView.getNames());

System.out.println("최대 사다리 높이는 몇 개인가요?");
Results results = Results.of(InputView.getResult(), users.getUserCount());
Ladder ladder = Ladder.of(users.getUserCount(), InputView.getHeight());

OutputView.printLadder(users.getUserList(), ladder.getLines());
OutputView.printLadder(users.getUserList(), ladder.getLines(), results.getResults());

boolean isEnd = false;
while (!isEnd) {
isEnd = OutputView
.printLadderResult(ladderResultService.getLadderResult(InputView.getTargetPlayer(), users.getUserList()
, ladder, results.getResults()));
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/ladder/domain/Ladder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ public static Ladder of(int userCount, int height) {
public List<Line> getLines() {
return lines;
}

public int move(int userIndex) {
int resultIndex = userIndex;
for (Line line : lines) {
resultIndex = line.move(resultIndex);
}

return resultIndex;
}
}
15 changes: 14 additions & 1 deletion src/main/java/ladder/domain/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class Line {

private static final Random RANDOM = new Random();
private static final Random RANDOM = new Random();

private final List<Boolean> points = new ArrayList<>();

Expand Down Expand Up @@ -36,5 +36,18 @@ private boolean validPoint(boolean currentPoint, boolean beforePoint) {
}
return currentPoint;
}

public int move(int index)
{
if (index > 0 && points.get(index - 1)) {
return --index;
}

if (index == points.size()) {
return points.get(index - 1) ? --index : index;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

삼항 연산자를 사용하지 말고 코드를 작성해보면 좋을 것 같아요!!

}

return points.get(index) ? ++index : index;
}
}

24 changes: 24 additions & 0 deletions src/main/java/ladder/domain/Result.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ladder.domain;

public class Result
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 코드는 도메인으로 관리되기엔 도메인 로직이 없는 객체 같아요!! 혹시 어떠한 의도로 생성했을까요?

{
private String result;

private Result(String result)
{
this.result = result;
}

public static Result from(String result) {
return new Result(result);
}

public String getResult() {
return result;
}

@Override
public String toString() {
return result;
}
}
29 changes: 29 additions & 0 deletions src/main/java/ladder/domain/Results.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ladder.domain;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Results
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 클래스도 일급 컬렉션으로 유의미하게 사용하고 싶다면 추가적인 도메인 로직을 담고 있다던지 하는게 더 좋을 것 같아요!!

{
private final List<Result> results;

private Results(List<Result> results)
{
this.results = results;
}

public static Results of(String[] resultArray, int userSize) {
if (userSize != resultArray.length) {
throw new IllegalArgumentException("결과값과 인원수가 다릅니다");
}
return new Results(Arrays.stream(resultArray)
.map(Result::from)
.collect(Collectors.toList())
);
}

public List<Result> getResults() {
return results;
}
}
5 changes: 5 additions & 0 deletions src/main/java/ladder/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public static User from(String name) {
public String getName() {
return name;
}

@Override
public String toString() {
return name;
}
}
51 changes: 51 additions & 0 deletions src/main/java/ladder/service/LadderResultService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ladder.service;


import ladder.domain.Ladder;
import ladder.domain.Result;
import ladder.domain.User;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LadderResultService
{
private static LadderResultService ladderResultService;

public static LadderResultService getInstance()
{
if (ladderResultService == null)
{
ladderResultService = new LadderResultService();
}

return ladderResultService;
}

public List<String> getLadderResult(String targetPlayer, List<User> userList
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

하나의 함수에 너무 많은 파라미터를 받고 있어요!! 한번 파라미터의 개수를 줄일 수 있도록 리팩토링 해보는 것은 어떨까요?

, Ladder ladder, List<Result> results) {
if (targetPlayer.equals("all")) {
return allLadderResult(userList, ladder, results);
}
return userLadderResult(findUserIndex(targetPlayer, userList), ladder, results);
}

private List<String> userLadderResult(int userIndex, Ladder ladder, List<Result> results) {
return List.of(results.get(ladder.move(userIndex)).getResult());
}

private List<String> allLadderResult(List<User> userList, Ladder ladder, List<Result> results) {
return IntStream.range(0, userList.size())
.mapToObj(index -> userList.get(index) + " : " + results.get(ladder.move(index)))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 라인은 출력문의 로직이 아닐까요?

.collect(Collectors.toList());
}

private int findUserIndex(String targetPlayer, List<User> userList) {
return IntStream.range(0, userList.size())
.filter(i -> Objects.equals(userList.get(i).getName(), targetPlayer))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("사다리게임에 참여하지 않은 사람입니다."));
}
}
15 changes: 14 additions & 1 deletion src/main/java/ladder/view/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,23 @@ private InputView() {
private static final Scanner scanner = new Scanner(System.in);

public static String[] getNames() {
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)");
return scanner.next().split(COMMA);
}

public static int getHeight() {
System.out.println("최대 사다리 높이는 몇 개인가요?");
return scanner.nextInt();
}
}

public static String[] getResult() {
System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)");
return scanner.next().split(COMMA);
}

public static String getTargetPlayer()
{
System.out.println("결과를 보고 싶은 사람은?");
return scanner.next();
}
}
22 changes: 18 additions & 4 deletions src/main/java/ladder/view/OutputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import ladder.domain.Line;
import ladder.domain.Result;
import ladder.domain.User;

import java.util.List;
Expand All @@ -17,19 +18,20 @@ public class OutputView {
private OutputView() {
}

public static void printLadder(List<User> userList, List<Line> lines)
public static void printLadder(List<User> userList, List<Line> lines, List<Result> results)
{
System.out.println("실행결과");
System.out.println("사다리 결과");
printUser(userList);
printLine(lines);
printResult(results);
}

private static void printLine(List<Line> lines)
{
lines.forEach(line -> {
System.out.print("\n" + BLANK.repeat(3));
System.out.print(BLANK.repeat(3));
printLines(line.getPoints());
System.out.print(LADDER_SHAPE);
System.out.print(LADDER_SHAPE+"\n");
});
}

Expand All @@ -56,10 +58,22 @@ private static void printUser(List<User> userList) {
.collect(Collectors.joining(BLANK)));
}

private static void printResult(List<Result> userList) {
System.out.println(userList.stream()
.map(result -> String.format("%4s ", result.getResult()))
.collect(Collectors.joining(BLANK)));
}

private static String outputPersonName(String name) {
if (name.length() < LINE_LENGTH_MAX) {
return String.format("%4s ", name);
}
return name;
}

public static boolean printLadderResult(List<String> ladderResult) {
ladderResult.forEach(System.out::println);

return ladderResult.size() != 1;
}
}