Skip to content

Commit b65a6fb

Browse files
authored
고생하셨습니다.
🎉 PR 머지 완료! 🎉
1 parent fa13b03 commit b65a6fb

19 files changed

+551
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#사다리게임
2+
3+
##1단계
4+
- 4X4 크기의 사다리를 출력한다.
5+
- 가로라인이 겹치지 않게 랜덤으로 줄을 긋는다.
6+
7+
##2단계
8+
-사다리 넓이와 높이를 입력받아, 그 크기의 사다리를 출력한다.
9+
10+
##3단계
11+
-0부터 인덱스 번호를 부여해 사다리 타고 난 후 결과를 출력하낟.
12+
13+
##4단계
14+
-사다리 게임에 참여하는 사람에 이름을 최대 5글자까지 부여할 수 있다. 사다리를 출력할 때 사람 이름도 같이 출력한다.
15+
사람 이름은 쉼표(,)를 기준으로 구분한다.
16+
-개인별 이름을 입력하면 개인별 결과를 출력하고, "all"을 입력하면 전체 참여자의 실행 결과를 출력한다.
17+
18+
##5단계
19+
-리팩토링

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
id 'java'
3+
id 'application'
34
}
45

56
group = 'cholog'

src/main/java/.gitkeep

Whitespace-only changes.

src/main/java/Main.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import controller.LadderController;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
new LadderController().gameStart();
6+
}
7+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package controller;
2+
3+
import model.Ladder;
4+
import model.LadderFactory;
5+
import model.LadderGame;
6+
import model.LadderSize;
7+
import model.Line;
8+
import model.Participants;
9+
import model.Results;
10+
import model.BuildLine;
11+
import view.InputView;
12+
import view.OutputView;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
public class LadderController {
18+
private final LadderFactory factory = new LadderFactory();
19+
private final OutputView outputView = new OutputView();
20+
private final InputView inputView = new InputView();
21+
22+
public void gameStart() {
23+
Participants participants = new Participants(inputView.inputParticipants());
24+
int width = participants.size();
25+
26+
Results results;
27+
while (true) {
28+
results = new Results(inputView.inputResults());
29+
if (participants.size() == results.size()) {
30+
break;
31+
}
32+
outputView.printError("참여자 수와 결과 수가 일치하지 않습니다.");
33+
}
34+
35+
int height;
36+
while (true) {
37+
height = inputView.heightSize();
38+
if (height >= 1) {
39+
break;
40+
}
41+
outputView.printError("높이는 1이상이어야 합니다.");
42+
}
43+
44+
LadderSize size = new LadderSize(width, height);
45+
Ladder ladder = factory.create(size, width);
46+
47+
List<String> lines = new ArrayList<>();
48+
for (Line line : ladder.lines()) {
49+
lines.add(BuildLine.build(line));
50+
}
51+
52+
outputView.printLadder(participants.getNames(), lines, results.getValues());
53+
54+
LadderGame game = new LadderGame(ladder, participants.size());
55+
gameResult(game, participants, results);
56+
}
57+
58+
private void gameResult(LadderGame game, Participants participants, Results results) {
59+
while (true) {
60+
String queryName = inputView.inputQueryName();
61+
62+
if ("all".equals(queryName)) {
63+
outputView.printAllResults(game.playAll(participants, results));
64+
break;
65+
}
66+
67+
if (!participants.contains(queryName)) {
68+
outputView.printError("존재하지 않는 이름입니다.");
69+
continue;
70+
}
71+
72+
String result = game.getResult(queryName, participants, results);
73+
outputView.printSingleResult(queryName, result);
74+
}
75+
}
76+
}

src/main/java/model/BuildLine.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package model;
2+
3+
import java.util.Objects;
4+
5+
public final class BuildLine {
6+
7+
public static final int LADDER_LINE = 5;
8+
9+
public static String build(Line line) {
10+
Objects.requireNonNull(line);
11+
StringBuilder b = new StringBuilder();
12+
for (Point p : line.points()) {
13+
b.append("|");
14+
String fill = p.isConnected() ? "-".repeat(LADDER_LINE) : " ".repeat(LADDER_LINE);
15+
b.append(fill);
16+
}
17+
b.append("|");
18+
return b.toString();
19+
}
20+
}

src/main/java/model/Ladder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package model;
2+
3+
import java.util.List;
4+
import java.util.Objects;
5+
6+
public class Ladder {
7+
private final List<Line> lines;
8+
9+
public Ladder(List<Line> lines) {
10+
this.lines = Objects.requireNonNull(lines);
11+
}
12+
13+
public List<Line> lines() {
14+
return lines;
15+
}
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package model;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Random;
6+
7+
public class LadderFactory {
8+
private final Random random = new Random();
9+
10+
public Ladder create(LadderSize size, int width) {
11+
List<Line> lines = new ArrayList<>();
12+
for (int i = 0; i < size.height(); i++) {
13+
lines.add(createLine(width));
14+
}
15+
return new Ladder(lines);
16+
}
17+
18+
private Line createLine(int width) {
19+
int pointsCount = width - 1;
20+
List<Point> points = new ArrayList<>();
21+
boolean prevConnected = false;
22+
23+
for (int i = 0; i < pointsCount; i++) {
24+
boolean connect = false;
25+
if (!prevConnected) {
26+
connect = random.nextBoolean();
27+
}
28+
points.add(new Point(connect));
29+
prevConnected = connect;
30+
}
31+
return new Line(points);
32+
}
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package model;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
public class LadderGame {
8+
private final Ladder ladder;
9+
10+
public LadderGame(Ladder ladder, int width) {
11+
this.ladder = ladder;
12+
}
13+
14+
public Map<String, String> playAll(Participants participants, Results results) {
15+
Map<String, String> allResults = new LinkedHashMap<>();
16+
for (int i = 0; i < participants.size(); i++) {
17+
int finalPosition = play(i);
18+
allResults.put(participants.get(i), results.get(finalPosition));
19+
}
20+
return allResults;
21+
}
22+
23+
public String getResult(String name, Participants participants, Results results) {
24+
int startIndex = participants.indexOf(name);
25+
int finalPosition = play(startIndex);
26+
return results.get(finalPosition);
27+
}
28+
29+
public int play(int startPosition) {
30+
int currentPosition = startPosition;
31+
List<Line> lines = ladder.lines();
32+
33+
for (Line line : lines) {
34+
currentPosition = moveOnLine(currentPosition, line);
35+
}
36+
37+
return currentPosition;
38+
}
39+
40+
private int moveOnLine(int position, Line line) {
41+
List<Point> points = line.points();
42+
43+
if (position > 0 && points.get(position - 1).isConnected()) {
44+
return position - 1;
45+
}
46+
47+
if (position < points.size() && points.get(position).isConnected()) {
48+
return position + 1;
49+
}
50+
51+
return position;
52+
}
53+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package model;
2+
3+
public class LadderSize {
4+
private final int height;
5+
private final int width;
6+
7+
public LadderSize(int width, int height) {
8+
validateHeight(height);
9+
this.height = height;
10+
this.width = width;
11+
}
12+
13+
private void validateHeight(int height) {
14+
if (height <= 0) {
15+
throw new IllegalArgumentException("사다리 높이는 1 이상이어야 합니다.");
16+
}
17+
}
18+
19+
public int height() {
20+
return height;
21+
}
22+
}

0 commit comments

Comments
 (0)