Skip to content
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

πŸš€ 2단계 - 사닀리(생성) #2120

Open
wants to merge 36 commits into
base: sang5c
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f851cb7
build: java 11
sang5c Apr 2, 2024
f887f8a
docs: step2 μš”κ΅¬μ‚¬ν•­ μž‘μ„±
sang5c Apr 2, 2024
0c09d1e
feat: 사닀리 κ²Œμž„ κΈ°λ³Έ 좜λ ₯ ν˜•νƒœ μž‘μ„±
sang5c Apr 3, 2024
f6311a6
docs: μš”κ΅¬μ‚¬ν•­ 뢄석 μΆ”κ°€
sang5c Apr 6, 2024
066ce9d
build: gradle 5.0
sang5c Apr 6, 2024
1adfd82
feat: 쒌, 우 λͺ¨λ‘ 이동 κ°€λŠ₯ν•œ PointλŠ” 생성할 수 μ—†λ‹€
sang5c Apr 6, 2024
dff1072
test: PointλŠ” 쒌, 우 이동 κ°€λŠ₯ μ—¬λΆ€λ₯Ό 가진닀.
sang5c Apr 6, 2024
85f0fcc
feat: μ‚¬λžŒ 수λ₯Ό λ°›μ•„ κ°€λ‘œ 라인을 μƒμ„±ν•œλ‹€
sang5c Apr 6, 2024
ad63923
refactor: 포인트 생성 λ©”μ„œλ“œ 포인트둜 이동
sang5c Apr 6, 2024
2368cef
refactor: 랜덀 포인트 생성 λ©”μ„œλ“œλͺ… λͺ…ν™•ν•˜κ²Œ λ³€κ²½
sang5c Apr 6, 2024
7f26af3
docs: μ™„λ£Œλœ μš”κ΅¬μ‚¬ν•­ μ™„λ£Œ 처리
sang5c Apr 6, 2024
7b8e81d
feat: λž˜λ”κ²Œμž„ μ™„μ„±
sang5c Apr 6, 2024
fab1fe5
refactor: 맀직 λ¦¬ν„°λŸ΄ μƒμˆ˜λ‘œ μ‚¬μš©
sang5c Apr 6, 2024
9427f15
feat: 일급 μ»¬λ ‰μ…˜ 적용
sang5c Apr 6, 2024
6929a1f
feat: μ°Έκ°€μžλŠ” 이름을 κ°–λŠ”λ‹€
sang5c Apr 6, 2024
53309ed
refactor: LadderGame 뢄리
sang5c Apr 6, 2024
e3bdceb
refactor: domain νŒ¨ν‚€μ§€ 뢄리
sang5c Apr 6, 2024
5f53b78
test: 도메인 ν…ŒμŠ€νŠΈ μž‘μ„±
sang5c Apr 6, 2024
73d2e59
refactor: 쀑간 포인트 생성 λ©”μ„œλ“œ 뢄리
sang5c Apr 6, 2024
99c8db7
refactor: Line λ©”μ„œλ“œ 뢄리
sang5c Apr 6, 2024
d10073d
feat: μ°Έκ°€μžλŠ” 1λͺ… 이상이닀
sang5c Apr 6, 2024
e4da849
refactor: Line 생성 둜직 LineBuilder둜 뢄리
sang5c Apr 6, 2024
a241c10
feat: 이동 κ°€λŠ₯ν•œ ν¬μΈνŠΈκ°„μ— μ„œλ‘œ 이동 κ°€λŠ₯ν•˜μ§€ μ•ŠμœΌλ©΄ μ˜ˆμ™Έκ°€ λ°œμƒν•œλ‹€
sang5c Apr 6, 2024
9d2ef6c
feat: λ§ˆμ§€λ§‰ 포인트λ₯Ό μ΄μš©ν•˜μ—¬ λ‹€μŒ 포인트λ₯Ό μƒμ„±ν•˜λ„λ‘ λ³€κ²½
sang5c Apr 6, 2024
82cb335
refactor: λ³€μˆ˜λͺ…, λ©”μ„œλ“œλͺ… μΌκ΄€μ„±μžˆκ³  λͺ…ν™•ν•˜κ²Œ λ³€κ²½
sang5c Apr 6, 2024
82abe22
refactor: ConsoleUtil 뢄리
sang5c Apr 7, 2024
6aaabc1
refactor: 멀버 λ³€μˆ˜ final
sang5c Apr 7, 2024
7e1e4ec
refactor: λ©”μ„œλ“œλͺ… λͺ…ν™•ν•˜κ²Œ λ³€κ²½
sang5c Apr 7, 2024
1b2b225
feat: μ‚¬μš©μž input λ°›μ•„ κ²Œμž„ μ‹€ν–‰
sang5c Apr 7, 2024
30de861
refactor: λΆˆν•„μš”ν•œ static 제거
sang5c Apr 11, 2024
439d1cc
refactor: 맀직 λ„˜λ²„ μƒμˆ˜ν™”
sang5c Apr 18, 2024
6498f49
feat: playerλŠ” λͺ‡ 번째 μžλ¦¬μΈμ§€ 확인할 수 μžˆλŠ” indexλ₯Ό κ°–λŠ”λ‹€
sang5c Apr 18, 2024
a8a84a8
feat: PointλŠ” λͺ‡ 번째 μžλ¦¬μΈμ§€ 확인할 수 μžˆλŠ” indexλ₯Ό κ°–λŠ”λ‹€
sang5c Apr 18, 2024
9ad91e8
refactor: collectingAndThen μ‚¬μš©ν•˜μ—¬ λ°”λ‘œ λ°˜ν™˜
sang5c Apr 18, 2024
9a91a2f
refactor: LineBuilderλ₯Ό Line λ‚΄λΆ€λ‘œ 이동
sang5c Apr 21, 2024
4a9ad30
refactor: Lines 생성 μ±…μž„μ„ LinesGenerator둜 뢄리
sang5c Apr 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
# 사닀리 κ²Œμž„

