Skip to content

사다리게임 Step1 완료했습니다 #13

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

Merged
merged 4 commits into from
Nov 19, 2018
Merged
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
14 changes: 14 additions & 0 deletions src/main/java/ConsoleMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import generator.AutoLadderGenerator;
import generator.LadderGenerator;
import model.Ladder;
import view.InputView;
import view.ResultView;

public class ConsoleMain {
public static void main(String[] args) {
String participants = InputView.getParticipants();
String maxHight = InputView.getMaxHight();
Ladder ladder = new Ladder(participants, maxHight, new AutoLadderGenerator());
ResultView.printResult(ladder.getParticipants(), ladder.getLines());
}
}
24 changes: 24 additions & 0 deletions src/main/java/generator/AutoLadderGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package generator;

import model.Line;
import model.Participant;
import model.Positive;

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

public class AutoLadderGenerator implements LadderGenerator {
@Override
public List<Line> getLines(String height, int countOfPerson) {
ArrayList<Line> lines = new ArrayList<Line>();
for (int i = 0; i < Positive.of(height).getNum(); i++) {
lines.add(Line.of(Positive.of(countOfPerson).getNum()));
}

lines.forEach(Line::addAutoLines);

return lines;
}
}
11 changes: 11 additions & 0 deletions src/main/java/generator/LadderGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package generator;

import model.Line;
import model.Participant;

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

public interface LadderGenerator {
public List<Line> getLines(String height, int countOfPerson);
}
25 changes: 25 additions & 0 deletions src/main/java/model/Ladder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package model;

import generator.LadderGenerator;

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

public class Ladder {
private List<Line> lines = new ArrayList<Line>();
private List<Participant> participants = new ArrayList<Participant>();


public Ladder(String names, String height, LadderGenerator ladderGenerator) {
participants = Participant.getParticipants(names);
lines = ladderGenerator.getLines(height, participants.size());
}

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

public List<Participant> getParticipants() {
return participants;
}
}
163 changes: 163 additions & 0 deletions src/main/java/model/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;

public class Line {
private ArrayList<Boolean> points = new ArrayList<>();
private int countOfPerson;

private Line(int countOfPerson) {
this.countOfPerson = countOfPerson;

for (int i = 0; i < countOfPerson; i++) {
addPoint(false);
}
}

public static Line of(int countOfPerson, List<Positive> indexs) {
Line line = new Line(countOfPerson);
line.addAutoLines(indexs);

return line;
}

public static Line of(int countOfPerson) {
return new Line(countOfPerson);
}

public boolean hasLine(int index) {
if (index >= points.size()) {
return false;
}

return points.get(index);
}

/**
* 좌표 추가
*
* @param b
*/
public void addPoint(boolean b) {
if (points.size() >= countOfPerson) {
throw new IllegalStateException("좌표의 최대값은 인원 수를 초과할 수 없습니다.");
}

points.add(b);
}

/**
* 선들 추가
*
* @param indexs
*/
public void addAutoLines(List<Positive> indexs) {
for (Positive idx : indexs) {
addLine(idx);
}
}

/**
* 선 추가
*
* @param index
*/
public void addLine(Integer index) {
addLine(Positive.of(index));
}

/**
* 선 만듦
*
* @param index
*/
public void addLine(Positive index) {
if (!canAddLine(index)) {
throw new IllegalArgumentException("선 생성 불가");
}

points.set(index.getNum(), true);
}

/**
* 왼쪽 좌표에 선이 있는지 여부
*
* @param index
* @return
*/
public boolean hasLeftLine(Positive index) {
return hasLine(index.getNum() - 1);
}

/**
* 오른쪽 좌표에 선이 있는지 여부
*
* @param index
* @return
*/
public boolean hasRightPoint(Positive index) {
return hasLine(index.getNum() + 1);
}

/**
* 해당 좌표에 선을 추가할 수 있는지 여부
*
* @param index
* @return
*/
public boolean canAddLine(int index) {
return canAddLine(Positive.of(index));
}

/**
* 해당 좌표에 선을 추가할 수 있는지 여부 (양수)
*
* @param index
* @return
*/
public boolean canAddLine(Positive index) {
int num = index.getNum();
if (num >= points.size() - 1) {
return false;
}

if (hasRightPoint(index)) {
return false;
}

if (num > 0 && hasLeftLine(index)) {
return false;
}

return true;
}

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

/**
* 자동으로 선 추가
*/
public void addAutoLines() {
Random random = new Random();
IntStream.rangeClosed(0, points.size())
.filter(i -> isAddLine(random.nextBoolean(), i))
.forEach(this::addLine);
}

/**
* 선 추가할 건지 여부
*
* @param isAdd
* @param i
* @return
*/
private boolean isAddLine(Boolean isAdd, int i) {
return canAddLine(i) && isAdd;
}
}
37 changes: 37 additions & 0 deletions src/main/java/model/Participant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package model;

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

public class Participant {
private static final String COMMA = ",";
private static final int LIMIT = 5;
private String name;

public Participant(String name) {
if (name.length() > LIMIT) {
throw new IllegalArgumentException();
}
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public static List<Participant> getParticipants(String names) {
return Arrays.stream(split(names))
.map(Participant::new)
.collect(Collectors.toList());
}

public static String[] split(String participants) {

return participants.split(COMMA);
}
}
25 changes: 25 additions & 0 deletions src/main/java/model/Positive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package model;

public class Positive {
private int num;

private Positive(int num) {
if (num < 0) {
throw new IllegalArgumentException("양수를 입력하십시오.");
}

this.num = num;
}

public static Positive of(String num) {
return of(Integer.valueOf(num));
}

public static Positive of(int num) {
return new Positive(num);
}

public int getNum() {
return num;
}
}
24 changes: 24 additions & 0 deletions src/main/java/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package view;

import java.io.InputStream;
import java.util.Scanner;

public class InputView {
private static final Scanner SCANNER = new Scanner(System.in);

/**
* 참가자 가져오기
*/
public static String getParticipants() {
System.out.println("참여할 사람 이름을 입력하세요.");
return SCANNER.next();
}

/**
* 최대 사다리 높이 가져오기
*/
public static String getMaxHight() {
System.out.println("최대 사다리 높이는 몇 개인가요?");
return SCANNER.next();
}
}
Loading