-
Notifications
You must be signed in to change notification settings - Fork 26
2주차 미션 / 서버 1조 강희진 #32
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
hd0rable
wants to merge
8
commits into
Konkuk-KUIT:1week-completed
Choose a base branch
from
hd0rable:1week-completed
base: 1week-completed
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b9162ed
feat : 기능 요구사항 2 사다리 출력 기능 추가
hd0rable 05ae984
refactor : LadderDraw 분리
hd0rable b02d770
refactor : LadderDraw 분리에 따른 코드 리펙토링
hd0rable 7e8b253
test : LadderDraw 분리에 따른 테스트코드 리펙토링
hd0rable 0663498
test : LadderDraw 분리에 따른 테스트코드 작성
hd0rable f5e9164
refactor : position 클래스 리펙토링
hd0rable 0e405b0
refactor : 전체 리펙토링
hd0rable af9361d
refactor : 전체 리펙토링
hd0rable 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 ladder; | ||
|
|
||
| import ladder.creator.LadderAutoCreator; | ||
| import ladder.creator.LadderInputCreator; | ||
| import ladder.info.LadderSize; | ||
|
|
||
| public class LadderGameFactory { | ||
|
|
||
| public static LadderGame createRandomLadderGame(LadderSize ladderSize) { | ||
| LadderAutoCreator ladderAutoCreator = LadderAutoCreator.of(ladderSize); | ||
| return LadderGame.of(ladderAutoCreator); | ||
| } | ||
|
|
||
| public static LadderGame createInputLadderGame(LadderSize ladderSize) { | ||
| LadderInputCreator ladderInputCreator = LadderInputCreator.of(ladderSize); | ||
| return LadderGame.of(ladderInputCreator); | ||
| } | ||
|
|
||
| } |
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 |
|---|---|---|
| @@ -1,17 +1,34 @@ | ||
| package ladder; | ||
|
|
||
| import ladder.info.Ladder; | ||
| import ladder.print.LadderPrint; | ||
|
|
||
| import static ladder.print.PrintState.AFTER; | ||
| import static ladder.print.PrintState.BEFORE; | ||
|
|
||
| public class LadderRunner { | ||
|
|
||
| private final Row[] rows; | ||
| private final Ladder ladder; | ||
| private final LadderPrint ladderPrint ; | ||
|
|
||
| private LadderRunner(Ladder ladder) { | ||
| this.ladder = ladder; | ||
| this.ladderPrint = LadderPrint.of(ladder); | ||
| } | ||
|
|
||
| public LadderRunner(Row[] rows) { | ||
| this.rows = rows; | ||
| public static LadderRunner of(Ladder ladder){ | ||
| return new LadderRunner(ladder); | ||
| } | ||
|
|
||
| public int run(Position position) { | ||
| for (int i = 0; i < rows.length; i++) { | ||
| rows[i].nextPosition(position); | ||
|
|
||
| for (int i = 0; i < ladder.getLadderSize().getRow(); i++) { | ||
|
|
||
| ladderPrint.printLadderState(position,BEFORE); | ||
| ladder.getRows()[i].nextPosition(position); | ||
hd0rable marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ladderPrint.printLadderState(position,AFTER); | ||
| ladder.getRows()[i].moveRow(position); | ||
| } | ||
| return position.getValue(); | ||
| return position.getCol(); | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -1,46 +1,77 @@ | ||
| package ladder; | ||
|
|
||
| import static ladder.ExceptionMessage.INVALID_LADDER_POSITION; | ||
| import java.util.Objects; | ||
|
|
||
| import static ladder.exception.ExceptionMessage.INVALID_LADDER_POSITION; | ||
|
|
||
| public class Position { | ||
| private int position; | ||
|
|
||
| private Position(int position) { | ||
| this.position = position; | ||
| private int row; | ||
| private int col; | ||
hd0rable marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| private Position(int row,int col) { | ||
| this.row = row; | ||
| this.col = col; | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return position; | ||
| public int getCol() { | ||
| return col; | ||
| } | ||
|
|
||
| public static Position from(int position) { | ||
| validatePosition(position); | ||
| return new Position(position); | ||
| public int getRow() { | ||
| return row; | ||
| } | ||
|
|
||
| public static Position of(int row, int col) { | ||
| validatePosition(row, col); | ||
| return new Position(row,col); | ||
| } | ||
|
|
||
|
|
||
| public void prev() { | ||
| position--; | ||
| col--; | ||
| } | ||
|
|
||
| public void next() { | ||
| position++; | ||
| col++; | ||
| } | ||
|
|
||
| public boolean isSmallerThan(int position) { | ||
| return this.position < position; | ||
| public boolean isSmallerThan(int col) { | ||
| return this.col < col; | ||
| } | ||
|
|
||
| public boolean isBiggerThan(int position) { | ||
| return this.position > position; | ||
| public boolean isBiggerThan(int col) { return this.col > col; } | ||
|
|
||
| public boolean isCurrentPosition(int row, int col) { | ||
| return this.row == row && this.col == col; | ||
| } | ||
|
|
||
| private static void validatePosition(int position) { | ||
| if (!isPosition(position)) { | ||
|
|
||
| private static void validatePosition(int row, int col) { | ||
| if (!isPosition(row, col)) { | ||
| throw new IllegalArgumentException(INVALID_LADDER_POSITION.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private static boolean isPosition(int position) { | ||
| return position >= 0; | ||
| private static boolean isPosition(int row, int col) { | ||
| return row >= 0 && col>=0 ; | ||
| } | ||
|
|
||
| public void UpdateRow() { | ||
| (this.row)++; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) return true; | ||
| if (obj == null || getClass() != obj.getClass()) return false; | ||
| Position position = (Position) obj; | ||
| return row == position.row && col == position.col; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(row, 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,84 @@ | ||
| package ladder.creator; | ||
|
|
||
| import ladder.exception.ExceptionMessage; | ||
| import ladder.info.Ladder; | ||
| import ladder.info.LadderSize; | ||
| import ladder.Position; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Random; | ||
|
|
||
| public class LadderAutoCreator implements LadderCreator { | ||
|
|
||
| private final Ladder ladder; | ||
|
|
||
| private LadderAutoCreator(LadderSize ladderSize) { | ||
| this.ladder = Ladder.create(ladderSize); | ||
| } | ||
|
|
||
| @Override | ||
| public void drawLine(Position position) { | ||
| throw new IllegalArgumentException(ExceptionMessage.INVALID_LADDER_DRAW_AUTO.getMessage()); | ||
| } | ||
|
|
||
| public void drawLine(LadderSize ladderSize) { | ||
|
|
||
| //자동생성 되는 사다리 좌표 값 수 | ||
| //실제로 라인은 하나가 생성되지만 이 클래스에서는 사다리가 라인을 가지고 있는걸 1,-1 값으로 표현하기 때문에 | ||
| int lineNum = getLineNum(ladderSize); | ||
|
|
||
| //중복 검증할 포지션 집합 | ||
| HashSet<Position> set = new HashSet<>(); | ||
|
|
||
| while(set.size()< lineNum){ | ||
|
|
||
| Position[] startPosition = getRandomPosition(ladderSize); | ||
|
|
||
| //포함되어있지않다면 == 라인이 생성되어있지않다면(오른쪽 생성했을때) | ||
| //오른쪽에도 라인이 생성되어있다면 그릴수 없기때문에 오른쪽 위치도 같이 확인 | ||
hd0rable marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if(!isDrawLine(startPosition,set)){ | ||
|
|
||
| ladder.getRows()[startPosition[0].getRow()].drawLine(startPosition[0]); | ||
hd0rable marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| set.add(Position.of(startPosition[0].getRow(),startPosition[0].getCol()-1)); //포지션이 객체라 col값이 올라가서 그전값으로 확인 | ||
| set.add(startPosition[1]); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| private boolean isDrawLine(Position[] startPosition,HashSet<Position> set){ | ||
| return (set.contains(startPosition[0]) || set.contains(startPosition[1])); | ||
hd0rable marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private Position[] getRandomPosition(LadderSize ladderSize) { | ||
|
|
||
| Random random = new Random(); | ||
|
|
||
| //사다리 범위내에서 랜덤생성 | ||
| int randomRow = random.nextInt(ladderSize.getRow()); | ||
| //사다리 생성로직이 오른쪽으로 선을 긋기때문에 맨 마지막 열에서는 사다리 라인생성 불가 | ||
| int randomCol = random.nextInt(ladderSize.getCol()-1); | ||
|
|
||
| //사다리 자동생성 시작값 | ||
| Position startPosition = Position.of(randomRow,randomCol); | ||
| //사다리가 자동생성되면 라인이 왼쪽에 그려지는 값 | ||
| Position startPositionplus = Position.of(randomRow,randomCol+1); | ||
|
|
||
| return new Position[]{startPosition, startPositionplus}; | ||
|
|
||
| } | ||
|
|
||
| private int getLineNum(LadderSize ladderSize) { | ||
| int lineNum = (int) Math.floor(ladderSize.getRow() * ladderSize.getCol() * 0.3) * 2 ; | ||
| return lineNum; | ||
| } | ||
|
|
||
| public static LadderAutoCreator of (LadderSize ladderSize) { | ||
| return new LadderAutoCreator(ladderSize); | ||
| } | ||
|
|
||
| @Override | ||
| public Ladder getLadder() { | ||
| return this.ladder; | ||
| } | ||
|
|
||
| } | ||
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 |
|---|---|---|
| @@ -1,25 +1,13 @@ | ||
| package ladder.creator; | ||
|
|
||
| import ladder.GreaterThanOne; | ||
| import ladder.Position; | ||
| import ladder.Row; | ||
| import ladder.*; | ||
| import ladder.info.Ladder; | ||
| import ladder.info.LadderSize; | ||
|
|
||
| public class LadderCreator { | ||
| public interface LadderCreator { | ||
|
|
||
| private final Row[] rows; | ||
| void drawLine(Position position); | ||
| void drawLine(LadderSize ladderSize); | ||
| Ladder getLadder(); | ||
|
|
||
| public LadderCreator(GreaterThanOne numberOfRow, GreaterThanOne numberOfPerson) { | ||
| rows = new Row[numberOfRow.getNumber()]; | ||
| for (int i = 0; i < numberOfRow.getNumber(); i++) { | ||
| rows[i] = new Row(numberOfPerson); | ||
| } | ||
| } | ||
|
|
||
| public void drawLine(Position row, Position col) { | ||
| rows[row.getValue()].drawLine(col); | ||
| } | ||
|
|
||
| public Row[] getRows() { | ||
| return rows; | ||
| } | ||
| } |
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,36 @@ | ||
| package ladder.creator; | ||
|
|
||
| import ladder.exception.ExceptionMessage; | ||
| import ladder.info.Ladder; | ||
| import ladder.info.LadderSize; | ||
| import ladder.Position; | ||
|
|
||
| public class LadderInputCreator implements LadderCreator { | ||
|
|
||
| private final Ladder ladder; | ||
|
|
||
| private LadderInputCreator(LadderSize ladderSize) { | ||
| this.ladder = Ladder.create(ladderSize); | ||
| } | ||
|
|
||
| @Override | ||
| public void drawLine(Position position) { | ||
| ladder.getRows()[position.getRow()].drawLine(position); | ||
| } | ||
|
|
||
| @Override | ||
| public void drawLine(LadderSize ladderSize) { | ||
| throw new IllegalArgumentException(ExceptionMessage.INVALID_LADDER_DRAW_INPUT.getMessage()); | ||
| } | ||
|
|
||
| public static LadderInputCreator of(LadderSize ladderSize) { | ||
| return new LadderInputCreator(ladderSize); | ||
| } | ||
|
|
||
| @Override | ||
| public Ladder getLadder() { | ||
| return this.ladder; | ||
| } | ||
|
|
||
|
|
||
| } |
6 changes: 4 additions & 2 deletions
6
src/main/java/ladder/ExceptionMessage.java → ...va/ladder/exception/ExceptionMessage.java
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
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.
Uh oh!
There was an error while loading. Please reload this page.