-
Notifications
You must be signed in to change notification settings - Fork 60
[그리디] 서현진 사다리 미션 제출합니다. #81
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: nonactress
Are you sure you want to change the base?
Changes from all commits
2c7ac0b
601989e
45188f7
6bc5102
25114d9
2d98c95
4ae4ed7
e2f073b
e46be8a
c29c860
5aeab91
d6ac423
c90102f
53b8d01
00f8644
35bc4d1
9eff889
4b32197
1007500
e6710c5
af2e6a8
7ed8f0a
04b80c8
4db07ab
4fe7fca
cf2700c
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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # 자바 사다리 게임 | ||
|
|
||
| ## 주요 기능 | ||
|
|
||
| * 쉼표로 구분하여 여러 플레이어 이름 입력 | ||
| * 사다리 끝에 위치할 보상 입력 | ||
| * 사다리 높이 설정 | ||
| * 가로 라인이 무작위로 생성되는 사다리 | ||
| * 최종 생성된 사다리 모양 출력 | ||
| * 각 플레이어의 최종 결과(보상) 표시 | ||
| * 전체 플레이어 또는 특정 플레이어의 결과 조회 기능 | ||
|
|
||
| ## 코드 설명 | ||
| * **`Application.java`**: 프로그램의 시작점입니다. 전체 게임의 흐름을 제어합니다. | ||
|
|
||
| * **`controller` 패키지**: | ||
| * `Controller.java`: 사용자의 입력을 받아 Model을 업데이트하고, 결과를 View에 전달하는 등 게임의 핵심 로직을 관리합니다. | ||
|
|
||
| * **`model` 패키지**: 게임의 데이터와 비즈니스 로직을 담당합니다. | ||
| * `Player.java`: 게임에 참여하는 플레이어들의 정보를 관리합니다. | ||
| * `Rewards.java`: 사다리 게임의 결과로 주어지는 보상 정보를 관리합니다. | ||
| * `Bridge.java`: 전체 사다리의 구조를 나타냅니다. 가로 줄(`BridgeStep`)들의 리스트로 구성됩니다. | ||
| * `BridgeStep.java`: 사다리의 한 칸이 가로 줄을 가지고 있는지 여부(`EXIST` 또는 `EMPTY`)를 나타내는 Enum입니다. | ||
| * `LadderDescentService.java`: 플레이어가 사다리를 내려가는 과정을 처리하는 서비스 클래스입니다. | ||
| * `SplitMethod.java`: 사용자 입력을 쉼표 기준으로 분리하는 유틸리티 클래스입니다. | ||
|
|
||
| * **`view` 패키지**: 사용자 인터페이스(입력/출력)를 담당합니다. | ||
| * `InputView.java`: 사용자로부터 플레이어 이름, 보상, 사다리 높이 등의 입력을 받습니다. | ||
| * `OutputView.java`: 게임 진행 상황, 사다리 모양, 최종 결과 등을 콘솔에 출력합니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import Controller.Controller; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| Controller controller = new Controller(); | ||
| controller.startLadder(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,78 @@ | ||||||
| package Controller; | ||||||
|
|
||||||
| import Model.Bridge; | ||||||
| import Model.Player; | ||||||
| import Model.Rewards; | ||||||
| import view.InputView; | ||||||
| import view.OutputView; | ||||||
|
|
||||||
| import java.util.Collections; | ||||||
| import java.util.List; | ||||||
| import java.util.regex.Pattern; | ||||||
| import java.util.stream.Collectors; | ||||||
|
|
||||||
| import Model.LadderDescentService; | ||||||
| import Model.LadderResult; | ||||||
|
|
||||||
| public class Controller { | ||||||
| InputView inputView = new InputView(); | ||||||
| OutputView outputView = new OutputView(); | ||||||
|
|
||||||
| public void startLadder() { | ||||||
| int height; | ||||||
|
|
||||||
| Player players = getPlayer(); | ||||||
| Rewards rewards = getRewards(); | ||||||
|
|
||||||
| outputView.askLadderHeight(); | ||||||
| height = inputView.getWidthAndHeight(); | ||||||
|
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. 맨위에 있는 height는 여기랑 합치면 좋아보여요~
Suggested change
Author
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. 수정하겠습니다! |
||||||
|
|
||||||
| outputView.printPlayersAndRewards(players.getPlayers()); | ||||||
| Bridge bridge = new Bridge(height, players.getPlayersNumber() - 1); | ||||||
| outputView.printBridge(bridge); | ||||||
|
|
||||||
| outputView.printPlayersAndRewards(rewards.getRewards()); | ||||||
|
|
||||||
| LadderDescentService descentService = new LadderDescentService(bridge); | ||||||
| LadderResult ladderResult = descentService.calculateAllResults(players.getPlayersNumber()); | ||||||
|
|
||||||
| inputView.getScanner().nextLine(); | ||||||
|
|
||||||
| while (true) { | ||||||
| outputView.askResults(); | ||||||
| String string = inputView.getScanner().nextLine(); | ||||||
|
|
||||||
|
Comment on lines
+38
to
+44
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. while문 밖에 찾아보니 해결방법이 이게 대표적으로 사연 있는 코드인데, 최대한 사연 없는 코드로 만들어주시는게 좋습니다 😄 |
||||||
| if (string.trim().equals("all")) { | ||||||
| outputView.printAllResults(players, rewards, ladderResult); | ||||||
| break; | ||||||
| } | ||||||
| outputView.printSpecificResults(players, rewards, ladderResult, string); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private Rewards getRewards() { | ||||||
| outputView.askRewards(); | ||||||
| String rewardsInputs = inputView.getScanner().nextLine(); | ||||||
| return new Rewards(split(rewardsInputs)); | ||||||
| } | ||||||
|
|
||||||
| private Player getPlayer() { | ||||||
| outputView.askPlayers(); | ||||||
| String playerInputs = inputView.getScanner().nextLine(); | ||||||
| return new Player(split(playerInputs)); | ||||||
| } | ||||||
|
|
||||||
| private static List<String> split(String input) { | ||||||
|
|
||||||
| final Pattern DELIMITER = Pattern.compile(","); | ||||||
|
|
||||||
| if (input == null || input.isEmpty()) { | ||||||
| return Collections.emptyList(); | ||||||
| } | ||||||
|
|
||||||
| return DELIMITER.splitAsStream(input) | ||||||
| .map(String::trim) // 양쪽 공백 제거 | ||||||
| .filter(name -> !name.isEmpty()) // 빈 값 제거 (예: "neo,,brie") | ||||||
| .collect(Collectors.toList()); | ||||||
| } | ||||||
|
Comment on lines
+65
to
+77
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. 헙 저는 SplitMethod.java는 그대로 두고 Controller에서 SplitMehtod.split(...)을 사용하는걸 생각했는데 Controller 안에 메서드를 넣으셨군요. 요것도 괜찮아보여요 |
||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package Model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
| import java.util.Collections; | ||
|
|
||
| public class Bridge { | ||
|
|
||
| private final List<BridgeRow> rows; | ||
| private final Random random = new Random(); | ||
|
|
||
| public Bridge(int height, int width) { | ||
| this.rows = new ArrayList<>(); | ||
| initializeBridgeRows(height, width); | ||
| } | ||
|
Comment on lines
+13
to
+16
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. 이거는 생성자 방식으로 만들기보다 정적 팩토리 메서드 방식으로 만드는게 좋아보여요~ |
||
|
|
||
| public Bridge(List<BridgeRow> rows) { | ||
| this.rows = rows; | ||
| } | ||
|
Comment on lines
+18
to
+20
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. 해당 생성자는 테스트 코드만을 위한 생성자인데, 테스트 코드만을 위한 코드를 만드는게 좋은가?에 대해서는 다른 분들이랑 이야기 해볼만한 주제인 것 같아요~ |
||
|
|
||
|
|
||
| private void initializeBridgeRows(int height, int width) { | ||
| for (int i = 0; i < height; i++) { | ||
| this.rows.add(new BridgeRow(width, this.random)); | ||
| } | ||
| } | ||
|
|
||
| public List<BridgeRow> getRows() { | ||
| return Collections.unmodifiableList(this.rows); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package Model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
| import java.util.Collections; | ||
|
|
||
| public class BridgeRow { | ||
|
|
||
| private final List<BridgeStep> steps; | ||
|
|
||
| public BridgeRow(int width, Random random) { | ||
| this.steps = createOneRow(width, random); | ||
| } | ||
|
|
||
| private List<BridgeStep> createOneRow(int width, Random random) { | ||
| List<BridgeStep> row = generateRandomRow(width, random); | ||
| ensureNoSideBySideSteps(row, random); | ||
| return row; | ||
| } | ||
|
|
||
| private List<BridgeStep> generateRandomRow(int width, Random random) { | ||
| List<BridgeStep> row = new ArrayList<>(); | ||
| for (int i = 0; i < width; i++) { | ||
| row.add(BridgeStep.createRandomStep(random)); | ||
| } | ||
| return row; | ||
| } | ||
|
|
||
| private void ensureNoSideBySideSteps(List<BridgeStep> row, Random random) { | ||
| for (int i = 0; i < row.size() - 1; i++) { | ||
| fixSideBySideStepIfNeeded(row, i, random); | ||
| } | ||
| } | ||
|
|
||
| private void fixSideBySideStepIfNeeded(List<BridgeStep> row, int index, Random random) { | ||
| if (areBothStepsExisting(row, index)) { | ||
| setOneToNoneRandomly(row, index, random); | ||
| } | ||
| } | ||
|
|
||
| private boolean areBothStepsExisting(List<BridgeStep> row, int index) { | ||
| return row.get(index).isExist() && row.get(index + 1).isExist(); | ||
| } | ||
|
|
||
| private void setOneToNoneRandomly(List<BridgeStep> row, int index, Random random) { | ||
| if (random.nextBoolean()) { | ||
| row.set(index, BridgeStep.NONE); | ||
| return; | ||
| } | ||
| row.set(index + 1, BridgeStep.NONE); | ||
| } | ||
|
|
||
| public List<BridgeStep> getSteps() { | ||
| return Collections.unmodifiableList(this.steps); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // BridgeStep.java | ||
| package Model; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public enum BridgeStep { | ||
| NONE(0), // 다리 없음 | ||
| EXIST(1); // 다리 있음 | ||
|
|
||
| private final int value; | ||
|
|
||
| BridgeStep(int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public static BridgeStep createRandomStep(Random random) { | ||
| if (random.nextBoolean()) { | ||
| return EXIST; | ||
| } | ||
| return NONE; | ||
| } | ||
|
|
||
| public boolean isExist() { | ||
| return this == EXIST; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package Model; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class LadderDescentService { | ||
|
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. 로직 깔끔해서 좋네요 👍👍 |
||
| private final Bridge bridge; | ||
|
|
||
| public LadderDescentService(Bridge bridge) { | ||
| this.bridge = bridge; | ||
| } | ||
|
|
||
| public LadderResult calculateAllResults(int numberOfPlayers) { | ||
| Map<Integer, Integer> resultMap = IntStream.range(0, numberOfPlayers) | ||
| .boxed() | ||
| .collect(Collectors.toMap(i -> i, this::descent)); | ||
| return new LadderResult(resultMap); | ||
| } | ||
|
|
||
| private int descent(int curPos){ | ||
| int pos = curPos; | ||
| for (BridgeRow row : bridge.getRows()) { | ||
| List<BridgeStep> steps = row.getSteps(); | ||
| pos += checkRightBridge(steps,pos) + checkLeftBridge(steps,pos); | ||
| } | ||
| return pos; | ||
| } | ||
|
|
||
|
|
||
| private int checkRightBridge(List<BridgeStep> row, int pos) { | ||
| if (pos >= row.size()) { | ||
| return 0; | ||
| } | ||
| if (row.get(pos) == BridgeStep.EXIST) { | ||
| return 1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| private int checkLeftBridge(List<BridgeStep> row, int pos) { | ||
| if (pos == 0) { | ||
| return 0; | ||
| } | ||
| if (row.get(pos - 1) == BridgeStep.EXIST) { | ||
| return -1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package Model; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| public class LadderResult { | ||
| private final Map<Integer, Integer> resultMap; | ||
|
|
||
| public LadderResult(Map<Integer, Integer> resultMap) { | ||
| this.resultMap = resultMap; | ||
| } | ||
|
|
||
| public int getDestinationIndex(int startIndex) { | ||
| return resultMap.get(startIndex); | ||
| } | ||
|
|
||
| public Map<Integer, Integer> getResultMap() { | ||
| return Collections.unmodifiableMap(resultMap); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package Model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Player { | ||
| List<String> players; | ||
|
Comment on lines
+7
to
+8
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. 객체명은 플레이어라는 단수지만, 내부는 플레이어들이라는 복수형을 사용하고 있네요 요런 내용은 일급 컬렉션으로 따로 단수 객체는 단수 객체로, 복수 객체는 일급 컬렉션으로 나눠주면 좋습니다~ 이렇게 나누게 되면 코드 반영하기엔 시간이 오래 걸릴 것 같아 상상 코딩을 하자면 아래 메서드를 각각 어느 클래스에 둘 수 있을까요??
|
||
|
|
||
| public Player(List<String> players) { | ||
| this.players = players; | ||
| } | ||
|
|
||
| public List<String> getPlayers() { | ||
| return Collections.unmodifiableList(players); | ||
| } | ||
|
|
||
| public int getPlayersNumber() { | ||
| return players.size(); | ||
| } | ||
|
|
||
| public String getPlayerName(int index) { | ||
| return players.get(index); | ||
| } | ||
|
|
||
| public int getPlayerIndex(String name) { | ||
| return players.indexOf(name); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package Model; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Rewards { | ||
| private final List<String> rewards; | ||
|
|
||
| public Rewards(List<String> rewards) { | ||
| this.rewards = rewards; | ||
| } | ||
|
|
||
| public String getReward(int index) { | ||
| return rewards.get(index); | ||
| } | ||
|
|
||
| public List<String> getRewards() { | ||
| return Collections.unmodifiableList(rewards); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package view; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| public int getWidthAndHeight() { | ||
| return scanner.nextInt(); | ||
| } | ||
|
|
||
| public Scanner getScanner() { | ||
| return scanner; | ||
| } | ||
| } |
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와 isAll이 가장 위에 있는데 먼저 선언한 이유가 있나요??
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.
따로 의미를 두고 필드를 위에 선언 한 이유는 없습니다!
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.
아하 보통 변수는 선언과 동시에 초기화하는게 좋아요