Skip to content
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

Merged
merged 90 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from 67 commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
8dca839
docs: Update README
JunHoPark93 May 14, 2019
d778d8c
feat: Define initial Calculator class
JunHoPark93 May 14, 2019
a8cb338
docs: Update README
JunHoPark93 May 14, 2019
89849da
feat: Add NaturalNumber class
JunHoPark93 May 14, 2019
e8b354f
feat: Add Line class
JunHoPark93 May 14, 2019
310e5b7
refactor: Change NaturalNumber in Line class
JunHoPark93 May 14, 2019
d8e6397
feat: Add Ladder class
JunHoPark93 May 14, 2019
f436157
test: Add Natural Number test
JunHoPark93 May 15, 2019
6c0a75f
test: Add PositionTest
JunHoPark93 May 15, 2019
e7214de
feat: Add toString method and test
JunHoPark93 May 15, 2019
ba18316
feat: Add exception handling while trying to putting bridge in same p…
JunHoPark93 May 15, 2019
05013b9
feat: Add Random Generator
JunHoPark93 May 15, 2019
eac776d
refactor: Change natural number boundary
JunHoPark93 May 15, 2019
106f537
test: Add test for comparing lines
JunHoPark93 May 15, 2019
fdf0b27
feat: Add LadderGenerator class
JunHoPark93 May 15, 2019
6fa401e
feat: Add toString method to ladder
JunHoPark93 May 15, 2019
2946d8a
docs: Update README
JunHoPark93 May 16, 2019
4d79c3f
feat: Add LadderVO
JunHoPark93 May 16, 2019
624ed90
refactor: Change generateLadder to static
JunHoPark93 May 16, 2019
bff7f44
feat: Add Player, Players class
JunHoPark93 May 16, 2019
1457271
feat: Add console view
JunHoPark93 May 16, 2019
545049f
feat: Add LadderStatusVO
JunHoPark93 May 16, 2019
66f7a7d
feat: Add LadderGameService
JunHoPark93 May 16, 2019
13d055c
feat: Add MadeLadder
JunHoPark93 May 16, 2019
c3c787d
feat: Add initial Main class
JunHoPark93 May 16, 2019
c6d88f7
style: Polish line
JunHoPark93 May 16, 2019
ca47a49
feat: Add input view for game result
JunHoPark93 May 16, 2019
43c893e
feat: Add ladder result input method
JunHoPark93 May 16, 2019
0ec4962
feat: Add Result class
JunHoPark93 May 16, 2019
72f936b
refactor: Change method name in LadderVO
JunHoPark93 May 16, 2019
3b24e3f
feat: Add result input method in console
JunHoPark93 May 16, 2019
8249df4
test: Update ladder integration test with user game result input
JunHoPark93 May 16, 2019
a3f402e
test: Add taking ladder test
JunHoPark93 May 16, 2019
0e24b7d
docs: Update README
JunHoPark93 May 16, 2019
c724e8f
feat: Add ResultNameVO class
JunHoPark93 May 16, 2019
423c981
feat: Add getting player index number function in Players
JunHoPark93 May 16, 2019
f41eabc
feat: Add getting result string method in Result class
JunHoPark93 May 16, 2019
49f214b
refactor: Add equals and hashcode in Player
JunHoPark93 May 16, 2019
b9bed67
feat: Add LadderGameResultVO class
JunHoPark93 May 16, 2019
5798216
feat: Add taking ladder method
JunHoPark93 May 16, 2019
d2e4718
feat: Add output view message
JunHoPark93 May 16, 2019
df8e6d0
feat: Add checking duplicate player names
JunHoPark93 May 17, 2019
7114bd1
refactor: Change class name to Dto
JunHoPark93 May 17, 2019
d409f85
feat: Add ladder, ladder result dto
JunHoPark93 May 17, 2019
b073ff4
feat: Add made ladder, winner value object
JunHoPark93 May 17, 2019
65fedb9
feat: Add duplicate name check condition in Player
JunHoPark93 May 17, 2019
4581449
feat: Add taking ladder function which will return result dto
JunHoPark93 May 17, 2019
52d5ec5
refactor: refactor dto test name
JunHoPark93 May 17, 2019
b1e402f
feat: Update input output view
JunHoPark93 May 17, 2019
a6ad587
Merge branch 'calculator' into JunHoPark93
JunHoPark93 May 17, 2019
b978641
test: Fix minor info message
JunHoPark93 May 17, 2019
cdd46ee
refactor: Separate methods in LadderDto
JunHoPark93 May 17, 2019
dda6aa4
refactor: Delete LadderGameResultDto
JunHoPark93 May 17, 2019
7f305d3
refactor: Add reserved word in LadderResultDto
JunHoPark93 May 17, 2019
9fff3ce
style: Polish lines
JunHoPark93 May 17, 2019
4b00d0b
style: Polish lines
JunHoPark93 May 17, 2019
28a6f6f
style: Polish lines
JunHoPark93 May 17, 2019
29546e5
refactor: Separate Player constructor condition checking
JunHoPark93 May 17, 2019
db227fa
refactor: Separate ladder game service method structure
JunHoPark93 May 17, 2019
c3a67e4
style: Polish lines and methods
JunHoPark93 May 17, 2019
4c0d854
refactor: Polish test method, naming, reduce repeated code
JunHoPark93 May 17, 2019
d11c006
fix: Fix input invalid name in getting ladder result
JunHoPark93 May 17, 2019
a775004
refactor: Move generator class in util package
JunHoPark93 May 17, 2019
9427ef1
refactor: Change todo comment
JunHoPark93 May 17, 2019
3be7c0a
refactor: Separate calculator constructor
JunHoPark93 May 17, 2019
aa7c554
refactor: Separate generating ladder method
JunHoPark93 May 17, 2019
d0f459d
style: Polish lines
JunHoPark93 May 17, 2019
68e8fe2
refactor: Remove console test
JunHoPark93 May 18, 2019
963e375
refactor: Add test case for Position
JunHoPark93 May 18, 2019
0177f8f
refactor: Add addAll method
JunHoPark93 May 18, 2019
6ade480
refactor: remove consecutive asserting in ladder test
JunHoPark93 May 18, 2019
c806b7b
refactor: replace generating ladder part to Ladder class
JunHoPark93 May 18, 2019
d99ffc1
refactor: Extract winners as an object
JunHoPark93 May 18, 2019
d175361
fix: Remove redundant recursion in output view
JunHoPark93 May 18, 2019
6cf0c43
refactor: Change hash map type to Player, Result object
JunHoPark93 May 18, 2019
65f58aa
refactor: Remove toString method in made ladder vo
JunHoPark93 May 18, 2019
09422ff
refactor: Remove raw integer value, add constant
JunHoPark93 May 18, 2019
13db4cb
refactor: Remove console random service test
JunHoPark93 May 18, 2019
f7b8c9b
refactor: Restore toString method
JunHoPark93 May 18, 2019
4839494
refactor: Change as first class collection
JunHoPark93 May 19, 2019
5898a44
refactor: Move ladder initialization method in Ladder
JunHoPark93 May 19, 2019
f8c4cb3
refactor: Change as first class collection winners class
JunHoPark93 May 19, 2019
c91913e
refactor: Remove redundant test case
JunHoPark93 May 19, 2019
71115f1
refactor: Extract raw value as constant
JunHoPark93 May 19, 2019
d8481fa
refactor: Polish lines
JunHoPark93 May 19, 2019
ef612e6
refactor: change to private in initLadder method
JunHoPark93 May 19, 2019
6bbb996
refactor: Check null logic
JunHoPark93 May 20, 2019
74440a8
refactor: Extract get line method in Ladder
JunHoPark93 May 20, 2019
631d9d4
refactor: Refactor GameService class
JunHoPark93 May 20, 2019
6dcdb00
style: Polish lines
JunHoPark93 May 20, 2019
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
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,48 @@
사다리타기 미션 저장소

