Skip to content

사다리 게임 1 단계 구현입니다. #9

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 18, 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
21 changes: 21 additions & 0 deletions src/main/java/ConsoleMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import game.ladder.domain.Height;
import game.ladder.domain.Ladder;
import game.ladder.domain.LinesGenerator;
import game.ladder.domain.Participants;
import game.ladder.view.InputView;
import game.ladder.view.OutputView;

public class ConsoleMain {

public static void main(String[] args) {
Participants participants = InputView.readParticipant();

Height height = InputView.readHeight();

LinesGenerator linesGenerator = new LinesGenerator(height);
Ladder ladder = new Ladder(linesGenerator, participants);

OutputView.printLadder(ladder);
}

}
Empty file removed src/main/java/empty.txt
Empty file.
55 changes: 55 additions & 0 deletions src/main/java/game/ladder/domain/Block.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package game.ladder.domain;

import java.util.Random;

public class Block {

public static final Block EMPTY_BLOCK = new Block(BlockType.EMPTY);
public static final Block FILLED_BLOCK = new Block(BlockType.FILLED);

public static final int WIDTH = 5;

private static final Random RANDOM = new Random();

private final BlockType blockType;

private Block(BlockType blockType) {
this.blockType = blockType;
}

public static Block nextBlock(Block before) {
if (before == FILLED_BLOCK) {
return EMPTY_BLOCK;
}

if (RANDOM.nextBoolean()) {
return FILLED_BLOCK;
}

return EMPTY_BLOCK;
}

public BlockType getType() {
return this.blockType;
}

@Override
public String toString() {
return blockType.makeShapes(WIDTH);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Block block = (Block) o;

return blockType == block.blockType;
}

@Override
public int hashCode() {
return blockType.hashCode();
}
}
25 changes: 25 additions & 0 deletions src/main/java/game/ladder/domain/BlockType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package game.ladder.domain;

public enum BlockType {

EMPTY(" "), FILLED("-");

private String shape;

BlockType(String shape) {
this.shape = shape;
}

public String getShape() {
return this.shape;
}

public String makeShapes(int width) {
StringBuilder sb = new StringBuilder(width);
for (int i = 0; i < width; i++) {
sb.append(this.shape);
}
return sb.toString();
}

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

import spark.utils.Assert;

public class Height {
public static final int MIN = 0;
private final int value;

public Height(int value) {
Assert.isTrue(value > MIN, "높이는 0보다 큰 숫자여야 합니다.");
this.value = value;
}

public int getValue() {
return value;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Height height = (Height) o;

return value == height.value;
}

@Override
public int hashCode() {
return value;
}
}
22 changes: 22 additions & 0 deletions src/main/java/game/ladder/domain/Ladder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package game.ladder.domain;

public class Ladder {

private final Participants participants;
private final Lines lines;

public Ladder(LinesGenerator generator, Participants participants) {
this.participants = participants;
this.lines = generator.generate(participants.size());
}

public String makeStringLadder() {
StringBuilder sb = new StringBuilder();
sb.append(participants.getAllFormattedNames()).append(System.lineSeparator());
sb.append(lines.getStringLines()).append(System.lineSeparator());

return sb.toString();
}


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

import spark.utils.Assert;

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

public class Line {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굳이 이 객체가 필요한가?
Blocks == Line이 아닌가? 두 객체의 역할이 애매모호함.


private static final String DELIMITER = "|";
private static final int MIN_SIZE = 1;

private final List<Block> blocks;

public Line(int blockSize) {
Assert.isTrue(blockSize >= MIN_SIZE, "blockSize는 1 이상이여야 합니다.");
this.blocks = makeBlocks(blockSize);
}

private List<Block> makeBlocks(int blockSize) {
List<Block> list = new ArrayList<>(blockSize);
list.add(Block.EMPTY_BLOCK);

for (int i = 1; i < blockSize; i++) {
Block before = list.get(i - 1);
Block newBlock = Block.nextBlock(before);
list.add(newBlock);
}

return list;
}

public int getBlockSize() {
return this.blocks.size();
}

public String getStringLine() {
return this.blocks.stream().map(Block::toString).collect(Collectors.joining(DELIMITER)) + DELIMITER;
}
}
27 changes: 27 additions & 0 deletions src/main/java/game/ladder/domain/Lines.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package game.ladder.domain;

import java.util.List;

public class Lines {

private final List<Line> lines;
private final Height height;

public Lines(List<Line> lines) {
this.lines = lines;
this.height = new Height(lines.size());
}

public Height getHeight() {
return this.height;
}

public String getStringLines() {
StringBuilder sb = new StringBuilder();
for (Line line : lines) {
sb.append(line.getStringLine()).append(System.lineSeparator());
}
return sb.toString();
}

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

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

public class LinesGenerator {

private final Height height;

public LinesGenerator(Height height) {
this.height = height;
}

public Lines generate(final int blockSize) {

List<Line> lines = IntStream.range(0, blockSize)
.mapToObj(i -> new Line(blockSize))
.collect(Collectors.toList());

return new Lines(lines);
}

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

import spark.utils.Assert;
import spark.utils.StringUtils;

public class Name {
private static final int MIN_LENGTH = 5;
private static final String FORMAT = "%6s";

private final String value;

public Name(String value) {
Assert.isTrue(StringUtils.isNotBlank(value), "이름은 공백일 수 없습니다.");
Assert.isTrue(value.length() <= MIN_LENGTH, "이름은 5자를 넘길 수 없습니다.");
this.value = value;
}

public String getValue() {
return this.value;
}

public String fomattedName() {
return String.format(FORMAT, this.value);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Name name = (Name) o;

return value.equals(name.value);
}

@Override
public int hashCode() {
return value.hashCode();
}
}
47 changes: 47 additions & 0 deletions src/main/java/game/ladder/domain/Participant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package game.ladder.domain;

public class Participant implements Comparable<Participant>{

private final Name name;
private final Position position;

public Participant(Name name, Position position) {
this.name = name;
this.position = position;
}

public Name getName() {
return this.name;
}

public Position getPosition() {
return this.position;
}

public String formattedParticipantName() {
return this.name.fomattedName();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Participant that = (Participant) o;

if (!name.equals(that.name)) return false;
return position.equals(that.position);
}

@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + position.hashCode();
return result;
}

@Override
public int compareTo(Participant other) {
return this.position.compareTo(other.position);
}
}
25 changes: 25 additions & 0 deletions src/main/java/game/ladder/domain/Participants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package game.ladder.domain;

import spark.utils.Assert;

import java.util.SortedSet;

public class Participants {

private static final int MIN_PARTICIPANTS = 2;

private final SortedSet<Participant> participants;

public Participants(SortedSet<Participant> participants) {
Assert.isTrue(participants.size() >= MIN_PARTICIPANTS, "적어도 두 명 이상의 참여자가 필요합니다.");
this.participants = participants;
}

public int size() {
return this.participants.size();
}

public String getAllFormattedNames() {
return this.participants.stream().map(Participant::formattedParticipantName).reduce("", String::concat);
}
}
Loading