-
Notifications
You must be signed in to change notification settings - Fork 747
π 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
Changes from all commits
602399d
36d6aaa
3715a86
dfcac76
afc8508
cc3a9d4
67f232f
6cb0344
2df7016
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 |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# [μ¬λ€λ¦¬νκΈ°] Step2 | ||
# π 3λ¨κ³ - μ¬λ€λ¦¬(κ²μ μ€ν) | ||
|
||
## # Step1 νΌλλ°± | ||
|
||
|
@@ -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
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 |
---|---|---|
@@ -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
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. 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(); | ||
} | ||
} |
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
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 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 |
---|---|---|
|
@@ -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
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. μΌλΆ μ±
μμ PointListλ‘ μ΄λνλ©΄ μ΄λ¨κΉμ? |
||
} | ||
|
||
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); | ||
} | ||
} |
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.
μ΄ νμΌμ κ°μΈμ€μ μ΄λ 컀λ°μ ν¬ν¨νμ§ μμλ λ κ² κ°μμ.