Skip to content
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

[사다리 미션] 박성빈 미션 제출합니다. #7

Open
wants to merge 4 commits into
base: imtotem
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.Random;

import controller.Controller;
import view.InputView;
import view.OutputView;

public class Application {
public static final InputView inputView = new InputView();
public static final OutputView outputView = new OutputView();
public static final Random random = new Random();

public static void main(String[] args) {
new Controller(inputView, outputView, random)
.start();
}
}
51 changes: 51 additions & 0 deletions src/main/java/controller/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package controller;

import java.util.List;
import java.util.Random;

import model.Ladder;
import model.Players;
import model.Result;
import view.InputView;
import view.OutputView;

public class Controller {

private final InputView inputView;
private final OutputView outputView;
private final Random random;

public Controller(InputView inputView, OutputView outputView, Random random) {
this.inputView = inputView;
this.outputView = outputView;
this.random = random;
}

public void start() {
List<String> names = inputView.getNames();
Players players = Players.from(names);

List<String> choices = inputView.getChoices();

int height = inputView.getHeight();

Ladder ladder = Ladder.of(players.size(), height, random);

players.move(ladder);

Result result = Result.of(players, choices);

outputView.outLadder(players, ladder, choices);
outResult(result);
}

private void outResult(Result result) {
String target = inputView.getName();

if (target.equalsIgnoreCase("all")) {
outputView.outAllResult(result);
} else {
outputView.outResult(target, result);
}
}
}
67 changes: 67 additions & 0 deletions src/main/java/model/Ladder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package model;

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

public class Ladder {
private final List<Line> lines;

private Ladder(List<Line> lines) {
this.lines = lines;
}

public List<Line> getLines() {
return lines;
}

public int size() {
return lines.size();
}

public boolean get(int x, int y) {
if (x < 0 || lines.size() - 1 <= x) {
return false;
}

return lines.get(y).getPoints().get(x);
}

public static Ladder of(int width, int height, Random random) {
List<Line> lines = generateLines(width, height, random);

while (!validate(width, lines)) {
lines = generateLines(width, height, random);
}

return new Ladder(lines);
}

private static List<Line> generateLines(int width, int height, Random random) {
List<Line> lines = new ArrayList<>();

for (int i = 1; i < height; i++) {
lines.add(Line.of(width, random));
}

return lines;
}

private static boolean validate(int width, List<Line> lines) {
boolean valid = true;
for (int i = 0; i < width - 1; i++) {
valid &= validateConnection(lines, i);
}

return valid;
}

private static boolean validateConnection(List<Line> lines, int index) {
int points = 0;
for (Line line : lines) {
points += Boolean.compare(line.getPoints().get(index), false);
}

return 0 < points;
}
}
40 changes: 40 additions & 0 deletions src/main/java/model/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package model;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Random;

public class Line {
private final List<Boolean> points;

private Line(List<Boolean> points) {
this.points = points;
}

public List<Boolean> getPoints() {
return points;
}

public static Line of(int width, Random random) {
Deque<Boolean> points = new ArrayDeque<>();
points.addLast(false);

while (points.size() < width) {
points.addLast(generateNext(points.peekLast(), random));
}

points.pollFirst();

return new Line(new ArrayList<>(points));
}

private static boolean generateNext(Boolean previous, Random random) {
if (Boolean.TRUE.equals(previous)) {
return false;
}

return random.nextBoolean();
}
}
43 changes: 43 additions & 0 deletions src/main/java/model/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package model;

public class Player {
private final String name;
private int x;
private int y;

private Player(String name, int x, int y) {
this.name = name;
this.x = x;
this.y = y;
}

public String getName() {
return name;
}

public int getX() {
return x;
}

public void move(Ladder ladder) {
while (y < ladder.size() - 1) {
moveX(ladder);
y++;
}
}

private void moveX(Ladder ladder) {
if (ladder.get(x-1, y)) {
x--;
return;
}

if (ladder.get(x, y)) {
x++;
}
}

public static Player of(String name, int x, int y) {
return new Player(name, x, y);
}
}
36 changes: 36 additions & 0 deletions src/main/java/model/Players.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package model;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class Players {
private final List<Player> players;

private Players(List<Player> players) {
this.players = players;
}

public Stream<Player> stream() {
return players.stream();
}

public int size() {
return players.size();
}

public void move(Ladder ladder) {
for (Player player : players) {
player.move(ladder);
}
}

public static Players from(List<String> names) {
List<Player> players = new ArrayList<>();
for (int i = 0; i < names.size(); i++) {
players.add(Player.of(names.get(i), i, 0));
}

return new Players(players);
}
}
36 changes: 36 additions & 0 deletions src/main/java/model/Result.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package model;

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

public class Result {
private final Map<String, String> result;

private Result(Map<String, String> result) {
this.result = result;
}

public int size() {
return result.size();
}

public Set<String> keys() {
return result.keySet();
}

public String get(String name) {
return result.get(name);
}

public static Result of(Players players, List<String> choices) {
Map<String, String> result = players.stream()
.collect(Collectors.toMap(
Player::getName,
player -> choices.get(player.getX())
));

return new Result(result);
}
}
57 changes: 57 additions & 0 deletions src/main/java/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package view;

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

public class InputView {

public List<String> getNames() {
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)");
List<String> names = getList();
System.out.println();
return names;
}

public List<String> getChoices() {
System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요");
List<String> choices = getList();
System.out.println();
return choices;
}

public int getWidth() {
System.out.println("사다리의 넓이는 몇 개인가요?");
int width = getInt();
System.out.println();
return width;
}

public int getHeight() {
System.out.println("사다리의 높이는 몇 개인가요?");
int height = getInt();
System.out.println();
return height;
}

public String getName() {
System.out.println("결과를 보고 싶은 사람은?");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println();

return name;
}

private int getInt() {
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}

private List<String> getList() {
Scanner scanner = new Scanner(System.in);
String list = scanner.nextLine();
return Arrays.stream(list.split(",")).collect(Collectors.toList());
}
}
Loading