diff --git a/README.md b/README.md new file mode 100644 index 0000000..4acc904 --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# java-ladder + +자바 사다리 미션 저장소 + +## 개요 + +참여자와 상품을 입력 받아 랜덤 사다리를 만들어 참여자와 상품을 연결시켜주는 게임이다. + +## 요구사항 + +### 사람(Person) + +- [x] 사람의 이름은 1자 이상 5자 이하여야 한다. + +### 참여자(Participant) + +- [x] 참여자들은 사다리에서 자신의 위치를 갖고 있다. +- [X] 참여자들은 정해진 라인을 따라 이동한다. + +### 참여자들(Participants) + +- [x] 참여자들의 이름은 중복이 될 수 없다. +- [x] 각 라인의 출발점에는 하나의 사람만 있을 수 있다. + +### 상품(Prize) + +- [x] 상품은 금액으로 이루어져 있다. +- [x] 상품의 금액이 값을 표현한다.(vo를 표현할 수 있는 말이 무엇이 있을까?) + +### 라인(RowLine) + +- [x] 라인들의 각 지점은 오른쪽, 왼쪽, 아래쪽 방향성 중 하나만을 가질 수 있다. +- [x] 라인이 연결성은 겹치지 않아야 한다. +- [x] 라인의 연결 여부는 랜덤으로 결정한다. + +### 사다리(Ladder) + +- [x] 여러개의 Line이 모여 하나의 사다리를 만든다. + +### 사다리 게임(LadderGame) + +- [x] 참여자들을 라인 따라 이동해 상품과 최종적으로 연결을 맺는다. +- [x] 게임이 완료되어야 최종 게임 결과를 확인할 수 있다. diff --git a/src/main/java/.gitkeep b/src/main/java/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/LadderGameApplication.java b/src/main/java/LadderGameApplication.java new file mode 100644 index 0000000..740ac21 --- /dev/null +++ b/src/main/java/LadderGameApplication.java @@ -0,0 +1,13 @@ +import view.InputView; +import view.OutputView; + +public class LadderGameApplication { + public static void main(String[] args) { + LadderGameController ladderGameController = new LadderGameController( + new InputView(), + new OutputView() + ); + + ladderGameController.ladderGame(); + } +} diff --git a/src/main/java/LadderGameController.java b/src/main/java/LadderGameController.java new file mode 100644 index 0000000..d1c7281 --- /dev/null +++ b/src/main/java/LadderGameController.java @@ -0,0 +1,94 @@ +import domain.*; +import view.InputView; +import view.OutputView; + +import java.util.List; + +public class LadderGameController { + + private final InputView inputView; + private final OutputView outputView; + + public LadderGameController(InputView inputView, OutputView outputView) { + this.inputView = inputView; + this.outputView = outputView; + } + + public void ladderGame() { + List people = readParticipants(); + List prizes = readPrizes(); + + validateInputsCount(people.size(), prizes.size()); + + int height = readLadderHeight(); + int width = people.size(); + + LadderFactory ladderGenerator = new RandomLadderFactory(height, width); + Ladder ladder = ladderGenerator.newInstance(); + + Participants participants = Participants.fromPeople(people); + LadderGame ladderGame = new LadderGame(participants, ladder, prizes); + + outputView.printResult(ladderGame.getParticipants(), ladderGame.getLadder(), ladderGame.getPrizes()); + + ladderGame.start(); + + GameResult gameResult = ladderGame.getParticipantsPrizes(); + + printSpecificParticipantResult(ladderGame.getParticipants(), gameResult); + + outputView.printParticipantsPrizesResult(ladderGame.getParticipants(), gameResult); + } + + private List readParticipants() { + outputView.printParticipantInquiry(); + List participants = inputView.readParticipants(); + outputView.printEmptyLine(); + + return participants; + } + + private List readPrizes() { + outputView.printResultPrizeInquiry(); + List prizes = inputView.readPrizeResults(); + outputView.printEmptyLine(); + + return prizes; + } + + private int readLadderHeight() { + outputView.printLadderHeightInquiry(); + int height = inputView.readLadderHeight(); + outputView.printEmptyLine(); + + return height; + } + + private String readParticipantName() { + outputView.printParticipantPrizeInquiry(); + String name = inputView.readPersonName(); + outputView.printEmptyLine(); + + return name; + } + + private void printSpecificParticipantResult(Participants participants, GameResult gameResult) { + while (true) { + String name = readParticipantName(); + + if (name.equals("all")) { + break; + } + + Person person = new Person(name); + Participant participant = participants.getParticipantByName(person); + outputView.printParticipantPrizeResult(gameResult.getPrize(participant)); + } + } + + private void validateInputsCount(int participantCount, int prizeCount) { + if (participantCount != prizeCount) { + throw new IllegalArgumentException("The number of participants does not match the number of prizes."); + } + } +} diff --git a/src/main/java/domain/Connection.java b/src/main/java/domain/Connection.java new file mode 100644 index 0000000..32c5c2b --- /dev/null +++ b/src/main/java/domain/Connection.java @@ -0,0 +1,6 @@ +package domain; + +public enum Connection { + CONNECTED, + UNCONNECTED, +} diff --git a/src/main/java/domain/Direction.java b/src/main/java/domain/Direction.java new file mode 100644 index 0000000..7f4f2be --- /dev/null +++ b/src/main/java/domain/Direction.java @@ -0,0 +1,11 @@ +package domain; + +public enum Direction { + LEFT(-1), RIGHT(1), DOWN(0); + + final int offset; + + Direction(int offset) { + this.offset = offset; + } +} diff --git a/src/main/java/domain/GameResult.java b/src/main/java/domain/GameResult.java new file mode 100644 index 0000000..82b5b9f --- /dev/null +++ b/src/main/java/domain/GameResult.java @@ -0,0 +1,15 @@ +package domain; + +import java.util.Map; + +public class GameResult { + private final Map gameResult; + + public GameResult(Map gameResult) { + this.gameResult = gameResult; + } + + public Prize getPrize(Participant participant) { + return gameResult.get(participant); + } +} diff --git a/src/main/java/domain/GameState.java b/src/main/java/domain/GameState.java new file mode 100644 index 0000000..360be06 --- /dev/null +++ b/src/main/java/domain/GameState.java @@ -0,0 +1,7 @@ +package domain; + +public enum GameState { + READY, + PROGRESS, + FINISH, +} diff --git a/src/main/java/domain/Ladder.java b/src/main/java/domain/Ladder.java new file mode 100644 index 0000000..e71d036 --- /dev/null +++ b/src/main/java/domain/Ladder.java @@ -0,0 +1,15 @@ +package domain; + +import java.util.List; + +public class Ladder { + private final List columnLines; + + public Ladder(List columnLines) { + this.columnLines = columnLines; + } + + public List getLines() { + return columnLines; + } +} diff --git a/src/main/java/domain/LadderFactory.java b/src/main/java/domain/LadderFactory.java new file mode 100644 index 0000000..a4957fc --- /dev/null +++ b/src/main/java/domain/LadderFactory.java @@ -0,0 +1,5 @@ +package domain; + +public interface LadderFactory { + Ladder newInstance(); +} diff --git a/src/main/java/domain/LadderGame.java b/src/main/java/domain/LadderGame.java new file mode 100644 index 0000000..d448df6 --- /dev/null +++ b/src/main/java/domain/LadderGame.java @@ -0,0 +1,84 @@ +package domain; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class LadderGame { + private final Participants participants; + private final Ladder ladder; + private final List prizes; + private GameState gameState; + private Map> result; + + public LadderGame(Participants participants, Ladder ladder, List prizes) { + this.participants = participants; + this.ladder = ladder; + this.prizes = prizes; + this.gameState = GameState.READY; + } + + public void start() { + gameState = GameState.PROGRESS; + + Map> gameResult = new HashMap<>(); + + int participantsCount = participants.getParticipantsCount(); + + for (int i = 0; i < participantsCount; i++) { + Participant participant = participants.getParticipantByIndex(i); + Integer startPosition = participant.getPosition(); + List participantPositions = moveParticipant(participant); + gameResult.put(startPosition, participantPositions); + } + + result = gameResult; + + gameState = GameState.FINISH; + } + + private List moveParticipant(Participant participant) { + List participantPositions = new ArrayList<>(); + + for (RowLine rowLine : ladder.getLines()) { + int currentPosition = participant.getPosition(); + Direction direction = rowLine.getPositionDirection(currentPosition); + + participant.move(direction); + + participantPositions.add(participant.getPosition()); + } + + return participantPositions; + } + + public GameResult getParticipantsPrizes() { + if (gameState != GameState.FINISH) { + throw new RuntimeException("게임이 진행중입니다!"); + } + + Map participantsPrizes = new HashMap<>(); + + for (Integer startPosition : result.keySet()) { + List positions = result.get(startPosition); + Integer lastPosition = positions.get(positions.size() - 1); + + participantsPrizes.put(participants.getParticipantByIndex(startPosition), prizes.get(lastPosition)); + } + + return new GameResult(participantsPrizes); + } + + public Participants getParticipants() { + return participants; + } + + public Ladder getLadder() { + return ladder; + } + + public List getPrizes() { + return prizes; + } +} diff --git a/src/main/java/domain/Participant.java b/src/main/java/domain/Participant.java new file mode 100644 index 0000000..ef16e35 --- /dev/null +++ b/src/main/java/domain/Participant.java @@ -0,0 +1,38 @@ +package domain; + +import java.util.Objects; + +public class Participant { + private final Person person; + private int position; + + public Participant(Person person, int position) { + this.person = person; + this.position = position; + } + + public void move(Direction direction) { + position = position + direction.offset; + } + + public String getParticipantName() { + return person.name(); + } + + public int getPosition() { + return position; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Participant that = (Participant) o; + return Objects.equals(person.name(), that.person.name()); + } + + @Override + public int hashCode() { + return Objects.hashCode(person.name()); + } +} diff --git a/src/main/java/domain/Participants.java b/src/main/java/domain/Participants.java new file mode 100644 index 0000000..7efe61c --- /dev/null +++ b/src/main/java/domain/Participants.java @@ -0,0 +1,45 @@ +package domain; + +import java.util.List; +import java.util.Set; +import java.util.stream.IntStream; + +public record Participants(List participants) { + public Participants { + validateParticipants(participants); + } + + public static Participants fromPeople(List people) { + List participants = IntStream.range(0, people.size()) + .mapToObj(i -> new Participant(people.get(i), i)) + .toList(); + + return new Participants(participants); + } + + private void validateParticipants(List participants) { + int participantsCount = participants.size(); + + Set deduplicateParticipants = Set.copyOf(participants); + int deduplicateParticipantsCount = deduplicateParticipants.size(); + + if (participantsCount != deduplicateParticipantsCount) { + throw new IllegalArgumentException("중복된 이름의 참여자는 참여할 수 없습니다."); + } + } + + public int getParticipantsCount() { + return participants.size(); + } + + public Participant getParticipantByIndex(int index) { + return participants.get(index); + } + + public Participant getParticipantByName(Person person) { + return participants.stream() + .filter(participant -> participant.getParticipantName().equals(person.name())) + .findFirst() + .orElseThrow(IllegalArgumentException::new); + } +} diff --git a/src/main/java/domain/Person.java b/src/main/java/domain/Person.java new file mode 100644 index 0000000..6e1fb01 --- /dev/null +++ b/src/main/java/domain/Person.java @@ -0,0 +1,14 @@ +package domain; + +public record Person(String name) { + private static final int MAX_LENGTH = 5; + + public Person { + validateName(name); + } + + private void validateName(String name) { + if (name.length() > MAX_LENGTH || name.isEmpty()) + throw new IllegalArgumentException("사람 이름은 1글자 이상 5글자 이하여야 합니다."); + } +} diff --git a/src/main/java/domain/Prize.java b/src/main/java/domain/Prize.java new file mode 100644 index 0000000..d895ce9 --- /dev/null +++ b/src/main/java/domain/Prize.java @@ -0,0 +1,12 @@ +package domain; + +public record Prize(int cost) { + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Prize prize = (Prize) o; + return cost == prize.cost; + } +} diff --git a/src/main/java/domain/RandomLadderFactory.java b/src/main/java/domain/RandomLadderFactory.java new file mode 100644 index 0000000..b2b31b4 --- /dev/null +++ b/src/main/java/domain/RandomLadderFactory.java @@ -0,0 +1,113 @@ +package domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.IntStream; + +public class RandomLadderFactory implements LadderFactory { + private static final ThreadLocalRandom random = ThreadLocalRandom.current(); + private static final int RANDOM_BOUND = 10; + + private final int lineHeight; + private final int ladderWidth; + + public RandomLadderFactory(int height, int width) { + this.lineHeight = height; + this.ladderWidth = width; + } + + @Override + public Ladder newInstance() { + List> columnRowConnections = generateLadderConnection(lineHeight, ladderWidth); + + List rowLines = IntStream.range(0, lineHeight) + .mapToObj(i -> generateRowLineByConnections(columnRowConnections.get(i))) + .toList(); + + return new Ladder(rowLines); + } + + private List> generateLadderConnection(int lineHeight, int ladderWidth) { + List> rowColumnConnections = new ArrayList<>(); + + for (int i = 0; i < lineHeight; i++) { + List rowConnections = generateRowConnections(ladderWidth); + rowColumnConnections.add(rowConnections); + } + + return rowColumnConnections; + } + + private List generateRowConnections(int ladderWidth) { + List rowConnections = new ArrayList<>(); + + int ladderConnectionCount = ladderWidth - 1; + for (int i = 0; i < ladderConnectionCount; i++) { + Connection isConnected = generateSpotToSpotConnection(i, rowConnections); + rowConnections.add(isConnected); + } + + return rowConnections; + } + + private Connection generateSpotToSpotConnection(int lineIndex, List rowSpotsConnection) { + if (lineIndex != 0 && rowSpotsConnection.get(lineIndex - 1) == Connection.CONNECTED) + return Connection.UNCONNECTED; + + int randomValue = random.nextInt(RANDOM_BOUND); + if (randomValue < 5) + return Connection.CONNECTED; + + return Connection.UNCONNECTED; + } + + private RowLine generateRowLineByConnections(List rowConnections) { + List directions = new ArrayList<>(); + + for (int i = 0; i < ladderWidth; i++) { + Direction direction = generateDirection(i, rowConnections); + directions.add(direction); + } + + return new RowLine(directions); + } + + private Direction generateDirection(int columnIndex, List rowConnections) { + if (columnIndex == 0) { + return getFirstLineDirection(rowConnections.get(columnIndex)); + } + + if (columnIndex == ladderWidth - 1) { + return getLastLineDirection(rowConnections.get(columnIndex - 1)); + } + + return getMiddleLineDirection(rowConnections.get(columnIndex - 1), rowConnections.get(columnIndex)); + } + + private Direction getFirstLineDirection(Connection connection) { + if (connection == Connection.CONNECTED) { + return Direction.RIGHT; + } + return Direction.DOWN; + } + + private Direction getLastLineDirection(Connection connection) { + if (connection == Connection.CONNECTED) { + return Direction.LEFT; + } + return Direction.DOWN; + } + + private Direction getMiddleLineDirection(Connection currentConnection, Connection nextConnection) { + if (currentConnection == Connection.CONNECTED) { + return Direction.LEFT; + } + + if (nextConnection == Connection.CONNECTED) { + return Direction.RIGHT; + } + + return Direction.DOWN; + } +} diff --git a/src/main/java/domain/RowLine.java b/src/main/java/domain/RowLine.java new file mode 100644 index 0000000..5d681e3 --- /dev/null +++ b/src/main/java/domain/RowLine.java @@ -0,0 +1,42 @@ +package domain; + +import java.util.List; + +public record RowLine(List points) { + public RowLine { + validateRowLine(points); + } + + private void validateRowLine(List rowDirections) { + int rowSize = rowDirections.size(); + int rightCount = 0; + + for (int i = 0; i < rowSize; i++) { + if (rowDirections.get(i) == Direction.RIGHT) { + validateRight(i, rowSize, rowDirections); + rightCount++; + } + + if (rowDirections.get(i) == Direction.LEFT) { + validateLeft(rightCount); + rightCount--; + } + } + } + + private void validateRight(int columnIndex, int rowSize, List rowDirections) { + if (columnIndex == rowSize - 1 || rowDirections.get(columnIndex + 1) != Direction.LEFT) { + throw new RuntimeException("사다리가 잘못 만들어졌습니다."); + } + } + + private void validateLeft(int rightCount) { + if (rightCount == 0) { + throw new RuntimeException("사다리가 잘못 만들어졌습니다."); + } + } + + public Direction getPositionDirection(int positionIndex) { + return points.get(positionIndex); + } +} diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 0000000..d8fd935 --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,41 @@ +package view; + +import domain.Person; +import domain.Prize; + +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +public class InputView { + private final Scanner scanner = new Scanner(System.in); + + public List readParticipants() { + String[] participants = scanner.nextLine().split(","); + + return Arrays.stream(participants) + .map(Person::new) + .toList(); + } + + public List readPrizeResults() { + String[] prizes = scanner.nextLine().split(","); + + return Arrays.stream(prizes) + .map(p -> { + if (p.equals("꽝")) + return new Prize(0); + + return new Prize(Integer.parseInt(p)); + }) + .toList(); + } + + public int readLadderHeight() { + return Integer.parseInt(scanner.nextLine()); + } + + public String readPersonName() { + return scanner.nextLine(); + } +} diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java new file mode 100644 index 0000000..20c75b9 --- /dev/null +++ b/src/main/java/view/OutputView.java @@ -0,0 +1,105 @@ +package view; + +import domain.*; + +import java.util.List; + +public class OutputView { + public void printEmptyLine() { + System.out.println(); + } + + public void printParticipantInquiry() { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + } + + public void printResultPrizeInquiry() { + System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); + } + + public void printLadderHeightInquiry() { + System.out.println("최대 사다리 높이는 몇 개인가요?"); + } + + public void printResult(Participants participants, Ladder ladder, List prizes) { + System.out.println("사다리 결과\n"); + + printParticipantsResult(participants.participants()); + + printLadderResult(ladder); + + printPrizesResult(prizes); + + printEmptyLine(); + } + + private void printParticipantsResult(List participants) { + for (Participant participant : participants) { + String name = participant.getParticipantName(); + String blanks = " ".repeat(5 - name.length()); + System.out.print(blanks + name + " "); + } + System.out.println(); + } + + private void printLadderResult(Ladder ladder) { + List rowLines = ladder.getLines(); + + for (RowLine rowLine : rowLines) { + System.out.print(" "); + printLadderRowResult(rowLine); + System.out.println(); + } + } + + private void printLadderRowResult(RowLine rowLine) { + for (Direction direction : rowLine.points()) { + printByDirection(direction); + } + } + + private void printByDirection(Direction direction) { + if (direction == Direction.RIGHT) { + System.out.print("|-----"); + return; + } + + System.out.print("| "); + } + + private void printPrizesResult(List prizes) { + for (Prize prize : prizes) { + String name = changePrizeToString(prize); + String blanks = " ".repeat(5 - name.length()); + System.out.print(blanks + name + " "); + } + System.out.println(); + } + + public void printParticipantPrizeInquiry() { + System.out.println("결과를 보고 싶은 사람은?"); + } + + public void printParticipantPrizeResult(Prize prize) { + System.out.println("실행 결과"); + System.out.println(changePrizeToString(prize) + "\n"); + } + + public void printParticipantsPrizesResult(Participants participants, GameResult gameResult) { + System.out.println("실행 결과"); + for (Participant participant : participants.participants()) { + String name = participant.getParticipantName(); + Prize prize = gameResult.getPrize(participant); + System.out.println(name + " : " + changePrizeToString(prize)); + } + } + + private String changePrizeToString(Prize prize) { + int cost = prize.cost(); + if (cost == 0) { + return "꽝"; + } + + return String.valueOf(cost); + } +} diff --git a/src/test/java/LadderGameTest.java b/src/test/java/LadderGameTest.java new file mode 100644 index 0000000..d7e16b7 --- /dev/null +++ b/src/test/java/LadderGameTest.java @@ -0,0 +1,61 @@ +import domain.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.List; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +@SuppressWarnings("NonAsciiCharacters") +public class LadderGameTest { + @ParameterizedTest + @MethodSource("methodSourceTestArguments") + public void 참여자들은_라인_따라_이동해_상품과_최종적으로_연결을_맺는다(Participants participants, Ladder ladder, List prizes) { + LadderGame game = new LadderGame(participants, ladder, prizes); + game.start(); + + GameResult gameResult = game.getParticipantsPrizes(); + + assertThat(gameResult.getPrize(participants.getParticipantByIndex(0))).isEqualTo(prizes.get(0)); + assertThat(gameResult.getPrize(participants.getParticipantByIndex(1))).isEqualTo(prizes.get(3)); + assertThat(gameResult.getPrize(participants.getParticipantByIndex(2))).isEqualTo(prizes.get(2)); + assertThat(gameResult.getPrize(participants.getParticipantByIndex(3))).isEqualTo(prizes.get(1)); + } + + @ParameterizedTest + @MethodSource("methodSourceTestArguments") + public void 게임이_완료되어야_최종_결과_학인_가능하다(Participants participants, Ladder ladder, List prizes) { + LadderGame game = new LadderGame(participants, ladder, prizes); + + Assertions.assertThrows(RuntimeException.class, game::getParticipantsPrizes); + } + + private static Stream methodSourceTestArguments() { + Person person1 = new Person("프론트"); + Person person2 = new Person("백엔드"); + Person person3 = new Person("데브옵스"); + Person person4 = new Person("그리디"); + + List people = List.of(person1, person2, person3, person4); + + LadderFactory ladderFactory = new MockRandomLadderFactory.NormalLadderFactory(); + Ladder ladder = ladderFactory.newInstance(); + + List prizes = List.of( + new Prize(0), + new Prize(1000), + new Prize(2000), + new Prize(3000) + ); + + return Stream.of( + Arguments.of(Participants.fromPeople(people), ladder, prizes) + ); + } +} diff --git a/src/test/java/MockRandomLadderFactory.java b/src/test/java/MockRandomLadderFactory.java new file mode 100644 index 0000000..5d6c47a --- /dev/null +++ b/src/test/java/MockRandomLadderFactory.java @@ -0,0 +1,48 @@ +import domain.Direction; +import domain.Ladder; +import domain.LadderFactory; +import domain.RowLine; + +import java.util.List; + +public class MockRandomLadderFactory { + public static class NormalLadderFactory implements LadderFactory { + @Override + public Ladder newInstance() { + List directions1 = List.of(Direction.RIGHT, Direction.LEFT, Direction.RIGHT, Direction.LEFT); + List directions2 = List.of(Direction.RIGHT, Direction.LEFT, Direction.DOWN, Direction.DOWN); + List directions3 = List.of(Direction.DOWN, Direction.RIGHT, Direction.LEFT, Direction.DOWN); + List directions4 = List.of(Direction.DOWN, Direction.DOWN, Direction.RIGHT, Direction.LEFT); + + List rowLines = List.of( + new RowLine(directions1), + new RowLine(directions2), + new RowLine(directions3), + new RowLine(directions4) + ); + + return new Ladder(rowLines); + } + } + + public static class AbnormalLadderFactory implements LadderFactory { + @Override + public Ladder newInstance() { + List directions1 = List.of(Direction.RIGHT, Direction.LEFT, Direction.RIGHT, Direction.LEFT); + List directions2 = List.of(Direction.RIGHT, Direction.LEFT, Direction.DOWN, Direction.DOWN); + List directions3 = List.of(Direction.DOWN, Direction.RIGHT, Direction.RIGHT, Direction.LEFT); + List directions4 = List.of(Direction.DOWN, Direction.DOWN, Direction.RIGHT, Direction.LEFT); + + + List rowLines = List.of( + new RowLine(directions1), + new RowLine(directions2), + new RowLine(directions3), + new RowLine(directions4) + + ); + + return new Ladder(rowLines); + } + } +} diff --git a/src/test/java/ParticipantTest.java b/src/test/java/ParticipantTest.java new file mode 100644 index 0000000..abb1295 --- /dev/null +++ b/src/test/java/ParticipantTest.java @@ -0,0 +1,40 @@ +import domain.Direction; +import domain.Participant; +import domain.Participants; +import domain.Person; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +@SuppressWarnings("NonAsciiCharacters") +public class ParticipantTest { + @Test + public void 중복된_이름을_가진_참여자는_존재할_수_없다() { + String name = "SJ"; + Person person1 = new Person(name); + Person person2 = new Person(name); + Person person3 = new Person("그리디"); + + List people = List.of(person1, person2, person3); + + Assertions.assertThrows(IllegalArgumentException.class, () -> Participants.fromPeople(people)); + } + + @Test + public void 참여자는_정해진_라인을따라_이동한다() { + Person person = new Person("그리디"); + + Participant participant = new Participant(person, 0); + + participant.move(Direction.RIGHT); + participant.move(Direction.RIGHT); + + assertThat(participant.getPosition()).isEqualTo(2); + } +} diff --git a/src/test/java/PersonTest.java b/src/test/java/PersonTest.java new file mode 100644 index 0000000..4d70282 --- /dev/null +++ b/src/test/java/PersonTest.java @@ -0,0 +1,20 @@ +import domain.Person; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +@SuppressWarnings("NonAsciiCharacters") +public class PersonTest { + @Test + public void 사람_이름은_1자_이상_5자_이하_여야_한다() { + String name1 = "Seung"; + String name2 = ""; + String name3 = "SeungJ"; + + Assertions.assertDoesNotThrow(() -> new Person(name1)); + Assertions.assertThrows(IllegalArgumentException.class, () -> new Person(name2)); + Assertions.assertThrows(IllegalArgumentException.class, () -> new Person(name3)); + } +} diff --git a/src/test/java/PrizeTest.java b/src/test/java/PrizeTest.java new file mode 100644 index 0000000..2695849 --- /dev/null +++ b/src/test/java/PrizeTest.java @@ -0,0 +1,22 @@ +import domain.Prize; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +@SuppressWarnings("NonAsciiCharacters") +public class PrizeTest { + @Test + public void vo_테스트() { + int cost1 = 1000; + int cost2 = 2000; + Prize prize1 = new Prize(cost1); + Prize prize2 = new Prize(cost1); + Prize prize3 = new Prize(cost2); + + assertThat(prize1.equals(prize2)).isTrue(); + assertThat(prize1.equals(prize3)).isFalse(); + } +} diff --git a/src/test/java/RowLineTest.java b/src/test/java/RowLineTest.java new file mode 100644 index 0000000..01fa269 --- /dev/null +++ b/src/test/java/RowLineTest.java @@ -0,0 +1,21 @@ +import domain.Direction; +import domain.RowLine; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +import java.util.List; + +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +@SuppressWarnings("NonAsciiCharacters") +public class RowLineTest { + @Test + public void 라인의_연결성은_겹치면_안된다() { + List directions1 = List.of(Direction.DOWN, Direction.RIGHT, Direction.RIGHT, Direction.LEFT); + List directions2 = List.of(Direction.RIGHT, Direction.LEFT, Direction.DOWN, Direction.DOWN); + + Assertions.assertThrows(RuntimeException.class, () -> new RowLine(directions1)); + Assertions.assertDoesNotThrow(() -> new RowLine(directions2)); + } +}