-
Notifications
You must be signed in to change notification settings - Fork 709
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
[Step2] ๐ 2๋จ๊ณ - ์ฌ๋ค๋ฆฌ(์์ฑ) #1776
base: simjung
Are you sure you want to change the base?
Changes from all commits
4919b56
7060fe1
c7a9148
c9f1605
51532c0
79be2ed
67146b7
9e2df9e
793a092
ef3f71c
d1728d4
5170347
226645d
20e0660
3bd6f44
59d1c9b
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,24 @@ | ||
package ladder; | ||
|
||
import ladder.domain.Ladder; | ||
import ladder.domain.LadderGenerator; | ||
import ladder.domain.player.Players; | ||
import ladder.domain.player.PlayersGenerator; | ||
import ladder.view.InputView; | ||
import ladder.view.ResultView; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
Players players = PlayersGenerator.create(InputView.getPlayerNames()); | ||
int width = players.getPlayerNumber(); | ||
System.out.println(); | ||
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. ์ค๋ฐ๊ฟ์ ๊ผญ ์ฌ๊ธฐ์ ํด ์ค์ผ ํ ๊น์ ? |
||
|
||
int height = InputView.getLadderHeight(); | ||
System.out.println(); | ||
|
||
Ladder ladder = new LadderGenerator().generate(height, width); | ||
ResultView.showResultMessage(); | ||
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. ์ด ์ธ ๊ฐ๋ฅผ ๋ฌถ๋ ํจ์๋ฅผ ๋ฐ๋ก ์ ๊ณตํด์ฃผ๋ ๊ฑด ์ด๋จ๊น์ ? |
||
ResultView.showPlayers(players); | ||
ResultView.showLadder(ladder); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# ๐2๋จ๊ณ - ์ฌ๋ค๋ฆฌ(์์ฑ) | ||
|
||
## ๊ธฐ๋ฅ ์๊ตฌ์ฌํญ | ||
|
||
- ์ฌ๋ค๋ฆฌ ๊ฒ์์ ์ฐธ์ฌํ๋ ์ฌ๋์ ์ด๋ฆ์ ์ต๋5๊ธ์๊น์ง ๋ถ์ฌํ ์ ์๋ค. ์ฌ๋ค๋ฆฌ๋ฅผ ์ถ๋ ฅํ ๋ ์ฌ๋ ์ด๋ฆ๋ ๊ฐ์ด ์ถ๋ ฅํ๋ค. | ||
- ์ฌ๋ ์ด๋ฆ์ ์ผํ(,)๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถํ๋ค. | ||
- ์ฌ๋ ์ด๋ฆ์ 5์ ๊ธฐ์ค์ผ๋ก ์ถ๋ ฅํ๊ธฐ ๋๋ฌธ์ ์ฌ๋ค๋ฆฌ ํญ๋ ๋์ด์ ธ์ผ ํ๋ค. | ||
- ์ฌ๋ค๋ฆฌ ํ๊ธฐ๊ฐ ์ ์์ ์ผ๋ก ๋์ํ๋ ค๋ฉด ๋ผ์ธ์ด ๊ฒน์น์ง ์๋๋ก ํด์ผ ํ๋ค. | ||
- |-----|-----| ๋ชจ์๊ณผ ๊ฐ์ด ๊ฐ๋ก ๋ผ์ธ์ด ๊ฒน์น๋ ๊ฒฝ์ฐ ์ด๋ ๋ฐฉํฅ์ผ๋ก ์ด๋ํ ์ง ๊ฒฐ์ ํ ์ ์๋ค. | ||
|
||
## ํ๋ก๊ทธ๋๋ฐ ์๊ตฌ์ฌํญ | ||
|
||
- ์๋ฐ 8์ ์คํธ๋ฆผ๊ณผ ๋๋ค๋ฅผ ์ ์ฉํด ํ๋ก๊ทธ๋๋ฐํ๋ค. | ||
- ๊ท์น 6: ๋ชจ๋ ์ํฐํฐ๋ฅผ ์๊ฒ ์ ์งํ๋ค. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ladder.domain; | ||
|
||
import ladder.exception.IllegalAdjacentVerticalLinesException; | ||
|
||
public class AdjacentVerticalLines { | ||
private final VerticalLine leftVerticalLine; | ||
private final VerticalLine rightVerticalLine; | ||
|
||
public AdjacentVerticalLines(VerticalLine firstVerticalLine, VerticalLine secondVerticalLine) { | ||
checkAdjacentVerticalLines(firstVerticalLine, secondVerticalLine); | ||
|
||
this.leftVerticalLine = min(firstVerticalLine, secondVerticalLine); | ||
this.rightVerticalLine = max(firstVerticalLine, secondVerticalLine); | ||
} | ||
|
||
public VerticalLine getLeftVerticalLine() { | ||
return leftVerticalLine; | ||
} | ||
|
||
public VerticalLine getRightVerticalLine() { | ||
return rightVerticalLine; | ||
} | ||
|
||
private void checkAdjacentVerticalLines(VerticalLine firstVerticalLine, VerticalLine secondVerticalLine) { | ||
if (Math.abs(firstVerticalLine.getIndex() - secondVerticalLine.getIndex()) > 1) { | ||
throw new IllegalAdjacentVerticalLinesException( | ||
String.format("์ ๋ ฅ๋ ์ธ๋ฑ์ค : %d, %d", firstVerticalLine.getIndex(), secondVerticalLine.getIndex()) | ||
); | ||
} | ||
} | ||
|
||
private VerticalLine max(VerticalLine firstVerticalLine, VerticalLine secondVerticalLine) { | ||
return firstVerticalLine.getIndex() > secondVerticalLine.getIndex() | ||
? firstVerticalLine | ||
: secondVerticalLine; | ||
} | ||
|
||
private VerticalLine min(VerticalLine firstVerticalLine, VerticalLine secondVerticalLine) { | ||
return firstVerticalLine.getIndex() > secondVerticalLine.getIndex() | ||
? secondVerticalLine | ||
: firstVerticalLine; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ladder.domain; | ||
|
||
import ladder.exception.IllegalHorizontalLineHeightException; | ||
|
||
public class HorizontalLine { | ||
static final int MINIMUM_HEIGHT = 0; | ||
|
||
private final AdjacentVerticalLines adjacentVerticalLines; | ||
private final int height; | ||
|
||
public HorizontalLine(AdjacentVerticalLines adjacentVerticalLines, int height) { | ||
checkHeight(height); | ||
|
||
this.adjacentVerticalLines = adjacentVerticalLines; | ||
this.height = height; | ||
} | ||
|
||
private void checkHeight(int height) { | ||
if (height < MINIMUM_HEIGHT) { | ||
throw new IllegalHorizontalLineHeightException(String.format("์ต์ ๋์ด : %d", MINIMUM_HEIGHT)); | ||
} | ||
} | ||
|
||
public AdjacentVerticalLines getAdjacentVerticalLines() { | ||
return adjacentVerticalLines; | ||
} | ||
|
||
public int getHeight() { | ||
return height; | ||
} | ||
|
||
public VerticalLine getLeftVerticalLine() { | ||
return adjacentVerticalLines.getLeftVerticalLine(); | ||
} | ||
|
||
public VerticalLine getRightVerticalLine() { | ||
return adjacentVerticalLines.getRightVerticalLine(); | ||
} | ||
|
||
public boolean isConnected(VerticalLine verticalLine) { | ||
return verticalLine == adjacentVerticalLines.getLeftVerticalLine() | ||
|| verticalLine == adjacentVerticalLines.getRightVerticalLine(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ladder.domain; | ||
|
||
import ladder.exception.IllegalHorizontalLineHeightException; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class HorizontalLines { | ||
private final Set<HorizontalLine> horizontalLineSet; | ||
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์ผ๋ก ์ ํ์ ์ด์ ๊ฐ ์์๊น์ ? |
||
private final int maxHeight; | ||
|
||
public HorizontalLines(Set<HorizontalLine> horizontalLineSet, int maxHeight) { | ||
checkValidHorizontalLineHeight(horizontalLineSet, maxHeight); | ||
|
||
this.horizontalLineSet = horizontalLineSet; | ||
this.maxHeight = maxHeight; | ||
} | ||
|
||
private void checkValidHorizontalLineHeight(Set<HorizontalLine> horizontalLineSet, int maxHeight) { | ||
if (horizontalLineSet.stream() | ||
.anyMatch(it -> it.getHeight() >= maxHeight) | ||
) { | ||
throw new IllegalHorizontalLineHeightException(String.format("์ต๋ ๋์ด : %d", maxHeight)); | ||
} | ||
} | ||
|
||
public Set<HorizontalLine> getHorizontalLineSet() { | ||
return Set.copyOf(horizontalLineSet); | ||
} | ||
|
||
public Set<HorizontalLine> getHorizontalLineSetByHeight(int height) { | ||
return horizontalLineSet.stream() | ||
.filter(it -> it.getHeight() == height) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
public int getMaxHeight() { | ||
return maxHeight; | ||
} | ||
|
||
public int getSize() { | ||
return horizontalLineSet.size(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package ladder.domain; | ||
|
||
import ladder.exception.ContinuousHorizontalLineException; | ||
import ladder.exception.NotEnoughVerticalLinesException; | ||
|
||
import java.util.Set; | ||
|
||
public class 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. ์ฌ๋ค๋ฆฌ๋ฅผ ๊ฐ๋ก์ / ์ธ๋ก์ ์ ๊ฐ๋
์ผ๋ก ์ ๊ทผํ๋ฉด์ ๋ก์ง์ด ๋ง์ด ๋ณต์กํด ์ง ๊ฒ ๊ฐ์์! |
||
static final int MINIMUM_VERTICAL_LINES_QUANTITY = 2; | ||
|
||
private final VerticalLines verticalLines; | ||
private final HorizontalLines horizontalLines; | ||
|
||
public Ladder(VerticalLines verticalLines, HorizontalLines horizontalLines) { | ||
checkVerticalLines(verticalLines); | ||
checkValidLines(verticalLines, horizontalLines); | ||
|
||
this.verticalLines = verticalLines; | ||
this.horizontalLines = horizontalLines; | ||
} | ||
|
||
private void checkVerticalLines(VerticalLines verticalLines) { | ||
if (verticalLines.getSize() < MINIMUM_VERTICAL_LINES_QUANTITY) { | ||
throw new NotEnoughVerticalLinesException(String.format("์ธ๋ก์ ๊ฐฏ์ : %d", verticalLines.getSize())); | ||
} | ||
} | ||
|
||
private void checkValidLines(VerticalLines verticalLines, HorizontalLines horizontalLines) { | ||
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. ์ ์ ํจ์ฑ๊ฒ์ฌ ํน์ ๊ผญ ํ์ ํ ๊น์ ? ์์ VerticalLines์ HorizontalLines๋ฅผ ๋ง๋ค ๋ ์ด๋ฏธ ๊ฒ์ฆ ๋๊ณ ์๋ ๊ฒ ๊ฐ์์์! |
||
for (int i = 0; i < horizontalLines.getMaxHeight(); i++) { | ||
Set<HorizontalLine> horizontalLineSet = horizontalLines.getHorizontalLineSetByHeight(i); | ||
checkExistContinuousHorizontalLinesOnSameHeight(verticalLines, horizontalLineSet); | ||
} | ||
} | ||
|
||
private void checkExistContinuousHorizontalLinesOnSameHeight(VerticalLines verticalLines, Set<HorizontalLine> horizontalLineSet) { | ||
verticalLines.getVerticalLineSet() | ||
.forEach(verticalLine -> | ||
checkConnectingWithManyHorizontalLines(verticalLine, horizontalLineSet) | ||
); | ||
} | ||
|
||
private void checkConnectingWithManyHorizontalLines(VerticalLine verticalLine, Set<HorizontalLine> horizontalLineSet) { | ||
if (horizontalLineSet.stream() | ||
.filter(it -> it.isConnected(verticalLine)) | ||
.count() > 1 | ||
) { | ||
throw new ContinuousHorizontalLineException(String.format("์ค๋ณต๋๋ ์ธ๋ก์ ์์น : %d", verticalLine.getIndex())); | ||
} | ||
} | ||
|
||
public int getHeight() { | ||
return horizontalLines.getMaxHeight(); | ||
} | ||
|
||
public int getWidth() { | ||
return verticalLines.getMaxWidth(); | ||
} | ||
|
||
public VerticalLines getVerticalLines() { | ||
return verticalLines; | ||
} | ||
|
||
public HorizontalLines getHorizontalLines() { | ||
return horizontalLines; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ladder.domain; | ||
|
||
public interface LadderGenerateStrategy { | ||
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. ์ธํฐํ์ด์ค ํ์ฉ ์ข๋ค์! ํน์ domain ํจํค์ง ๋ด๋ถ์์๋ ๊ด๊ณ์๋ ํด๋์ค๋ผ๋ฆฌ ๋ฐ๋ก ํจํค์ง ํ ์๋ ์์๊น์ ? |
||
boolean canGenerate(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package ladder.domain; | ||
|
||
import ladder.exception.IllegalLadderParameterException; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class LadderGenerator { | ||
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. ์ด ํด๋์ค๋ ์ ํธ ํด๋์ค๋ก ์ ์ธ ํด ๋ณด๋ ๊ฑด ์ด๋จ๊น์ ? Strategy๋ฅผ generate์์ ๋ฐ์๋ ๋ ๊ฒ ๊ฐ์์์! |
||
private static final double DEFAULT_CHANCE = 0.5; | ||
|
||
private final LadderGenerateStrategy ladderGenerateStrategy; | ||
|
||
public LadderGenerator() { | ||
this.ladderGenerateStrategy = () -> Math.random() < DEFAULT_CHANCE; | ||
} | ||
|
||
public LadderGenerator(LadderGenerateStrategy ladderGenerateStrategy) { | ||
this.ladderGenerateStrategy = ladderGenerateStrategy; | ||
} | ||
|
||
public Ladder generate(int height, int width) { | ||
checkHeightAndWidth(height, width); | ||
|
||
VerticalLines verticalLines = VerticalLines.create(width); | ||
HorizontalLines horizontalLines = createHorizontalLines(verticalLines, height); | ||
|
||
return new Ladder(verticalLines, horizontalLines); | ||
} | ||
|
||
private HorizontalLines createHorizontalLines(VerticalLines verticalLines, int height) { | ||
HashSet<HorizontalLine> horizontalLineHashSet = new HashSet<>(); | ||
|
||
for (int i = 0; i < height; i++) { | ||
Set<HorizontalLine> sameHeightHorizontalLineSet = createSameHeightHorizontalLineSet(verticalLines, i); | ||
horizontalLineHashSet.addAll(sameHeightHorizontalLineSet); | ||
} | ||
|
||
return new HorizontalLines(horizontalLineHashSet, height); | ||
} | ||
|
||
private Set<HorizontalLine> createSameHeightHorizontalLineSet(VerticalLines verticalLines, int height) { | ||
HashSet<HorizontalLine> sameHeightHorizontalLineHashSet = new HashSet<>(); | ||
|
||
for (int i = 0; i < verticalLines.getMaxWidth() - 1; i++) { | ||
VerticalLine nowVerticalLine = verticalLines.getVerticalLineByIndex(i); | ||
VerticalLine nextVerticalLine = verticalLines.getVerticalLineByIndex(i + 1); | ||
|
||
boolean notExistPreviousHorizontalLine = sameHeightHorizontalLineHashSet.stream() | ||
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. ์ด๋ ๊ฒ ๋ค ๋น๊ตํด์ผ ํ ๊น์ ? ์ด์ฐจํผ ์์ฐจ์ฒ๋ฆฌ๋ฅผ ํ๋๊ฑฐ๋ผ๋ฉด ์ด์ ์นธ์์ ์์ฑ์ ํ๋์ง๋ง ํ์ธํ๋ฉด ๋ ๊ฒ ๊ฐ์์์! |
||
.noneMatch(horizontalLine -> | ||
horizontalLine.getRightVerticalLine() == nowVerticalLine | ||
); | ||
|
||
if (notExistPreviousHorizontalLine && ladderGenerateStrategy.canGenerate()) { | ||
sameHeightHorizontalLineHashSet.add( | ||
new HorizontalLine( | ||
new AdjacentVerticalLines(nowVerticalLine, nextVerticalLine), | ||
height | ||
) | ||
); | ||
} | ||
} | ||
return sameHeightHorizontalLineHashSet; | ||
} | ||
|
||
private void checkHeightAndWidth(int height, int width) { | ||
if (height < HorizontalLine.MINIMUM_HEIGHT | ||
|| width < Ladder.MINIMUM_VERTICAL_LINES_QUANTITY) { | ||
throw new IllegalLadderParameterException(String.format("์ ๋ ฅ๋ ๋์ด : %d, ์ ๋ ฅ๋ ๋๋น : %d", height, width)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ladder.domain; | ||
|
||
public class VerticalLine { | ||
private final int index; | ||
|
||
public VerticalLine(int index) { | ||
this.index = index; | ||
} | ||
|
||
public int getIndex() { | ||
return index; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package ladder.domain; | ||
|
||
import ladder.exception.IllegalVerticalLineWidthException; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class VerticalLines { | ||
private final Set<VerticalLine> verticalLineSet; | ||
private final int maxWidth; | ||
|
||
public VerticalLines(Set<VerticalLine> verticalLineSet, int maxWidth) { | ||
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. ํน์ maxWidth์ verticalLineSet์ ํฌ๊ธฐ๊ฐ ๋ค๋ฅธ ๊ฒฝ์ฐ๊ฐ ์๋๊ฑธ๊น์? ํน์ ๋ฌ๋ผ๋ ๋๋๊ฑด๊ฐ์ ? |
||
checkValidVerticalLineWidth(verticalLineSet, maxWidth); | ||
|
||
this.verticalLineSet = verticalLineSet; | ||
this.maxWidth = maxWidth; | ||
} | ||
|
||
private void checkValidVerticalLineWidth(Set<VerticalLine> verticalLineSet, int maxWidth) { | ||
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. ์ ์ ํจ์ฑ๊ฒ์ฌ๊ฐ ์ ๋ง ํ์ํ๊ฑธ๊น์ ? |
||
if (verticalLineSet.stream() | ||
.anyMatch(it -> it.getIndex() >= maxWidth) | ||
) { | ||
throw new IllegalVerticalLineWidthException(String.format("์ต๋ ๋๋น : %d", maxWidth)); | ||
} | ||
} | ||
|
||
public Set<VerticalLine> getVerticalLineSet() { | ||
return Set.copyOf(verticalLineSet); | ||
} | ||
|
||
public int getMaxWidth() { | ||
return maxWidth; | ||
} | ||
|
||
public VerticalLine getVerticalLineByIndex(int index) { | ||
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. ์ด๋ฐ ๋ก์ง์ด ํ์ํ๋ค๋ฉด Map์ ์ฐ๋๊ฒ ๋ซ์ง ์์๊น์ ? |
||
return verticalLineSet.stream() | ||
.filter(it -> it.getIndex() == index) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
public int getSize() { | ||
return verticalLineSet.size(); | ||
} | ||
|
||
public static VerticalLines create(int count) { | ||
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์ผ๋ก ๋ง๋ค์ด์ฃผ๋ ๋ถ๋ถ๋ง ๋ฐ๋ก ๋นผ๊ณ ๊ทธ๋ฅ ์์ฑ์์์ count๋งํผ์ set์ ๋ง๋ค์ด์ฃผ๋ ๊ฑด ์ด๋จ๊น์ ? |
||
HashSet<VerticalLine> verticalLineHashSet = new HashSet<>(); | ||
for (int i = 0; i < count; i++) { | ||
verticalLineHashSet.add(new VerticalLine(i)); | ||
} | ||
return new VerticalLines(verticalLineHashSet, count); | ||
} | ||
} |
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.
InputView.getPlayerNames()
๋ ๋ณ์๋ก ํ๋ฒ ๋นผ๋ ๊ฑด ์ด๋จ๊น์ ?