-
Notifications
You must be signed in to change notification settings - Fork 34
[사다리] 김의천 미션 제출합니다. #34
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: wzrabbit
Are you sure you want to change the base?
Changes from all commits
52d07e8
ed36c10
7488fa2
61db22a
4cf72cc
b53b159
9fd10c3
291d0a7
dbf81cd
273efea
1c1faaf
5a69f09
78c4c89
bb897a0
c947713
7263db0
5a588ed
4a85f14
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,11 @@ | ||
import controller.LadderGameController; | ||
|
||
public class LadderApplication { | ||
public static void main(String[] args) { | ||
final LadderGameController ladderGameController = new LadderGameController(); | ||
|
||
ladderGameController.inputGameInfos(); | ||
ladderGameController.showGameStatus(); | ||
ladderGameController.showGameResult(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,91 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
package controller; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
import model.*; | ||||||||||||||||||||||||||||||||||||||||||||||
import view.InputView; | ||||||||||||||||||||||||||||||||||||||||||||||
import view.OutputView; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
import java.util.InputMismatchException; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
public class LadderGameController { | ||||||||||||||||||||||||||||||||||||||||||||||
private LadderGame ladderGame; | ||||||||||||||||||||||||||||||||||||||||||||||
private final LadderGenerator ladderGenerator = new LadderGenerator(new LadderConnectionGenerator()); | ||||||||||||||||||||||||||||||||||||||||||||||
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 void inputGameInfos() { | ||||||||||||||||||||||||||||||||||||||||||||||
PlayerNames playerNames = inputPlayerNames(); | ||||||||||||||||||||||||||||||||||||||||||||||
PrizeNames prizeNames = inputPrizeNames(playerNames.size()); | ||||||||||||||||||||||||||||||||||||||||||||||
int ladderHeight = inputLadderHeight(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
ladderGame = new LadderGame(ladderGenerator, playerNames, prizeNames, ladderHeight); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
public void showGameStatus() { | ||||||||||||||||||||||||||||||||||||||||||||||
OutputView.printGameStatus(ladderGame.getGameStatus()); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
public void showGameResult() { | ||||||||||||||||||||||||||||||||||||||||||||||
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. 혹시 이쪽에 enum을 사용해보실 생각이 있으실까요? ENUM을 직접 활용한 부분이 없어서 아쉬워요
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
while (true) { | ||||||||||||||||||||||||||||||||||||||||||||||
PlayerName playerNameForResult = InputView.inputPlayerNameForResult(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
if (playerNameForResult.getName().equals("all")) { | ||||||||||||||||||||||||||||||||||||||||||||||
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. all키워드는 자주 사용되는 것 같아서 상수로 추출해도 좋을 것 같아요 |
||||||||||||||||||||||||||||||||||||||||||||||
OutputView.printAllPlayersResult(ladderGame.getAllPlayersResult()); | ||||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
if (playerNameForResult.getName().equals("exit")) { | ||||||||||||||||||||||||||||||||||||||||||||||
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. exit키워드도 상수로 추출해도 좋을 것 같아여 |
||||||||||||||||||||||||||||||||||||||||||||||
OutputView.printExitMessage(); | ||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
OutputView.printPlayerResult(ladderGame.getPlayerPrize(playerNameForResult)); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
private PlayerNames inputPlayerNames() { | ||||||||||||||||||||||||||||||||||||||||||||||
while (true) { | ||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||
return InputView.inputPlayerNames(); | ||||||||||||||||||||||||||||||||||||||||||||||
} catch (IllegalArgumentException e) { | ||||||||||||||||||||||||||||||||||||||||||||||
OutputView.printErrorMessage(e.getMessage()); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+45
to
+52
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-try-catch 로직이 자주 등장하는데. 뭔가 메서드로 추상화해서 사용해도 좋을 것 같아요 |
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
private PrizeNames inputPrizeNames(int prizeCount) { | ||||||||||||||||||||||||||||||||||||||||||||||
while (true) { | ||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||
return InputView.inputPrizeNames(); | ||||||||||||||||||||||||||||||||||||||||||||||
} catch (IllegalArgumentException e) { | ||||||||||||||||||||||||||||||||||||||||||||||
OutputView.printErrorMessage(e.getMessage()); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
private int inputLadderHeight() { | ||||||||||||||||||||||||||||||||||||||||||||||
while (true) { | ||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||
int ladderHeight = InputView.inputLadderHeight(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
if (ladderHeight <= 0) { | ||||||||||||||||||||||||||||||||||||||||||||||
throw new IllegalArgumentException("사다리 높이를 양수로 입력해주세요."); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
return ladderHeight; | ||||||||||||||||||||||||||||||||||||||||||||||
} catch (NumberFormatException e) { | ||||||||||||||||||||||||||||||||||||||||||||||
OutputView.printErrorMessage("사다리 높이를 정수로 입력해주세요!"); | ||||||||||||||||||||||||||||||||||||||||||||||
} catch (IllegalArgumentException e) { | ||||||||||||||||||||||||||||||||||||||||||||||
OutputView.printErrorMessage(e.getMessage()); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
private PlayerName inputPlayerNameForResult() { | ||||||||||||||||||||||||||||||||||||||||||||||
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 (true) { | ||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||
return inputPlayerNameForResult(); | ||||||||||||||||||||||||||||||||||||||||||||||
} catch (IllegalArgumentException e) { | ||||||||||||||||||||||||||||||||||||||||||||||
OutputView.printErrorMessage(e.getMessage()); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package model; | ||
|
||
public interface BooleanValueGenerator { | ||
boolean generate(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class Ladder { | ||
private final List<LadderRow> ladder; | ||
private final int rowCount; | ||
private final int columnCount; | ||
|
||
public Ladder(List<LadderRow> ladder) { | ||
validateLadder(ladder); | ||
this.ladder = new ArrayList<>(ladder); | ||
this.rowCount = ladder.size(); | ||
this.columnCount = ladder.get(0).size() + 1; | ||
} | ||
|
||
private void validateLadder(List<LadderRow> ladder) { | ||
boolean isAllLadderRowSizeSame = ladder.stream() | ||
.map(LadderRow::size) | ||
.distinct() | ||
.count() == 1; | ||
|
||
if (!isAllLadderRowSizeSame) { | ||
throw new IllegalArgumentException("사다리를 이루는 행의 크기는 모두 같아야 합니다."); | ||
} | ||
} | ||
|
||
private int getBottomIndex(int topIndex) { | ||
int column = topIndex; | ||
|
||
for (int row = 0; row < rowCount; row += 1) { | ||
if (column > 0 && ladder.get(row).get(column - 1)) { | ||
column -= 1; | ||
continue; | ||
} | ||
|
||
if (column < columnCount - 1 && ladder.get(row).get(column)) { | ||
column += 1; | ||
} | ||
} | ||
|
||
return column; | ||
} | ||
|
||
public List<Integer> getConnections() { | ||
return IntStream.range(0, columnCount) | ||
.mapToObj((topIndex) -> getBottomIndex(topIndex)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<LadderRow> getLadder() { | ||
return Collections.unmodifiableList(ladder); | ||
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. 오 고오급 문법! |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package model; | ||
|
||
import java.util.Random; | ||
|
||
public class LadderConnectionGenerator implements BooleanValueGenerator { | ||
private final Random random = new Random(); | ||
|
||
@Override | ||
public boolean generate() { | ||
return random.nextBoolean(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class LadderGame { | ||
private final Ladder ladder; | ||
private final PlayerNames playerNames; | ||
private final PrizeNames prizeNames; | ||
private final List<Integer> ladderConnections; | ||
Comment on lines
+8
to
+11
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 LadderGame(LadderGenerator ladderGenerator, PlayerNames playerNames, PrizeNames prizeNames, | ||
int ladderHeight) { | ||
this.ladder = ladderGenerator.generateLadder(ladderHeight, playerNames.size()); | ||
this.playerNames = playerNames; | ||
this.prizeNames = prizeNames; | ||
this.ladderConnections = ladder.getConnections(); | ||
} | ||
|
||
public LadderGameStatus getGameStatus() { | ||
return new LadderGameStatus(ladder, playerNames, prizeNames); | ||
} | ||
|
||
public String getPlayerPrize(PlayerName playerName) { | ||
int playerIndex = playerNames.getNames().indexOf(playerName.getName()); | ||
|
||
if (playerIndex == -1) { | ||
return null; | ||
} | ||
|
||
return prizeNames.get(ladderConnections.get(playerIndex)); | ||
} | ||
|
||
public List<LadderGamePlayerResult> getAllPlayersResult() { | ||
return playerNames | ||
.getNames() | ||
.stream() | ||
.map(playerName -> { | ||
String prizeName = getPlayerPrize(new PlayerName(playerName)); | ||
return new LadderGamePlayerResult(new PlayerName(playerName), new PrizeName(prizeName)); | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package model; | ||
|
||
public class LadderGamePlayerResult { | ||
private final PlayerName playerName; | ||
private final PrizeName prizeName; | ||
|
||
public LadderGamePlayerResult(PlayerName playerName, PrizeName prizeName) { | ||
this.playerName = playerName; | ||
this.prizeName = prizeName; | ||
} | ||
|
||
public String getPlayerName() { | ||
return this.playerName.getName(); | ||
} | ||
|
||
public String getPrizeName() { | ||
return this.prizeName.getName(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
|
||
public class LadderGameStatus { | ||
private final Ladder ladder; | ||
private final PlayerNames playerNames; | ||
private final PrizeNames prizeNames; | ||
|
||
public LadderGameStatus(Ladder ladder, PlayerNames playerNames, PrizeNames prizeNames) { | ||
this.ladder = ladder; | ||
this.playerNames = playerNames; | ||
this.prizeNames = prizeNames; | ||
} | ||
|
||
public List<LadderRow> getLadder() { | ||
return ladder.getLadder(); | ||
} | ||
|
||
public List<String> getPlayerNames() { | ||
return playerNames.getNames(); | ||
} | ||
|
||
public List<String> getPrizeNames() { | ||
return prizeNames.getNames(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class LadderGenerator { | ||
private final BooleanValueGenerator booleanValueGenerator; | ||
|
||
public LadderGenerator(BooleanValueGenerator booleanValueGenerator) { | ||
this.booleanValueGenerator = booleanValueGenerator; | ||
} | ||
|
||
public Ladder generateLadder(int rowCount, int columnCount) { | ||
List<LadderRow> ladder = IntStream.range(0, rowCount) | ||
.mapToObj((index) -> generateRow(columnCount - 1)) | ||
.collect(Collectors.toList()); | ||
|
||
return new Ladder(ladder); | ||
} | ||
|
||
private LadderRow generateRow(int ladderRowSize) { | ||
List<Boolean> ladderRow = new ArrayList<>(); | ||
|
||
while (ladderRow.size() < ladderRowSize) { | ||
if (ladderRow.size() == 0 || !ladderRow.get(ladderRow.size() - 1)) { | ||
ladderRow.add(booleanValueGenerator.generate()); | ||
continue; | ||
} | ||
|
||
ladderRow.add(false); | ||
} | ||
|
||
return new LadderRow(ladderRow); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class LadderRow { | ||
private final List<Boolean> ladderRow; | ||
|
||
public LadderRow(List<Boolean> ladderRow) { | ||
validateLadderLine(ladderRow); | ||
this.ladderRow = new ArrayList<>(ladderRow); | ||
} | ||
|
||
private void validateLadderLine(List<Boolean> ladderRow) { | ||
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. 올 연결선 겹치는거 유효성 처리는 완전 굿인데요 |
||
if (ladderRow.isEmpty()) { | ||
throw new IllegalArgumentException("사다리를 이루는 행은 비어서는 안 됩니다."); | ||
} | ||
|
||
for (int i = 0; i < ladderRow.size() - 1; i++) { | ||
if (ladderRow.get(i) && ladderRow.get(i + 1)) { | ||
throw new IllegalArgumentException("사다리를 이루는 행의 연결선은 겹칠 수 없습니다."); | ||
} | ||
} | ||
} | ||
|
||
public List<Boolean> getRow() { | ||
return Collections.unmodifiableList(ladderRow); | ||
} | ||
|
||
public boolean get(int index) { | ||
return ladderRow.get(index); | ||
} | ||
|
||
public int size() { | ||
return ladderRow.size(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PlayerName { | ||
private final String playerName; | ||
|
||
private static final int MIN_PLAYER_NAME_LENGTH = 1; | ||
private static final int MAX_PLAYER_NAME_LENGTH = 5; | ||
|
||
public PlayerName(String playerName) { | ||
validatePlayerName(playerName); | ||
this.playerName = playerName; | ||
} | ||
|
||
private void validatePlayerName(String playerName) { | ||
if (playerName.length() < MIN_PLAYER_NAME_LENGTH || playerName.length() > MAX_PLAYER_NAME_LENGTH) { | ||
throw new IllegalArgumentException("참가자의 이름은 " + MIN_PLAYER_NAME_LENGTH + "글자 이상 " + | ||
MAX_PLAYER_NAME_LENGTH + "이하여야 합니다."); | ||
} | ||
} | ||
|
||
public String getName() { | ||
return playerName; | ||
} | ||
} |
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.
가독성을 위해 자잘하게 함수를 나눈거죠? 좋네요 확인하기!
컨트롤러에서 input output에 대한 처리도 좀 하는걸로 보이는데 유효성 검사 때문에 상단에서 몰아서 유효한지 확인하는건가요?