Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
26 changes: 0 additions & 26 deletions src/main/java/Bridge.java

This file was deleted.

14 changes: 14 additions & 0 deletions src/main/java/LadderGame.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import data.InputData;
import domain.ladder.Ladder;
import domain.user.UserManage;
import view.InputView;
import view.OutputView;

public class LadderGame {

public static void main(String args[]) {
InputView inputView = new InputView(System.in);
InputData inputData = new InputData(inputView.participants(), inputView.ladderHeight());
Comment thread
jyami-kim marked this conversation as resolved.
Outdated
Ladder ladder = new Ladder(inputData);
UserManage userManage = new UserManage(inputData);
OutputView outputView = new OutputView(userManage, ladder);
outputView.printLadder();
}

}
3 changes: 0 additions & 3 deletions src/main/java/LinkedType.java

This file was deleted.

30 changes: 0 additions & 30 deletions src/main/java/Point.java

This file was deleted.

28 changes: 28 additions & 0 deletions src/main/java/domain/ladder/Bridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package domain.ladder;

public class Bridge {
private Integer location;
private LinkedType linkPillarDirection;

public Bridge(Integer location, LinkedType linkPillarDirection) {
this.location = location;
this.linkPillarDirection = linkPillarDirection;
}

public Integer getLocation() {
return location;
}

public LinkedType getLinkPillarDirection() {
return linkPillarDirection;
}

public static Bridge createOneRightBridge(Integer location) {
return (new Bridge(location, LinkedType.RIGHT));
}

public static Bridge createOneLeftBridge(Integer location) {
return (new Bridge(location, LinkedType.LEFT));
}

}
51 changes: 51 additions & 0 deletions src/main/java/domain/ladder/Ladder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package domain.ladder;

import dto.GameStartOption;

import java.util.ArrayList;
import java.util.List;

public class Ladder {

private List<Pillar> pillars;
private Integer width;
private Integer height;

public Ladder(GameStartOption gameStartOption) {
this.width = gameStartOption.getLadderWidth();
this.height = gameStartOption.getLadderHeight();
this.pillars = createLadder(gameStartOption);
}

private List<Pillar> createLadder(GameStartOption gameStartOption) {
List<Pillar> pillars = new ArrayList<>();
Pillar previous = new Pillar(gameStartOption, null); //팩토리 메소드 패턴 or construction 1개
pillars.add(previous);
for (int i = 1; i < width; i++) {
Pillar now = new Pillar(gameStartOption, previous);
pillars.add(now);
previous = now;
}
return pillars;
}

public Pillar getPillarByNum(Integer pillarNum) {
return pillars.stream()
.filter(p -> p.getPillarNum() == pillarNum)
.findFirst()
.orElseThrow(IllegalArgumentException :: new); // optional orElse에 null 던지지 말기 throw Exception
}

public List<Pillar> getPillars() {
return pillars;
}

public Integer getWidth() {
return width;
}

public Integer getHeight() {
return height;
}

}
5 changes: 5 additions & 0 deletions src/main/java/domain/ladder/LinkedType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package domain.ladder;

public enum LinkedType {
RIGHT, LEFT
}
92 changes: 92 additions & 0 deletions src/main/java/domain/ladder/Pillar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package domain.ladder;


import dto.GameStartOption;

import java.util.List;
import java.util.stream.Collectors;

import static util.RandomIntegerMaker.createRandomIntegers;
import static util.RandomIntegerMaker.createRandomIntegersWithRestriction;


