-
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
[사다리 게임] 제이 미션 제출합니다. #32
Changes from 67 commits
8dca839
d778d8c
a8cb338
89849da
e8b354f
310e5b7
d8e6397
f436157
6c0a75f
e7214de
ba18316
05013b9
eac776d
106f537
fdf0b27
6fa401e
2946d8a
4d79c3f
624ed90
bff7f44
1457271
545049f
66f7a7d
13d055c
c3c787d
c6d88f7
ca47a49
43c893e
0ec4962
72f936b
3b24e3f
8249df4
a3f402e
0e24b7d
c724e8f
423c981
f41eabc
49f214b
b9bed67
5798216
d2e4718
df8e6d0
7114bd1
d409f85
b073ff4
65fedb9
4581449
52d5ec5
b1e402f
a6ad587
b978641
cdd46ee
dda6aa4
7f305d3
9fff3ce
4b00d0b
28a6f6f
29546e5
db227fa
c3a67e4
4c0d854
d11c006
a775004
9427ef1
3be7c0a
aa7c554
d0f459d
68e8fe2
963e375
0177f8f
6ade480
c806b7b
d99ffc1
d175361
6cf0c43
65f58aa
09422ff
13db4cb
f7b8c9b
4839494
5898a44
f8c4cb3
c91913e
71115f1
d8481fa
ef612e6
6bbb996
74440a8
631d9d4
6dcdb00
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,79 @@ | ||
package com.woowacourse.calculator.domain; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
public class Calculator { | ||
private static final Pattern PATTERN = Pattern.compile("//(.)\n(.*)"); | ||
private static final String DEFAULT_NUMBER = "0"; | ||
private static final String DEFAULT_DELIMETER = ":|,"; | ||
|
||
private String expression; | ||
private String delimeter; | ||
private List<Integer> numbers; | ||
|
||
public Calculator(String input) { | ||
init(input); | ||
numbers = parseNumbers(expression); | ||
} | ||
|
||
void init(String input) { | ||
if (checkEmptyOrNull(input)) return; | ||
if (checkCustomDelimiter(input)) return; | ||
|
||
expression = input; | ||
delimeter = DEFAULT_DELIMETER; | ||
} | ||
|
||
private boolean checkCustomDelimiter(String input) { | ||
Matcher m = PATTERN.matcher(input); | ||
if (m.find()) { | ||
String customDelimiter = m.group(1); | ||
expression = m.group(2); | ||
delimeter = customDelimiter; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private boolean checkEmptyOrNull(String input) { | ||
if (ifNullOrEmpty(input)) { | ||
expression = DEFAULT_NUMBER; | ||
delimeter = DEFAULT_DELIMETER; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
|
||
List<Integer> parseNumbers(String expression) { | ||
return Arrays.stream(expression.split(delimeter)) | ||
.map(this::checkIfNegative) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
int checkIfNegative(String number) { | ||
int no = Integer.parseInt(number); | ||
|
||
if (no < 0) { | ||
throw new RuntimeException("음수는 입력할 수 없습니다"); | ||
} | ||
|
||
return no; | ||
} | ||
|
||
private boolean ifNullOrEmpty(String input) { | ||
return input == null || input.isEmpty(); | ||
} | ||
|
||
public int calculate() { | ||
if (numbers.size() == 0) { | ||
return 0; | ||
} | ||
|
||
return numbers.stream().mapToInt(Integer::intValue).sum(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.woowacourse.laddergame; | ||
|
||
import com.woowacourse.laddergame.domain.vo.LadderDto; | ||
import com.woowacourse.laddergame.domain.vo.LadderResultDto; | ||
import com.woowacourse.laddergame.domain.vo.ResultNameDto; | ||
import com.woowacourse.laddergame.service.LadderGameService; | ||
import com.woowacourse.laddergame.view.InputView; | ||
import com.woowacourse.laddergame.view.OutputView; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
LadderDto ladderDto = new LadderDto(); | ||
InputView.inputPlayerNames(ladderDto); | ||
InputView.inputHeight(ladderDto); | ||
InputView.inputGameResult(ladderDto); | ||
|
||
LadderResultDto ladderResultDto = LadderGameService.play(ladderDto); | ||
|
||
OutputView.printLadderStatus(ladderResultDto); | ||
|
||
while (true) { | ||
ResultNameDto resultNameDto = new ResultNameDto(); | ||
InputView.inputResultName(resultNameDto); | ||
String targetName = resultNameDto.getName(); | ||
OutputView.printLadderGameResult(targetName, ladderResultDto, resultNameDto); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.woowacourse.laddergame.domain; | ||
|
||
import com.woowacourse.laddergame.util.NaturalNumber; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Ladder { | ||
private List<Line> lines; | ||
|
||
public Ladder(NaturalNumber height, NaturalNumber countOfPerson) { | ||
lines = new ArrayList<>(); | ||
for (int h = 0; h < height.getNumber(); h++) { | ||
lines.add(new Line(countOfPerson)); | ||
} | ||
} | ||
|
||
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. lines.get 하는 대신 Line에 메시지를 보내 Bridge를 놓을 수 있는지 판단하는 것은 어떨까요? get 하는 대신 제이님이 이미 잘 추출해낸 객체에 메시지를 보내면 좋을 것 같아요! 또한 아래 isContainsLine 메서드 구현의 경우 lines.contains등과 같이 List가 제공하는 메서드를 사용해도 되지 않을까요? 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. get을 한 이유는 어떤 라인에(get) 어떤 위치에 (put bridge) 다리를 놓을지 결정하기 위해서 사용을 했는데 필드가 List lines 라 한 개의 Line을 꺼내기 위해 get을 사용했습니다! isContainsLine(int 높이, Line 라인) 의 의도는 사다리에게 특정 높이에 특정 라인이 있는지 확인하는 의도로 작성하였습니다! 그래서 그냥 사다리에 contains를 하면 라인이 있는 것은 확인이 되지만 어느 높이에 있는지는 테스트로 알 수 없기 때문에 저렇게 하였습니다! |
||
public int getHeight() { | ||
return lines.size(); | ||
} | ||
|
||
public int takeLadder(NaturalNumber personNo) { | ||
int currentPosition = personNo.getNumber(); | ||
for (int i = 0; i < getHeight(); i++) { | ||
currentPosition = lines.get(i).takeLine(new NaturalNumber(currentPosition)); | ||
} | ||
return currentPosition; | ||
} | ||
|
||
public boolean isContainsLine(NaturalNumber height, Line line) { | ||
return lines.get(height.convertIndex()).equals(line); | ||
} | ||
|
||
public void putBridge(NaturalNumber height, NaturalNumber position) { | ||
lines.get(height.convertIndex()).putBridge(position); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Ladder ladder = (Ladder) o; | ||
|
||
return lines != null ? lines.equals(ladder.lines) : ladder.lines == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return lines != null ? lines.hashCode() : 0; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
for (Line line : lines) { | ||
sb.append(line.toString()).append("\n"); | ||
} | ||
return sb.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.woowacourse.laddergame.domain; | ||
|
||
import com.woowacourse.laddergame.util.NaturalNumber; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Line { | ||
private List<Position> positions; | ||
|
||
public Line(NaturalNumber countOfPerson) { | ||
positions = new ArrayList<>(); | ||
|
||
for (int i = 0; i < countOfPerson.getNumber(); i++) { | ||
positions.add(Position.NONE); | ||
} | ||
} | ||
|
||
public int getPositionCount() { | ||
return positions.size(); | ||
} | ||
|
||
public void putBridge(NaturalNumber number) { | ||
if (number.getNumber() >= positions.size()) { | ||
throw new IllegalArgumentException("다리를 놓을 수 없습니다"); | ||
} | ||
|
||
if (positions.get(number.convertIndex()) != Position.NONE) { | ||
throw new IllegalArgumentException("다리가 존재하거나 연속되게 놓을 수 없습니다."); | ||
} | ||
|
||
positions.set(number.convertIndex(), Position.RIGHT); | ||
positions.set(number.convertIndex() + 1, Position.LEFT); | ||
} | ||
|
||
public boolean isBridgeExist(int index) { | ||
return positions.get(index) != Position.NONE; | ||
} | ||
|
||
public int takeLine(NaturalNumber positionNo) { | ||
return positions.get(positionNo.convertIndex()).move(positionNo.getNumber()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
for (Position position : positions) { | ||
if (position.equals(Position.LEFT)) { | ||
sb.append("-----|"); | ||
continue; | ||
} | ||
sb.append(" |"); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Line line = (Line) o; | ||
|
||
return positions != null ? positions.equals(line.positions) : line.positions == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return positions != null ? positions.hashCode() : 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.woowacourse.laddergame.domain; | ||
|
||
public class Player { | ||
private static final int MAX_NAME_LENGTH = 5; | ||
private static final String ILLEGAL_NAME = "all"; | ||
|
||
private final String name; | ||
|
||
public Player(String name) { | ||
checkPlayerNameIfNull(name); | ||
checkPlayerNameEmptySpace(name); | ||
checkPlayerNameBlankSpace(name); | ||
checkPlayerNameLength(name); | ||
checkPlayerNamePattern(name); | ||
|
||
this.name = name; | ||
} | ||
|
||
private void checkPlayerNameIfNull(String name) { | ||
if (name == null) { | ||
throw new IllegalArgumentException("null을 입력할 수 없습니다"); | ||
} | ||
} | ||
|
||
private void checkPlayerNameEmptySpace(String name) { | ||
if (name.contains(" ")) { | ||
throw new IllegalArgumentException("이름에 공백이 있으면 안됩니다"); | ||
} | ||
} | ||
|
||
private void checkPlayerNameBlankSpace(String name) { | ||
if (name.trim().length() == 0) { | ||
throw new IllegalArgumentException("공백을 입력할 수 없습니다"); | ||
} | ||
} | ||
|
||
private void checkPlayerNameLength(String name) { | ||
if (name.length() > MAX_NAME_LENGTH) { | ||
throw new IllegalArgumentException("이름은 5글자 까지 가능합니다"); | ||
} | ||
} | ||
|
||
private void checkPlayerNamePattern(String name) { | ||
if (name.equals(ILLEGAL_NAME)) { | ||
throw new IllegalArgumentException("all은 player 이름으로 입력할 수 없습니다"); | ||
} | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Player player = (Player) o; | ||
|
||
return name != null ? name.equals(player.name) : player.name == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return name != null ? name.hashCode() : 0; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%6.6s", name); | ||
} | ||
} |
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.
Calculator 클래스에도 숫자가 있습니다!
의미를 나타낼 수 있는 경우 상수를 활용하면 어떨까요!?