Skip to content

Commit dab05c3

Browse files
authored
3단계 - 사다리(게임 실행) (#2421)
* feat:🚀 2단계 - 사다리(생성) * refactor:스트림활용, 안쓰는 코드 정리 * feat:PR반영, README에 TODO 갱신 * refactor:리팩토링 및 테스트 케이스 보강 * feat:3단계 사다리(게임 실행) * refactor:PR 반영 * chore:게임 화면 개선. 안쓰는 import제거
1 parent 6eb09ad commit dab05c3

27 files changed

+704
-138
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
# 사다리 게임
22

3+
4+
## 3단계 - 사다리(게임 실행)
5+
### TODO
6+
- [x] PR 반영
7+
- [x] Line 객체에 가변 인자를 받은 생성자를 추가
8+
- [x] LineGenerator가 generateLine에서 Line 객체를 리턴
9+
- [x] LineGeneratorTest의 너무 많은 가변인자 테스트 정리->보다 적은수로 경우의 수를 커버
10+
- [x] Ladder.java에 game Play에 대한 책임 부여 검토
11+
- [x] Line.java에 LEFT, RIGHT, PASS와 같은 상태값을 enum으로 구현
12+
- [x] Line의 index 원시값 포장
13+
- [x] LineGenerator에 현재값,이전값을 가지는 Point 객체 도입 검토
14+
15+
- [x] 사다리 실행 결과를 출력해야 한다.
16+
- [x] 개인별 이름을 입력하면 개인별 결과를 출력하고, "all"을 입력하면 전체 참여자의 실행 결과를 출력한다.
17+
18+
### 프로그래밍 요구사항
19+
- 자바 8의 스트림과 람다를 적용해 프로그래밍한다.
20+
- 규칙 6: 모든 엔티티를 작게 유지한다.
21+
- **규칙 7: 3개 이상의 인스턴스 변수를 가진 클래스를 쓰지 않는다.**
22+
23+
24+
325
## 2단계 - 사다리 생성
426
### TODO
27+
- [x] 사다리 실행 결과를 출력해야 한다.
28+
- [x] 개인별 이름을 입력하면 개인별 결과를 출력하고, "all"을 입력하면 전체 참여자의 실행 결과를 출력한다.
29+
30+
- [x] PR 반영
531
- [x] 사다리 게임에 참여하는 사람에 이름을 최대5글자까지 부여할 수 있다. 사다리를 출력할 때 사람 이름도 같이 출력한다.
632
- [x] 사람 이름은 쉼표(,)를 기준으로 구분한다.
733
- [x] 사람 이름을 5자 기준으로 출력하기 때문에 사다리 폭도 넓어져야 한다.
@@ -11,6 +37,7 @@
1137
### 프로그래밍 요구사항
1238
- 자바 8의 스트림과 람다를 적용해 프로그래밍한다.
1339
- 규칙 6: 모든 엔티티를 작게 유지한다.
40+
- **규칙 7: 3개 이상의 인스턴스 변수를 가진 클래스를 쓰지 않는다.**
1441

1542

1643
## 진행 방법
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,53 @@
11
package nextstep.ladder;
22

33
import nextstep.ladder.domain.ladder.Height;
4+
import nextstep.ladder.domain.ladder.Index;
45
import nextstep.ladder.domain.ladder.Ladder;
56
import nextstep.ladder.domain.generator.LadderGenerator;
7+
import nextstep.ladder.domain.name.Name;
68
import nextstep.ladder.domain.name.Names;
9+
import nextstep.ladder.domain.reward.Reward;
10+
import nextstep.ladder.domain.reward.Rewards;
711
import nextstep.ladder.view.InputView;
812
import nextstep.ladder.view.ResultView;
913

14+
import java.util.List;
15+
import java.util.stream.Collectors;
16+
1017
public class LadderGame {
1118

1219
public static void main(String[] args) {
1320
Names names = new Names(InputView.getNames());
1421
Height height = new Height(InputView.getHeight());
22+
Rewards rewards = new Rewards(InputView.getRewards());
1523

1624
Ladder ladder = LadderGenerator.generateLadder(names.count(), height);
1725

18-
ResultView.result(names, ladder);
26+
ResultView.ladderResult(names, ladder, rewards);
27+
28+
while(true) {
29+
String playerName = InputView.getPlayerName();
30+
31+
if (playerName.equalsIgnoreCase("quit")) {
32+
break;
33+
}
34+
35+
if (playerName.equalsIgnoreCase("all")) {
36+
List<Integer> rewardList = ladder.all().stream()
37+
.map(Index::value)
38+
.collect(Collectors.toList());
39+
ResultView.rewardResult(names, rewards, rewardList);
40+
continue;
41+
}
42+
43+
Name name = new Name(playerName);
44+
Index rewardIdx = ladder.resultOf(names.positionOf(name));
45+
try {
46+
Reward reward = rewards.get(rewardIdx.value());
47+
System.out.println(reward);
48+
} catch(IndexOutOfBoundsException e) {
49+
System.out.println("이름이 명단에 없습니다.");
50+
}
51+
}
1952
}
2053
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package nextstep.ladder.domain.common;
2+
3+
import java.util.List;
4+
5+
public abstract class AbstractValueList<T> {
6+
protected final List<T> values;
7+
8+
protected AbstractValueList(List<T> values) {
9+
if (values == null || values.isEmpty()) {
10+
throw new IllegalArgumentException("리스트는 비어 있을 수 없습니다.");
11+
}
12+
this.values = List.copyOf(values);
13+
}
14+
15+
public List<T> values() {
16+
return values;
17+
}
18+
19+
public int count() {
20+
return values.size();
21+
}
22+
23+
public T get(int index) {
24+
return values.get(index);
25+
}
26+
27+
public int positionOf(Object value) {
28+
if (value == null) {
29+
throw new IllegalArgumentException("찾는 값은 null일 수 없습니다.");
30+
}
31+
32+
return values.indexOf(value);
33+
}
34+
}

src/main/java/nextstep/ladder/domain/generator/CombinationGenerator.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/main/java/nextstep/ladder/domain/generator/LadderGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import nextstep.ladder.domain.ladder.Line;
66

77
public class LadderGenerator {
8-
public static Ladder generateLadder(int countOfPerson, Height height) {
9-
Ladder ladder = new Ladder();
8+
public static Ladder generateLadder(int countOfPeople, Height height) {
9+
Ladder ladder = new Ladder(countOfPeople);
1010
for (int i = 0; i < height.value(); i++) {
11-
Line line = new Line(countOfPerson);
11+
Line line = new Line(countOfPeople);
1212
ladder.add(line);
1313
}
1414

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package nextstep.ladder.domain.generator;
2+
3+
import nextstep.ladder.domain.ladder.Line;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.concurrent.ThreadLocalRandom;
8+
9+
public class LineGenerator {
10+
public static Line generateLine(int countOfPerson) {
11+
List<Boolean> line = new ArrayList<>();
12+
Point point = new Point(false, false);
13+
14+
for (int i = 0; i < countOfPerson - 1; i++) {
15+
boolean nextBoolean = randomBoolean();
16+
point = new Point(point.next(), nextBoolean);
17+
18+
if (point.isConsecutive()) {
19+
nextBoolean = false;
20+
point = new Point(point.current(), false);
21+
}
22+
23+
line.add(nextBoolean);
24+
}
25+
return new Line(line);
26+
}
27+
28+
public static void assertValidLine(List<Boolean> values) {
29+
if (values == null || values.isEmpty()) {
30+
throw new IllegalArgumentException("사다리에는 빈값이 올 수 없습니다. values=" + values);
31+
}
32+
33+
for(int i = 0; i < values.size() -1; i++) {
34+
Point point = new Point(values.get(i), values.get(i + 1));
35+
if(point.isConsecutive()) {
36+
throw new IllegalArgumentException("연속된 사다리 계단은 나올 수 없습니다.");
37+
}
38+
}
39+
}
40+
41+
private static boolean randomBoolean() {
42+
return ThreadLocalRandom.current().nextBoolean();
43+
}
44+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package nextstep.ladder.domain.generator;
2+
3+
public class Point {
4+
private final boolean current;
5+
private final boolean next;
6+
7+
public Point(boolean current, boolean next) {
8+
this.current = current;
9+
this.next = next;
10+
}
11+
12+
public boolean isConsecutive() {
13+
return current && next;
14+
}
15+
16+
public boolean current() {
17+
return current;
18+
}
19+
20+
public boolean next() {
21+
return next;
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package nextstep.ladder.domain.ladder;
2+
3+
public enum Direction {
4+
LEFT {
5+
@Override
6+
public Index move(Index index) {
7+
return index.decrement();
8+
}
9+
},
10+
RIGHT {
11+
@Override
12+
public Index move(Index index) {
13+
return index.increment();
14+
}
15+
},
16+
PASS {
17+
@Override
18+
public Index move(Index index) {
19+
return index;
20+
}
21+
};
22+
23+
public abstract Index move(Index index);
24+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package nextstep.ladder.domain.ladder;
2+
3+
public class Index {
4+
private final int value;
5+
private final int max;
6+
7+
public Index(int value, int max) {
8+
validate(value, max);
9+
this.value = value;
10+
this.max = max;
11+
}
12+
13+
private void validate(int value, int max) {
14+
if (value < 0 || value >= max) {
15+
throw new IllegalArgumentException("인덱스는 0 이상 " + (max - 1) + " 이하여야 합니다.");
16+
}
17+
}
18+
19+
public int value() {
20+
return value;
21+
}
22+
23+
public Index increment() {
24+
return new Index(value + 1, max);
25+
}
26+
27+
public Index decrement() {
28+
return new Index(value - 1, max);
29+
}
30+
31+
@Override
32+
public boolean equals(Object o) {
33+
if (this == o) return true;
34+
if (o == null || getClass() != o.getClass()) return false;
35+
Index index = (Index) o;
36+
return value == index.value && max == index.max;
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return value;
42+
}
43+
}

src/main/java/nextstep/ladder/domain/ladder/Ladder.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,35 @@
44
import java.util.List;
55

66
public class Ladder {
7-
List<Line> lineList;
7+
private final List<Line> lineList;
8+
private final int countOfPeople;
89

9-
public Ladder() {
10+
public Ladder(int countOfPeople) {
11+
this.countOfPeople = countOfPeople;
1012
this.lineList = new ArrayList<>();
1113
}
1214

13-
public List<Line> values() {
14-
return lineList;
15-
}
15+
public List<Line> values() {
16+
return lineList;
17+
}
18+
19+
public void add(Line line) {
20+
lineList.add(line);
21+
}
1622

17-
public void add(Line line) {
18-
lineList.add(line);
19-
}
23+
public Index resultOf(int position) {
24+
Index index = new Index(position, countOfPeople);
25+
for(Line line: values()) {
26+
index = line.movePerson(index);
27+
}
28+
return index;
29+
}
30+
31+
public List<Index> all() {
32+
List<Index> results = new ArrayList<>();
33+
for(int i = 0; i < countOfPeople; i++) {
34+
results.add(resultOf(i));
35+
}
36+
return results;
37+
}
2038
}

0 commit comments

Comments
 (0)