Skip to content

πŸš€ 3단계 - 사닀리(κ²Œμž„ μ‹€ν–‰) #2370

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions .editorconfig
Copy link
Member

Choose a reason for hiding this comment

The 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,24 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true

# Java files
[*.java]
indent_style = space
indent_size = 4
max_line_length = 120
continuation_indent_size = 8

# Prevent breaking method chains
ij_java_method_call_chain_wrap = off
ij_java_keep_line_breaks = true
ij_java_align_multiline_chained_methods = true
ij_java_keep_multiple_expressions_in_one_line = true
14 changes: 13 additions & 1 deletion src/main/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [사닀리타기] Step2
# πŸš€ 3단계 - 사닀리(κ²Œμž„ μ‹€ν–‰)

## # Step1 ν”Όλ“œλ°±

Expand Down Expand Up @@ -36,3 +36,15 @@
- [x] μ»¨λ²€μ…˜ 확인
- [x] `Point` 좜λ ₯ 기호 μ‚­μ œ
- [x] `NameList` κ°€ `Name` 을 μ‚¬μš©ν•˜λ„λ‘ λ³€κ²½

---

- [x] `PointList` 객체가 슀슀둜 νŒλ‹¨ν•˜λŠ” ꡬ쑰둜 개편
- [x] `Point` ν…ŒμŠ€νŠΈ μ½”λ“œμ—μ„œ 이전 이름인 "PointX" μ‚­μ œ
- [x] `Point` ReultView μ½”λ“œ μ‚­μ œ -> 역할을 λ„˜μ–΄κ°„λ‹€κ³  생각

## # Step3 μš”κ΅¬μ‚¬ν•­

* 사닀리 μ‹€ν–‰ κ²°κ³Ό 좜λ ₯
- [x] κ°œμΈλ³„ 이름을 μž…λ ₯ν•˜λ©΄ κ°œμΈλ³„ κ²°κ³Όλ₯Ό 좜λ ₯
- [x] "all" 을 μž…λ ₯ν•˜λ©΄ 전체 μ°Έμ—¬μžμ˜ μ‹€ν–‰ κ²°κ³Όλ₯Ό 좜λ ₯ν•˜κ³  μ’…λ£Œ
Comment on lines +46 to +50
Copy link
Member

Choose a reason for hiding this comment

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

쑰금 더 μž‘μ€ λ‹¨μœ„μ˜ κΈ°λŠ₯으둜 λΆ„λ¦¬ν•˜λ©΄ μ–΄λ–¨κΉŒμš”?
TDDλ₯Ό κΈ°λ°˜μœΌλ‘œν•˜λ‹ˆ ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€μ™€ κΈ°λŠ₯ λͺ©λ‘κ³Ό λ§€μΉ­μ‹œμΌœλ„ 쒋을 것 κ°™μ•„μš”.

59 changes: 55 additions & 4 deletions src/main/java/nextstep/ladder/controller/Game.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,81 @@
package nextstep.ladder.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

import nextstep.ladder.module.Board;
import nextstep.ladder.module.BoardResult;
import nextstep.ladder.module.Height;
import nextstep.ladder.module.Line;
import nextstep.ladder.module.Name;
import nextstep.ladder.module.NameList;
import nextstep.ladder.module.PointList;
import nextstep.ladder.module.ResultList;

