diff --git a/src/main/java/nextstep/ladder/LadderController.java b/src/main/java/nextstep/ladder/LadderController.java index 2fddb6f85e..c50097eec4 100644 --- a/src/main/java/nextstep/ladder/LadderController.java +++ b/src/main/java/nextstep/ladder/LadderController.java @@ -4,6 +4,7 @@ import nextstep.ladder.domain.ladder.LadderResult; import nextstep.ladder.domain.laddergame.LadderGame; import nextstep.ladder.domain.laddergame.LadderGameResult; +import nextstep.ladder.domain.laddergame.position.ResultPosition; import nextstep.ladder.domain.player.Players; import nextstep.ladder.ui.InputView; import nextstep.ladder.ui.ResultView; @@ -19,10 +20,9 @@ public static void main(String[] args) { ResultView.printLadder(ladder, ladderResult); - LadderGame ladderGame = new LadderGame(ladder, ladderResult); - LadderGameResult ladderGameResult = ladderGame.play(); - - ResultView.printGameResult(ladderGameResult); + ResultPosition resultPosition = new LadderGame().play(ladder); + LadderGameResult ladderGameResult = resultPosition.match(players, ladderResult); + ResultView.printGameResult2(ladderGameResult); } } diff --git a/src/main/java/nextstep/ladder/domain/laddergame/LadderGame.java b/src/main/java/nextstep/ladder/domain/laddergame/LadderGame.java index 320629a41c..d000d6eb68 100644 --- a/src/main/java/nextstep/ladder/domain/laddergame/LadderGame.java +++ b/src/main/java/nextstep/ladder/domain/laddergame/LadderGame.java @@ -1,28 +1,27 @@ package nextstep.ladder.domain.laddergame; import nextstep.ladder.domain.ladder.Ladder; -import nextstep.ladder.domain.ladder.LadderResult; import nextstep.ladder.domain.laddergame.position.Positions; +import nextstep.ladder.domain.laddergame.position.ResultPosition; +import nextstep.ladder.domain.line.Line; import java.util.List; -import java.util.stream.Collectors; import java.util.stream.IntStream; public class LadderGame { - private final Ladder ladder; - private final LadderResult ladderResult; - public LadderGame(Ladder ladder, LadderResult ladderResult) { - this.ladder = ladder; - this.ladderResult = ladderResult; - } + public ResultPosition play(Ladder ladder) { + ResultPosition resultPosition = new ResultPosition(); - public LadderGameResult play() { - List positionsList = IntStream.range(0, ladder.getPlayers().size()) - .mapToObj(i -> new Positions().add(ladder.getLines(), i)) - .collect(Collectors.toList()); + IntStream.range(0, ladder.getPlayers().size()) + .forEach(i -> { + resultPosition.add(i, getLastPosition(ladder.getLines(), i)); + }); - return new LadderGameResult(ladder.getPlayers(), positionsList, ladderResult); + return resultPosition; } + private int getLastPosition(List lines, int playerPosition) { + return new Positions().add(lines, playerPosition).getLastPosition(); + } } diff --git a/src/main/java/nextstep/ladder/domain/laddergame/LadderGameResult.java b/src/main/java/nextstep/ladder/domain/laddergame/LadderGameResult.java index 24ed7a7ce2..20345f4fed 100644 --- a/src/main/java/nextstep/ladder/domain/laddergame/LadderGameResult.java +++ b/src/main/java/nextstep/ladder/domain/laddergame/LadderGameResult.java @@ -1,36 +1,38 @@ package nextstep.ladder.domain.laddergame; -import nextstep.ladder.domain.ladder.LadderResult; -import nextstep.ladder.domain.laddergame.position.PlayerPositions; -import nextstep.ladder.domain.laddergame.position.Positions; -import nextstep.ladder.domain.player.Player; -import nextstep.ladder.domain.player.Players; - -import java.util.List; -import java.util.stream.Collectors; +import java.util.HashMap; +import java.util.Map; public class LadderGameResult { + private final Map ladderGameResult; - private final PlayerPositions playerPositions; - private final LadderResult ladderResult; + public LadderGameResult() { + this(new HashMap<>()); + } - public LadderGameResult(List players, List positions, LadderResult ladderResult) { - this(new PlayerPositions(new Players(players), positions), ladderResult); + public LadderGameResult(Map ladderGameResult) { + this.ladderGameResult = ladderGameResult; } - public LadderGameResult(PlayerPositions playerPositions, LadderResult ladderResult) { - this.playerPositions = playerPositions; - this.ladderResult = ladderResult; + public void add(String name, String result) { + ladderGameResult.put(name, result); } - public String getResult(String playerName) { - return ladderResult.getResultByIndex(playerPositions.getLastPosition(playerName)); + public String getResult(String name) { + return ladderGameResult.get(name); } - public List getResultAll() { - return playerPositions.getPlayers().stream() - .map(player -> player.getName() + " : " + getResult(player.getName())) - .collect(Collectors.toList()); + public StringBuilder getResultAll() { + StringBuilder result = new StringBuilder(); + + ladderGameResult.forEach((key, value) -> { + result.append(key) + .append(" : ") + .append(value) + .append("\n"); + }); + + return result; } } diff --git a/src/main/java/nextstep/ladder/domain/laddergame/position/MoveDirection.java b/src/main/java/nextstep/ladder/domain/laddergame/position/MoveDirection.java new file mode 100644 index 0000000000..7dd5703cfc --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/laddergame/position/MoveDirection.java @@ -0,0 +1,41 @@ +package nextstep.ladder.domain.laddergame.position; + +import java.util.Arrays; + +public enum MoveDirection { + LEFT(-1) { + public boolean canMove(boolean left, boolean right) { + return left && !right; + } + }, + RIGHT(1) { + public boolean canMove(boolean left, boolean right) { + return !left && right; + } + }, + PASS(0) { + public boolean canMove(boolean left, boolean right) { + return !left && !right; + } + }; + + private int direction; + + MoveDirection(int direction) { + this.direction = direction; + } + + public static int move(boolean left, boolean right) { + return Arrays.stream(values()) + .filter(direction -> direction.canMove(left, right)) + .findFirst() + .map(MoveDirection::getDirection) + .orElseThrow(() -> new IllegalArgumentException("포인트를 확인해주세요.")); + } + + public int getDirection() { + return direction; + } + + public abstract boolean canMove(boolean left, boolean right); +} diff --git a/src/main/java/nextstep/ladder/domain/laddergame/position/Position.java b/src/main/java/nextstep/ladder/domain/laddergame/position/Position.java index 6dad0fcc66..bd26005692 100644 --- a/src/main/java/nextstep/ladder/domain/laddergame/position/Position.java +++ b/src/main/java/nextstep/ladder/domain/laddergame/position/Position.java @@ -14,15 +14,7 @@ public Position(int position) { } public Position move(Point point) { - if (point.canMoveLeft()) { - return new Position(position - 1); - } - - if (point.canMoveRight()) { - return new Position(position + 1); - } - - return new Position(position); + return new Position(position + MoveDirection.move(point.canMoveLeft(), point.canMoveRight())); } public int getPosition() { diff --git a/src/main/java/nextstep/ladder/domain/laddergame/position/ResultPosition.java b/src/main/java/nextstep/ladder/domain/laddergame/position/ResultPosition.java new file mode 100644 index 0000000000..ff72869371 --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/laddergame/position/ResultPosition.java @@ -0,0 +1,43 @@ +package nextstep.ladder.domain.laddergame.position; + +import nextstep.ladder.domain.ladder.LadderResult; +import nextstep.ladder.domain.laddergame.LadderGameResult; +import nextstep.ladder.domain.player.Players; + +import java.util.HashMap; +import java.util.Map; +import java.util.stream.IntStream; + +public class ResultPosition { + private final Map positionMap; + + public ResultPosition() { + this(new HashMap<>()); + } + + public ResultPosition(Map positionMap) { + this.positionMap = positionMap; + } + + public void add(int start, int finish) { + positionMap.put(start, finish); + } + + public int getFinishPosition(int startPosition) { + return positionMap.get(startPosition); + } + + public LadderGameResult match(Players players, LadderResult ladderResult) { + LadderGameResult ladderGameResult = new LadderGameResult(); + + IntStream.range(0, players.getPlayers().size()) + .forEach(i -> { + String name = players.getPlayers().get(i).getName(); + String result = ladderResult.getResultByIndex(getFinishPosition(i)); + ladderGameResult.add(name, result); + }); + + return ladderGameResult; + } + +} diff --git a/src/main/java/nextstep/ladder/ui/ResultView.java b/src/main/java/nextstep/ladder/ui/ResultView.java index c2da0ba967..38e6052a58 100644 --- a/src/main/java/nextstep/ladder/ui/ResultView.java +++ b/src/main/java/nextstep/ladder/ui/ResultView.java @@ -74,14 +74,14 @@ public static String generatePrintingLine(Line line, int pointSize) { return stringBuilder.toString(); } - public static void printGameResult(LadderGameResult ladderGameResult) { + public static void printGameResult2(LadderGameResult ladderGameResult) { String playerName = InputView.getResultPlayer(); System.out.print(PLAY_RESULT_MESSAGE); Optional.of(playerName) .filter("all"::equals) .ifPresentOrElse( - name -> ladderGameResult.getResultAll().forEach(System.out::println), + name -> System.out.println(ladderGameResult.getResultAll()), () -> System.out.println(ladderGameResult.getResult(playerName)) ); } diff --git a/src/test/java/nextstep/ladder/domain/laddergame/LadderGameResultTest.java b/src/test/java/nextstep/ladder/domain/laddergame/LadderGameResultTest.java index dce227a53f..1cf01a92b3 100644 --- a/src/test/java/nextstep/ladder/domain/laddergame/LadderGameResultTest.java +++ b/src/test/java/nextstep/ladder/domain/laddergame/LadderGameResultTest.java @@ -1,51 +1,41 @@ package nextstep.ladder.domain.laddergame; -import nextstep.ladder.domain.ladder.Ladder; -import nextstep.ladder.domain.ladder.LadderResultTest; -import nextstep.ladder.domain.ladder.LadderTest; -import nextstep.ladder.domain.laddergame.position.Positions; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; -import java.util.ArrayList; -import java.util.List; +import java.util.Map; class LadderGameResultTest { @Test - void getResult() { - Ladder ladder = LadderTest.getLadder(); + void create() { + LadderGameResult ladderGameResult = new LadderGameResult(); - List positionsList = new ArrayList<>(); - - for (int i = 0; i < ladder.getPlayers().size(); i++) { - Positions positions = new Positions(); - positionsList.add(positions.add(ladder.getLines(), i)); - } + Assertions.assertThat(ladderGameResult).isNotNull(); + } - LadderGameResult ladderGameResult = new LadderGameResult(ladder.getPlayers(), positionsList, LadderResultTest.ladderResult); + @Test + void create_map() { + LadderGameResult ladderGameResult = new LadderGameResult(Map.of("test1", "5000")); - Assertions.assertThat(ladderGameResult.getResult("test1")).isEqualTo("10000원"); - Assertions.assertThat(ladderGameResult.getResult("test2")).isEqualTo("5000원"); - Assertions.assertThat(ladderGameResult.getResult("test3")).isEqualTo("15000원"); - Assertions.assertThat(ladderGameResult.getResult("test4")).isEqualTo("꽝"); + Assertions.assertThat(ladderGameResult).isNotNull(); } @Test - void getResultAll() { - Ladder ladder = LadderTest.getLadder(); + void add_and_getResult() { + LadderGameResult ladderGameResult = new LadderGameResult(); - List positionsList = new ArrayList<>(); + ladderGameResult.add("test1", "5000"); - for (int i = 0; i < ladder.getPlayers().size(); i++) { - Positions positions = new Positions(); - positionsList.add(positions.add(ladder.getLines(), i)); - } + Assertions.assertThat(ladderGameResult.getResult("test1")).isEqualTo("5000"); + } - LadderGameResult ladderGameResult = new LadderGameResult(ladder.getPlayers(), positionsList, LadderResultTest.ladderResult); + @Test + void getResultAll() { + LadderGameResult ladderGameResult = new LadderGameResult(Map.of("test1", "5000")); - Assertions.assertThat(ladderGameResult.getResultAll()).isNotNull(); - Assertions.assertThat(ladderGameResult.getResultAll().size()).isEqualTo(4); - Assertions.assertThat(ladderGameResult.getResultAll().get(0)).isEqualTo("test1 : 10000원"); + Assertions.assertThat(ladderGameResult.getResultAll()).contains("test1"); + Assertions.assertThat(ladderGameResult.getResultAll()).contains(":"); + Assertions.assertThat(ladderGameResult.getResultAll()).contains("5000"); } } \ No newline at end of file diff --git a/src/test/java/nextstep/ladder/domain/laddergame/LadderGameTest.java b/src/test/java/nextstep/ladder/domain/laddergame/LadderGameTest.java new file mode 100644 index 0000000000..592ca4a84b --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/laddergame/LadderGameTest.java @@ -0,0 +1,19 @@ +package nextstep.ladder.domain.laddergame; + +import nextstep.ladder.domain.ladder.LadderTest; +import nextstep.ladder.domain.laddergame.position.ResultPosition; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +class LadderGameTest { + + @Test + void play() { + ResultPosition resultPosition = new LadderGame().play(LadderTest.getLadder()); + + Assertions.assertThat(resultPosition.getFinishPosition(0)).isEqualTo(2); + Assertions.assertThat(resultPosition.getFinishPosition(1)).isEqualTo(1); + Assertions.assertThat(resultPosition.getFinishPosition(2)).isEqualTo(3); + Assertions.assertThat(resultPosition.getFinishPosition(3)).isEqualTo(0); + } +} \ No newline at end of file diff --git a/src/test/java/nextstep/ladder/domain/laddergame/position/MoveDirectionTest.java b/src/test/java/nextstep/ladder/domain/laddergame/position/MoveDirectionTest.java new file mode 100644 index 0000000000..4f2edfe5f6 --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/laddergame/position/MoveDirectionTest.java @@ -0,0 +1,50 @@ +package nextstep.ladder.domain.laddergame.position; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class MoveDirectionTest { + + @Test + @DisplayName("왼쪽으로 이동이 가능할 경우 -1을 반환한다") + void left() { + Assertions.assertThat(MoveDirection.LEFT.getDirection()).isEqualTo(-1); + } + + @Test + @DisplayName("현재 위치가 1이고, 왼쪽으로 이동이 가능할 경우 최종 위치는 0이 된다") + void move_left_position() { + Position position = new Position(1 + MoveDirection.LEFT.getDirection()); + + Assertions.assertThat(position.getPosition()).isEqualTo(0); + } + + @Test + @DisplayName("오른쪽으로 이동이 가능할 경우 +1을 반환한다") + void right() { + Assertions.assertThat(MoveDirection.RIGHT.getDirection()).isEqualTo(1); + } + + @Test + @DisplayName("현재 위치가 1이고, 오른쪽으로 이동이 가능할 경우 최종 위치는 2가 된다") + void move_right_position() { + Position position = new Position(1 + MoveDirection.RIGHT.getDirection()); + + Assertions.assertThat(position.getPosition()).isEqualTo(2); + } + + @Test + @DisplayName("왼쪽, 오른쪽 모두 이동이 불가능한 경우 0을 반환한다") + void pass() { + Assertions.assertThat(MoveDirection.PASS.getDirection()).isEqualTo(0); + } + + @Test + @DisplayName("현재 위치가 1이고, 왼쪽과 오른쪽 모두 이동이 불가능한 경우 최종 위치는 그대로 1이 된다") + void move_pass_position() { + Position position = new Position(1 + MoveDirection.PASS.getDirection()); + + Assertions.assertThat(position.getPosition()).isEqualTo(1); + } +} \ No newline at end of file diff --git a/src/test/java/nextstep/ladder/domain/laddergame/position/ResultPositionTest.java b/src/test/java/nextstep/ladder/domain/laddergame/position/ResultPositionTest.java new file mode 100644 index 0000000000..61a3f6fa33 --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/laddergame/position/ResultPositionTest.java @@ -0,0 +1,57 @@ +package nextstep.ladder.domain.laddergame.position; + +import nextstep.ladder.domain.ladder.LadderResult; +import nextstep.ladder.domain.laddergame.LadderGameResult; +import nextstep.ladder.domain.player.Player; +import nextstep.ladder.domain.player.Players; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class ResultPositionTest { + + @Test + void create() { + ResultPosition resultPosition = new ResultPosition(); + + Assertions.assertThat(resultPosition).isNotNull(); + } + + @Test + void create_map() { + ResultPosition resultPosition = new ResultPosition(Map.of(0, 3)); + + Assertions.assertThat(resultPosition).isNotNull(); + } + + @Test + void getFinishPosition() { + ResultPosition resultPosition = new ResultPosition(Map.of(0, 3)); + + Assertions.assertThat(resultPosition.getFinishPosition(0)).isEqualTo(3); + } + + @Test + void match() { + Map positionMap = new HashMap<>(); + positionMap.put(0, 1); + positionMap.put(1, 0); + + ResultPosition resultPosition = new ResultPosition(positionMap); + + Player player1 = new Player("test1"); + Player player2 = new Player("test2"); + Players players = new Players(List.of(player1, player2)); + + LadderResult ladderResult = new LadderResult(List.of("꽝", "5000")); + + LadderGameResult gameResult = resultPosition.match(players, ladderResult); + + Assertions.assertThat(gameResult.getResult("test1")).isEqualTo("5000"); + Assertions.assertThat(gameResult.getResult("test2")).isEqualTo("꽝"); + } + +} \ No newline at end of file