Skip to content
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
42 changes: 35 additions & 7 deletions src/main/java/controller/GameController.java
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;
}
}
}
42 changes: 0 additions & 42 deletions src/main/java/domain/Ladder.java

This file was deleted.

58 changes: 0 additions & 58 deletions src/main/java/domain/Line.java

This file was deleted.

47 changes: 0 additions & 47 deletions src/main/java/domain/User.java

This file was deleted.

68 changes: 68 additions & 0 deletions src/main/java/domain/gameservice/GameService.java
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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

이 클래스가 수행하는 기능으로 봤을때 GameService 보다는 Game 그 자체에 더 가까운 것 같고요...
그렇다면 지금처럼 UserCreator 를 주입받는지 등의 고민을 할 필요 없이 게임 그 자체에 필요한 데이터만 보관하는 형태, 즉 지금 형태의 구현을 유지해도 될 것 같습니다.

@seokho-ham seokho-ham Feb 28, 2022

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

이 부분에 대해서 질문이 생겼습니다..ㅎㅎ
리뷰를 보고 서비스 객체였으면 Creator를 다른 방식으로 사용해야하는건지, GameService와 Game은 어떤 차이점이 있는건지 궁금합니다.
네이밍의 문제인건가요?

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;
}


}
18 changes: 18 additions & 0 deletions src/main/java/domain/item/Item.java
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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Item 인데 createResult 라는 이름을 쓰는게 조금 어색해 보이긴 합니다

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

처음에 클래스명을 Result로 작성했다가 Item으로 변경했는데 메서드명을 수정을 안했네요🥲

return new Item(name);
}

public String getName() {
return name;
}
}
17 changes: 17 additions & 0 deletions src/main/java/domain/item/ItemCreator.java
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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

모든 메소드가 static 이라면 생성자도 private 으로 가려야할 것 같군요

int i = 0;
Map<Integer, Item> map = new HashMap<>();
for (String result : results) {
map.put(i, Item.createResult(result));
i += 2;
}
return map;
}
}
58 changes: 58 additions & 0 deletions src/main/java/domain/ladder/Ladder.java
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

여기서 왜 리턴 타입이 int 인지 궁금합니다.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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;
}


}
15 changes: 15 additions & 0 deletions src/main/java/domain/ladder/LadderCreator.java
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;
}

}
Loading