-
Notifications
You must be signed in to change notification settings - Fork 26
2주차 미션 / 서버 1조 신종윤 #37
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
base: 1week-completed
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
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.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| public enum ExceptionMessage { | ||
|
|
||
| INVALID_LADDER_SIZE("사다리의 열과 행은 2이상의 자연수입니다."), | ||
| INVALID_LINE_POSITION("선의 위치가 사다리를 벗어납니다."), | ||
| INVALID_START_POSITION("출발 위치는 1이상의 자연수입니다."); | ||
|
|
||
| private final String message; | ||
|
|
||
| ExceptionMessage(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,56 @@ | ||
| public class Ladder { | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| private final int[][] rows; | ||
| class Ladder { | ||
| private final int[][] rows; | ||
| private final List<Line> lines; | ||
|
|
||
| public Ladder(int row, int numberOfPerson) { | ||
| rows = new int[row][numberOfPerson]; | ||
| public Ladder(int row, int column) { | ||
| validateLadderDimensions(row, column); | ||
| this.rows = new int[row][column]; | ||
| lines = new ArrayList<>(); | ||
| } | ||
|
|
||
| private void validateLadderDimensions(int row, int column) { | ||
| if(row < 2 || column < 2) { | ||
| throw new IllegalArgumentException(ExceptionMessage.INVALID_LADDER_SIZE.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public void addLine(int fromLine, int toLine, int currentRow) { | ||
| Line line = new Line(fromLine, toLine, currentRow, this.rows); | ||
| lines.add(line); | ||
| } | ||
|
|
||
| public boolean isLine(int row, int col) { | ||
| return rows[row][col] == 1 || rows[row][col] == -1; | ||
| } | ||
|
|
||
| public boolean isLineAtLeft(int row, int col) { | ||
| return col > 0 && rows[row][col - 1] == - 1; | ||
| } | ||
|
|
||
| public boolean isLineAtRight(int row, int col) { | ||
| return col + 1 < rows[row].length && rows[row][col + 1] == 1; | ||
| } | ||
|
|
||
| public int getHeight() { | ||
| return rows.length; | ||
| } | ||
|
|
||
| public int getWidth() { | ||
| return rows[0].length; | ||
| } | ||
|
|
||
| public int getRows(int row, int col) { | ||
| return rows[row][col]; | ||
| } | ||
|
|
||
| public boolean isLeftLine(int currentRow, int currentCol) { | ||
| return rows[currentRow][currentCol] == 1; | ||
| } | ||
|
|
||
| public boolean isRightLine(int currentRow, int currentCol) { | ||
| return rows[currentRow][currentCol] == - 1; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| public interface LadderCreator { | ||
| Ladder createLine(int rows, int columns); | ||
| } | ||
|
Comment on lines
+1
to
+3
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. 이렇게 인터페이스로 따로 나누자고 하는 것은 사용자가 직접 선을 그리는 경우, 랜덤으로 생성되는 경우 각각을 creator로 생각하고 이에 대해 조합을 이용해서 코드를 짜보자 라는것이 사실 목적이에요. 하지만, 이 경우는 Random 경우에만 ladderCreator를 상속받는 creator가 있고 바로 ladder를 만들고 바로 반환해요. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class LadderGame { | ||
| private Ladder ladder; | ||
| private static Scanner scanner; | ||
| private int startPosition; | ||
| private LadderRunner ladderRunner; | ||
|
|
||
| public LadderGame(Ladder ladder) { | ||
| this.ladder = ladder; | ||
| } | ||
|
|
||
| public void play() { | ||
| System.out.print("Enter the start position: "); | ||
| startPosition = scanner.nextInt(); | ||
|
|
||
| ladderRunner = new LadderRunner(ladder, startPosition); | ||
| int result = ladderRunner.run()+1; | ||
| System.out.println("Final position: " + result); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| scanner = new Scanner(System.in); | ||
| LadderGame game = LadderGameFactory.createRandomLadderGame(5, 5); | ||
| game.play(); | ||
| scanner.close(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| public class LadderGameFactory { | ||
| public static LadderGame createRandomLadderGame(int rows, int columns) { | ||
| LadderCreator creator = new RandomLadderCreator(); | ||
| Ladder ladder = creator.createLine(rows, columns); | ||
| return new LadderGame(ladder); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| public class LadderPrinter { | ||
|
|
||
| // 정적메소드 적용 | ||
| public static void print(Ladder ladder, Player player) { | ||
| for (int i = 0; i < ladder.getHeight(); i++) { | ||
| for (int j = 0; j < ladder.getWidth(); j++) { | ||
| System.out.print(ladder.getRows(i, j)); | ||
| if (player.getxPosition() == j && player.getyPosition() == i) { | ||
| System.out.print("*"); | ||
| } | ||
| System.out.print(" "); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| public class LadderRunner { | ||
|
|
||
| private final Ladder ladder; | ||
| private Player player; | ||
|
|
||
| public LadderRunner(Ladder ladder, int startPosition) { | ||
| this.ladder = ladder; | ||
| this.player = new Player(startPosition); | ||
| } | ||
|
|
||
| int run() { | ||
| while (true) { | ||
| printLadderState("Before"); | ||
|
|
||
| // 오른쪽으로 가기 | ||
| if (ladder.isLeftLine(player.getyPosition(), player.getxPosition())) { | ||
| player.right(); | ||
| printLadderState("After"); | ||
| player.down(); | ||
| } | ||
| // 왼쪽으로 가기 | ||
| else if (ladder.isRightLine(player.getyPosition(), player.getxPosition())) { | ||
| player.left(); | ||
| printLadderState("After"); | ||
| player.down(); | ||
| } | ||
| // 그냥 아래로 가기 | ||
| else { | ||
| player.down(); | ||
| if (isGameOver()) { | ||
| printLadderState("Final"); | ||
| break; | ||
| } | ||
| printLadderState("After"); | ||
| } | ||
|
|
||
| if (isGameOver()) { | ||
| printLadderState("Final"); | ||
| break; | ||
| } | ||
| } | ||
| return player.getxPosition(); | ||
| } | ||
|
|
||
| private boolean isGameOver() { | ||
| return player.getyPosition() == ladder.getHeight() - 1; | ||
| } | ||
|
|
||
| private void printLadderState(String state) { | ||
| System.out.println(state); | ||
| LadderPrinter.print(ladder, player); | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||||||||||||||||||||||||||||
| public class Line { | ||||||||||||||||||||||||||||||
| private int fromLine; | ||||||||||||||||||||||||||||||
| private int toLine; | ||||||||||||||||||||||||||||||
| private int t; | ||||||||||||||||||||||||||||||
| private int[][] rows; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| public Line(int fromLine, int toLine, int currentRow, int[][] rows) { | ||||||||||||||||||||||||||||||
| sort(fromLine, toLine); | ||||||||||||||||||||||||||||||
| this.rows = rows; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (validateFromLine(rows) && currentRow >= 0 && validateCurrentRow(currentRow, rows)) { | ||||||||||||||||||||||||||||||
| this.rows[currentRow][this.fromLine] = 1; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if (validateToLine(rows) && currentRow >= 0 && validateCurrentRow(currentRow, rows)) { | ||||||||||||||||||||||||||||||
| this.rows[currentRow][this.toLine] = - 1; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private static boolean validateCurrentRow(int currentRow, int[][] rows) { | ||||||||||||||||||||||||||||||
| if(currentRow < rows.length){ | ||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| else{ | ||||||||||||||||||||||||||||||
| throw new IllegalArgumentException(ExceptionMessage.INVALID_LINE_POSITION.getMessage()); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+19
to
+26
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. else를 안쓰고 throw를 바로 써도 좋아요. 무조건 이렇게 써야하는거는 아니지만, 이런식으로 쓰면 편한 경우가 자주 있어서 알아두시면 좋아요. 👍
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private boolean validateToLine(int[][] rows) { | ||||||||||||||||||||||||||||||
| if(this.toLine >= 0 && this.toLine <= rows[0].length){ | ||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| else{ | ||||||||||||||||||||||||||||||
| throw new IllegalArgumentException(ExceptionMessage.INVALID_LINE_POSITION.getMessage()); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private boolean validateFromLine(int[][] rows) { | ||||||||||||||||||||||||||||||
| if(this.fromLine >= 0 && this.fromLine <= rows[0].length){ | ||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| else{ | ||||||||||||||||||||||||||||||
| throw new IllegalArgumentException(ExceptionMessage.INVALID_LINE_POSITION.getMessage()); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| public void sort(int x, int y) { | ||||||||||||||||||||||||||||||
| this.fromLine = x; | ||||||||||||||||||||||||||||||
| this.toLine = y; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (this.fromLine > this.toLine) { | ||||||||||||||||||||||||||||||
| this.t = y; | ||||||||||||||||||||||||||||||
| this.toLine = x; | ||||||||||||||||||||||||||||||
| this.fromLine = t; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
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.
요런 정해진 상수값도 enum으로 관리해주면 좋을 것 같아요.