public class Game {

private final List<String> peopleNames;
private final NameList peopleNames;
private final ResultList resultNames;
private final Height height;

public Game(List<String> peopleNames, Height height) {
public Game(NameList peopleNames, ResultList resultNames, Height height) {
this.peopleNames = peopleNames;
this.resultNames = resultNames;
this.height = height;
validateGame();
}

public Board createBoard() {
List<Line> lines = new ArrayList<>();
for (int i = 0; i < height.value(); i++) {
lines.add(new Line(new PointList(peopleNames.size())));
}
lines.forEach(Line::createLadders);
return new Board(lines, new NameList(peopleNames));
lines.forEach(Line::createRandomBridges);
return new Board(lines, peopleNames);
}

private void validateGame() {
if (peopleNames.size() != resultNames.size()) {
throw new IllegalArgumentException("μ‚¬λžŒ 이름과 κ²°κ³Ό μ΄λ¦„μ˜ κ°œμˆ˜κ°€ λ‹€λ¦…λ‹ˆλ‹€.");
}
}

public BoardResult play(Board board) {
Map<Name, Integer> resultMap = new HashMap<>();
IntStream.range(0, peopleNames.size())
.forEach(i -> {
Name name = peopleNames.get(i);
resultMap.put(name, i);
});
for (Line line : board.lines()) {
IntStream.range(0, peopleNames.size())
.forEach(i -> {
Name name = peopleNames.get(i);
resultMap.put(name, move(resultMap.get(name), line));
});
}

return new BoardResult(resultMap, resultNames);
}
Comment on lines +46 to +62
Copy link
Member

Choose a reason for hiding this comment

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

play, move 같은 λ©”μ„œλ“œλŠ” 도메인 κΈ°λŠ₯으둜 λ΄μ•Όν•˜μ§€ μ•Šμ„κΉŒμš”?

BoardResult result = board.play();
Result result = board.play(μ°Έκ°€μž1);

μ»¨νŠΈλ‘€λŸ¬κ°€ μ•„λ‹Œ 도메인이 μˆ˜ν–‰ν•  수 μžˆλ„λ‘ 이동해 λ³΄μ‹œλ©΄ 쒋을 것 κ°™μ•„μš”.


private int move(int index, Line line) {
if (canMoveLeft(index, line)) {
return index - 1;
}
if (canMoveRight(index, line)) {
return index + 1;
}
return index;
}

private boolean canMoveLeft(int index, Line line) {
return line.points().get(index).leftBridge().isBuilt();
}

private boolean canMoveRight(int index, Line line) {
return line.points().get(index).rightBridge().isBuilt();
}
}
22 changes: 18 additions & 4 deletions src/main/java/nextstep/ladder/controller/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package nextstep.ladder.controller;

import java.util.List;

import nextstep.ladder.module.Board;
import nextstep.ladder.module.BoardResult;
import nextstep.ladder.module.Height;
import nextstep.ladder.module.Name;
import nextstep.ladder.module.NameList;
import nextstep.ladder.module.ResultList;
import nextstep.ladder.view.InputView;
import nextstep.ladder.view.ResultView;

Expand All @@ -13,12 +15,24 @@ public class Main {
private static final ResultView resultView = new ResultView();

public static void main(String[] args) {
List<String> peopleNames = inputView.getPeopleNames();
NameList peopleNames = new NameList(inputView.getPeopleNames());
ResultList resultNames = new ResultList(inputView.getResultNames());
Height height = new Height(inputView.getHeight());

Game game = new Game(peopleNames, height);
Game game = new Game(peopleNames, resultNames, height);
Board board = game.createBoard();

resultView.printBoard(board);
resultView.printResultNames(resultNames);

BoardResult boardResult = game.play(board);

while (true) {
Name wantedName = new Name(inputView.getWantedName());
resultView.printResults(wantedName, boardResult);
if (wantedName.equals(new Name("all"))) {
break;
}
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/nextstep/ladder/module/BoardResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package nextstep.ladder.module;

import java.util.Map;
import java.util.stream.Collectors;

public class BoardResult {

private final Map<Name, Integer> resultMap;
private final ResultList resultNames;
Comment on lines +8 to +9
Copy link
Member

Choose a reason for hiding this comment

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

λ§€μΉ­ κ²°κ³Όλ₯Ό Map<Name, Result> νƒ€μž…μœΌλ‘œ κ°€μ§€λ©΄ μ–΄λ–¨κΉŒμš”?


public BoardResult(Map<Name, Integer> resultMap, ResultList resultNames) {
this.resultMap = resultMap;
this.resultNames = resultNames;
}

public Result of(Name name) {
return resultNames.get(resultMap.get(name));
}

public Map<Name, Result> all() {
return resultMap.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> resultNames.get(entry.getValue())));
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package nextstep.ladder.module;

public class Bridge {
public class LeftBridge {

private final boolean isBuilt;

public Bridge() {
public LeftBridge() {
this(false);
}

public Bridge(boolean isBuilt) {
public LeftBridge(boolean isBuilt) {
this.isBuilt = isBuilt;
}

Expand Down
39 changes: 29 additions & 10 deletions src/main/java/nextstep/ladder/module/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,39 @@ private void validate(PointList points) {
if (points.size() < 2) {
throw new IllegalArgumentException("Line의 ν¬κΈ°λŠ” 2 이상이어야 ν•©λ‹ˆλ‹€.");
}
if (points.get(0).bridge().isBuilt()) {
if (points.get(0).leftBridge().isBuilt()) {
throw new IllegalArgumentException("Line의 μ‹œμž‘μ€ 사닀리가 μ—†μ–΄μ•Ό ν•©λ‹ˆλ‹€.");
}
for (int i = 1; i < points.size(); i++) {
if (points.get(i).bridge().isBuilt() && points.get(i + 1).bridge().isBuilt()) {
throw new IllegalArgumentException("Lineμ—μ„œ μ—°μ†λœ 사닀리가 있으면 μ•ˆλ©λ‹ˆλ‹€.");
}
if (points.get(points.size() - 1).rightBridge().isBuilt()) {
throw new IllegalArgumentException("Line의 끝은 사닀리가 μ—†μ–΄μ•Ό ν•©λ‹ˆλ‹€.");
}

IntStream.range(1, points.size() - 1)
.filter(i -> points.get(i).leftBridge().isBuilt() && points.get(i + 1).leftBridge().isBuilt())
.findAny()
.ifPresent(i -> {
throw new IllegalArgumentException("Lineμ—μ„œ μ—°μ†λœ 사닀리가 있으면 μ•ˆλ©λ‹ˆλ‹€.");
});

IntStream.range(0, points.size() - 2)
.filter(i -> points.get(i).rightBridge().isBuilt() && points.get(i + 1).rightBridge().isBuilt())
.findAny()
.ifPresent(i -> {
throw new IllegalArgumentException("Lineμ—μ„œ μ—°μ†λœ 사닀리가 있으면 μ•ˆλ©λ‹ˆλ‹€.");
});
Comment on lines +30 to +42
Copy link
Member

Choose a reason for hiding this comment

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

일뢀 μ±…μž„μ€ PointList둜 μ΄λ™ν•˜λ©΄ μ–΄λ–¨κΉŒμš”?
Line#validate λ©”μ„œλ“œμ˜ 길이가 λ„ˆλ¬΄ 긴데 그만큼 μ±…μž„μ΄ λ§Žμ€κ²ƒμ΄ μ•„λ‹Œμ§€ κ³ λ €ν•΄ λ³΄μ‹œλ©΄ 쒋을 것 κ°™μ•„μš”

}

public void createLadders() {
IntStream.range(1, points.size())
.filter(i -> !points.get(i-1).bridge().isBuilt())
.filter(i -> RANDOM.nextBoolean())
.forEach(i -> points.get(i).createBridge());
public void createRandomBridges() {
IntStream.range(1, points.size()).filter(i -> !points.get(i - 1).leftBridge().isBuilt())
.filter(i -> RANDOM.nextBoolean()).forEach(i -> {
points.get(i).createLeftBridge();
points.get(i - 1).createRightBridge();
});
}

public void createLeftBridge(int index) {
points.get(index).createLeftBridge();
points.get(index - 1).createRightBridge();
validate(points);
}
}
4 changes: 2 additions & 2 deletions src/main/java/nextstep/ladder/module/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public class Name {
private final String name;

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

public String value() {
return name;
}

private void validate(String name) {
private void validateName(String name) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("이름은 빈 λ¬Έμžμ—΄μ΄ 될 수 μ—†μŠ΅λ‹ˆλ‹€.");
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/nextstep/ladder/module/NameList.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class NameList implements Iterable<Name> {
private final List<Name> names;

public NameList(List<String> names) {
validate(names);
validateNameList(names);
this.names = names.stream().map(Name::new).collect(Collectors.toList());
}

Expand All @@ -20,12 +20,16 @@ public int size() {
return names.size();
}

public Name get(int index) {
return names.get(index);
}

@Override
public Iterator<Name> iterator() {
return names.iterator();
}

private void validate(List<String> names) {
private void validateNameList(List<String> names) {
if (names == null || names.isEmpty()) {
throw new IllegalArgumentException("이름 λ¦¬μŠ€νŠΈλŠ” λΉ„μ–΄μžˆμ„ 수 μ—†μŠ΅λ‹ˆλ‹€.");
}
Expand Down
Loading