public class Pillar {
Comment thread
jyami-kim marked this conversation as resolved.
public static final int MINIMUM_PILLAR_NUM = 0;
private List<Bridge> bridges;
private Integer pillarNum;

public Pillar(GameStartOption gameStartOption, Pillar previousPillar) { //자동으로 다리생성
pillarNum = nowPillarNum(previousPillar);
bridges = createBridges(gameStartOption, previousPillar);
}

public Integer getPillarNum() {
return pillarNum;
}

public List<Bridge> getBridges() {
return bridges;
}

private Integer nowPillarNum(Pillar previousPillar) {
if (previousPillar == null)
return MINIMUM_PILLAR_NUM;
return previousPillar.getPillarNum() + 1;
}

public List<Integer> getBridgesLocations() {
return bridges.stream()
.map(b -> b.getLocation())
.collect(Collectors.toList());
}

public List<Integer> getBridgesDirectionLocation(LinkedType linkedType) {
return bridges.stream()
.filter(b -> b.getLinkPillarDirection() == linkedType)
.map(bridge -> bridge.getLocation())
.collect(Collectors.toList());
}

private List<Bridge> createBridges(GameStartOption gameStartOption, Pillar previousPillar) {
if (isFirstPillar(previousPillar))
return createRightBridges(createRandomIntegers(gameStartOption.getLadderHeight()));
if (isLastPillar(gameStartOption, previousPillar))
return createLeftBridges(previousPillar);
return createLeftRightBridges(previousPillar, gameStartOption.getLadderHeight());
}


private boolean isLastPillar(GameStartOption gameStartOption, Pillar previousPillar) {
Comment thread
jyami-kim marked this conversation as resolved.
Outdated
if (gameStartOption.getLadderWidth() - previousPillar.getPillarNum() == 2)
return true;
return false;
}

private boolean isFirstPillar(Pillar previousPillar) {
Comment thread
jyami-kim marked this conversation as resolved.
Outdated
if (previousPillar == null)
return true;
return false;
}

private List<Bridge> createLeftRightBridges(Pillar previous, Integer height) {
Comment thread
jyami-kim marked this conversation as resolved.
Outdated
List<Integer> previousLocations = previous.getBridgesDirectionLocation(LinkedType.RIGHT);
Comment thread
jyami-kim marked this conversation as resolved.
Outdated
List<Bridge> nowPillarsBridges = createLeftBridges(previous);
List<Integer> rightBridgeNumbers = createRandomIntegersWithRestriction(height, previousLocations);
nowPillarsBridges.addAll(createRightBridges(rightBridgeNumbers));
return nowPillarsBridges;
}

private List<Bridge> createRightBridges(List<Integer> locationsOfBridge) {
return locationsOfBridge.stream()
.map(b -> Bridge.createOneRightBridge(b))
.collect(Collectors.toList());
}


private List<Bridge> createLeftBridges(Pillar previous) {
List<Integer> locationsOfBridge = previous.getBridgesDirectionLocation(LinkedType.RIGHT);
return locationsOfBridge.stream()
.map(b -> Bridge.createOneLeftBridge(b))
.collect(Collectors.toList());
}
}
36 changes: 36 additions & 0 deletions src/main/java/domain/user/ParticipantUsers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package domain.user;

import dto.GameStartOption;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static util.RandomIntegerMaker.separateUserName;

public class ParticipantUsers {
private List<User> users;

public ParticipantUsers(GameStartOption gameStartOption) {
this.users = createUser(gameStartOption.getParticipants());
}

public List<User> getUsers() {
return users;
}

private List<User> createUser(String users) {
return separateUserName(users).stream()
.map(u -> new User(u))
.collect(Collectors.toList());
}

public int getUserCharMaxNum() {
List<Integer> lengths = users.stream()
.map(u -> u.getName().length())
.collect(Collectors.toList());
return Collections.max(lengths);
}


}
18 changes: 18 additions & 0 deletions src/main/java/domain/user/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package domain.user;

public class User {

//user's PillarNum 갖고있게 하기,
//결과 반환하는것 만들기

private String name;

public User(String name) {
this.name = name;
}

public String getName() {
return name;
}

}
33 changes: 33 additions & 0 deletions src/main/java/dto/GameStartOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dto;

public class GameStartOption {

public static final String DELIMETER = ",";

private int ladderWidth;
private int ladderHeight;
private String participants;

public GameStartOption(String participants, int ladderHeight) {
this.ladderWidth = ladderWidth(participants);
this.ladderHeight = ladderHeight;
this.participants = participants;
}

private int ladderWidth(String participants) {
String[] parts = participants.split(DELIMETER);
return parts.length;
}

public String getParticipants() {
return participants;
}

public int getLadderWidth() {
return ladderWidth;
}

public int getLadderHeight() {
return ladderHeight;
}
}
45 changes: 45 additions & 0 deletions src/main/java/util/RandomIntegerMaker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package util;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static dto.GameStartOption.DELIMETER;

public class RandomIntegerMaker {

private RandomIntegerMaker() {

}

public static final int MIN_HEIGHT_INDEX = 0;

public static List<Integer> createRandomIntegers(Integer heights) {
List<Integer> randoms = IntStream.range(MIN_HEIGHT_INDEX, heights)
.mapToObj(Integer::new)
.collect(Collectors.toList());
Collections.shuffle(randoms);

return randoms.stream()
.limit(randInt(heights))
.collect(Collectors.toList());
}

public static List<Integer> createRandomIntegersWithRestriction(Integer heights, List<Integer> restrictions) {
List<Integer> randoms = createRandomIntegers(heights);
return randoms.stream()
.filter(i -> !restrictions.contains(i))
.collect(Collectors.toList());
}

public static List<String> separateUserName(String participants) {
String[] users = participants.split(DELIMETER);
return Arrays.asList(users);
}

public static Integer randInt(int bound) {
return (int) (Math.random() * bound + 1);
}
}
Loading