-
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
Merged
Merged
Changes from 78 commits
Commits
Show all changes
90 commits
Select commit
Hold shift + click to select a range
8dca839
docs: Update README
JunHoPark93 d778d8c
feat: Define initial Calculator class
JunHoPark93 a8cb338
docs: Update README
JunHoPark93 89849da
feat: Add NaturalNumber class
JunHoPark93 e8b354f
feat: Add Line class
JunHoPark93 310e5b7
refactor: Change NaturalNumber in Line class
JunHoPark93 d8e6397
feat: Add Ladder class
JunHoPark93 f436157
test: Add Natural Number test
JunHoPark93 6c0a75f
test: Add PositionTest
JunHoPark93 e7214de
feat: Add toString method and test
JunHoPark93 ba18316
feat: Add exception handling while trying to putting bridge in same p…
JunHoPark93 05013b9
feat: Add Random Generator
JunHoPark93 eac776d
refactor: Change natural number boundary
JunHoPark93 106f537
test: Add test for comparing lines
JunHoPark93 fdf0b27
feat: Add LadderGenerator class
JunHoPark93 6fa401e
feat: Add toString method to ladder
JunHoPark93 2946d8a
docs: Update README
JunHoPark93 4d79c3f
feat: Add LadderVO
JunHoPark93 624ed90
refactor: Change generateLadder to static
JunHoPark93 bff7f44
feat: Add Player, Players class
JunHoPark93 1457271
feat: Add console view
JunHoPark93 545049f
feat: Add LadderStatusVO
JunHoPark93 66f7a7d
feat: Add LadderGameService
JunHoPark93 13d055c
feat: Add MadeLadder
JunHoPark93 c3c787d
feat: Add initial Main class
JunHoPark93 c6d88f7
style: Polish line
JunHoPark93 ca47a49
feat: Add input view for game result
JunHoPark93 43c893e
feat: Add ladder result input method
JunHoPark93 0ec4962
feat: Add Result class
JunHoPark93 72f936b
refactor: Change method name in LadderVO
JunHoPark93 3b24e3f
feat: Add result input method in console
JunHoPark93 8249df4
test: Update ladder integration test with user game result input
JunHoPark93 a3f402e
test: Add taking ladder test
JunHoPark93 0e24b7d
docs: Update README
JunHoPark93 c724e8f
feat: Add ResultNameVO class
JunHoPark93 423c981
feat: Add getting player index number function in Players
JunHoPark93 f41eabc
feat: Add getting result string method in Result class
JunHoPark93 49f214b
refactor: Add equals and hashcode in Player
JunHoPark93 b9bed67
feat: Add LadderGameResultVO class
JunHoPark93 5798216
feat: Add taking ladder method
JunHoPark93 d2e4718
feat: Add output view message
JunHoPark93 df8e6d0
feat: Add checking duplicate player names
JunHoPark93 7114bd1
refactor: Change class name to Dto
JunHoPark93 d409f85
feat: Add ladder, ladder result dto
JunHoPark93 b073ff4
feat: Add made ladder, winner value object
JunHoPark93 65fedb9
feat: Add duplicate name check condition in Player
JunHoPark93 4581449
feat: Add taking ladder function which will return result dto
JunHoPark93 52d5ec5
refactor: refactor dto test name
JunHoPark93 b1e402f
feat: Update input output view
JunHoPark93 a6ad587
Merge branch 'calculator' into JunHoPark93
JunHoPark93 b978641
test: Fix minor info message
JunHoPark93 cdd46ee
refactor: Separate methods in LadderDto
JunHoPark93 dda6aa4
refactor: Delete LadderGameResultDto
JunHoPark93 7f305d3
refactor: Add reserved word in LadderResultDto
JunHoPark93 9fff3ce
style: Polish lines
JunHoPark93 4b00d0b
style: Polish lines
JunHoPark93 28a6f6f
style: Polish lines
JunHoPark93 29546e5
refactor: Separate Player constructor condition checking
JunHoPark93 db227fa
refactor: Separate ladder game service method structure
JunHoPark93 c3a67e4
style: Polish lines and methods
JunHoPark93 4c0d854
refactor: Polish test method, naming, reduce repeated code
JunHoPark93 d11c006
fix: Fix input invalid name in getting ladder result
JunHoPark93 a775004
refactor: Move generator class in util package
JunHoPark93 9427ef1
refactor: Change todo comment
JunHoPark93 3be7c0a
refactor: Separate calculator constructor
JunHoPark93 aa7c554
refactor: Separate generating ladder method
JunHoPark93 d0f459d
style: Polish lines
JunHoPark93 68e8fe2
refactor: Remove console test
JunHoPark93 963e375
refactor: Add test case for Position
JunHoPark93 0177f8f
refactor: Add addAll method
JunHoPark93 6ade480
refactor: remove consecutive asserting in ladder test
JunHoPark93 c806b7b
refactor: replace generating ladder part to Ladder class
JunHoPark93 d99ffc1
refactor: Extract winners as an object
JunHoPark93 d175361
fix: Remove redundant recursion in output view
JunHoPark93 6cf0c43
refactor: Change hash map type to Player, Result object
JunHoPark93 65f58aa
refactor: Remove toString method in made ladder vo
JunHoPark93 09422ff
refactor: Remove raw integer value, add constant
JunHoPark93 13db4cb
refactor: Remove console random service test
JunHoPark93 f7b8c9b
refactor: Restore toString method
JunHoPark93 4839494
refactor: Change as first class collection
JunHoPark93 5898a44
refactor: Move ladder initialization method in Ladder
JunHoPark93 f8c4cb3
refactor: Change as first class collection winners class
JunHoPark93 c91913e
refactor: Remove redundant test case
JunHoPark93 71115f1
refactor: Extract raw value as constant
JunHoPark93 d8481fa
refactor: Polish lines
JunHoPark93 ef612e6
refactor: change to private in initLadder method
JunHoPark93 6bbb996
refactor: Check null logic
JunHoPark93 74440a8
refactor: Extract get line method in Ladder
JunHoPark93 631d9d4
refactor: Refactor GameService class
JunHoPark93 6dcdb00
style: Polish lines
JunHoPark93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/woowacourse/calculator/domain/Calculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/com/woowacourse/laddergame/domain/Ladder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.woowacourse.laddergame.domain; | ||
|
||
import com.woowacourse.laddergame.util.BooleanGenerator; | ||
import com.woowacourse.laddergame.util.NaturalNumber; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Ladder { | ||
private List<Line> lines; | ||
private NaturalNumber height; | ||
private NaturalNumber countOfPerson; | ||
|
||
public Ladder(NaturalNumber height, NaturalNumber countOfPerson) { | ||
lines = new ArrayList<>(); | ||
for (int h = 0; h < height.getNumber(); h++) { | ||
lines.add(new Line(countOfPerson)); | ||
} | ||
this.height = height; | ||
this.countOfPerson = countOfPerson; | ||
} | ||
|
||
public void initLadder(BooleanGenerator booleanGenerator) { | ||
for (int h = 1; h <= height.getNumber(); h++) { | ||
loopInPerson(booleanGenerator, h); | ||
} | ||
} | ||
|
||
private void loopInPerson(BooleanGenerator booleanGenerator, int h) { | ||
for (int i = 1; i < countOfPerson.getNumber(); i++) { | ||
if (booleanGenerator.generate()) { | ||
putBridge(new NaturalNumber(h), new NaturalNumber(i)); | ||
i++; | ||
} | ||
} | ||
} | ||
|
||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), positions.size()); | ||
} | ||
|
||
@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
72
src/main/java/com/woowacourse/laddergame/domain/Player.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 클래스에도 숫자가 있습니다!
의미를 나타낼 수 있는 경우 상수를 활용하면 어떨까요!?