-
Notifications
You must be signed in to change notification settings - Fork 37
[포키] 사다리 구현 5단계 - 실행결과 출력 #190
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: seokho-ham
Are you sure you want to change the base?
Changes from all commits
05d2a1c
1fabc23
ae16b2c
c507bad
8531f0f
ce41cf7
aa4bf91
efad216
4fc5b7c
66a1da5
ea9d71c
639a103
8c61cdc
250c190
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,18 +1,46 @@ | ||
| package controller; | ||
|
|
||
| import domain.service.GameService; | ||
| import view.InputUtil; | ||
| import view.OutputUtil; | ||
| import domain.gameservice.GameService; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
|
|
||
| public class GameController { | ||
| private final InputView inputView = new InputView(); | ||
| private final OutputView outputView = new OutputView(); | ||
|
|
||
|
|
||
| public void run() { | ||
| String[] users = InputUtil.getUserNameInput();; | ||
| int lineHeight = InputUtil.getLadderSizeInput(); | ||
| boolean status = true; | ||
| List<String> users = inputView.inputUserName(); | ||
| List<String> results = inputView.inputItems(); | ||
| int lineHeight = inputView.inputLadderSize(); | ||
|
|
||
| GameService ladderGame = new GameService(lineHeight, users, results); | ||
|
|
||
| GameService ladderGame = new GameService(lineHeight, users); | ||
| OutputUtil.printLadder(ladderGame.getGameResult()); | ||
| outputView.printUsers(ladderGame.getUsers()); | ||
| outputView.printLadder(ladderGame.getLadder()); | ||
| outputView.printItems(ladderGame.getItems()); | ||
|
|
||
| ladderGame.playGame(); | ||
|
|
||
| while (status) { | ||
| String target = inputView.inputResultTarget(); | ||
| status = handleTargetInput(target, ladderGame); | ||
| } | ||
| } | ||
|
|
||
| public boolean handleTargetInput(String target, GameService ladderGame) { | ||
| if (target.equals("춘식이")) { | ||
| return false; | ||
| } else if (target.equals("all")) { | ||
| outputView.printAllResult(ladderGame.getResults()); | ||
| return true; | ||
| } else { | ||
| outputView.printSingleResult(ladderGame.getSingleUserResult(target)); | ||
| return true; | ||
| } | ||
| } | ||
| } |
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package domain.gameservice; | ||
|
|
||
| import domain.ladder.Ladder; | ||
| import domain.ladder.LadderCreator; | ||
| import domain.ladder.Line; | ||
| import domain.item.Item; | ||
| import domain.item.ItemCreator; | ||
| import domain.user.User; | ||
| import domain.user.UserCreator; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class GameService { | ||
| private Map<String, User> users; | ||
| private Map<Integer, Item> items; | ||
| private Ladder ladder; | ||
| private Map<String, String> results; | ||
|
|
||
| public GameService( int lineHeight, List<String> users,List<String> items) { | ||
| this.users = UserCreator.createUserMap(users); | ||
| this.items = ItemCreator.createResultMap(items); | ||
| this.ladder = LadderCreator.createLadder(lineHeight, calculateLadderWidth()); | ||
| this.results = new HashMap<>(); | ||
| } | ||
|
|
||
| public void playGame() { | ||
| for (String user : users.keySet()) { | ||
| results.putAll(playSinglePlayer(users.get(user))); | ||
| } | ||
| } | ||
|
|
||
| public List<User> getUsers() { | ||
| return new ArrayList<>(users.values()); | ||
| } | ||
|
|
||
| public List<Item> getItems() { | ||
| return new ArrayList<>(items.values()); | ||
| } | ||
|
|
||
| public List<Line> getLadder() { | ||
| return ladder.getLadders(); | ||
| } | ||
|
|
||
| public Map<String, String> getResults() { | ||
| return Collections.unmodifiableMap(results); | ||
| } | ||
|
|
||
| public String getSingleUserResult(String username) { | ||
| return results.get(username); | ||
| } | ||
|
|
||
| private Map<String, String> playSinglePlayer(User user) { | ||
| int userPoint = user.getPoint(); | ||
|
|
||
| for (int i = 0; i < ladder.getLadderSize(); i++) { | ||
| userPoint += ladder.move(i, userPoint); | ||
| } | ||
| Item item = items.get(userPoint); | ||
| return new HashMap<>(){{ | ||
| put(user.getName(), item.getName());}}; | ||
| } | ||
|
|
||
| private int calculateLadderWidth() { | ||
| return users.size() * 2 - 1; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package domain.item; | ||
|
|
||
|
|
||
| public class Item { | ||
| private final String name; | ||
|
|
||
| private Item(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public static Item createResult(String name) { | ||
|
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.
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. 처음에 클래스명을 Result로 작성했다가 Item으로 변경했는데 메서드명을 수정을 안했네요🥲 |
||
| return new Item(name); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package domain.item; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class ItemCreator { | ||
| public static Map<Integer, Item> createResultMap(List<String> results) { | ||
|
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. 모든 메소드가 |
||
| int i = 0; | ||
| Map<Integer, Item> map = new HashMap<>(); | ||
| for (String result : results) { | ||
| map.put(i, Item.createResult(result)); | ||
| i += 2; | ||
| } | ||
| return map; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package domain.ladder; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Ladder { | ||
| private final int ladderWidth; | ||
| private final int ladderHeight; | ||
| private final List<Line> ladders; | ||
|
|
||
|
|
||
| public Ladder(int ladderHeight, int ladderWidth) { | ||
| this.ladderHeight = ladderHeight; | ||
| this.ladderWidth = ladderWidth; | ||
| ladders = initLadder(); | ||
| } | ||
|
|
||
| public int getLadderSize() { | ||
| return ladderHeight; | ||
| } | ||
|
|
||
| public List<Line> getLadders() { | ||
| return Collections.unmodifiableList(ladders); | ||
| } | ||
|
|
||
| public int move(int rowNumber, int userPoint) { | ||
| Line line = ladders.get(rowNumber); | ||
| if (userPoint == 0) { | ||
| return canMoveRight(line, userPoint); | ||
| } | ||
| if (userPoint == ladderWidth-1) { | ||
| return canMoveLeft(line, userPoint); | ||
| } | ||
| return canMoveLeft(line, userPoint) + canMoveRight(line, userPoint); | ||
|
|
||
| } | ||
|
|
||
| private int canMoveLeft(Line line, int userPoint) { | ||
| if (line.getPrev(userPoint) == 1) return -2; | ||
| return 0; | ||
| } | ||
|
|
||
| private int canMoveRight(Line line, int userPoint) { | ||
| if (line.getNext(userPoint) == 1) return 2; | ||
| return 0; | ||
| } | ||
|
Comment on lines
+39
to
+47
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. 여기서 왜 리턴 타입이
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. move 메서드에서 인덴트를 1로 유지해야겠다는 생각에 유저가 이동할 값을 바로 리턴하도록 작성했고 그래서 리턴타입을 int로 정했습습니다. |
||
|
|
||
| private List<Line> initLadder() { | ||
| List<Line> result = new ArrayList<>(); | ||
| for (int i = 0; i < ladderHeight; i++) { | ||
| result.add(Line.createRandomStep(ladderWidth)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package domain.ladder; | ||
|
|
||
| public class LadderCreator { | ||
| public static Ladder createLadder(int ladderHeight, int ladderWidth) { | ||
| if (!validateLadderHeight(ladderHeight)) { | ||
| throw new IllegalArgumentException("입력값이 올바르지 않습니다."); | ||
| } | ||
| return new Ladder(ladderHeight, ladderWidth); | ||
| } | ||
|
|
||
| private static boolean validateLadderHeight(int lineCount) { | ||
| return lineCount >= 1; | ||
| } | ||
|
|
||
| } |
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.
이 클래스가 수행하는 기능으로 봤을때
GameService보다는Game그 자체에 더 가까운 것 같고요...그렇다면 지금처럼
UserCreator를 주입받는지 등의 고민을 할 필요 없이 게임 그 자체에 필요한 데이터만 보관하는 형태, 즉 지금 형태의 구현을 유지해도 될 것 같습니다.Uh oh!
There was an error while loading. Please reload this page.
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.
이 부분에 대해서 질문이 생겼습니다..ㅎㅎ
리뷰를 보고 서비스 객체였으면 Creator를 다른 방식으로 사용해야하는건지, GameService와 Game은 어떤 차이점이 있는건지 궁금합니다.
네이밍의 문제인건가요?