-
Notifications
You must be signed in to change notification settings - Fork 55
[LBP] 박세은 사다리 1단계 제출합니다. #36
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
Open
seun0123
wants to merge
6
commits into
next-step:seun0123
Choose a base branch
from
seun0123:seun0123
base: seun0123
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
75e0513
Feat: 1단계 - 사다리 출력
seun0123 ca3bd92
Refactor: 코드 컨벤션 준수
seun0123 083c07e
Refactor: 클래스 구조 변경
seun0123 99959e7
Refactor: RandomUtil, Controller, DTO 추가 및 책임 분배
seun0123 fb27903
Refactor: 객체 무결성 보장
seun0123 8abac53
Feat: 사다리 InputView 추가
seun0123 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 hidden or 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
This file contains hidden or 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,14 @@ | ||
import model.Ladder; | ||
import utils.ExceptionHandler; | ||
import view.LadderView; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
try { | ||
Ladder ladder = Ladder.of(4, 4); | ||
LadderView.printLadder(ladder); | ||
} catch (Exception e) { | ||
ExceptionHandler.handleException(e); | ||
} | ||
} | ||
} |
This file contains hidden or 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,17 @@ | ||
package model; | ||
|
||
public class Ladder { | ||
private final Lines lines; | ||
|
||
public Ladder(Lines lines) { | ||
this.lines = lines; | ||
} | ||
|
||
public static Ladder of(int width, int height) { | ||
return new Ladder(LadderGenerator.generate(width, height)); | ||
} | ||
|
||
public Lines getLines() { | ||
return lines; | ||
} | ||
} |
This file contains hidden or 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,7 @@ | ||
package model; | ||
|
||
public class LadderGenerator { | ||
public static Lines generate(int width, int height) { | ||
return LineGenerator.generate(width, height); | ||
} | ||
} |
This file contains hidden or 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,31 @@ | ||
package model; | ||
|
||
import java.util.*; | ||
import java.util.stream.*; | ||
|
||
public class LadderValidator { | ||
private static final Random RANDOM = new Random(); | ||
|
||
public static void validate(List<Line> lines, int width) { | ||
IntStream.rangeClosed(0, width) | ||
.forEach(i -> validateColumn(lines, i, width)); | ||
} | ||
seun0123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
private static void validateColumn(List<Line> lines, int col, int width) { | ||
boolean emptyColumn = lines.stream().noneMatch(line -> hasBridgeAt(line, col, width)); | ||
if (emptyColumn) { | ||
connect(lines.get(RANDOM.nextInt(lines.size())), col, width); | ||
} | ||
} | ||
|
||
private static boolean hasBridgeAt(Line line, int col, int width) { | ||
if (col == 0) return line.hasBridgeAt(col); | ||
if (col == width) return line.hasBridgeAt(col - 1); | ||
return line.hasBridgeAt(col - 1) || line.hasBridgeAt(col); | ||
} | ||
|
||
private static void connect(Line line, int col, int width) { | ||
if (col == width) line.setBridgeAt(col - 1); | ||
else line.setBridgeAt(col); | ||
} | ||
} |
This file contains hidden or 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,23 @@ | ||
package model; | ||
|
||
import java.util.*; | ||
|
||
public class Line { | ||
private final List<Boolean> points; | ||
|
||
public Line(List<Boolean> points) { | ||
this.points = points; | ||
} | ||
|
||
public List<Boolean> getPoints() { | ||
return points; | ||
} | ||
seun0123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
public boolean hasBridgeAt(int index) { | ||
return points.get(index); | ||
} | ||
|
||
public void setBridgeAt(int index) { | ||
points.set(index, true); | ||
} | ||
seun0123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} |
This file contains hidden or 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,26 @@ | ||
package model; | ||
|
||
import java.util.*; | ||
|
||
public class LineGenerator { | ||
public static Lines generate(int width, int height) { | ||
List<Set<Integer>> reserved = ReservedPositionGenerator.generate(width - 1, height); | ||
List<Line> lines = new ArrayList<>(); | ||
|
||
for (int row = 0; row < height; row++) { | ||
Line previous = getPreviousLine(lines, row); | ||
Line line = SingleLineGenerator.generate(width - 1, reserved.get(row), previous); | ||
lines.add(line); | ||
} | ||
|
||
LadderValidator.validate(lines, width - 1); | ||
return new Lines(lines); | ||
seun0123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
private static Line getPreviousLine(List<Line> lines, int row) { | ||
if (row == 0) { | ||
return null; | ||
} | ||
return lines.get(row - 1); | ||
} | ||
} |
This file contains hidden or 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,15 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
|
||
public class Lines { | ||
private final List<Line> lines; | ||
|
||
public Lines(List<Line> lines) { | ||
this.lines = lines; | ||
} | ||
|
||
public List<Line> getLines() { | ||
return lines; | ||
} | ||
} | ||
seun0123 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,19 @@ | ||
package model; | ||
|
||
import java.util.*; | ||
import java.util.stream.*; | ||
|
||
public class ReservedPositionGenerator { | ||
private static final Random RANDOM = new Random(); | ||
|
||
public static List<Set<Integer>> generate(int width, int height) { | ||
List<Set<Integer>> reserved = IntStream.range(0, height) | ||
.mapToObj(i -> new HashSet<Integer>()) | ||
.collect(Collectors.toList()); | ||
|
||
IntStream.range(0, width) | ||
.forEach(i -> reserved.get(RANDOM.nextInt(height)).add(i)); | ||
|
||
return reserved; | ||
} | ||
} |
This file contains hidden or 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,47 @@ | ||
package model; | ||
|
||
import java.util.*; | ||
import java.util.stream.*; | ||
seun0123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
public class SingleLineGenerator { | ||
private static final Random RANDOM = new Random(); | ||
|
||
public static Line generate(int width, Set<Integer> reserved, Line prev) { | ||
seun0123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
List<Boolean> points = new ArrayList<>(Collections.nCopies(width, false)); | ||
|
||
reserved.forEach(i -> { | ||
if (isNotOverlap(points, prev, i)) { | ||
points.set(i, true); | ||
} | ||
}); | ||
|
||
IntStream.range(0, width).forEach(i -> { | ||
if (!points.get(i) && isNotOverlap(points, prev, i)) { | ||
points.set(i, RANDOM.nextBoolean()); | ||
} | ||
}); | ||
|
||
ensureOneBridge(points, prev); | ||
return new Line(points); | ||
} | ||
seun0123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
private static boolean isNotOverlap(List<Boolean> points, Line prev, int i) { | ||
if (prev != null && prev.hasBridgeAt(i)) return false; | ||
if (i > 0 && points.get(i - 1)) return false; | ||
return true; | ||
} | ||
|
||
private static void ensureOneBridge(List<Boolean> points, Line prev) { | ||
if (points.contains(true)) return; | ||
|
||
IntStream.range(0, points.size()) | ||
.filter(i -> canSetBridge(points, prev, i)) | ||
.findFirst() | ||
.ifPresent(i -> points.set(i, true)); | ||
} | ||
|
||
private static boolean canSetBridge(List<Boolean> points, Line prev, int index) { | ||
return (prev == null || !prev.hasBridgeAt(index)) | ||
&& (index == 0 || !points.get(index - 1)); | ||
} | ||
} |
This file contains hidden or 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,12 @@ | ||
package utils; | ||
|
||
import org.slf4j.*; | ||
|
||
public class ExceptionHandler { | ||
private static final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class); | ||
|
||
public static void handleException(Exception e) { | ||
System.err.println("시스템 오류 : " + e.getMessage()); | ||
logger.error("시스템 오류 : ", e); | ||
} | ||
} |
This file contains hidden or 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,31 @@ | ||
package view; | ||
|
||
import model.*; | ||
|
||
public class LadderView { | ||
private static final String BRIDGE = "-----|"; | ||
private static final String SPACE = " |"; | ||
private static final String BAR = "|"; | ||
|
||
public static void printLadder(Ladder ladder) { | ||
printLines(ladder.getLines()); | ||
} | ||
|
||
private static void printLines(Lines lines) { | ||
lines.getLines().forEach(LadderView::printLine); | ||
} | ||
|
||
private static void printLine(Line line) { | ||
System.out.print(BAR); | ||
line.getPoints().forEach(LadderView::printPoint); | ||
System.out.println(); | ||
} | ||
|
||
private static void printPoint(Boolean point) { | ||
if (point) { | ||
System.out.print(BRIDGE); | ||
return; | ||
} | ||
System.out.print(SPACE); | ||
} | ||
seun0123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.