Skip to content

Commit 81cf9b4

Browse files
authored
Merge pull request #8 from mjung1798/master
kmj - 사다리게임
2 parents 022c735 + a41a8ee commit 81cf9b4

29 files changed

Lines changed: 1102 additions & 0 deletions

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ repositories {
1313

1414
dependencies {
1515
testCompile group: 'junit', name: 'junit', version: '4.12'
16+
testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
1617
}

src/main/java/LadderGame.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
1+
import domain.ladder.Ladder;
2+
import domain.ladderCalculator.LadderIO;
3+
import dto.GameStartOption;
4+
import view.InputView;
5+
import view.OutputLadder;
6+
17
public class LadderGame {
28

9+
public static void main(String args[]) {
10+
InputView inputView = InputView.of(System.in);
11+
GameStartOption gameStartOption = inputView.initGameStartOption();
12+
Ladder ladder = Ladder.of(gameStartOption);
13+
LadderIO ladderIO = LadderIO.of(gameStartOption);
14+
OutputLadder ladderPrint = OutputLadder.of(ladderIO, ladder);
15+
16+
ladderPrint.drawUserNames();
17+
ladderPrint.drawOutput();
18+
ladderPrint.drawResults();
19+
20+
while (true) {
21+
String ask = inputView.showResultUser();
22+
ladderPrint.showLadderResult(ask);
23+
}
24+
25+
}
326

427
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package domain.converter;
2+
3+
import domain.ladder.Ladder;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class OutputConverter {
9+
private List<Row> rows = new ArrayList<>();
10+
11+
private OutputConverter(Ladder ladder) {
12+
for (int location = 0; location < ladder.getHeight(); location++) {
13+
rows.add(Row.of(location, ladder));
14+
}
15+
}
16+
17+
public static OutputConverter of(Ladder ladder) {
18+
return new OutputConverter(ladder);
19+
}
20+
21+
public List<Row> getRows() {
22+
return new ArrayList<>(rows);
23+
}
24+
25+
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package domain.converter;
2+
3+
import domain.ladder.Bridge;
4+
5+
import java.util.Optional;
6+
7+
public class Point implements Comparable<Point> {
8+
private Integer pillarNum;
9+
private Bridge bridge;
10+
11+
private Point(Integer pillarNum, Bridge bridge) {
12+
this.pillarNum = pillarNum;
13+
this.bridge = bridge;
14+
}
15+
16+
private Point(Integer pillarNum) {
17+
this.pillarNum = pillarNum;
18+
}
19+
20+
public static Point of(Integer pillarNum, Optional<Bridge> bridge) {
21+
if (bridge.isPresent())
22+
return new Point(pillarNum, bridge.get());
23+
return new Point(pillarNum);
24+
}
25+
26+
public boolean isBlankPoint() {
27+
return bridge == null;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "pillarNum:" + pillarNum + "/" + "bridge:" + bridge.getLocation();
33+
}
34+
35+
public Integer getPillarNum() {
36+
return pillarNum;
37+
}
38+
39+
public Bridge getBridge() {
40+
return bridge;
41+
}
42+
43+
@Override
44+
public int compareTo(Point o) {
45+
return pillarNum.compareTo(o.pillarNum);
46+
}
47+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package domain.converter;
2+
3+
import domain.ladder.Ladder;
4+
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
public class Row implements Comparable<Row> {
9+
private Integer location;
10+
private List<Point> points;
11+
12+
private Row(Integer location, Ladder ladder) {
13+
this.location = location;
14+
this.points = findLocationBridge(ladder);
15+
}
16+
17+
public static Row of(Integer location, Ladder ladder) {
18+
return new Row(location, ladder);
19+
}
20+
21+
private List<Point> findLocationBridge(Ladder ladder) {
22+
return ladder.getPillars().stream()
23+
.map(pillar -> Point.of(pillar.getPillarNum(), pillar.getLevelBridges(location)))
24+
.collect(Collectors.toList());
25+
}
26+
27+
public Integer getLocation() {
28+
return location;
29+
}
30+
31+
public List<Point> getPoints() {
32+
return points;
33+
}
34+
35+
@Override
36+
public int compareTo(Row o) {
37+
return location.compareTo(o.location);
38+
}
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package domain.factory;
2+
3+
import domain.ladder.Bridge;
4+
import domain.ladder.Pillar;
5+
import domain.maker.PillarMaker;
6+
import dto.GameStartOption;
7+
8+
import java.util.List;
9+
10+
public class PillarFactory {
11+
12+
public static final int MINIMUM_PILLAR_NUM = 0;
13+
14+
public Pillar createFirstPillar (GameStartOption gameStartOption) {
15+
16+
List<Bridge> bridges = PillarMaker.of().createBridgesInThisPillar(gameStartOption);
17+
Integer pillarNum = MINIMUM_PILLAR_NUM;
18+
19+
return Pillar.of(bridges, pillarNum);
20+
}
21+
22+
public Pillar createNotFirstPillar (GameStartOption gameStartOption, Pillar previousPillar) {
23+
24+
List<Bridge> bridges = PillarMaker.of().createBridgesInThisPillar(gameStartOption, previousPillar);
25+
Integer pillarNum = nowPillarNum(previousPillar);
26+
27+
return Pillar.of(bridges, pillarNum);
28+
29+
}
30+
31+
private Integer nowPillarNum(Pillar previousPillar) {
32+
return previousPillar.getPillarNum() + 1;
33+
}
34+
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package domain.ladder;
2+
3+
public class Bridge {
4+
private Integer location;
5+
private LinkedType linkPillarDirection;
6+
7+
private Bridge(Integer location, LinkedType linkPillarDirection) {
8+
this.location = location;
9+
this.linkPillarDirection = linkPillarDirection;
10+
}
11+
12+
public static Bridge of(Integer location , LinkedType linkPillarDirection){
13+
return new Bridge(location, linkPillarDirection);
14+
}
15+
16+
public Integer getLocation() {
17+
return location;
18+
}
19+
20+
public LinkedType getLinkPillarDirection() {
21+
return linkPillarDirection;
22+
}
23+
24+
public static Bridge createOneRightBridge(Integer location) {
25+
return (new Bridge(location, LinkedType.RIGHT));
26+
}
27+
28+
public static Bridge createOneLeftBridge(Integer location) {
29+
return (new Bridge(location, LinkedType.LEFT));
30+
}
31+
32+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package domain.ladder;
2+
3+
import domain.maker.LadderMaker;
4+
import dto.GameStartOption;
5+
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
public class Ladder {
10+
11+
private List<Pillar> pillars;
12+
private Integer width;
13+
private Integer height;
14+
15+
private Ladder(GameStartOption gameStartOption) {
16+
this.width = gameStartOption.getLadderWidth();
17+
this.height = gameStartOption.getLadderHeight();
18+
this.pillars = LadderMaker.of().createLadder(gameStartOption);
19+
}
20+
21+
public static Ladder of(GameStartOption gameStartOption) {
22+
return new Ladder(gameStartOption);
23+
}
24+
25+
public Pillar getPillarByNum(Integer pillarNum) {
26+
return pillars.stream()
27+
.filter(p -> p.getPillarNum() == pillarNum)
28+
.findFirst()
29+
.orElseThrow(IllegalArgumentException::new); // optional orElse에 null 던지지 말기 throw Exception
30+
}
31+
32+
public List<Pillar> getPillars() {
33+
return pillars;
34+
}
35+
36+
public Integer getWidth() {
37+
return width;
38+
}
39+
40+
public Integer getHeight() {
41+
return height;
42+
}
43+
44+
public Pillar getNextPillar(Pillar nowPillar, Integer nowLocation) {
45+
Optional<Bridge> linkBridge = nowPillar.getLevelBridges(nowLocation);
46+
if (!linkBridge.isPresent())
47+
return nowPillar;
48+
return linkBridge.get().getLinkPillarDirection() == LinkedType.RIGHT ?
49+
getPillarByNum(nowPillar.getPillarNum() + 1) :
50+
getPillarByNum(nowPillar.getPillarNum() - 1);
51+
}
52+
53+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package domain.ladder;
2+
3+
public enum LinkedType {
4+
RIGHT, LEFT
5+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package domain.ladder;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Collectors;
6+
7+
public class Pillar {
8+
9+
private List<Bridge> bridges;
10+
private Integer pillarNum;
11+
12+
private Pillar(List<Bridge> bridges, Integer pillarNum) {
13+
this.bridges = bridges;
14+
this.pillarNum = pillarNum;
15+
}
16+
17+
public static Pillar of(List<Bridge> bridges, Integer pillarNum) {
18+
return new Pillar(bridges, pillarNum);
19+
}
20+
21+
public Integer getPillarNum() {
22+
return pillarNum;
23+
}
24+
25+
public List<Bridge> getBridges() {
26+
return bridges;
27+
}
28+
29+
public Optional<Bridge> getLevelBridges(Integer level) {
30+
return bridges.stream()
31+
.filter(bridge -> bridge.getLocation().equals(level))
32+
.findFirst();
33+
}
34+
35+
public List<Integer> getBridgesLocations() {
36+
return bridges.stream()
37+
.map(b -> b.getLocation())
38+
.collect(Collectors.toList());
39+
}
40+
41+
public List<Integer> getBridgesDirectionLocation(LinkedType linkedType) {
42+
return bridges.stream()
43+
.filter(b -> b.getLinkPillarDirection() == linkedType)
44+
.map(bridge -> bridge.getLocation())
45+
.collect(Collectors.toList());
46+
}
47+
}

0 commit comments

Comments
 (0)