-
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 all 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,36 @@ | ||
package ladder; | ||
|
||
import ladder.domain.GameResult; | ||
import ladder.domain.Ladder; | ||
import ladder.domain.Players; | ||
import ladder.domain.Rewards; | ||
import ladder.domain.generator.DirectionsGeneratorFactory; | ||
import ladder.domain.generator.PlayersGenerator; | ||
import ladder.domain.generator.RewardsGenerator; | ||
import ladder.view.InputConsoleView; | ||
import ladder.view.OutputConsoleView; | ||
|
||
public class LadderConsoleApp { | ||
public static void main(String[] args) { | ||
Players players = generatePlayers(InputConsoleView.inputNames()); | ||
Rewards rewards = generateRewards(InputConsoleView.inputRewards()); | ||
Ladder ladder = new Ladder(InputConsoleView.inputHeight(), DirectionsGeneratorFactory.getInstance(players.size())); | ||
GameResult gameResult = new GameResult(ladder.play(players, rewards)); | ||
|
||
OutputConsoleView.printLadderGame(ladder, players, rewards); | ||
|
||
String name; | ||
while (!(name = InputConsoleView.inputResultName()).equals("all")) { | ||
OutputConsoleView.printResult(gameResult, name); | ||
} | ||
OutputConsoleView.printResult(gameResult); | ||
} | ||
|
||
private static Players generatePlayers(String names) { | ||
return new PlayersGenerator(names).generate(); | ||
} | ||
|
||
private static Rewards generateRewards(String results) { | ||
return new RewardsGenerator(results).generate(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package ladder.domain; | ||
|
||
import java.util.Objects; | ||
|
||
public class Direction { | ||
private static final int MOVE_LEFT = -1; | ||
private static final int MOVE_RIGHT = 1; | ||
private static final int MOVE_STRAIGHT = 0; | ||
|
||
private final boolean left; | ||
private final boolean right; | ||
|
||
private Direction(final boolean left, final boolean right) { | ||
if (left && right) { | ||
throw new IllegalArgumentException("연속 true 불가능"); | ||
} | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
public static Direction of(final boolean left, final boolean right) { | ||
return new Direction(left, right); | ||
} | ||
|
||
public static Direction first(final boolean right) { | ||
return of(false, right); | ||
} | ||
|
||
public Direction last() { | ||
return of(this.right, false); | ||
} | ||
|
||
public Direction next(final boolean right) { | ||
if (this.right) { | ||
return of(true, false); | ||
} | ||
return of(false, right); | ||
} | ||
|
||
public int move() { | ||
if (left) { | ||
return MOVE_LEFT; | ||
} | ||
if (right) { | ||
return MOVE_RIGHT; | ||
} | ||
return MOVE_STRAIGHT; | ||
} | ||
|
||
public boolean isRight() { | ||
return right; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
final Direction direction = (Direction) o; | ||
return left == direction.left && | ||
right == direction.right; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(left, right); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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(final Map<String, String> results) { | ||
this.results = results; | ||
} | ||
|
||
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,62 @@ | ||
package ladder.domain; | ||
|
||
import ladder.domain.generator.DirectionsGenerator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public final class Ladder { | ||
private static final int MIN_HEIGHT = 1; | ||
|
||
private final List<Line> lines; | ||
|
||
public Ladder(int height, DirectionsGenerator directionsGenerator) { | ||
validate(height); | ||
this.lines = generateLines(height, directionsGenerator); | ||
} | ||
|
||
private void validate(int height) { | ||
validateHeight(height); | ||
} | ||
|
||
private void validateHeight(int height) { | ||
if (height < MIN_HEIGHT) { | ||
throw new IllegalArgumentException("높이는 1이상 이어야 합니다."); | ||
} | ||
} | ||
|
||
private List<Line> generateLines(final int height, final DirectionsGenerator directionsGenerator) { | ||
List<Line> lines = new ArrayList<>(); | ||
for (int i = 0; i < height; i++) { | ||
lines.add(new Line(directionsGenerator.generate())); | ||
} | ||
return lines; | ||
} | ||
|
||
public List<Line> getLines() { | ||
return new ArrayList<>(lines); | ||
} | ||
|
||
public Map<String, String> play(final Players players, final Rewards rewards) { | ||
validate(players.size(), rewards.size()); | ||
Map<String, String> result = new HashMap<>(players.size()); | ||
|
||
for (final Player player : players.getPlayers()) { | ||
int position = player.getPosition(); | ||
for (final Line line : lines) { | ||
position += line.move(position); | ||
} | ||
result.put(player.getName(), rewards.getReward(position)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private void validate(final int playersSize, final int rewardsSize) { | ||
if (playersSize != rewardsSize){ | ||
throw new IllegalArgumentException("사람 수와 결과의 수가 다릅니다."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ladder.domain; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public final class Line { | ||
private final List<Direction> directions; | ||
|
||
public Line(final List<Direction> directions) { | ||
this.directions = directions; | ||
} | ||
|
||
public int move(int position) { | ||
return directions.get(position).move(); | ||
} | ||
|
||
public List<Direction> getDirections() { | ||
return directions; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Line line = (Line) o; | ||
return Objects.equals(directions, line.directions); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(directions); | ||
} | ||
} |
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 영상 잘 봤습니다 👍