-
Notifications
You must be signed in to change notification settings - Fork 252
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
[사다리 게임] 베디 미션 제출합니다 #25
Changes from 39 commits
2cc3e99
cb3b717
6df9cc2
01d5811
6608d8d
eb86751
74363d7
aadab6e
3c0bd46
55f30a5
649509b
c420611
a0b95aa
e098dda
2f850a8
b827d25
d662d9b
f4b91f0
3e2a108
3450cfb
cc4ffcc
d42089b
cd88537
8897d37
f50eedd
b725d33
dfcbd41
78801fa
9f0c993
392cac3
fb41b73
7e08808
f5dbd97
b18c3ba
f15db16
3aa21d4
1554134
62b6493
50e91f9
1b44dff
02902e6
0f86542
7cb9333
6162346
df1b9d2
f36dd55
e733222
59c15ed
a052ea2
7ed1cc9
80fbdc1
3357048
14b251f
de74195
b2a38d0
d8498ed
73e243a
746e6d5
f801ebc
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,20 @@ | ||
package calculator; | ||
|
||
public class Positive { | ||
private final int number; | ||
|
||
public Positive(final String number) { | ||
this(Integer.parseInt(number)); | ||
} | ||
|
||
public Positive(final int number) { | ||
if (number < 0) { | ||
throw new RuntimeException(); | ||
} | ||
this.number = number; | ||
} | ||
|
||
int getNumber() { | ||
return number; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package calculator; | ||
|
||
public class StringCalculator { | ||
static int calculate(String[] numbers) { | ||
if (isValidSize(numbers)) { | ||
return 0; | ||
} | ||
return plus(numbers); | ||
} | ||
|
||
private static boolean isValidSize(String[] numbers) { | ||
return numbers == null || numbers.length == 0; | ||
} | ||
|
||
private static int plus(String[] numbers) { | ||
int sum = 0; | ||
for (String number : numbers) { | ||
sum += toPositive(number).getNumber(); | ||
} | ||
return sum; | ||
} | ||
|
||
private static Positive toPositive(String number) { | ||
return new Positive(number); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package calculator; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class StringSplitter { | ||
private static final String CUSTOM_DELIMITER_REGEX = "//(.)\n(.*)"; | ||
private static final String DEFAULT_DELIMITER = ",|;"; | ||
|
||
static String[] split(String numbers) { | ||
Matcher m = Pattern.compile(CUSTOM_DELIMITER_REGEX).matcher(numbers); | ||
|
||
return m.find() ? splitCustom(m) : splitDefault(numbers); | ||
} | ||
|
||
private static String[] splitDefault(String numbers) { | ||
return numbers.split(DEFAULT_DELIMITER); | ||
} | ||
|
||
private static String[] splitCustom(Matcher m) { | ||
String customDelimiter = m.group(1); | ||
return m.group(2).split(Pattern.quote(customDelimiter)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ladder; | ||
|
||
import ladder.controller.LadderGameController; | ||
|
||
public class LadderConsoleApp { | ||
public static void main(String[] args) { | ||
LadderGameController ladderGameController = new LadderGameController(); | ||
ladderGameController.run(); | ||
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. 굳이 Controller를 별도로 추가하지 않고 main() 메소드를 controller처럼 사용해도 되지 않을까? 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,45 @@ | ||
package ladder.controller; | ||
|
||
import ladder.domain.*; | ||
import ladder.domain.generator.PlayerGenerator; | ||
import ladder.domain.generator.PlayerRewardsGenerator; | ||
import ladder.view.InputConsoleView; | ||
import ladder.view.OutputConsoleView; | ||
|
||
import java.util.List; | ||
|
||
public class LadderGameController { | ||
public void run() { | ||
GamePlayers gamePlayers = new GamePlayers(generatePlayers(inputNames())); | ||
PlayerRewards playerRewards = generateRewards(inputRewards()); | ||
Ladder ladder = new Ladder(inputHeight(), gamePlayers.size()); | ||
GameResult gameResult = new GameResult(ladder, gamePlayers, playerRewards); | ||
|
||
OutputConsoleView.printLadderGame(ladder, gamePlayers, playerRewards); | ||
String name; | ||
while (!(name = InputConsoleView.inputResultName()).equals("all")) { | ||
OutputConsoleView.printResult(gameResult, name); | ||
} | ||
OutputConsoleView.printResult(gameResult); | ||
} | ||
|
||
private List<Player> generatePlayers(String names) { | ||
return new PlayerGenerator(names).generate(); | ||
} | ||
|
||
private String inputNames() { | ||
return InputConsoleView.inputNames(); | ||
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. 굳이 이런 부분까지 메소드로 분리할 필요가 있을까? 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 int inputHeight() { | ||
return InputConsoleView.inputHeight(); | ||
} | ||
|
||
private String inputRewards() { | ||
return InputConsoleView.inputRewards(); | ||
} | ||
|
||
private PlayerRewards generateRewards(String results) { | ||
return new PlayerRewardsGenerator(results).generate(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package ladder.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public final class GamePlayers { | ||
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 List<Player> players; | ||
|
||
public GamePlayers(final List<Player> players) { | ||
validate(players); | ||
this.players = new ArrayList<>(players); | ||
} | ||
|
||
private void validate(List<Player> players) { | ||
validateSize(players); | ||
validateDuplication(players); | ||
} | ||
|
||
private void validateSize(List<Player> players) { | ||
if (players.isEmpty()) { | ||
throw new IllegalArgumentException("두 명 이상 입력 해주세요."); | ||
} | ||
} | ||
|
||
private void validateDuplication(List<Player> players) { | ||
Set<Player> set = new HashSet<>(players); | ||
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. 중복 체크를 위해 Set을 사용한다면 인스턴스 변수를 'List players' 대신 'Set players`로 구현하는 것은 어떨까? 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. List를 한 이유가 Player는 필드로 이름만 가지고 있어서 List의 index로 position을 찾기 위해서 한거였는데요. |
||
if (set.size() != players.size()) { | ||
throw new IllegalArgumentException("이름 중복은 안됩니다."); | ||
} | ||
} | ||
|
||
String getPlayerName(int index) { | ||
return players.get(index).getName(); | ||
} | ||
|
||
public int size() { | ||
return players.size(); | ||
} | ||
|
||
public List<Player> getPlayers() { | ||
return players; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ladder.domain; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class GameResult { | ||
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. GameResult에 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. 네 다시 생각해보니 이렇게 하는게 좋을거 같아서 반영했습니다. |
||
private final Map<String, String> results; | ||
|
||
public GameResult(Ladder ladder, GamePlayers gamePlayers, PlayerRewards playerRewards) { | ||
this.results = new HashMap<>(); | ||
init(ladder, gamePlayers, playerRewards); | ||
} | ||
|
||
private void init(Ladder ladder, GamePlayers gamePlayers, PlayerRewards playerRewards) { | ||
validate(gamePlayers.size(), playerRewards.size()); | ||
for (int i = 0; i < gamePlayers.size(); i++) { | ||
int result = ladder.moveLadder(i); | ||
results.put(gamePlayers.getPlayerName(i), playerRewards.getReward(result)); | ||
} | ||
} | ||
|
||
private void validate(int countOfPlayers, int countOfRewards) { | ||
if (countOfPlayers != countOfRewards) { | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
public String get(String playerName) { | ||
return results.get(playerName); | ||
} | ||
|
||
public Map<String, String> getAll() { | ||
return new HashMap<>(results); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ladder.domain; | ||
|
||
import ladder.domain.generator.LineGenerator; | ||
import ladder.domain.generator.LineRandomGenerator; | ||
|
||
import java.util.List; | ||
|
||
public final class Ladder { | ||
private static final int MIN_HEIGHT = 1; | ||
private static final int MIN_PLAYER_COUNT = 2; | ||
|
||
private final List<Line> lines; | ||
|
||
public Ladder(int height, int countOfPlayers) { | ||
this(height, countOfPlayers, new LineRandomGenerator(countOfPlayers, height)); | ||
} | ||
|
||
public Ladder(int height, int countOfPlayers, LineGenerator lineGenerator) { | ||
validate(height, countOfPlayers); | ||
this.lines = lineGenerator.generate(); | ||
} | ||
|
||
private void validate(int height, int countOfPlayers) { | ||
validateHeight(height); | ||
validateCount(countOfPlayers); | ||
} | ||
|
||
private void validateHeight(int height) { | ||
if (height < MIN_HEIGHT) { | ||
throw new IllegalArgumentException("높이는 1이상 이어야 합니다."); | ||
} | ||
} | ||
|
||
private void validateCount(int countOfPlayers) { | ||
if (countOfPlayers < MIN_PLAYER_COUNT) { | ||
throw new IllegalArgumentException("사람수는 2명 이상 이어야 합니다."); | ||
} | ||
} | ||
|
||
public int moveLadder(int position) { | ||
for (Line line : lines) { | ||
position += line.move(position); | ||
} | ||
return position; | ||
} | ||
|
||
public List<Line> getLines() { | ||
return lines; | ||
} | ||
} |
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.
의식적인 연습으로 TDD 영상 잘 봤습니다 👍