## 진행 방법

* 사닀리 κ²Œμž„ κ²Œμž„ μš”κ΅¬μ‚¬ν•­μ„ νŒŒμ•…ν•œλ‹€.
* μš”κ΅¬μ‚¬ν•­μ— λŒ€ν•œ κ΅¬ν˜„μ„ μ™„λ£Œν•œ ν›„ μžμ‹ μ˜ github 아이디에 ν•΄λ‹Ήν•˜λŠ” λΈŒλžœμΉ˜μ— Pull Request(μ΄ν•˜ PR)λ₯Ό 톡해 μ½”λ“œ 리뷰 μš”μ²­μ„ ν•œλ‹€.
* μ½”λ“œ 리뷰 ν”Όλ“œλ°±μ— λŒ€ν•œ κ°œμ„  μž‘μ—…μ„ ν•˜κ³  λ‹€μ‹œ PUSHν•œλ‹€.
* λͺ¨λ“  ν”Όλ“œλ°±μ„ μ™„λ£Œν•˜λ©΄ λ‹€μŒ 단계λ₯Ό λ„μ „ν•˜κ³  μ•žμ˜ 과정을 λ°˜λ³΅ν•œλ‹€.

## 온라인 μ½”λ“œ 리뷰 κ³Όμ •
* [ν…μŠ€νŠΈμ™€ μ΄λ―Έμ§€λ‘œ μ‚΄νŽ΄λ³΄λŠ” 온라인 μ½”λ“œ 리뷰 κ³Όμ •](https://github.com/nextstep-step/nextstep-docs/tree/master/codereview)

* [ν…μŠ€νŠΈμ™€ μ΄λ―Έμ§€λ‘œ μ‚΄νŽ΄λ³΄λŠ” 온라인 μ½”λ“œ 리뷰 κ³Όμ •](https://github.com/nextstep-step/nextstep-docs/tree/master/codereview)

---

## Step 2. 사닀리(생성)

### κΈ°λŠ₯ μš”κ΅¬μ‚¬ν•­

- 사닀리 κ²Œμž„μ— μ°Έμ—¬ν•˜λŠ” μ‚¬λžŒμ— 이름을 μ΅œλŒ€5κΈ€μžκΉŒμ§€ λΆ€μ—¬ν•  수 μžˆλ‹€. 사닀리λ₯Ό 좜λ ₯ν•  λ•Œ μ‚¬λžŒ 이름도 같이 좜λ ₯ν•œλ‹€.
- μ‚¬λžŒ 이름은 μ‰Όν‘œ(,)λ₯Ό κΈ°μ€€μœΌλ‘œ κ΅¬λΆ„ν•œλ‹€.
- μ‚¬λžŒ 이름을 5자 κΈ°μ€€μœΌλ‘œ 좜λ ₯ν•˜κΈ° λ•Œλ¬Έμ— 사닀리 폭도 λ„“μ–΄μ Έμ•Ό ν•œλ‹€.
- 사닀리 타기가 μ •μƒμ μœΌλ‘œ λ™μž‘ν•˜λ €λ©΄ 라인이 κ²ΉμΉ˜μ§€ μ•Šλ„λ‘ ν•΄μ•Ό ν•œλ‹€.
- |-----|-----| λͺ¨μ–‘κ³Ό 같이 κ°€λ‘œ 라인이 κ²ΉμΉ˜λŠ” 경우 μ–΄λŠ λ°©ν–₯으둜 이동할지 κ²°μ •ν•  수 μ—†λ‹€.

### ν”„λ‘œκ·Έλž˜λ° μš”κ΅¬μ‚¬ν•­

- μžλ°” 8의 슀트림과 λžŒλ‹€λ₯Ό μ μš©ν•΄ ν”„λ‘œκ·Έλž˜λ°ν•œλ‹€.
- κ·œμΉ™ 6: λͺ¨λ“  μ—”ν‹°ν‹°λ₯Ό μž‘κ²Œ μœ μ§€ν•œλ‹€.

### μš”κ΅¬μ‚¬ν•­ 뢄석

- [X] μ°Έκ°€μžλŠ” 이름을 κ°–λŠ”λ‹€.
- [X] 이름은 μ΅œλŒ€ 5μžμ΄λ‹€.
- [X] 이름이 λΉ„μ–΄μžˆκ±°λ‚˜ 6자 이상이면 μ˜ˆμ™Έκ°€ λ°œμƒν•œλ‹€.
- [ ] 사닀리
- [X] μ‚¬λ‹€λ¦¬μ˜ κ°€λ‘œ 라인을 수λ₯Ό μ„€μ •ν•  수 μžˆλ‹€. (높이)
- [X] μ‚¬λ‹€λ¦¬μ˜ μ„Έλ‘œ 라인은 μ‚¬λžŒ 수 만큼 μƒμ„±λœλ‹€. (λ„ˆλΉ„)
- [X] μ‚¬λ‹€λ¦¬μ˜ 각 κ°€λ‘œ λΌμΈμ—λŠ” μ‚¬λžŒ 수 만큼 포인트(점)κ°€ μƒμ„±λœλ‹€.
- [X] 쒌, μš°μ— ν¬μΈνŠΈκ°€ μ‘΄μž¬ν•˜λŠ” 경우 이동할 수 μžˆλ‹€.
- [X] κ°€μž₯ μ™Όμͺ½ ν¬μΈνŠΈμ—μ„œλŠ” 쒌둜 이동 λΆˆκ°€ν•˜λ‹€.
- [X] κ°€μž₯ 였λ₯Έμͺ½ ν¬μΈνŠΈμ—μ„œλŠ” 우둜 이동 λΆˆκ°€ν•˜λ‹€.
- [X] μ—°μ†μœΌλ‘œ 이동 κ°€λŠ₯ν•œ ν¬μΈνŠΈλŠ” μ‘΄μž¬ν•  수 μ—†λ‹€. -> 쒌 우 λͺ¨λ‘ 이동 κ°€λŠ₯ν•œ ν¬μΈνŠΈλŠ” μ‘΄μž¬ν•  수 μ—†λ‹€
- [ ] 포인트
- [X] 쒌, 우 λͺ¨λ‘ 이동 κ°€λŠ₯ν•œ ν¬μΈνŠΈλŠ” μ‘΄μž¬ν•  수 μ—†λ‹€. μ„Έ 가지 경우만 μ‘΄μž¬ν•œλ‹€.
- [X] 쒌 이동 λΆˆκ°€, 우 이동 λΆˆκ°€
- [X] 쒌 이동 κ°€λŠ₯, 우 이동 λΆˆκ°€
- [X] 쒌 이동 λΆˆκ°€, 우 이동 κ°€λŠ₯
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java'
apply plugin: 'eclipse'

version = '1.0.0'
sourceCompatibility = 1.8
sourceCompatibility = 11
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ―Έμ…˜ λ‹€μ‹œ μ‹œμž‘ν•  땐 λ¦¬λ·°μš”μ²­μ„ 염두에 두지 μ•Šκ³  μ§„ν–‰ν•΄μ„œ μžλ°” 버전을 λ°”κΏ¨μ—ˆλŠ”λ°μš”!
ν˜Ήμ‹œ λ²„μ „λ‹€μš΄μ΄ ν•„μš”ν•˜λ©΄ 내리고 λ‹€μ‹œ μš”μ²­λ“œλ¦¬κ² μŠ΅λ‹ˆλ‹€!


repositories {
mavenCentral()
Expand Down
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Sat Apr 06 16:04:18 KST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
18 changes: 18 additions & 0 deletions src/main/java/nextstep/ladder/LadderGameApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package nextstep.ladder;

import nextstep.ladder.domain.LadderGame;
import nextstep.ladder.util.ConsoleUtil;

import java.util.List;

public class LadderGameApplication {

public static void main(String[] args) {
List<String> playerNames = ConsoleUtil.inputPlayerNames();
int height = ConsoleUtil.inputHeight();

LadderGame ladderGame = LadderGame.start(playerNames, height);

ConsoleUtil.printGameResults(ladderGame);
}
}
29 changes: 29 additions & 0 deletions src/main/java/nextstep/ladder/domain/LadderGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nextstep.ladder.domain;

import java.util.List;

public class LadderGame {

private final Players players;
private final Lines lines;

private LadderGame(final Players players, final Lines lines) {
this.players = players;
this.lines = lines;
}

public static LadderGame start(List<String> playerNames, int height) {
Players players = Players.of(playerNames);
Lines lines = Lines.of(players.count(), height);

return new LadderGame(players, lines);
}

public Players getPlayers() {
return players;
}

public Lines getLines() {
return lines;
}
}
59 changes: 59 additions & 0 deletions src/main/java/nextstep/ladder/domain/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package nextstep.ladder.domain;

import java.util.List;

public class Line {
private final List<Point> points;

Line(List<Point> points) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

νŒ©ν† λ¦¬λ‘œ μ‚¬μš©ν•˜κΈ° μœ„ν•΄ default 둜 μ„ μ–Έν•˜μ‹ κ±ΈκΉŒμš”? πŸ‘
κ·Έλ ‡λ‹€λ©΄ νŒ¨ν‚€μ§€ 쑰정을 쑰금 더 잘 ν•΄λ³Όμˆ˜ μžˆμ§€ μ•Šμ„κΉŒμš”?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κ°μ‚¬ν•©λ‹ˆλ‹€
리뷰 μ£Όμ‹  λ‚΄μš©μ„ κ³ λ―Όν•΄λ΄€λŠ”λ°μš”!
Line을 생성 방식(맨 μ’ŒμΈ‘μ€ 쒌둜 이동 λΆˆκ°€, 맨 μš°μΈ‘μ€ 우츑 이동 λΆˆκ°€)에도 κ°•μ œκ°€ ν•„μš”ν•˜μ—¬ 직접 생성이 λΆˆκ°€λŠ₯(private μƒμ„±μž)ν•˜λ„λ‘ λ§Œλ“€κ³  Builderλ₯Ό Line λ‚΄λΆ€λ‘œ μ΄λ™ν•˜λŠ” ν˜•νƒœλ‘œ ν•΄κ²°ν•΄ λ΄€μŠ΅λ‹ˆλ‹€.
(LineBuilderλ₯Ό ν†΅ν•΄μ•Όλ§Œ 라인 생성이 κ°€λŠ₯ν•˜κ²Œ ꡬ성)

validate(points);
this.points = points;
}

private void validate(List<Point> points) {
validatePointsListSize(points);
validatePointMovements(points);
}

private void validatePointsListSize(List<Point> points) {
if (points == null || points.isEmpty()) {
throw new IllegalArgumentException("점이 μ—†μŠ΅λ‹ˆλ‹€.");
}

if (points.size() < 2) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 λ§€μ§λ„˜λ²„κ°€ μ‚¬μš©λ˜μ—ˆμ–΄μš”.

λ‹€λ§Œ ν•΄λ‹Ή 값은 사닀리 κ²Œμž„μ—μ„œλŠ” λΆˆλ³€μ˜ 진리에 κ°€κΉŒμš΄ 값이라 ꡬ지 μƒμˆ˜λ‘œ μ„ μ–Έν•΄μ•Όν• κΉŒ 고민이 μžˆλ„€μš”.

μƒμˆ˜λ‘œ μ„ μ–Έν–ˆμ„λ•Œ 였히렀 넀이밍을 잘 ν•˜μ§€ μ•Šμ•˜μ„ 경우 ν–‡κ°ˆλ¦΄ 수 μžˆμ§€ μžˆμ„κΉŒ μ‹Άμ–΄μ„œμš”!

μƒμ˜€λ‹˜ 생각이 κΆκΈˆν•˜λ„€μš”!

Copy link
Author

@sang5c sang5c Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ½”λ“œ 일관성과 μ—λŸ¬ λ©”μ‹œμ§€ (μ§μ „μ˜ name.legnth()와 같은 이유둜)μ—μ„œ λ™μΌν•œ 값이 쓰일 수 μžˆλ„λ‘ μƒμˆ˜λ‘œ λΆ„λ¦¬ν–ˆμŠ΅λ‹ˆλ‹€.
포인트의 μˆ˜λŠ” μ°Έμ—¬μžμ˜ 수λ₯Ό μ˜λ―Έν•˜κΈ°μ—, κ²Œμž„μ˜ μ„€μ •μœΌλ‘œ 빠질 수 μžˆλŠ” λΆ€λΆ„μ΄λΌλŠ” 생각도 λ“œλ„€μš”!

예λ₯Ό λ“€μ–΄ "μ°Έμ—¬μžκ°€ ν•œλͺ…일 수 μžˆλ‹€" λ˜λŠ” "μ„Έ λͺ… 이상이어야 κ²Œμž„μ΄ κ°€λŠ₯ν•˜λ‹€"λ“±μœΌλ‘œ μΆ”κ°€ μš”κ΅¬μ‚¬ν•­μ΄ 생긴닀면 μ£Όμž…λ°›λŠ” ν˜•νƒœλ‘œ ꡬ성해야 ν•œλ‹€κ³  λŠκΌˆμŠ΅λ‹ˆλ‹€.
μ μš©ν•˜μ§€ μ•Šμ€ μ΄μœ λŠ” λ¬΅μ‹œμ μœΌλ‘œ μ‚¬λ‹€λ¦¬μ˜ μ„Έλ‘œ 라인은 2개 μ΄μƒμ΄λΌλŠ” 약속이 μ‘΄μž¬ν•œλ‹€κ³  μƒκ°ν–ˆκ³ . 1개 κ°€λŠ₯ λ˜λŠ” 3개 이상은 μΌλ°˜μ μ΄μ§€ μ•Šμ€ μΌ€μ΄μŠ€λΌ ν˜„μž¬ μƒνƒœ(2개 이상)λ₯Ό μœ μ§€ν•˜λŠ” λ°©ν–₯으둜 κ²°μ •ν–ˆμŠ΅λ‹ˆλ‹€!

throw new IllegalArgumentException("점이 두 개 이상 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.");
}
}

private void validatePointMovements(List<Point> points) {
if (firstPoint(points).canMoveLeft() || lastPoint(points).canMoveRight()) {
throw new IllegalArgumentException("첫 μ μ—μ„œλŠ” μ™Όμͺ½μœΌλ‘œ 이동할 수 μ—†κ³ , λ§ˆμ§€λ§‰ μ μ—μ„œλŠ” 였λ₯Έμͺ½μœΌλ‘œ 이동할 수 μ—†μŠ΅λ‹ˆλ‹€.");
}

for (int i = 0; i < points.size() - 1; i++) {
assertConsecutivePointsAreMovable(points, i);
}
}

private void assertConsecutivePointsAreMovable(List<Point> points, int i) {
if (points.get(i).canMoveRight() && !points.get(i + 1).canMoveLeft()) {
throw new IllegalArgumentException("μ—°μ†λœ μ μ—μ„œλŠ” μ„œλ‘œ 이동 κ°€λŠ₯ν•΄μ•Ό ν•©λ‹ˆλ‹€.");
}
}

private Point firstPoint(List<Point> points) {
return points.get(0);
}

private Point lastPoint(List<Point> points) {
return points.get(points.size() - 1);
}

public int width() {
return points.size();
}

public Point getPoint(int index) {
return points.get(index);
}
}
40 changes: 40 additions & 0 deletions src/main/java/nextstep/ladder/domain/LineBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package nextstep.ladder.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class LineBuilder {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LineBuilder 클래슀의 μœ„μΉ˜μ— λŒ€ν•΄ 고민이 λ§Žμ•˜μŠ΅λ‹ˆλ‹€.
λ³„λ„μ˜ 클래슀둜 λΆ„λ¦¬ν•˜λŠ”κ²Œ 쒋을지 Line 클래슀 내뢀에 μžˆμ–΄μ•Ό 할지 κ³ λ―Όν•˜λ‹€κ°€ Factory의 역할도 일뢀 μˆ˜ν–‰ν•œλ‹€κ³  λŠκ»΄μ Έμ„œ λ³„λ„λ‘œ λΆ„λ¦¬ν–ˆμŠ΅λ‹ˆλ‹€.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

νŒ©ν† λ¦¬λ₯Ό μ¨μ„œ μ–»κ³ μž ν•˜λŠ” 이득이 λ¬΄μ—‡μΈκ°€μš”? πŸ€” πŸ’­

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ œκ°€ μƒκ°ν•˜λŠ” νŒ©ν† λ¦¬μ˜ μž₯점은 μ•„λž˜ 정도인데 해당사항이 μ—†λŠ”κ²ƒ κ°™μ•„μ„œμš” πŸ€”

객체 생성에 쑰건이 μžˆμ„ λ•Œ: 객체λ₯Ό 생성할 λ•Œ 쑰건에 따라 λ‹€λ₯Έ μ’…λ₯˜μ˜ 객체λ₯Ό 생성해야 ν•˜λŠ” 경우, νŒ©ν† λ¦¬ νŒ¨ν„΄μ„ μ‚¬μš©ν•˜μ—¬ 쑰건에 따라 μ μ ˆν•œ 객체λ₯Ό λ°˜ν™˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

κ΅¬ν˜„μ„ λ³€κ²½ν•  ν•„μš”κ°€ μžˆμ„ λ•Œ: μΈν„°νŽ˜μ΄μŠ€λ₯Ό 톡해 μ—¬λŸ¬ κ΅¬ν˜„ 클래슀 쀑 ν•˜λ‚˜λ₯Ό μ„ νƒν•΄μ„œ μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•˜μ—¬, μΆ”ν›„ κ΅¬ν˜„μ„ λ³€κ²½ν•  λ•Œ κΈ°μ‘΄ μ½”λ“œλ₯Ό μˆ˜μ •ν•˜μ§€ μ•Šκ³ λ„ μƒˆλ‘œμš΄ κ΅¬ν˜„μ„ μ‰½κ²Œ μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν•΄λ‹Ή 클래슀λ₯Ό 톡해 두 가지λ₯Ό ν•œ λ²ˆμ— λ‹¬μ„€ν•˜λ €κ³  ν–ˆμ—ˆμŠ΅λ‹ˆλ‹€ πŸ˜…
"랜덀 포인트λ₯Ό κ°–λŠ”", "Line을 생성"ν•œλ‹€

이런 μƒνƒœμ—μ„œ λ‚˜μ€‘μ— 이동이 지정 κ°€λŠ₯ν•œ Line을 λ§Œλ“€ μš”κ΅¬μ‚¬ν•­μ΄ 생기지 μ•Šμ„κΉŒ? λΌλŠ” μƒκ°μœΌλ‘œ 애맀λͺ¨ν˜Έν•œ ν΄λž˜μŠ€κ°€ νƒ„μƒν–ˆλ˜ 것 κ°™μŠ΅λ‹ˆλ‹€. 이 뢀뢄은 μ±…μž„μ„ 뢄리(RandomGenerator, Builder)ν•˜μ—¬ μƒˆλ‘œ κ΅¬μ„±ν–ˆμœΌλ‹ˆ 확인 λΆ€νƒλ“œλ¦½λ‹ˆλ‹€!


private static final Random RANDOM = new Random();

private final List<Point> points = new ArrayList<>();

public static Line buildWithRandomPoints(int count) {
return new LineBuilder().initRandomMoveablePoint()
.addRandomMoveablePoints(count - 2)
.build();
}

private Line build() {
points.add(lastPoint().createRightmost());
return new Line(points);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

클래슀의 이름이 λΉŒλ”μΈλ° λΉŒλ“œν•¨μˆ˜κ°€ private ν•˜κ²Œ μ„ μ–Έλ˜λ©΄ μ‚¬μš©ν• λ•Œ ν–‡κ°ˆλ¦¬μ§€ μ•Šμ„κΉŒμš”?
πŸ€”

Copy link
Author

@sang5c sang5c Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이전 리뷰인 #2120 (comment) 에 λŒ“κΈ€μ΄ μ•ˆλ‹¬λ € 이곳에 ν•œλ²ˆμ— λ‹΅λ³€λ“œλ¦½λ‹ˆλ‹€!

νŒ©ν† λ¦¬ μ¨μ„œ μ–»κ³ μž ν•˜λŠ” 이득

졜초 μž‘μ„± μ˜λ„λŠ” νŒ©ν† λ¦¬λ₯Ό 톡해 μ›ν•˜λŠ” ν˜•νƒœλ‘œ(manual) μƒμ„±ν•˜κ±°λ‚˜, 랜덀(auto)ν•˜κ²Œ μƒμ„±ν•œλ‹€. λ₯Ό λͺ©ν‘œλ‘œ ν–ˆμ—ˆμŠ΅λ‹ˆλ‹€.

클래슀의 이름이 λΉŒλ”μΈλ° build()κ°€ private이 된 이유

ν˜„μž¬λŠ” μ›ν•˜λŠ” ν˜•νƒœλ‘œ 생성(manual)에 λŒ€ν•œ μš”κ΅¬μ‚¬ν•­μ΄ μ—†μœΌλ‹ˆ 막아야 ν•˜μ§€ μ•Šμ„κΉŒ? λΌλŠ” 생각과 λ‚˜μ€‘μ— 직접 라인을 ꡬ성할 수 μžˆλ‹€λŠ” μš”κ΅¬μ‚¬ν•­μ΄ 생겼을 λ•Œλ₯Ό μœ„ν•΄ build()λ₯Ό 두고 이λ₯Ό public으둜 μ—΄λ©΄ λ˜μ§€ μ•Šμ„κΉŒ? λΌλŠ” μ—¬λŸ¬κ°€μ§€ 생각이 κ²°ν•©λ˜μ–΄ μ• λ§€ν•œ μ½”λ“œκ°€ λ‚˜μ™”μŠ΅λ‹ˆλ‹€ γ…Žγ…Ž

κ²°κ΅­ λΉŒλ”μ™€ μ œλ„ˆλ ˆμ΄ν„°μ˜ μ±…μž„μ„ λΆ„λ¦¬ν•˜κ³  build() λ©”μ„œλ“œλŠ” public으둜 μ μš©ν–ˆμŠ΅λ‹ˆλ‹€ :)


private LineBuilder addRandomMoveablePoints(int numberOfMiddlePoints) {
for (int i = 0; i < numberOfMiddlePoints; i++) {
Point point = lastPoint().createNext(RANDOM.nextBoolean());
points.add(point);
}
return this;
}

private LineBuilder initRandomMoveablePoint() {
points.add(Point.createLeftmost(RANDOM.nextBoolean()));
return this;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

두 단계λ₯Ό λ‚˜λˆˆ μ΄μœ κ°€ κΆκΈˆν•΄μš”!

μ•„λž˜μ™€κ°™μ΄ ν•œλ‹¨κ³„λ‘œ μ‚¬μš©ν•˜λŠ”κ²ƒμ΄ 더 νŽΈλ¦¬ν•˜μ§€ μ•Šμ„κΉŒμš”? πŸ€”

(μ œκ°€ μžλ°”λ₯Ό μ˜€λžœλ§Œμ— μ“°κ³  + IDE이 ν™˜κ²½μ΄ μ•„λ‹ˆλΌμ„œ λ¬Έλ²•μ˜€λ₯˜ μ–‘ν•΄λΆ€νƒλ“œλ¦΄κ²Œμš”)

private LineBuilder create(int count) {
Point current = Point.createLeftmost(RANDOM.nextBoolean())
potins.add(current)
for (int i = 0; i < count - 2; i++) {
     current = current.next()
     potins.add(current()
}
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

첫 번째 포인트의 μΆ”κ°€ 방식이 λ‹€λ₯΄λ‹€λŠ” 것을 λͺ…μ‹œμ μœΌλ‘œ λ‚˜νƒ€λ‚΄κ³  μ‹Άμ—ˆμŠ΅λ‹ˆλ‹€. 이전 λŒ“κΈ€κ³Ό λ§ˆμ°¬κ°€μ§€λ‘œ builder 뢄리λ₯Ό μ§„ν–‰ν–ˆκ³  둜직이 ν•˜λ‚˜λ‘œ ν†΅ν•©λ˜μ—ˆμŠ΅λ‹ˆλ‹€!


private Point lastPoint() {
return points.get(points.size() - 1);
}
}
36 changes: 36 additions & 0 deletions src/main/java/nextstep/ladder/domain/Lines.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package nextstep.ladder.domain;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Lines {

private final List<Line> lines;

private Lines(List<Line> lines) {
validate(lines);
this.lines = lines;
}

private void validate(List<Line> lines) {
if (lines == null || lines.isEmpty()) {
throw new IllegalArgumentException("사닀리 λ†’μ΄λŠ” 1 이상이어야 ν•©λ‹ˆλ‹€.");
}
}

public static Lines of(int numberOfPlayers, int height) {
List<Line> lines = IntStream.range(0, height)
.mapToObj(i -> LineBuilder.buildWithRandomPoints(numberOfPlayers))
.collect(Collectors.toList());
return new Lines(lines);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ œκ°€ μžλ°”λ₯Ό μ•ˆμ“΄μ§€ μ˜€λž˜λ˜μ„œ κ°€λ¬Ό κ°€λ¬Όν•˜μ§€λ§Œ.... μ•„λž˜μ™€ 같이 ν•œλ²ˆμ— μ‚¬μš©ν•  수 μžˆμ–΄μš”.

Suggested change
.collect(Collectors.toList());
return new Lines(lines);
.collect(collectingAndThen(toList(), Lines::new)

}

public int getHeight() {
return lines.size();
}

public Line getLine(int targetHeight) {
return lines.get(targetHeight);
}
}
24 changes: 24 additions & 0 deletions src/main/java/nextstep/ladder/domain/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nextstep.ladder.domain;

public class Player {
private final String name;

public Player(String name) {
validate(name);
this.name = name;
}

private void validate(String name) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("이름은 λΉ„μ–΄μžˆμ„ 수 μ—†μŠ΅λ‹ˆλ‹€.");
}

if (name.length() > 5) {
throw new IllegalArgumentException("이름은 5자 μ΄ν•˜μ—¬μ•Ό ν•©λ‹ˆλ‹€.");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맀직 λ„˜λ²„κ°€ μ‚¬μš©λ˜μ—ˆλ„€μš”.
νŠΉνžˆλ‚˜ 이름 같은 κ²½μš°μ—λŠ” μƒλŒ€μ μœΌλ‘œ 변경이 μ‰½κ²Œ λ°œμƒν•  수 μžˆλŠ” μš”μ†Œμ΄λ‹ˆκΉŒ
μƒμˆ˜λ‘œ κ΄€λ¦¬ν•œλ‹€λ©΄ μž₯점이 μžˆμ„ 것 κ°™μ•„μš”.

"이름은 " + MAX_NAME_LENGTH +"자 μ΄ν•˜μ—¬μ•Ό ν•©λ‹ˆλ‹€."

μœ„ 처럼 μ˜ˆμ™Έμ—μ„œλ„ μ‚¬μš©ν•œλ‹€λ©΄ μ‚¬μ΄λ“œ μ΄νŽ™νŠΈλ„ 쀄일 수 μžˆκ² λ„€μš”!

}

public String getName() {
return name;
}
}
35 changes: 35 additions & 0 deletions src/main/java/nextstep/ladder/domain/Players.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package nextstep.ladder.domain;

import java.util.List;
import java.util.stream.Collectors;

public class Players {
private final List<Player> players;

private Players(List<Player> players) {
validate(players);
this.players = players;
}

private void validate(List<Player> players) {
if (players == null || players.isEmpty()) {
throw new IllegalArgumentException("μ°Έκ°€μžκ°€ μ—†μŠ΅λ‹ˆλ‹€.");
}
}

public static Players of(List<String> playerNames) {
List<Player> players = playerNames.stream()
.map(Player::new)
.collect(Collectors.toList());

return new Players(players);
}

public int count() {
return players.size();
}

public Player getPlayer(int index) {
return players.get(index);
}
}
38 changes: 38 additions & 0 deletions src/main/java/nextstep/ladder/domain/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package nextstep.ladder.domain;

public class Point {
private final boolean left;
private final boolean right;

public Point(boolean left, boolean right) {
validate(left, right);
this.left = left;
this.right = right;
}

private void validate(boolean left, boolean right) {
if (left && right) {
throw new IllegalArgumentException("쒌 우 λͺ¨λ‘ 이동 κ°€λŠ₯ν•œ Point 생성 λΆˆκ°€");
}
}

public static Point createLeftmost(boolean canMoveRight) {
return new Point(false, canMoveRight);
}

public Point createRightmost() {
return new Point(this.right, false);
}

public Point createNext(boolean canMoveRight) {
return new Point(this.right, !this.right && canMoveRight);
}

public boolean canMoveLeft() {
return left;
}

public boolean canMoveRight() {
return right;
}
}
Loading