-
Notifications
You must be signed in to change notification settings - Fork 12
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
[사다리 타기] 김나윤 미션 제출합니다. #11
base: bbggr1209
Are you sure you want to change the base?
Changes from all commits
50eb618
cb73001
ccc5605
591de41
69068c2
3d7b3f2
a5bf955
3c41b2e
6d814d7
d9e3d6a
ec1d27b
8b3365a
1096c5c
80f57f7
8b39d36
cfa3d32
f6a17d7
cc688ba
2521ee3
d614e57
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 |
---|---|---|
|
@@ -30,3 +30,6 @@ out/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### DS_Store ### | ||
*.DS_Store |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
## 구현할 기능 목록 | ||
|
||
- [x] 플레이어 이름 입력 | ||
- [x] 이름을 입력 받을 메세지 출력 | ||
- [x] 이름을 입력 받음 | ||
- [x] 이름을 쉼표(,)를 기준으로 분리 | ||
- [x] 이름의 유효성 검사 | ||
- [x] 이름이 5글자 이하여야 한다. | ||
- [x] 이름에는 공백이 들어갈 수 없다. | ||
- [x] 이름이 중복되어서는 안된다. | ||
|
||
- [x] 사다리 높이 입력 | ||
- [x] 사다리 높이 입력 메세지 출력 | ||
- [x] 사다리 높이 입력 받음 | ||
- [x] 사다리 높이의 유효성 검사 | ||
- [x] 사다리 높이는 1 이상이어야 한다. | ||
- [x] 사다리 높이는 0~9 사이의 숫자여야 한다. | ||
|
||
- [x] 결과 출력 | ||
- [x] 플레이어 이름 출력 | ||
- [x] 사다리 출력 | ||
|
||
--- | ||
|
||
## 필요한 객체 | ||
|
||
### Player | ||
- 이름을 입력받고 유효성 검사를 수행하는 객체 | ||
|
||
### Players | ||
- Player 객체를 관리하는 객체 | ||
|
||
### Line | ||
- 사다리의 선을 관리하는 객체 | ||
|
||
### Point | ||
- 선의 시작점과 끝점을 관리하는 객체 | ||
|
||
### Line Generator | ||
- Line을 랜덤으로 생성하는 객체 | ||
|
||
### Ladder | ||
- 여러 개의 선 객체를 관리하는 객체 | ||
|
||
### 입출력 | ||
- 입력을 받고 출력하는 객체 | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import controller.LadderGameController; | ||
|
||
public class LadderGame { | ||
public static void main(String[] args) { | ||
LadderGameController ladderGameController = new LadderGameController(); | ||
ladderGameController.run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package controller; | ||
|
||
import model.Ladder; | ||
import model.Players; | ||
import view.InputView; | ||
import view.ResultView; | ||
|
||
public class LadderGameController { | ||
|
||
public void run() { | ||
Players players = initPlayer(); | ||
int playerCount = players.getPlayerCount(); | ||
int height = InputView.getLadderHeight(); | ||
Ladder ladder = initLadder(playerCount, height); | ||
ResultView.printLadder(ladder.getLines(), players.getPlayerNames()); | ||
} | ||
|
||
private Players initPlayer() { | ||
return new Players(InputView.getPlayerNames()); | ||
} | ||
|
||
private Ladder initLadder(int playerCount, int height) { | ||
return new Ladder(playerCount, height); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import util.LineGenerator; | ||
|
||
public class Ladder { | ||
|
||
private final List<Line> lines; | ||
|
||
public Ladder(int numberOfPlayers, int height) { | ||
lines = new ArrayList<>(); | ||
for (int i = 0; i < height; i++) { | ||
lines.add(LineGenerator.generateLine(numberOfPlayers)); | ||
} | ||
} | ||
|
||
public List<Line> getLines() { | ||
return lines; | ||
} | ||
|
||
} | ||
Comment on lines
+7
to
+22
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. 일급 컬렉션 활용 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
|
||
public class Line { | ||
|
||
private List<Point> points; | ||
|
||
public Line(List<Point> points) { | ||
this.points = points; | ||
} | ||
|
||
public List<Point> getPoints() { | ||
return points; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package model; | ||
|
||
public class Player { | ||
private final static int MAX_NAME_LENGTH = 5; | ||
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. Java Language Specification에 따르면 |
||
|
||
private final String name; | ||
|
||
public Player(String name) { | ||
validateNameNull(name); | ||
validateNameEmpty(name); | ||
validateNameLength(name); | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void validateNameNull(String name) { | ||
if (name == null) { | ||
throw new IllegalArgumentException("이름은 null이 될 수 없습니다."); | ||
} | ||
} | ||
|
||
public void validateNameEmpty(String name) { | ||
if (name.isEmpty()) { | ||
throw new IllegalArgumentException("이름은 빈 문자열이 될 수 없습니다."); | ||
} | ||
} | ||
|
||
public void validateNameLength(String name) { | ||
if (name.length() > MAX_NAME_LENGTH) { | ||
throw new IllegalArgumentException("이름 길이를 초과할 수 없습니다."); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class Players { | ||
private static final int MINIMUM_PLAYER_COUNT = 2; | ||
|
||
private final List<Player> players; | ||
|
||
public Players(List<String> playerNames) { | ||
validatePlayerCount(playerNames); | ||
validateDuplicatePlayerName(playerNames); | ||
this.players = generatePlayers(playerNames); | ||
} | ||
|
||
public List<String> getPlayerNames() { | ||
return players.stream() | ||
.map(Player::getName) | ||
.collect(Collectors.toList()); | ||
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.
|
||
} | ||
|
||
public int getPlayerCount() { | ||
return players.size(); | ||
} | ||
|
||
public void validatePlayerCount(List<String> playerNames) { | ||
if (playerNames.size() < MINIMUM_PLAYER_COUNT) { | ||
throw new IllegalArgumentException("플레이어는 2명 이상이어야 합니다."); | ||
} | ||
} | ||
|
||
public void validateDuplicatePlayerName(List<String> playerNames) { | ||
if (playerNames.size() != playerNames.stream().distinct().count()) { | ||
throw new IllegalArgumentException("중복된 이름이 존재합니다."); | ||
} | ||
} | ||
|
||
public List<Player> generatePlayers(List<String> playerNames) { | ||
return playerNames.stream() | ||
.map(Player::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package model; | ||
|
||
public class Point { | ||
|
||
private final boolean hasLine; | ||
|
||
public Point(boolean hasLine) { | ||
this.hasLine = hasLine; | ||
} | ||
|
||
public boolean hasLine() { | ||
return hasLine; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package util; | ||
|
||
import model.Line; | ||
import model.Point; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class LineGenerator { | ||
|
||
private static boolean previous = false; | ||
private static Random random = new Random(); | ||
|
||
public static Line generateLine(int numberOfPlayers) { | ||
List<Point> points = new ArrayList<>(); | ||
for (int i = 0; i < numberOfPlayers - 1; i++) { | ||
boolean next = random.nextBoolean() && !previous; | ||
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. 부정 연산자 조건문으로 리팩토링 하거나 다른 방법이 있다면 해당 방법으로 구현하는 편이 나을 수 있습니다. |
||
points.add(new Point(next)); | ||
previous = next; | ||
} | ||
return new Line(points); | ||
} | ||
|
||
public static List<Point> createPointsForLine(int numberOfPoints) { | ||
List<Point> points = new ArrayList<>(); | ||
for (int i = 0; i < numberOfPoints; i++) { | ||
boolean current = random.nextDouble() > 0.5 && !previous; | ||
points.add(new Point(current)); | ||
previous = current; | ||
} | ||
return points; | ||
} | ||
|
||
public static boolean hasSerialTrue(List<Point> points) { | ||
for (int i = 0; i < points.size() - 1; i++) { | ||
if (points.get(i).hasLine() && points.get(i + 1).hasLine()) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
bbggr1209 marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+35
to
+42
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. 요구 사항에도 적혀있지만 이번 미션에서 메소드 indent(들여쓰기)는 1단계까지만 허용하고 있습니다. |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package view; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.List; | ||
|
||
public class InputView { | ||
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. 이 클래스는 Util 클래스로 보입니다. 이 클래스 뿐만 아니라 모든 메소드가 |
||
private static final String HEIGHT_INPUT_REGEX = "[0-9]+"; | ||
|
||
private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
public static List<String> getPlayerNames() { | ||
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); | ||
try { | ||
String input = reader.readLine(); | ||
return List.of(input.split(",")); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("잘못된 입력입니다."); | ||
} | ||
} | ||
|
||
private static boolean isNotValidLadderHeight(String input) { | ||
return !input.matches(HEIGHT_INPUT_REGEX); | ||
} | ||
|
||
private static boolean isNotValidLadderHeightRange(int height) { | ||
return height < 1; | ||
} | ||
|
||
public static int getLadderHeight() { | ||
System.out.println("최대 사다리 높이는 몇 개인가요?"); | ||
try { | ||
String input = reader.readLine(); | ||
|
||
if (isNotValidLadderHeight(input)) { | ||
throw new IllegalArgumentException("사다리 높이는 숫자여야 합니다."); | ||
} | ||
if (isNotValidLadderHeightRange(Integer.parseInt(input))) { | ||
throw new IllegalArgumentException("사다리 높이는 1 이상이어야 합니다."); | ||
} | ||
Comment on lines
+36
to
+41
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. 이 로직 자체를 해당 메소드들에게 넘겨줘도 무방하지 않을까요? indent 단계까지 덤으로 낮출 수 있겠죠 😁 |
||
|
||
return Integer.parseInt(input); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("잘못된 입력입니다."); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package view; | ||
|
||
import java.util.List; | ||
import model.Line; | ||
import model.Point; | ||
|
||
public class ResultView { | ||
|
||
private static final int NAME_WIDTH = 6; | ||
|
||
public static void printLadder(List<Line> ladder, List<String> playerNames) { | ||
System.out.println("실행 결과"); | ||
printPlayerNames(playerNames); | ||
for (Line line : ladder) { | ||
printLine(line); | ||
} | ||
} | ||
Comment on lines
+11
to
+17
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.
|
||
|
||
private static void printPlayerNames(List<String> playerNames) { | ||
for (String playerName : playerNames) { | ||
System.out.printf("%-" + NAME_WIDTH + "s", playerName); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
private static void printLine(Line line) { | ||
List<Point> points = line.getPoints(); | ||
for (Point point : points) { | ||
if (point.hasLine()) { | ||
drawLine(); | ||
} else { | ||
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.
|
||
drawEmptyLine(); | ||
} | ||
} | ||
System.out.println("|"); | ||
} | ||
|
||
private static void drawLine() { | ||
System.out.print("|-----"); | ||
} | ||
|
||
private static void drawEmptyLine() { | ||
System.out.print("| "); | ||
} | ||
|
||
} |
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.
구현 기능 목록 작성 👍