-
Notifications
You must be signed in to change notification settings - Fork 748
사다리 게임 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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
굳이 이 객체가 필요한가?
Blocks == Line이 아닌가? 두 객체의 역할이 애매모호함.