## 우아한테크코스 코드리뷰
* [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md)
* [온라인 코드 리뷰 과정](https://github.com/woowacourse/woowacourse-docs/blob/master/maincourse/README.md)

## TODO
### ladder
* equals 좀 정확히 이해해보자
* vo로 입력을 받는다
* 입력
* 사다리 게임에 참여하는 사람에 이름을 최대5글자까지 부여할 수 있다.
* 이름은 대,소문자를 구분한다
* 허용하지 않음
* null
* 공백
* 앞,뒤 공백
* 중간 공백
* 영어가 아닌 문자
* 5글자 초과

* 사다리를 출력할 때 사람 이름도 같이 출력한다.
* 사람 이름은 쉼표(,)를 기준으로 구분한다.
* all 은 사람 이름이 될 수 없다.
* 사다리
* 사람 이름을 5자 기준으로 출력하기 때문에 사다리 폭도 넓어져야 한다.
* 사다리 타기가 정상적으로 동작하려면 라인이 겹치지 않도록 해야 한다.
* |-----|-----| 모양과 같이 가로 라인이 겹치는 경우 어느 방향으로 이동할지 결정할 수 없다.
* 포지션은 (왼쪽, 오른쪽, 상태없음) 로 정의한다.

## DONE

---

### calculator

* 커스텀 문자열을 쓰지 않는 경우
* ,와 ;로 구분한다.
* 빈 문자열 또는 null 값을 입력할 경우 0을 반환해야 한다.
* 숫자 하나를 문자열로 입력할 경우 해당 숫자를 반환한다.
* 숫자 두개를 컴마(,) 구분자로 입력할 경우 두 숫자의 합을 반환한다.
* 구분자를 컴마(,) 이외에 콜론(:)을 사용할 수 있다.
* “//”와 “\n” 문자 사이에 커스텀 구분자를 지정할 수 있다.
* 음수를 전달할 경우 RuntimeException 예외가 발생해야 한다.

* 커스텀 문자열을 쓰는 경우
* //와 \n 사이의 커스텀 문자로 구분한다.

## DONE
79 changes: 79 additions & 0 deletions src/main/java/com/woowacourse/calculator/domain/Calculator.java
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("음수는 입력할 수 없습니다");

Choose a reason for hiding this comment

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

Calculator 클래스에도 숫자가 있습니다!
의미를 나타낼 수 있는 경우 상수를 활용하면 어떨까요!?

}

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();
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/woowacourse/laddergame/Main.java
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);
}
}
}
61 changes: 61 additions & 0 deletions src/main/java/com/woowacourse/laddergame/domain/Ladder.java
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));
}
}

Choose a reason for hiding this comment

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

lines.get 하는 대신 Line에 메시지를 보내 Bridge를 놓을 수 있는지 판단하는 것은 어떨까요?

get 하는 대신 제이님이 이미 잘 추출해낸 객체에 메시지를 보내면 좋을 것 같아요!
해당 클래스 전체적으로 적용할 수 있는 부분들은 적용해보면 어떨까요?

또한 아래 isContainsLine 메서드 구현의 경우 lines.contains등과 같이 List가 제공하는 메서드를 사용해도 되지 않을까요?

Copy link
Author

Choose a reason for hiding this comment

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

get을 한 이유는 어떤 라인에(get) 어떤 위치에 (put bridge) 다리를 놓을지 결정하기 위해서 사용을 했는데 필드가 List lines 라 한 개의 Line을 꺼내기 위해 get을 사용했습니다!
그래도 수정할 부분을 찾다가 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();
}
}
71 changes: 71 additions & 0 deletions src/main/java/com/woowacourse/laddergame/domain/Line.java
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;
}
}
72 changes: 72 additions & 0 deletions src/main/java/com/woowacourse/laddergame/domain/Player.java
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);
}
}
Loading