From b48b9b38181b13b02a28591e541a908448758426 Mon Sep 17 00:00:00 2001 From: pporotoss Date: Fri, 16 Nov 2018 01:28:24 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=20=EB=B0=9B=EA=B8=B0=20=EA=B9=8C=EC=A7=80=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ConsoleMain.java | 17 ++++++ src/main/java/empty.txt | 0 src/main/java/game/ladder/domain/Block.java | 29 +++++++++ .../java/game/ladder/domain/BlockType.java | 25 ++++++++ src/main/java/game/ladder/domain/Line.java | 15 +++++ .../java/game/ladder/domain/Participant.java | 39 ++++++++++++ .../java/game/ladder/domain/Position.java | 43 +++++++++++++ src/main/java/game/ladder/util/Spliter.java | 15 +++++ src/main/java/game/ladder/view/InputView.java | 25 ++++++++ .../java/game/ladder/domain/BlockTest.java | 28 +++++++++ .../java/game/ladder/domain/LineTest.java | 16 +++++ .../game/ladder/domain/ParticipantTest.java | 60 +++++++++++++++++++ .../java/game/ladder/domain/PositionTest.java | 51 ++++++++++++++++ .../java/game/ladder/util/SpliterTest.java | 46 ++++++++++++++ 14 files changed, 409 insertions(+) create mode 100644 src/main/java/ConsoleMain.java delete mode 100644 src/main/java/empty.txt create mode 100644 src/main/java/game/ladder/domain/Block.java create mode 100644 src/main/java/game/ladder/domain/BlockType.java create mode 100644 src/main/java/game/ladder/domain/Line.java create mode 100644 src/main/java/game/ladder/domain/Participant.java create mode 100644 src/main/java/game/ladder/domain/Position.java create mode 100644 src/main/java/game/ladder/util/Spliter.java create mode 100644 src/main/java/game/ladder/view/InputView.java create mode 100644 src/test/java/game/ladder/domain/BlockTest.java create mode 100644 src/test/java/game/ladder/domain/LineTest.java create mode 100644 src/test/java/game/ladder/domain/ParticipantTest.java create mode 100644 src/test/java/game/ladder/domain/PositionTest.java create mode 100644 src/test/java/game/ladder/util/SpliterTest.java diff --git a/src/main/java/ConsoleMain.java b/src/main/java/ConsoleMain.java new file mode 100644 index 0000000000..fd6ce3d2fd --- /dev/null +++ b/src/main/java/ConsoleMain.java @@ -0,0 +1,17 @@ +import game.ladder.domain.Participant; +import game.ladder.view.InputView; + +import java.util.List; + +public class ConsoleMain { + + public static void main(String[] args) { + // TODO 참여자 입력 + List participants = InputView.readParticipant(); + + // TODO 사다리 높이 입력 + + // TODO 사다리 출력 + } + +} diff --git a/src/main/java/empty.txt b/src/main/java/empty.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/main/java/game/ladder/domain/Block.java b/src/main/java/game/ladder/domain/Block.java new file mode 100644 index 0000000000..7b19cb9b58 --- /dev/null +++ b/src/main/java/game/ladder/domain/Block.java @@ -0,0 +1,29 @@ +package game.ladder.domain; + +public class Block { + + public static final int WIDTH = 5; + + private final BlockType blockType; + + private Block(BlockType blockType) { + this.blockType = blockType; + } + + public static Block emptyBlock() { + return new Block(BlockType.EMPTY); + } + + public static Block filledBlock() { + return new Block(BlockType.FILLED); + } + + public BlockType getType() { + return this.blockType; + } + + @Override + public String toString() { + return blockType.makeShapes(WIDTH); + } +} diff --git a/src/main/java/game/ladder/domain/BlockType.java b/src/main/java/game/ladder/domain/BlockType.java new file mode 100644 index 0000000000..6ce6d58b5c --- /dev/null +++ b/src/main/java/game/ladder/domain/BlockType.java @@ -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(); + } + +} diff --git a/src/main/java/game/ladder/domain/Line.java b/src/main/java/game/ladder/domain/Line.java new file mode 100644 index 0000000000..bda41c1884 --- /dev/null +++ b/src/main/java/game/ladder/domain/Line.java @@ -0,0 +1,15 @@ +package game.ladder.domain; + +public class Line { + + + private final int blockSize; + + public Line(int blockSize) { + this.blockSize = blockSize; + } + + public int getBlockSize() { + return 0; + } +} diff --git a/src/main/java/game/ladder/domain/Participant.java b/src/main/java/game/ladder/domain/Participant.java new file mode 100644 index 0000000000..e3e74f6b16 --- /dev/null +++ b/src/main/java/game/ladder/domain/Participant.java @@ -0,0 +1,39 @@ +package game.ladder.domain; + +import spark.utils.Assert; +import spark.utils.StringUtils; + +public class Participant { + + private final String name; + private final Position position; + + public Participant(String name, Position position) { + Assert.isTrue(StringUtils.isNotBlank(name), "name은 공백일 수 없습니다."); + + this.name = name; + this.position = position; + } + + public String getName() { + return this.name; + } + + @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; + } +} diff --git a/src/main/java/game/ladder/domain/Position.java b/src/main/java/game/ladder/domain/Position.java new file mode 100644 index 0000000000..3b9914bc6f --- /dev/null +++ b/src/main/java/game/ladder/domain/Position.java @@ -0,0 +1,43 @@ +package game.ladder.domain; + +import spark.utils.Assert; + +public class Position implements Comparable { + + public static final int MIN_VALUE = 1; + + private final int value; + + public Position(int value) { + Assert.isTrue(isNotLowerThanMinValue(value), String.format("value의 크기는 %d 보다 작을 수 없습니다.", MIN_VALUE)); + this.value = value; + } + + private boolean isNotLowerThanMinValue(int value) { + return value >= MIN_VALUE; + } + + @Override + public int compareTo(Position other) { + return Integer.compare(this.value, other.value); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Position position = (Position) o; + + return value == position.value; + } + + @Override + public int hashCode() { + return value; + } + + public int getValue() { + return this.value; + } +} diff --git a/src/main/java/game/ladder/util/Spliter.java b/src/main/java/game/ladder/util/Spliter.java new file mode 100644 index 0000000000..545d81a20b --- /dev/null +++ b/src/main/java/game/ladder/util/Spliter.java @@ -0,0 +1,15 @@ +package game.ladder.util; + +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; + +public final class Spliter { + + public static final Pattern SPLIT_PATTERN = Pattern.compile("\\s*,\\s*"); + + public static List split(String str) { + return Arrays.asList(SPLIT_PATTERN.split(str)); + } + +} diff --git a/src/main/java/game/ladder/view/InputView.java b/src/main/java/game/ladder/view/InputView.java new file mode 100644 index 0000000000..109d332d34 --- /dev/null +++ b/src/main/java/game/ladder/view/InputView.java @@ -0,0 +1,25 @@ +package game.ladder.view; + +import game.ladder.domain.Participant; +import game.ladder.domain.Position; +import game.ladder.util.Spliter; + +import java.util.List; +import java.util.Scanner; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class InputView { + + private static final Scanner scanner = new Scanner(System.in); + + public static List readParticipant() { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + final List names = Spliter.split(scanner.nextLine()); + + return IntStream.range(0, names.size()) + .mapToObj(i -> new Participant(names.get(i), new Position(i + 1))) + .collect(Collectors.toList()); + } + +} diff --git a/src/test/java/game/ladder/domain/BlockTest.java b/src/test/java/game/ladder/domain/BlockTest.java new file mode 100644 index 0000000000..a4b6cc326b --- /dev/null +++ b/src/test/java/game/ladder/domain/BlockTest.java @@ -0,0 +1,28 @@ +package game.ladder.domain; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class BlockTest { + + @Test + public void 만들기_비어있는_블록() { + final Block block = Block.emptyBlock(); + final String expectedShapes = BlockType.EMPTY.makeShapes(Block.WIDTH); + + assertThat(block.getType()).isSameAs(BlockType.EMPTY); + assertThat(block.toString()).isEqualTo(expectedShapes); + } + + @Test + public void 만들기_채워진_블록() { + final Block block = Block.filledBlock(); + final String expectedShapes = BlockType.FILLED.makeShapes(Block.WIDTH); + + assertThat(block.getType()).isSameAs(BlockType.FILLED); + assertThat(block.toString()).isEqualTo(expectedShapes); + } + + +} \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/LineTest.java b/src/test/java/game/ladder/domain/LineTest.java new file mode 100644 index 0000000000..e9b8f48564 --- /dev/null +++ b/src/test/java/game/ladder/domain/LineTest.java @@ -0,0 +1,16 @@ +package game.ladder.domain; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LineTest { + + @Test + public void 만들기() { + final int blockSize = 3; + Line line = new Line(blockSize); + assertThat(line.getBlockSize()).isEqualTo(blockSize); + } + +} \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/ParticipantTest.java b/src/test/java/game/ladder/domain/ParticipantTest.java new file mode 100644 index 0000000000..88627887ee --- /dev/null +++ b/src/test/java/game/ladder/domain/ParticipantTest.java @@ -0,0 +1,60 @@ +package game.ladder.domain; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ParticipantTest { + + @Test + public void 참여자_만들기() { + final String name = "name"; + final int value = 2; + Participant participant = new Participant(name, new Position(value)); + + assertThat(participant.getName()).isEqualTo(name); + } + + @Test(expected = IllegalArgumentException.class) + public void 참여자_만들기_공백_이름() { + final String name = ""; + new Participant(name, new Position(2)); + } + + @Test(expected = IllegalArgumentException.class) + public void 참여자_만들기_null_이름() { + final String name = null; + new Participant(name, new Position(3)); + } + + @Test + public void 참여자_동등성_확인() { + final String name = "test"; + final Position position = new Position(1); + + final Participant participant1 = new Participant(name, position); + final Participant participant2 = new Participant(name, position); + + assertThat(participant1).isEqualTo(participant2); + } + + @Test + public void 참여자_동등성_확인_이름_다를때() { + final Position position = new Position(1); + + final Participant participant1 = new Participant("a", position); + final Participant participant2 = new Participant("b", position); + + assertThat(participant1).isNotEqualTo(participant2); + } + + @Test + public void 참여자_동등성_확인_포지션_다를때() { + final String name = "name"; + + final Participant participant1 = new Participant(name, new Position(3)); + final Participant participant2 = new Participant(name, new Position(4)); + + assertThat(participant1).isNotEqualTo(participant2); + } +} \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/PositionTest.java b/src/test/java/game/ladder/domain/PositionTest.java new file mode 100644 index 0000000000..6e44ee5ff7 --- /dev/null +++ b/src/test/java/game/ladder/domain/PositionTest.java @@ -0,0 +1,51 @@ +package game.ladder.domain; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PositionTest { + + @Test + public void 만들기() { + final int expected = 2; + Position position = new Position(expected); + + assertThat(position.getValue()).isEqualTo(expected); + } + + @Test(expected = IllegalArgumentException.class) + public void MIN_POSITION_보다_작은값_생성() { + final int lower = Position.MIN_VALUE - 1; + + new Position(lower); + } + + @Test + public void 동등성_테스트() { + final int value = 3; + + final Position position1 = new Position(value); + final Position position2 = new Position(value); + + assertThat(position1).isEqualTo(position2); + } + + @Test + public void 정렬하기() { + final Position position1 = new Position(1); + final Position position2 = new Position(2); + final Position position3 = new Position(3); + + List positions = Arrays.asList(position3, position1, position2); + + Collections.sort(positions); + + assertThat(positions).containsSequence(position1, position2, position3); + } + +} \ No newline at end of file diff --git a/src/test/java/game/ladder/util/SpliterTest.java b/src/test/java/game/ladder/util/SpliterTest.java new file mode 100644 index 0000000000..cffdda3580 --- /dev/null +++ b/src/test/java/game/ladder/util/SpliterTest.java @@ -0,0 +1,46 @@ +package game.ladder.util; + +import org.junit.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SpliterTest { + + @Test + public void 붙어있는_콤마() { + String line = "a,b,c,d"; + + List split = Spliter.split(line); + + assertThat(split).hasSize(4).containsSequence("a","b","c","d"); + } + + @Test + public void 콤마_뒤만_떨어져_있을때() { + String line = "a, b, c, d"; + + List split = Spliter.split(line); + + assertThat(split).hasSize(4).containsSequence("a","b","c","d"); + } + + @Test + public void 콤마_앞에만_떨어져_있을때() { + String line = "a ,b ,c ,d"; + + List split = Spliter.split(line); + + assertThat(split).hasSize(4).containsSequence("a","b","c","d"); + } + + @Test + public void 불규칙하게_떨어져_있을때() { + String line = "a , b ,c, d"; + + List split = Spliter.split(line); + + assertThat(split).hasSize(4).containsSequence("a","b","c","d"); + } +} \ No newline at end of file From e5c2bebcb6db2307ff72f4085c6985c5d2eae633 Mon Sep 17 00:00:00 2001 From: pporotoss Date: Fri, 16 Nov 2018 02:38:56 +0900 Subject: [PATCH 2/4] =?UTF-8?q?Line=ED=81=B4=EB=9E=98=EC=8A=A4=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=EA=B9=8C=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ConsoleMain.java | 4 +++ src/main/java/game/ladder/domain/Block.java | 30 ++++++++++++---- .../java/game/ladder/domain/BlockFactory.java | 16 +++++++++ src/main/java/game/ladder/domain/Line.java | 36 ++++++++++++++++--- src/main/java/game/ladder/view/InputView.java | 4 +++ .../java/game/ladder/domain/BlockTest.java | 35 ++++++++++++++++-- .../java/game/ladder/domain/LineTest.java | 23 ++++++++++++ 7 files changed, 136 insertions(+), 12 deletions(-) create mode 100644 src/main/java/game/ladder/domain/BlockFactory.java diff --git a/src/main/java/ConsoleMain.java b/src/main/java/ConsoleMain.java index fd6ce3d2fd..cd18c22e47 100644 --- a/src/main/java/ConsoleMain.java +++ b/src/main/java/ConsoleMain.java @@ -10,6 +10,10 @@ public static void main(String[] args) { List participants = InputView.readParticipant(); // TODO 사다리 높이 입력 + int height = InputView.readHeight(); + + // TODO 사다리 만들기 + // TODO 사다리 출력 } diff --git a/src/main/java/game/ladder/domain/Block.java b/src/main/java/game/ladder/domain/Block.java index 7b19cb9b58..817854b559 100644 --- a/src/main/java/game/ladder/domain/Block.java +++ b/src/main/java/game/ladder/domain/Block.java @@ -2,6 +2,9 @@ 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 final BlockType blockType; @@ -10,20 +13,35 @@ private Block(BlockType blockType) { this.blockType = blockType; } - public static Block emptyBlock() { - return new Block(BlockType.EMPTY); + public BlockType getType() { + return this.blockType; } - public static Block filledBlock() { - return new Block(BlockType.FILLED); + public boolean isSequenceFilledBlock(Block other) { + return this.isFilledBlock() && this.equals(other); } - public BlockType getType() { - return this.blockType; + private boolean isFilledBlock() { + return this.equals(FILLED_BLOCK); } @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(); + } } diff --git a/src/main/java/game/ladder/domain/BlockFactory.java b/src/main/java/game/ladder/domain/BlockFactory.java new file mode 100644 index 0000000000..d7aa18f703 --- /dev/null +++ b/src/main/java/game/ladder/domain/BlockFactory.java @@ -0,0 +1,16 @@ +package game.ladder.domain; + +import java.util.Random; + +public class BlockFactory { + private static final Random RANDOM = new Random(); + + public static Block generateRandomBlock() { + if (RANDOM.nextBoolean()) { + return Block.FILLED_BLOCK; + } + + return Block.EMPTY_BLOCK; + } + +} diff --git a/src/main/java/game/ladder/domain/Line.java b/src/main/java/game/ladder/domain/Line.java index bda41c1884..016a8f2546 100644 --- a/src/main/java/game/ladder/domain/Line.java +++ b/src/main/java/game/ladder/domain/Line.java @@ -1,15 +1,43 @@ package game.ladder.domain; -public class Line { +import spark.utils.Assert; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +public class Line { - private final int blockSize; + private final List blocks; public Line(int blockSize) { - this.blockSize = blockSize; + Assert.isTrue(isNotLowerThanOne(blockSize), "blockSize는 1 이상이여야 합니다."); + this.blocks = new ArrayList<>(blockSize); + makeBlocks(blockSize); + } + + private boolean isNotLowerThanOne(int blockSize) { + return blockSize >= 1; + } + + private void makeBlocks(int blockSize) { + this.blocks.add(BlockFactory.generateRandomBlock()); + for (int i = 1; i < blockSize; i++) { + Block before = this.blocks.get(i - 1); + Block newBlock = BlockFactory.generateRandomBlock(); + if (before.isSequenceFilledBlock(newBlock)) { + i--; + continue; + } + this.blocks.add(newBlock); + } } public int getBlockSize() { - return 0; + return this.blocks.size(); + } + + public List getBlocks() { + return Collections.unmodifiableList(this.blocks); } } diff --git a/src/main/java/game/ladder/view/InputView.java b/src/main/java/game/ladder/view/InputView.java index 109d332d34..b876d96979 100644 --- a/src/main/java/game/ladder/view/InputView.java +++ b/src/main/java/game/ladder/view/InputView.java @@ -22,4 +22,8 @@ public static List readParticipant() { .collect(Collectors.toList()); } + public static int readHeight() { + System.out.println("최대 사다리 높이는 몇 개인가요?"); + return Integer.parseInt(scanner.nextLine().trim()); + } } diff --git a/src/test/java/game/ladder/domain/BlockTest.java b/src/test/java/game/ladder/domain/BlockTest.java index a4b6cc326b..dedcd3f504 100644 --- a/src/test/java/game/ladder/domain/BlockTest.java +++ b/src/test/java/game/ladder/domain/BlockTest.java @@ -3,12 +3,13 @@ import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.filter; public class BlockTest { @Test public void 만들기_비어있는_블록() { - final Block block = Block.emptyBlock(); + final Block block = Block.EMPTY_BLOCK; final String expectedShapes = BlockType.EMPTY.makeShapes(Block.WIDTH); assertThat(block.getType()).isSameAs(BlockType.EMPTY); @@ -17,12 +18,42 @@ public class BlockTest { @Test public void 만들기_채워진_블록() { - final Block block = Block.filledBlock(); + final Block block = Block.FILLED_BLOCK; final String expectedShapes = BlockType.FILLED.makeShapes(Block.WIDTH); assertThat(block.getType()).isSameAs(BlockType.FILLED); assertThat(block.toString()).isEqualTo(expectedShapes); } + @Test + public void 동일성_테스트() { + final Block block1 = Block.EMPTY_BLOCK; + final Block block2 = Block.EMPTY_BLOCK; + + assertThat(block1).isSameAs(block2); + } + + @Test + public void equals_동등하지_않음() { + final Block block = Block.FILLED_BLOCK; + final Block other = Block.EMPTY_BLOCK; + assertThat(block.equals(other)).isFalse(); + } + @Test + public void equals_동등함() { + final Block block = Block.EMPTY_BLOCK; + final Block other = Block.EMPTY_BLOCK; + assertThat(block.equals(other)).isTrue(); + } + + @Test + public void 연속_채워진_블록_확인() { + final Block before = Block.FILLED_BLOCK; + final Block afterFilled = Block.FILLED_BLOCK; + final Block afterEmpty = Block.EMPTY_BLOCK; + + assertThat(before.isSequenceFilledBlock(afterFilled)).isTrue(); + assertThat(before.isSequenceFilledBlock(afterEmpty)).isFalse(); + } } \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/LineTest.java b/src/test/java/game/ladder/domain/LineTest.java index e9b8f48564..201259930b 100644 --- a/src/test/java/game/ladder/domain/LineTest.java +++ b/src/test/java/game/ladder/domain/LineTest.java @@ -2,6 +2,8 @@ import org.junit.Test; +import java.util.List; + import static org.assertj.core.api.Assertions.assertThat; public class LineTest { @@ -13,4 +15,25 @@ public class LineTest { assertThat(line.getBlockSize()).isEqualTo(blockSize); } + @Test(expected = IllegalArgumentException.class) + public void 만들기_0넣을때() { + final int blockSize = 0; + + new Line(blockSize); + } + + @Test + public void 만들어진_블럭들_연속_채운블록_존재여부() { + final int blockSize = 10; + + Line line = new Line(blockSize); + + List blocks = line.getBlocks(); + for (int i = 1; i < blockSize; i++) { + Block before = blocks.get(i - 1); + Block current = blocks.get(i); + assertThat(before.isSequenceFilledBlock(current)).as(String.format("before : %d, after : %d", i - 1, i)).isFalse(); + } + } + } \ No newline at end of file From 4a8affb255cd18428c18b22e451723d1280f8e32 Mon Sep 17 00:00:00 2001 From: pporotoss Date: Sat, 17 Nov 2018 23:27:30 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=EC=82=AC=EB=8B=A4=EB=A6=AC=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=201=EB=8B=A8=EA=B3=84=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ConsoleMain.java | 20 +++---- src/main/java/game/ladder/domain/Blocks.java | 47 ++++++++++++++++ src/main/java/game/ladder/domain/Height.java | 32 +++++++++++ src/main/java/game/ladder/domain/Ladder.java | 22 ++++++++ src/main/java/game/ladder/domain/Line.java | 33 ++---------- src/main/java/game/ladder/domain/Lines.java | 27 ++++++++++ .../game/ladder/domain/LinesGenerator.java | 24 +++++++++ src/main/java/game/ladder/domain/Name.java | 40 ++++++++++++++ .../java/game/ladder/domain/Participant.java | 26 +++++---- .../java/game/ladder/domain/Participants.java | 25 +++++++++ src/main/java/game/ladder/view/InputView.java | 21 ++++---- .../java/game/ladder/view/OutputView.java | 9 ++++ .../java/game/ladder/domain/BlocksTest.java | 31 +++++++++++ .../java/game/ladder/domain/HeightTest.java | 41 ++++++++++++++ .../java/game/ladder/domain/LadderTest.java | 25 +++++++++ .../java/game/ladder/domain/LineTest.java | 14 ----- .../ladder/domain/LinesGeneratorTest.java | 22 ++++++++ .../java/game/ladder/domain/LinesTest.java | 20 +++++++ .../java/game/ladder/domain/NameTest.java | 38 +++++++++++++ .../game/ladder/domain/ParticipantTest.java | 53 ++++++++++++------- .../game/ladder/domain/ParticipantsTest.java | 38 +++++++++++++ 21 files changed, 516 insertions(+), 92 deletions(-) create mode 100644 src/main/java/game/ladder/domain/Blocks.java create mode 100644 src/main/java/game/ladder/domain/Height.java create mode 100644 src/main/java/game/ladder/domain/Ladder.java create mode 100644 src/main/java/game/ladder/domain/Lines.java create mode 100644 src/main/java/game/ladder/domain/LinesGenerator.java create mode 100644 src/main/java/game/ladder/domain/Name.java create mode 100644 src/main/java/game/ladder/domain/Participants.java create mode 100644 src/main/java/game/ladder/view/OutputView.java create mode 100644 src/test/java/game/ladder/domain/BlocksTest.java create mode 100644 src/test/java/game/ladder/domain/HeightTest.java create mode 100644 src/test/java/game/ladder/domain/LadderTest.java create mode 100644 src/test/java/game/ladder/domain/LinesGeneratorTest.java create mode 100644 src/test/java/game/ladder/domain/LinesTest.java create mode 100644 src/test/java/game/ladder/domain/NameTest.java create mode 100644 src/test/java/game/ladder/domain/ParticipantsTest.java diff --git a/src/main/java/ConsoleMain.java b/src/main/java/ConsoleMain.java index cd18c22e47..ba8eb94ef2 100644 --- a/src/main/java/ConsoleMain.java +++ b/src/main/java/ConsoleMain.java @@ -1,21 +1,21 @@ -import game.ladder.domain.Participant; +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 java.util.List; +import game.ladder.view.OutputView; public class ConsoleMain { public static void main(String[] args) { - // TODO 참여자 입력 - List participants = InputView.readParticipant(); - - // TODO 사다리 높이 입력 - int height = InputView.readHeight(); + Participants participants = InputView.readParticipant(); - // TODO 사다리 만들기 + Height height = InputView.readHeight(); + LinesGenerator linesGenerator = new LinesGenerator(height); + Ladder ladder = new Ladder(linesGenerator, participants); - // TODO 사다리 출력 + OutputView.printLadder(ladder); } } diff --git a/src/main/java/game/ladder/domain/Blocks.java b/src/main/java/game/ladder/domain/Blocks.java new file mode 100644 index 0000000000..604ab21e72 --- /dev/null +++ b/src/main/java/game/ladder/domain/Blocks.java @@ -0,0 +1,47 @@ +package game.ladder.domain; + +import spark.utils.Assert; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class Blocks { + + private static final String DELIMITER = "|"; + private static final int MIN_SIZE = 1; + + private final List blocks; + + public Blocks(int blockSize) { + Assert.isTrue(blockSize >= MIN_SIZE, "blockSize는 1 이상이여야 합니다."); + this.blocks = new ArrayList<>(blockSize); + initBlocks(blockSize); + } + + private void initBlocks(int blockSize) { + this.blocks.add(Block.EMPTY_BLOCK); + for (int i = 1; i < blockSize; i++) { + Block before = this.blocks.get(i - 1); + Block newBlock = BlockFactory.generateRandomBlock(); + if (before.isSequenceFilledBlock(newBlock)) { + i--; + continue; + } + this.blocks.add(newBlock); + } + } + + public int size() { + return this.blocks.size(); + } + + List getBlocks() { + return Collections.unmodifiableList(this.blocks); + } + + public String getStringBlocks() { + return this.blocks.stream().map(Block::toString).collect(Collectors.joining(DELIMITER)) + DELIMITER; + } +} diff --git a/src/main/java/game/ladder/domain/Height.java b/src/main/java/game/ladder/domain/Height.java new file mode 100644 index 0000000000..f31d20b235 --- /dev/null +++ b/src/main/java/game/ladder/domain/Height.java @@ -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; + } +} diff --git a/src/main/java/game/ladder/domain/Ladder.java b/src/main/java/game/ladder/domain/Ladder.java new file mode 100644 index 0000000000..a1b60f2866 --- /dev/null +++ b/src/main/java/game/ladder/domain/Ladder.java @@ -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(); + } + + +} diff --git a/src/main/java/game/ladder/domain/Line.java b/src/main/java/game/ladder/domain/Line.java index 016a8f2546..874109c4ac 100644 --- a/src/main/java/game/ladder/domain/Line.java +++ b/src/main/java/game/ladder/domain/Line.java @@ -1,43 +1,18 @@ package game.ladder.domain; -import spark.utils.Assert; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - public class Line { - private final List blocks; + private final Blocks blocks; public Line(int blockSize) { - Assert.isTrue(isNotLowerThanOne(blockSize), "blockSize는 1 이상이여야 합니다."); - this.blocks = new ArrayList<>(blockSize); - makeBlocks(blockSize); - } - - private boolean isNotLowerThanOne(int blockSize) { - return blockSize >= 1; - } - - private void makeBlocks(int blockSize) { - this.blocks.add(BlockFactory.generateRandomBlock()); - for (int i = 1; i < blockSize; i++) { - Block before = this.blocks.get(i - 1); - Block newBlock = BlockFactory.generateRandomBlock(); - if (before.isSequenceFilledBlock(newBlock)) { - i--; - continue; - } - this.blocks.add(newBlock); - } + this.blocks = new Blocks(blockSize); } public int getBlockSize() { return this.blocks.size(); } - public List getBlocks() { - return Collections.unmodifiableList(this.blocks); + public String getStringLine() { + return this.blocks.getStringBlocks(); } } diff --git a/src/main/java/game/ladder/domain/Lines.java b/src/main/java/game/ladder/domain/Lines.java new file mode 100644 index 0000000000..47c74b92d9 --- /dev/null +++ b/src/main/java/game/ladder/domain/Lines.java @@ -0,0 +1,27 @@ +package game.ladder.domain; + +import java.util.List; + +public class Lines { + + private final List lines; + private final Height height; + + public Lines(List 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(); + } + +} diff --git a/src/main/java/game/ladder/domain/LinesGenerator.java b/src/main/java/game/ladder/domain/LinesGenerator.java new file mode 100644 index 0000000000..9ebf2945f2 --- /dev/null +++ b/src/main/java/game/ladder/domain/LinesGenerator.java @@ -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 lines = IntStream.range(0, blockSize) + .mapToObj(i -> new Line(blockSize)) + .collect(Collectors.toList()); + + return new Lines(lines); + } + +} diff --git a/src/main/java/game/ladder/domain/Name.java b/src/main/java/game/ladder/domain/Name.java new file mode 100644 index 0000000000..65c4309765 --- /dev/null +++ b/src/main/java/game/ladder/domain/Name.java @@ -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(); + } +} diff --git a/src/main/java/game/ladder/domain/Participant.java b/src/main/java/game/ladder/domain/Participant.java index e3e74f6b16..4f081a5cd9 100644 --- a/src/main/java/game/ladder/domain/Participant.java +++ b/src/main/java/game/ladder/domain/Participant.java @@ -1,24 +1,27 @@ package game.ladder.domain; -import spark.utils.Assert; -import spark.utils.StringUtils; +public class Participant implements Comparable{ -public class Participant { - - private final String name; + private final Name name; private final Position position; - public Participant(String name, Position position) { - Assert.isTrue(StringUtils.isNotBlank(name), "name은 공백일 수 없습니다."); - + public Participant(Name name, Position position) { this.name = name; this.position = position; } - public String getName() { + 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; @@ -36,4 +39,9 @@ public int hashCode() { result = 31 * result + position.hashCode(); return result; } + + @Override + public int compareTo(Participant other) { + return this.position.compareTo(other.position); + } } diff --git a/src/main/java/game/ladder/domain/Participants.java b/src/main/java/game/ladder/domain/Participants.java new file mode 100644 index 0000000000..2a672c194b --- /dev/null +++ b/src/main/java/game/ladder/domain/Participants.java @@ -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 participants; + + public Participants(SortedSet 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); + } +} diff --git a/src/main/java/game/ladder/view/InputView.java b/src/main/java/game/ladder/view/InputView.java index b876d96979..5f1416b33c 100644 --- a/src/main/java/game/ladder/view/InputView.java +++ b/src/main/java/game/ladder/view/InputView.java @@ -1,11 +1,9 @@ package game.ladder.view; -import game.ladder.domain.Participant; -import game.ladder.domain.Position; +import game.ladder.domain.*; import game.ladder.util.Spliter; -import java.util.List; -import java.util.Scanner; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -13,17 +11,18 @@ public class InputView { private static final Scanner scanner = new Scanner(System.in); - public static List readParticipant() { + public static Participants readParticipant() { System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); - final List names = Spliter.split(scanner.nextLine()); + final List names = Spliter.split(scanner.nextLine()).stream().map(Name::new).collect(Collectors.toList()); + TreeSet participants = IntStream.range(0, names.size()) + .mapToObj(i -> new Participant(names.get(i), new Position(i + 1))) + .collect(Collectors.toCollection(TreeSet::new)); - return IntStream.range(0, names.size()) - .mapToObj(i -> new Participant(names.get(i), new Position(i + 1))) - .collect(Collectors.toList()); + return new Participants(participants); } - public static int readHeight() { + public static Height readHeight() { System.out.println("최대 사다리 높이는 몇 개인가요?"); - return Integer.parseInt(scanner.nextLine().trim()); + return new Height(Integer.parseInt(scanner.nextLine().trim())); } } diff --git a/src/main/java/game/ladder/view/OutputView.java b/src/main/java/game/ladder/view/OutputView.java new file mode 100644 index 0000000000..471a6902df --- /dev/null +++ b/src/main/java/game/ladder/view/OutputView.java @@ -0,0 +1,9 @@ +package game.ladder.view; + +import game.ladder.domain.Ladder; + +public class OutputView { + public static void printLadder(Ladder ladder) { + System.out.println(ladder.makeStringLadder()); + } +} diff --git a/src/test/java/game/ladder/domain/BlocksTest.java b/src/test/java/game/ladder/domain/BlocksTest.java new file mode 100644 index 0000000000..b30462bb33 --- /dev/null +++ b/src/test/java/game/ladder/domain/BlocksTest.java @@ -0,0 +1,31 @@ +package game.ladder.domain; + +import org.junit.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class BlocksTest { + + @Test(expected = IllegalArgumentException.class) + public void 만들기_0넣을때() { + final int blockSize = 0; + + new Blocks(blockSize); + } + + @Test + public void 만들어진_블럭들_연속_채운블록_존재여부() { + final int blockSize = 10; + Blocks blocks = new Blocks(blockSize); + + List blockList = blocks.getBlocks(); + for (int i = 1; i < blockSize; i++) { + Block before = blockList.get(i - 1); + Block current = blockList.get(i); + assertThat(before.isSequenceFilledBlock(current)).as(String.format("before : %d, after : %d", i - 1, i)).isFalse(); + } + } + +} \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/HeightTest.java b/src/test/java/game/ladder/domain/HeightTest.java new file mode 100644 index 0000000000..0206d072d2 --- /dev/null +++ b/src/test/java/game/ladder/domain/HeightTest.java @@ -0,0 +1,41 @@ +package game.ladder.domain; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class HeightTest { + + @Test + public void 만들기() { + final int input = 5; + Height height = new Height(input); + + assertThat(height.getValue()).isEqualTo(input); + } + + @Test(expected = IllegalArgumentException.class) + public void 값이_0일때() { + final int input = 0; + + new Height(input); + } + + @Test + public void 동등성_확인_같을때() { + final int value = 3; + + Height height1 = new Height(value); + Height height2 = new Height(value); + + assertThat(height1).isEqualTo(height2); + } + + @Test + public void 동등성_확인_다를때() { + Height height1 = new Height(2); + Height height2 = new Height(3); + + assertThat(height1).isNotEqualTo(height2); + } +} \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/LadderTest.java b/src/test/java/game/ladder/domain/LadderTest.java new file mode 100644 index 0000000000..a5715e6375 --- /dev/null +++ b/src/test/java/game/ladder/domain/LadderTest.java @@ -0,0 +1,25 @@ +package game.ladder.domain; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class LadderTest { + + @Test + public void 만들기() { + final LinesGenerator generator = new LinesGenerator(new Height(5)); + final Participants participants = new Participants(makeParticipants()); + Ladder ladder = new Ladder(generator, participants); + } + + private SortedSet makeParticipants() { + return IntStream.rangeClosed(1, 3) + .mapToObj(i -> new Participant(new Name(String.valueOf(i)), new Position(i))) + .collect(Collectors.toCollection(TreeSet::new)); + } +} \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/LineTest.java b/src/test/java/game/ladder/domain/LineTest.java index 201259930b..cfe7a5a022 100644 --- a/src/test/java/game/ladder/domain/LineTest.java +++ b/src/test/java/game/ladder/domain/LineTest.java @@ -22,18 +22,4 @@ public class LineTest { new Line(blockSize); } - @Test - public void 만들어진_블럭들_연속_채운블록_존재여부() { - final int blockSize = 10; - - Line line = new Line(blockSize); - - List blocks = line.getBlocks(); - for (int i = 1; i < blockSize; i++) { - Block before = blocks.get(i - 1); - Block current = blocks.get(i); - assertThat(before.isSequenceFilledBlock(current)).as(String.format("before : %d, after : %d", i - 1, i)).isFalse(); - } - } - } \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/LinesGeneratorTest.java b/src/test/java/game/ladder/domain/LinesGeneratorTest.java new file mode 100644 index 0000000000..eef00e23a2 --- /dev/null +++ b/src/test/java/game/ladder/domain/LinesGeneratorTest.java @@ -0,0 +1,22 @@ +package game.ladder.domain; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LinesGeneratorTest { + + @Test + public void 라인_만들기() { + final int blockSize = 3; + final Height height = new Height(3); + LinesGenerator generator = new LinesGenerator(height); + + Lines lines = generator.generate(blockSize); + + assertThat(lines.getHeight()).isEqualTo(height); + } + + + +} \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/LinesTest.java b/src/test/java/game/ladder/domain/LinesTest.java new file mode 100644 index 0000000000..18e90b0fdf --- /dev/null +++ b/src/test/java/game/ladder/domain/LinesTest.java @@ -0,0 +1,20 @@ +package game.ladder.domain; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LinesTest { + + @Test + public void 만들기() { + List inputs = Arrays.asList(new Line(3)); + Lines lines = new Lines(inputs); + + assertThat(lines.getHeight()).isEqualTo(new Height(inputs.size())); + } + +} \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/NameTest.java b/src/test/java/game/ladder/domain/NameTest.java new file mode 100644 index 0000000000..ce5bfeae63 --- /dev/null +++ b/src/test/java/game/ladder/domain/NameTest.java @@ -0,0 +1,38 @@ +package game.ladder.domain; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class NameTest { + + @Test + public void 만들기() { + final String value = "name"; + Name name = new Name(value); + + assertThat(name.getValue()).isEqualTo(value); + } + + @Test(expected = IllegalArgumentException.class) + public void 이름이_공백일때() { + final String emptyValue = ""; + + new Name(emptyValue); + } + + @Test(expected = IllegalArgumentException.class) + public void 이름이_6자일때() { + final String value = "123456"; + + new Name(value); + } + + @Test + public void 포메팅된_이름() { + Name name = new Name("name"); + + assertThat(name.fomattedName().length()).isEqualTo(6); + } + +} \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/ParticipantTest.java b/src/test/java/game/ladder/domain/ParticipantTest.java index 88627887ee..7dc96c0ff5 100644 --- a/src/test/java/game/ladder/domain/ParticipantTest.java +++ b/src/test/java/game/ladder/domain/ParticipantTest.java @@ -2,34 +2,25 @@ import org.junit.Test; +import java.util.*; + import static org.assertj.core.api.Assertions.assertThat; public class ParticipantTest { @Test public void 참여자_만들기() { - final String name = "name"; - final int value = 2; - Participant participant = new Participant(name, new Position(value)); + final Name name = new Name("name"); + final Position position = new Position(2); + Participant participant = new Participant(name, position); assertThat(participant.getName()).isEqualTo(name); - } - - @Test(expected = IllegalArgumentException.class) - public void 참여자_만들기_공백_이름() { - final String name = ""; - new Participant(name, new Position(2)); - } - - @Test(expected = IllegalArgumentException.class) - public void 참여자_만들기_null_이름() { - final String name = null; - new Participant(name, new Position(3)); + assertThat(participant.getPosition()).isEqualTo(position); } @Test public void 참여자_동등성_확인() { - final String name = "test"; + final Name name = new Name("test"); final Position position = new Position(1); final Participant participant1 = new Participant(name, position); @@ -42,19 +33,43 @@ public class ParticipantTest { public void 참여자_동등성_확인_이름_다를때() { final Position position = new Position(1); - final Participant participant1 = new Participant("a", position); - final Participant participant2 = new Participant("b", position); + final Participant participant1 = new Participant(new Name("a"), position); + final Participant participant2 = new Participant(new Name("b"), position); assertThat(participant1).isNotEqualTo(participant2); } @Test public void 참여자_동등성_확인_포지션_다를때() { - final String name = "name"; + final Name name = new Name("name"); final Participant participant1 = new Participant(name, new Position(3)); final Participant participant2 = new Participant(name, new Position(4)); assertThat(participant1).isNotEqualTo(participant2); } + + @Test + public void 참여자_정렬() { + final List testList = makeTestList(); + + SortedSet participants = new TreeSet<>(testList); + + assertThat(participants).containsSequence(testList.toArray(new Participant[]{})); + } + + + private List makeTestList() { + List list = new ArrayList<>(); + final Position first = new Position(1); + final Position second = new Position(2); + final Position third = new Position(3); + + list.add(new Participant(new Name("1"), first)); + list.add(new Participant(new Name("2"), second)); + list.add(new Participant(new Name("3"), third)); + + return list; + } + } \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/ParticipantsTest.java b/src/test/java/game/ladder/domain/ParticipantsTest.java new file mode 100644 index 0000000000..7285120712 --- /dev/null +++ b/src/test/java/game/ladder/domain/ParticipantsTest.java @@ -0,0 +1,38 @@ +package game.ladder.domain; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.SortedSet; +import java.util.TreeSet; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ParticipantsTest { + + private SortedSet inputs; + private Participants participants; + + @Before + public void setup() { + inputs = new TreeSet<>(Arrays.asList( + new Participant(new Name("1"), new Position(1)), new Participant(new Name("2"), new Position(2)) + ) + ); + + participants = new Participants(inputs); + } + + @Test + public void 만들기() { + assertThat(participants.size()).isEqualTo(inputs.size()); + } + + @Test(expected = IllegalArgumentException.class) + public void 만들기_참여자_한명_일때() { + inputs = new TreeSet<>(Arrays.asList(new Participant(new Name("1"), new Position(1)))); + + new Participants(inputs); + } +} \ No newline at end of file From 7ef92dddb5664d9df03d127e982b2dc387981f1a Mon Sep 17 00:00:00 2001 From: pporotoss Date: Sun, 18 Nov 2018 17:30:28 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=EC=BD=94=EB=93=9C=EB=A6=AC=EB=B7=B0=20?= =?UTF-8?q?=EC=82=AC=ED=95=AD=20=EC=88=98=EC=A0=95=20&=20=EC=95=88?= =?UTF-8?q?=EC=93=B0=EB=8A=94=20import=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/game/ladder/domain/Block.java | 22 ++++++--- .../java/game/ladder/domain/BlockFactory.java | 16 ------- src/main/java/game/ladder/domain/Blocks.java | 47 ------------------- src/main/java/game/ladder/domain/Line.java | 29 ++++++++++-- .../java/game/ladder/domain/BlockTest.java | 9 ++-- .../java/game/ladder/domain/BlocksTest.java | 31 ------------ .../java/game/ladder/domain/LadderTest.java | 1 - .../java/game/ladder/domain/LineTest.java | 2 - .../game/ladder/domain/ParticipantTest.java | 5 +- 9 files changed, 48 insertions(+), 114 deletions(-) delete mode 100644 src/main/java/game/ladder/domain/BlockFactory.java delete mode 100644 src/main/java/game/ladder/domain/Blocks.java delete mode 100644 src/test/java/game/ladder/domain/BlocksTest.java diff --git a/src/main/java/game/ladder/domain/Block.java b/src/main/java/game/ladder/domain/Block.java index 817854b559..1b7214e12a 100644 --- a/src/main/java/game/ladder/domain/Block.java +++ b/src/main/java/game/ladder/domain/Block.java @@ -1,5 +1,7 @@ package game.ladder.domain; +import java.util.Random; + public class Block { public static final Block EMPTY_BLOCK = new Block(BlockType.EMPTY); @@ -7,22 +9,28 @@ public class Block { 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 BlockType getType() { - return this.blockType; - } + public static Block nextBlock(Block before) { + if (before == FILLED_BLOCK) { + return EMPTY_BLOCK; + } + + if (RANDOM.nextBoolean()) { + return FILLED_BLOCK; + } - public boolean isSequenceFilledBlock(Block other) { - return this.isFilledBlock() && this.equals(other); + return EMPTY_BLOCK; } - private boolean isFilledBlock() { - return this.equals(FILLED_BLOCK); + public BlockType getType() { + return this.blockType; } @Override diff --git a/src/main/java/game/ladder/domain/BlockFactory.java b/src/main/java/game/ladder/domain/BlockFactory.java deleted file mode 100644 index d7aa18f703..0000000000 --- a/src/main/java/game/ladder/domain/BlockFactory.java +++ /dev/null @@ -1,16 +0,0 @@ -package game.ladder.domain; - -import java.util.Random; - -public class BlockFactory { - private static final Random RANDOM = new Random(); - - public static Block generateRandomBlock() { - if (RANDOM.nextBoolean()) { - return Block.FILLED_BLOCK; - } - - return Block.EMPTY_BLOCK; - } - -} diff --git a/src/main/java/game/ladder/domain/Blocks.java b/src/main/java/game/ladder/domain/Blocks.java deleted file mode 100644 index 604ab21e72..0000000000 --- a/src/main/java/game/ladder/domain/Blocks.java +++ /dev/null @@ -1,47 +0,0 @@ -package game.ladder.domain; - -import spark.utils.Assert; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -public class Blocks { - - private static final String DELIMITER = "|"; - private static final int MIN_SIZE = 1; - - private final List blocks; - - public Blocks(int blockSize) { - Assert.isTrue(blockSize >= MIN_SIZE, "blockSize는 1 이상이여야 합니다."); - this.blocks = new ArrayList<>(blockSize); - initBlocks(blockSize); - } - - private void initBlocks(int blockSize) { - this.blocks.add(Block.EMPTY_BLOCK); - for (int i = 1; i < blockSize; i++) { - Block before = this.blocks.get(i - 1); - Block newBlock = BlockFactory.generateRandomBlock(); - if (before.isSequenceFilledBlock(newBlock)) { - i--; - continue; - } - this.blocks.add(newBlock); - } - } - - public int size() { - return this.blocks.size(); - } - - List getBlocks() { - return Collections.unmodifiableList(this.blocks); - } - - public String getStringBlocks() { - return this.blocks.stream().map(Block::toString).collect(Collectors.joining(DELIMITER)) + DELIMITER; - } -} diff --git a/src/main/java/game/ladder/domain/Line.java b/src/main/java/game/ladder/domain/Line.java index 874109c4ac..fab042602a 100644 --- a/src/main/java/game/ladder/domain/Line.java +++ b/src/main/java/game/ladder/domain/Line.java @@ -1,11 +1,34 @@ 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 final Blocks blocks; + private static final String DELIMITER = "|"; + private static final int MIN_SIZE = 1; + + private final List blocks; public Line(int blockSize) { - this.blocks = new Blocks(blockSize); + Assert.isTrue(blockSize >= MIN_SIZE, "blockSize는 1 이상이여야 합니다."); + this.blocks = makeBlocks(blockSize); + } + + private List makeBlocks(int blockSize) { + List 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() { @@ -13,6 +36,6 @@ public int getBlockSize() { } public String getStringLine() { - return this.blocks.getStringBlocks(); + return this.blocks.stream().map(Block::toString).collect(Collectors.joining(DELIMITER)) + DELIMITER; } } diff --git a/src/test/java/game/ladder/domain/BlockTest.java b/src/test/java/game/ladder/domain/BlockTest.java index dedcd3f504..62f0073a14 100644 --- a/src/test/java/game/ladder/domain/BlockTest.java +++ b/src/test/java/game/ladder/domain/BlockTest.java @@ -3,7 +3,6 @@ import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.filter; public class BlockTest { @@ -48,12 +47,10 @@ public class BlockTest { } @Test - public void 연속_채워진_블록_확인() { + public void 다음_블록_안채워진_블록인지_확인() { final Block before = Block.FILLED_BLOCK; - final Block afterFilled = Block.FILLED_BLOCK; - final Block afterEmpty = Block.EMPTY_BLOCK; + Block next = Block.nextBlock(before); - assertThat(before.isSequenceFilledBlock(afterFilled)).isTrue(); - assertThat(before.isSequenceFilledBlock(afterEmpty)).isFalse(); + assertThat(next).isSameAs(Block.EMPTY_BLOCK); } } \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/BlocksTest.java b/src/test/java/game/ladder/domain/BlocksTest.java deleted file mode 100644 index b30462bb33..0000000000 --- a/src/test/java/game/ladder/domain/BlocksTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package game.ladder.domain; - -import org.junit.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; - -public class BlocksTest { - - @Test(expected = IllegalArgumentException.class) - public void 만들기_0넣을때() { - final int blockSize = 0; - - new Blocks(blockSize); - } - - @Test - public void 만들어진_블럭들_연속_채운블록_존재여부() { - final int blockSize = 10; - Blocks blocks = new Blocks(blockSize); - - List blockList = blocks.getBlocks(); - for (int i = 1; i < blockSize; i++) { - Block before = blockList.get(i - 1); - Block current = blockList.get(i); - assertThat(before.isSequenceFilledBlock(current)).as(String.format("before : %d, after : %d", i - 1, i)).isFalse(); - } - } - -} \ No newline at end of file diff --git a/src/test/java/game/ladder/domain/LadderTest.java b/src/test/java/game/ladder/domain/LadderTest.java index a5715e6375..195351ffd2 100644 --- a/src/test/java/game/ladder/domain/LadderTest.java +++ b/src/test/java/game/ladder/domain/LadderTest.java @@ -2,7 +2,6 @@ import org.junit.Test; -import java.util.Arrays; import java.util.SortedSet; import java.util.TreeSet; import java.util.stream.Collectors; diff --git a/src/test/java/game/ladder/domain/LineTest.java b/src/test/java/game/ladder/domain/LineTest.java index cfe7a5a022..bdb222002b 100644 --- a/src/test/java/game/ladder/domain/LineTest.java +++ b/src/test/java/game/ladder/domain/LineTest.java @@ -2,8 +2,6 @@ import org.junit.Test; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; public class LineTest { diff --git a/src/test/java/game/ladder/domain/ParticipantTest.java b/src/test/java/game/ladder/domain/ParticipantTest.java index 7dc96c0ff5..6754a6c01d 100644 --- a/src/test/java/game/ladder/domain/ParticipantTest.java +++ b/src/test/java/game/ladder/domain/ParticipantTest.java @@ -2,7 +2,10 @@ import org.junit.Test; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.SortedSet; +import java.util.TreeSet; import static org.assertj.core.api.Assertions.assertThat;