From fcbf2e3279ec5ec4bcbea83c89210eca376a393c Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 11:06:39 +0900 Subject: [PATCH 01/80] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EC=8B=A4?= =?UTF-8?q?=ED=96=89=20=EB=A1=9C=EC=A7=81=EC=97=90=EC=84=9C=20=ED=83=80?= =?UTF-8?q?=EC=9E=85=20=ED=99=95=EC=9D=B8=20=EC=A0=9C=EA=B1=B0=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/controller/ChessGameController.java | 15 +++++++-------- .../chess/controller/command/Command.java | 6 +----- .../chess/controller/command/EndCommand.java | 13 ++----------- .../controller/command/ExecuteResult.java | 19 +++++++++++++++++++ .../chess/controller/command/MoveCommand.java | 13 ++----------- .../controller/command/StartCommand.java | 13 ++----------- 6 files changed, 33 insertions(+), 46 deletions(-) create mode 100644 src/main/java/chess/controller/command/ExecuteResult.java diff --git a/src/main/java/chess/controller/ChessGameController.java b/src/main/java/chess/controller/ChessGameController.java index 4d4c34956e9..6161e18bd2a 100644 --- a/src/main/java/chess/controller/ChessGameController.java +++ b/src/main/java/chess/controller/ChessGameController.java @@ -1,6 +1,7 @@ package chess.controller; import chess.controller.command.Command; +import chess.controller.command.ExecuteResult; import chess.domain.ChessGame; import chess.domain.board.ChessBoard; import chess.domain.board.ChessBoardCreator; @@ -18,16 +19,14 @@ public ChessGameController(InputView inputView, OutputView outputView) { public void run() { outputView.printStartMessage(); - Command command = inputView.readCommand(); - if (command.isNotStartCommand()) { - return; - } - ChessGame chessGame = initializeChessGame(); - while (command.isNotEndCommand()) { - command.execute(chessGame, outputView); - command = inputView.readCommand(); + + ExecuteResult result; + do { + Command command = inputView.readCommand(); + result = command.execute(chessGame, outputView); } + while (result.isSuccess() && result.isNeedNextCommand()); } private ChessGame initializeChessGame() { diff --git a/src/main/java/chess/controller/command/Command.java b/src/main/java/chess/controller/command/Command.java index 0ed9399be92..bd076ac4ff3 100644 --- a/src/main/java/chess/controller/command/Command.java +++ b/src/main/java/chess/controller/command/Command.java @@ -4,9 +4,5 @@ import chess.view.OutputView; public interface Command { - void execute(ChessGame chessGame, OutputView outputView); - - boolean isNotEndCommand(); - - boolean isNotStartCommand(); + ExecuteResult execute(ChessGame chessGame, OutputView outputView); } diff --git a/src/main/java/chess/controller/command/EndCommand.java b/src/main/java/chess/controller/command/EndCommand.java index 7d91d8c77af..db380f46762 100644 --- a/src/main/java/chess/controller/command/EndCommand.java +++ b/src/main/java/chess/controller/command/EndCommand.java @@ -5,16 +5,7 @@ public class EndCommand implements Command { @Override - public void execute(ChessGame chessGame, OutputView outputView) { - } - - @Override - public boolean isNotEndCommand() { - return false; - } - - @Override - public boolean isNotStartCommand() { - return true; + public ExecuteResult execute(ChessGame chessGame, OutputView outputView) { + return new ExecuteResult(true, false); } } diff --git a/src/main/java/chess/controller/command/ExecuteResult.java b/src/main/java/chess/controller/command/ExecuteResult.java new file mode 100644 index 00000000000..b9b8371d19a --- /dev/null +++ b/src/main/java/chess/controller/command/ExecuteResult.java @@ -0,0 +1,19 @@ +package chess.controller.command; + +public class ExecuteResult { + private final boolean success; + private final boolean needNextCommand; + + public ExecuteResult(boolean success, boolean needNextCommand) { + this.success = success; + this.needNextCommand = needNextCommand; + } + + public boolean isSuccess() { + return success; + } + + public boolean isNeedNextCommand() { + return needNextCommand; + } +} diff --git a/src/main/java/chess/controller/command/MoveCommand.java b/src/main/java/chess/controller/command/MoveCommand.java index 8b333094a90..6434c6fae89 100644 --- a/src/main/java/chess/controller/command/MoveCommand.java +++ b/src/main/java/chess/controller/command/MoveCommand.java @@ -14,18 +14,9 @@ public MoveCommand(Position start, Position destination) { } @Override - public void execute(ChessGame chessGame, OutputView outputView) { + public ExecuteResult execute(ChessGame chessGame, OutputView outputView) { chessGame.move(start, destination); outputView.printChessBoardMessage(chessGame.getChessBoard()); - } - - @Override - public boolean isNotEndCommand() { - return true; - } - - @Override - public boolean isNotStartCommand() { - return true; + return new ExecuteResult(true, true); } } diff --git a/src/main/java/chess/controller/command/StartCommand.java b/src/main/java/chess/controller/command/StartCommand.java index 1ad36bfc2c1..e52a95952e6 100644 --- a/src/main/java/chess/controller/command/StartCommand.java +++ b/src/main/java/chess/controller/command/StartCommand.java @@ -5,17 +5,8 @@ public class StartCommand implements Command { @Override - public void execute(ChessGame chessGame, OutputView outputView) { + public ExecuteResult execute(ChessGame chessGame, OutputView outputView) { outputView.printChessBoardMessage(chessGame.getChessBoard()); - } - - @Override - public boolean isNotEndCommand() { - return true; - } - - @Override - public boolean isNotStartCommand() { - return false; + return new ExecuteResult(true, true); } } From 72d8cf8e8fcad2af60e329cfa02ee1ebb9b30ebf Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 11:12:09 +0900 Subject: [PATCH 02/80] =?UTF-8?q?feat:=20=EB=B3=B4=EB=93=9C=EC=9D=98=20?= =?UTF-8?q?=EC=B1=85=EC=9E=84=EA=B3=BC=20=EC=B2=B4=EC=8A=A4=EB=B3=B4?= =?UTF-8?q?=EB=93=9C=EC=9D=98=20=EA=B2=80=EC=A6=9D=20=EC=B1=85=EC=9E=84=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/ChessGame.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/main/java/chess/domain/ChessGame.java b/src/main/java/chess/domain/ChessGame.java index c7b8ca7ecdf..b625e113b46 100644 --- a/src/main/java/chess/domain/ChessGame.java +++ b/src/main/java/chess/domain/ChessGame.java @@ -17,10 +17,7 @@ public ChessGame(ChessBoard chessBoard) { } public void move(Position start, Position destiantion) { - validatePieceExist(start); validateTurn(chessBoard.findPieceByPosition(start)); - validateLegalMovement(start, destiantion); - chessBoard.move(start, destiantion); turn = turn.otherTeam(); } @@ -29,21 +26,9 @@ public ChessBoard getChessBoard() { return chessBoard; } - private void validatePieceExist(Position start) { - if (chessBoard.positionIsEmpty(start)) { - throw new IllegalArgumentException("움직임을 시작하는 위치에 기물이 존재하지 않습니다"); - } - } - private void validateTurn(Piece piece) { if (piece.isOtherTeam(turn)) { throw new IllegalArgumentException(turn + "의 차례입니다"); } } - - private void validateLegalMovement(Position start, Position destination) { - if (!chessBoard.canMove(start, destination)) { - throw new IllegalArgumentException("기물의 행마법에 어긋나는 움직임입니다"); - } - } } From c3cae93356c92a3eacd0996569a9ecf4670e1e1f Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 11:23:06 +0900 Subject: [PATCH 03/80] =?UTF-8?q?test:=20=EC=B2=B4=EC=8A=A4=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20=EB=8F=84=EB=A9=94=EC=9D=B8=20=ED=84=B4=20=EA=B0=9C?= =?UTF-8?q?=EB=85=90=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/chess/domain/ChessGameTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/test/java/chess/domain/ChessGameTest.java diff --git a/src/test/java/chess/domain/ChessGameTest.java b/src/test/java/chess/domain/ChessGameTest.java new file mode 100644 index 00000000000..ad0f2f3c2a9 --- /dev/null +++ b/src/test/java/chess/domain/ChessGameTest.java @@ -0,0 +1,43 @@ +package chess.domain; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import chess.domain.board.ChessBoard; +import chess.domain.piece.King; +import chess.domain.piece.Piece; +import chess.domain.piece.Team; +import chess.domain.position.Position; +import chess.fixture.PositionFixtures; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class ChessGameTest { + @DisplayName("턴에 맞는 움직임을 시도하면 예외를 발생시키지 않는다.") + @Test + void should_DoesNotThrowAnyException_When_MovementTurnIsValid() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, new King(Team.WHITE)); + board.put(PositionFixtures.A8, new King(Team.BLACK)); + ChessBoard chessBoard = new ChessBoard(board); + ChessGame chessGame = new ChessGame(chessBoard); + + assertThatCode(() -> chessGame.move(PositionFixtures.A1, PositionFixtures.A2)) + .doesNotThrowAnyException(); + } + + @DisplayName("턴에 맞지 않는 움직임을 시도하면 예외를 발생시킨다") + @Test + void should_ThrowException_When_IllegalTurnMove() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, new King(Team.WHITE)); + board.put(PositionFixtures.A8, new King(Team.BLACK)); + ChessBoard chessBoard = new ChessBoard(board); + ChessGame chessGame = new ChessGame(chessBoard); + + assertThatThrownBy(() -> chessGame.move(PositionFixtures.A8, PositionFixtures.A7)) + .isInstanceOf(IllegalArgumentException.class); + } +} From 61d679ff4136aeaf5e787fd3a0d7cacd11f4459b Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 11:27:47 +0900 Subject: [PATCH 04/80] =?UTF-8?q?test:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20static=20import=20=EC=A0=9C=EA=B1=B0,=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EA=B0=80=EB=8F=85=EC=84=B1=20=ED=96=A5?= =?UTF-8?q?=EC=83=81=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/domain/position/DirectionTest.java | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/test/java/chess/domain/position/DirectionTest.java b/src/test/java/chess/domain/position/DirectionTest.java index 671a989346b..a2622f5c36f 100644 --- a/src/test/java/chess/domain/position/DirectionTest.java +++ b/src/test/java/chess/domain/position/DirectionTest.java @@ -1,19 +1,11 @@ package chess.domain.position; -import static chess.domain.position.Direction.E; -import static chess.domain.position.Direction.N; -import static chess.domain.position.Direction.NE; -import static chess.domain.position.Direction.NW; -import static chess.domain.position.Direction.S; -import static chess.domain.position.Direction.SE; -import static chess.domain.position.Direction.SW; -import static chess.domain.position.Direction.W; -import static chess.domain.position.Direction.of; + import static chess.fixture.PositionFixtures.A1; import static chess.fixture.PositionFixtures.A2; import static chess.fixture.PositionFixtures.B1; import static chess.fixture.PositionFixtures.B2; -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -23,48 +15,48 @@ class DirectionTest { @DisplayName("방향을 계산할 수 있다 : a1 -> a2 = N") @Test void should_ReturnDirectionNorth() { - assertThat(of(A1, A2)).isEqualTo(N); + assertThat(Direction.of(A1, A2)).isEqualTo(Direction.N); } @DisplayName("방향을 계산할 수 있다 : a1 -> b1 = E") @Test void should_ReturnDirectionEast() { - assertThat(of(A1, B1)).isEqualTo(E); + assertThat(Direction.of(A1, B1)).isEqualTo(Direction.E); } @DisplayName("방향을 계산할 수 있다 : b1 -> a1 = W") @Test void should_ReturnDirectionWest() { - assertThat(of(B1, A1)).isEqualTo(W); + assertThat(Direction.of(B1, A1)).isEqualTo(Direction.W); } @DisplayName("방향을 계산할 수 있다 : a2 -> a1 = S") @Test void should_ReturnDirectionSouth() { - assertThat(of(A2, A1)).isEqualTo(S); + assertThat(Direction.of(A2, A1)).isEqualTo(Direction.S); } @DisplayName("방향을 계산할 수 있다 : a1 -> b2 = NE") @Test void should_ReturnDirectionNorthEast() { - assertThat(of(A1, B2)).isEqualTo(NE); + assertThat(Direction.of(A1, B2)).isEqualTo(Direction.NE); } @DisplayName("방향을 계산할 수 있다 : b2 -> a1 = SW") @Test void should_ReturnDirectionSouthWest() { - assertThat(of(B2, A1)).isEqualTo(SW); + assertThat(Direction.of(B2, A1)).isEqualTo(Direction.SW); } @DisplayName("방향을 계산할 수 있다 : b1 -> a2 = NW") @Test void should_ReturnDirectionNorthWest() { - assertThat(of(B1, A2)).isEqualTo(NW); + assertThat(Direction.of(B1, A2)).isEqualTo(Direction.NW); } @DisplayName("방향을 계산할 수 있다 : a2 -> b1 = SE") @Test void should_ReturnDirectionSoutWest() { - assertThat(of(A2, B1)).isEqualTo(SE); + assertThat(Direction.of(A2, B1)).isEqualTo(Direction.SE); } } From ce8bffc57c30a67b98232b19aa1ef38b4cab9e7c Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 11:28:45 +0900 Subject: [PATCH 05/80] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=A3=BC=EC=84=9D=20=EC=A0=9C=EA=B1=B0=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/piece/Pawn.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/main/java/chess/domain/piece/Pawn.java b/src/main/java/chess/domain/piece/Pawn.java index 562c3402e23..51059120d4f 100644 --- a/src/main/java/chess/domain/piece/Pawn.java +++ b/src/main/java/chess/domain/piece/Pawn.java @@ -16,19 +16,15 @@ public Pawn(Team team) { @Override boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { int distance = start.calculateDistance(destination); - //폰의 이동 방향이 팀의 전진 방향과 다르다면 이동할 수 없다 if (!team.isTeamForwardDirectionsContains(Direction.of(start, destination))) { return true; } - //폰은 팀의 수직 전진 방향으로 한 칸 이동할 수 있다 if (distance == DEFAULT_MOVE_DISTANCE && start.isOrthogonalWith(destination)) { return false; } - //폰은 팀의 수직 전진 방향으로 두 칸 이동할 수 있다 if (distance == INITIAL_MOVE_DISTANCE && start.isOrthogonalWith(destination)) { return false; } - // 폰은 대각선으로 한 칸 이동할 수 있다 if (distance == DEFAULT_MOVE_DISTANCE && start.isDiagonalWith(destination)) { return false; } @@ -38,19 +34,15 @@ boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { @Override boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { int distance = start.calculateDistance(destination); - // 폰의 이동 경로에 기물이 있다면 이동할 수 없다 if (chessBoard.isPathHasObstacle(start.calculateSlidingPath(destination))) { return true; } - // 폰이 수직으로 이동했고 도착지에 기물이 있다면 이동할 수 없다 if (!chessBoard.positionIsEmpty(destination) && start.isOrthogonalWith(destination)) { return true; } - // 폰이 두칸 수직으로 이동할 때 초기 위치에서 움직이지 않았다면 이동할 수 없다 if (distance == INITIAL_MOVE_DISTANCE) { return !team.isPositionOnTeamInitialPawnRank(start); } - // 다른 팀의 기물이 목적지에 존재하지 않을 경우 폰은 대각선으로 이동할 수 없다. if (distance == DEFAULT_MOVE_DISTANCE && start.isDiagonalWith(destination)) { return chessBoard.isNoHostilePieceAt(destination, team); } From 7b5c587ba840754b8c512a8875f682a3a93a833a Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 11:40:33 +0900 Subject: [PATCH 06/80] =?UTF-8?q?test:=20Piece=20=EA=B8=B0=EB=AC=BC=20?= =?UTF-8?q?=EA=B3=B5=ED=86=B5=20=EB=A1=9C=EC=A7=81=20=EB=88=84=EB=9D=BD?= =?UTF-8?q?=EB=90=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/chess/domain/piece/PieceTest.java | 66 +++++++++++++++++-- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/src/test/java/chess/domain/piece/PieceTest.java b/src/test/java/chess/domain/piece/PieceTest.java index cc80763f011..57513858507 100644 --- a/src/test/java/chess/domain/piece/PieceTest.java +++ b/src/test/java/chess/domain/piece/PieceTest.java @@ -3,16 +3,50 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; +import chess.domain.board.ChessBoard; +import chess.domain.position.Position; +import chess.fixture.PositionFixtures; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; class PieceTest { + private Piece blackPiece; + private Piece whitePiece; + + + @BeforeEach + void setUp() { + blackPiece = new Piece(Team.BLACK) { + @Override + boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { + return false; + } + + @Override + boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { + return false; + } + }; + + whitePiece = new Piece(Team.WHITE) { + @Override + boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { + return false; + } + + @Override + boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { + return false; + } + }; + } + @DisplayName("기물의 팀이 흑팀인지 확인할 수 있다") @Test void should_CheckPieceIsBlackTeam() { - King blackPiece = new King(Team.BLACK); - King whitePiece = new King(Team.WHITE); - assertAll( () -> assertThat(blackPiece.isBlackTeam()).isTrue(), () -> assertThat(whitePiece.isBlackTeam()).isFalse() @@ -22,12 +56,32 @@ void should_CheckPieceIsBlackTeam() { @DisplayName("기물간 서로 같은 팀인지 다른 팀인지 확인할 수 있다") @Test void should_ComparePieceTeam() { - King blackPiece = new King(Team.BLACK); - King whitePiece = new King(Team.WHITE); - assertAll( () -> assertThat(blackPiece.isOtherTeam(Team.WHITE)).isTrue(), () -> assertThat(whitePiece.isOtherTeam(Team.WHITE)).isFalse() ); } + + @DisplayName("모든 기물은 시작위치와 같은 곳으로 이동하지 못한다") + @Test + void should_AllPieceCanNotMove_When_StartAndDestinationIsSame() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, whitePiece); + ChessBoard chessBoard = new ChessBoard(board); + boolean canMoveSamePosition = whitePiece.canMove(PositionFixtures.A1, PositionFixtures.A1, chessBoard); + + assertThat(canMoveSamePosition).isFalse(); + } + + @DisplayName("모든 기물은 도착지에 아군이 있는 경우 이동하지 못한다") + @Test + void should_AllPieceCanNotMove_When_FriendlyPieceAtDestination() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, whitePiece); + board.put(PositionFixtures.A2, new King(Team.WHITE)); + ChessBoard chessBoard = new ChessBoard(board); + boolean canMoveFriendlyPiecePosition = whitePiece.canMove(PositionFixtures.A1, PositionFixtures.A2, chessBoard); + + assertThat(canMoveFriendlyPiecePosition).isFalse(); + } } From 60e59aae20494c3a267d8b382826bde1404549c8 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 11:45:24 +0900 Subject: [PATCH 07/80] =?UTF-8?q?refactor:=20=EC=9D=98=EB=AF=B8=20?= =?UTF-8?q?=EC=9E=88=EB=8A=94=20=EB=A7=A4=EC=A7=81=20=EB=84=98=EB=B2=84=20?= =?UTF-8?q?=EC=83=81=EC=88=98=ED=99=94=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/view/CommandParser.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/chess/view/CommandParser.java b/src/main/java/chess/view/CommandParser.java index e580e791965..c2308b5cc71 100644 --- a/src/main/java/chess/view/CommandParser.java +++ b/src/main/java/chess/view/CommandParser.java @@ -18,6 +18,7 @@ public class CommandParser { private static final int RANK_INDEX = 1; private static final char RANK_TO_ROW_NUMBER_WEIGHT = '0'; private static final char FILE_TO_COLUMN_NUMBER_WEIGHT = 'a'; + public static final int MAX_RANK = 8; public Command parse(String input) { if (input.startsWith(START)) { @@ -36,7 +37,7 @@ public Command parse(String input) { } private Position parsePosition(String input) { - Rank rank = Rank.from(8 - (input.charAt(RANK_INDEX) - RANK_TO_ROW_NUMBER_WEIGHT)); + Rank rank = Rank.from(MAX_RANK - (input.charAt(RANK_INDEX) - RANK_TO_ROW_NUMBER_WEIGHT)); File file = File.from(input.charAt(FILE_INDEX) - FILE_TO_COLUMN_NUMBER_WEIGHT); return new Position(file, rank); } From 7367eb18b3c2853c330d046ccb51669e3128c7c7 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 11:55:25 +0900 Subject: [PATCH 08/80] =?UTF-8?q?fix:=20=EB=AA=85=EB=A0=B9=EC=96=B4=20?= =?UTF-8?q?=EB=B3=80=ED=99=98=20=EA=B3=BC=EC=A0=95=20=EA=B2=AC=EA=B3=A0?= =?UTF-8?q?=ED=99=94=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/view/CommandParser.java | 4 +- .../java/chess/view/CommandParserTest.java | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/test/java/chess/view/CommandParserTest.java diff --git a/src/main/java/chess/view/CommandParser.java b/src/main/java/chess/view/CommandParser.java index c2308b5cc71..e2766bf9463 100644 --- a/src/main/java/chess/view/CommandParser.java +++ b/src/main/java/chess/view/CommandParser.java @@ -21,10 +21,10 @@ public class CommandParser { public static final int MAX_RANK = 8; public Command parse(String input) { - if (input.startsWith(START)) { + if (input.equals(START)) { return new StartCommand(); } - if (input.startsWith(END)) { + if (input.equals(END)) { return new EndCommand(); } if (input.startsWith(MOVE)) { diff --git a/src/test/java/chess/view/CommandParserTest.java b/src/test/java/chess/view/CommandParserTest.java new file mode 100644 index 00000000000..45706eec8ef --- /dev/null +++ b/src/test/java/chess/view/CommandParserTest.java @@ -0,0 +1,40 @@ +package chess.view; + +import static org.assertj.core.api.Assertions.assertThat; + +import chess.controller.command.EndCommand; +import chess.controller.command.MoveCommand; +import chess.controller.command.StartCommand; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class CommandParserTest { + private CommandParser commandParser; + + @BeforeEach + void setUp() { + commandParser = new CommandParser(); + } + + @DisplayName("시작 입력이 들어오면 시작 명령으로 파싱할 수 있다") + @Test + void should_ParseStartCommand_When_InputIsAboutStart() { + String input = "start"; + assertThat(commandParser.parse(input)).isInstanceOf(StartCommand.class); + } + + @DisplayName("종료 입력이 들어오면 종료 명령으로 파싱할 수 있다") + @Test + void should_ParseEndCommand_When_InputIsAboutEnd() { + String input = "end"; + assertThat(commandParser.parse(input)).isInstanceOf(EndCommand.class); + } + + @DisplayName("움직임 입력이 들어오면 움직임 명령으로 파싱할 수 있다") + @Test + void should_ParseMoveCommand_When_InputIsAboutMove() { + String input = "move b2 b4"; + assertThat(commandParser.parse(input)).isInstanceOf(MoveCommand.class); + } +} From 40c5e337381ab230113e9472bb4f2311fef637fd Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 13:29:12 +0900 Subject: [PATCH 09/80] =?UTF-8?q?docs:=203=EB=8B=A8=EA=B3=84=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1e9660d7ca5..647a3470d0a 100644 --- a/README.md +++ b/README.md @@ -7,15 +7,22 @@ ## 1-2단계 요구사항 -콘솔 UI에서 체스 게임을 할 수 있는 기능을 구현한다. -1단계는 체스 게임을 할 수 있는 체스판을 초기화한다. -체스판에서 말의 위치 값은 가로 위치는 왼쪽부터 a ~ h이고, 세로는 아래부터 위로 1 ~ 8로 구현한다. +- 콘솔 UI에서 체스 게임을 할 수 있는 기능을 구현한다. +- 1단계는 체스 게임을 할 수 있는 체스판을 초기화한다. +- 체스판에서 말의 위치 값은 가로 위치는 왼쪽부터 a ~ h이고, 세로는 아래부터 위로 1 ~ 8로 구현한다. + +# 3단계 요구사항 + +- 체스 게임은 상대편 King이 잡히는 경우 게임에서 진다. King이 잡혔을 때 게임을 종료해야 한다. +- 체스 게임은 현재 남아 있는 말에 대한 점수를 구할 수 있어야 한다. +- "status" 명령을 입력하면 각 진영의 점수를 출력하고 어느 진영이 이겼는지 결과를 볼 수 있어야 한다. ### 입력 요구사항 - [x] 체스 게임을 시작할지 여부를 입력받을 수 있다. - [x] 체스 게임을 종료할지 여부를 입력받을 수 있다. -- [x] 체스 말의 이동정보를 입력받을 수 있다 +- [x] 체스 말의 이동정보를 입력받을 수 있다. +- [ ] 각 진영의 점수를 확인하는 명령어를 입력받을 수 있다. ``` move b2 b3 ``` @@ -66,5 +73,14 @@ - [x] 기물들이 이동할 수 없다면 기물의 위치를 옮기지 않는다 - [x] 도착지에 같은 팀의 기물이 있다면 이동할 수 없다 - [x] 비숍/룩/퀸은 이동 경로에 다른 기물이 있다면 이동할 수 없다 - + +- [ ] 체스 프로그램에서 현재까지 남아 있는 말에 따라 점수를 계산할 수 있어야 한다. + - [ ] Queen은 9점 + - [ ] Rook은 5점 + - [ ] Bishop은 3점 + - [ ] Knight는 2.5점 + - [ ] Pawn의 기본 점수는 1점, 같은 세로줄에 같은 색의 폰이 있는 경우는 0.5점으로 계산 + - [ ] King은 점수가 없음 (잡히는 경우 게임 끝) +- [ ] King이 잡히는 경우 경기에서 진다. +- [ ] King이 잡히는 경우 게임이 종료된다. From 6d24abb9cebb9df0e6b9c75557f6c91671bab471 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 14:04:06 +0900 Subject: [PATCH 10/80] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EC=9A=94?= =?UTF-8?q?=EA=B5=AC=EC=82=AC=ED=95=AD=20=EA=B0=84=EB=9E=B5=ED=99=94=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 131 ++++++++++++++++++++++++++---------------------------- 1 file changed, 63 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 647a3470d0a..d71aabf348d 100644 --- a/README.md +++ b/README.md @@ -1,86 +1,81 @@ # java-chess -## 체스 게임 설명 +### 입력 요구사항 + +- [x] 체스 게임을 시작하는 명령어를 입력받을 수 있다. +- [x] 체스 게임을 종료하는 명령어를 입력받을 수 있다. +- [x] 체스 말을 이동하는 명령어를 입력받을 수 있다. +- [ ] 각 진영의 점수를 확인하는 명령어를 입력받을 수 있다. +- [x] 명령어 입력시 형식에 맞는 입력이 들어오지 않으면 예외를 발생시킨다. -- 체스 게임은 화이트 팀과 블랙 팀으로 나뉜다 -- 체스 게임이 시작되면 화이트 팀부터 한 수씩 턴이 진행된다 +### 출력 요구사항 -## 1-2단계 요구사항 +- [x] 체스판의 초기 상태를 출력할 수 있다 +- [x] 매 턴마다 최신화된 체스판의 상태를 출력할 수 있다 +- [x] 체스판에서 각 진영은 대문자(검은색)와 소문자(흰색)로 출력한다. -- 콘솔 UI에서 체스 게임을 할 수 있는 기능을 구현한다. -- 1단계는 체스 게임을 할 수 있는 체스판을 초기화한다. -- 체스판에서 말의 위치 값은 가로 위치는 왼쪽부터 a ~ h이고, 세로는 아래부터 위로 1 ~ 8로 구현한다. +### 도메인 요구사항 -# 3단계 요구사항 +#### 기물 -- 체스 게임은 상대편 King이 잡히는 경우 게임에서 진다. King이 잡혔을 때 게임을 종료해야 한다. -- 체스 게임은 현재 남아 있는 말에 대한 점수를 구할 수 있어야 한다. -- "status" 명령을 입력하면 각 진영의 점수를 출력하고 어느 진영이 이겼는지 결과를 볼 수 있어야 한다. +- [x] 기물들은 화이트 또는 블랙의 팀을 가진다. +- [x] 기물들은 각각의 행마법에 따라 이동할 수 있다 -### 입력 요구사항 +``` +- 룩은 직선으로 이동할 수 있다 +- 비숍은 대각선으로 이동할 수 있다 +- 퀸은 직선/대각선으로 이동할 수 있다 +- 룩과 비숍, 퀸은 이동하는 경로에 기물이 존재하면 이동할 수 없다. +- 킹은 방향과 무관하게 1칸 이동할 수 있다 +- 나이트는 알파벳 L자 모양으로 앞으로 두칸 이동한 다음 왼쪽, 오른쪽 으로 한칸 움직일 수 있다 +- 폰은 초기 상태에서 한칸 또는 두칸씩 전진할 수 있다 +- 폰은 초기 상태에서 움직인 이후에는 한칸씩만 전진할 수 있다 +- 폰은 상대 기물이 한칸 대각선에 있다면, 이동할 수 있다 +- 모든 기물은 도착지에 같은 팀의 기물이 있다면 이동할 수 없다 +``` -- [x] 체스 게임을 시작할지 여부를 입력받을 수 있다. -- [x] 체스 게임을 종료할지 여부를 입력받을 수 있다. -- [x] 체스 말의 이동정보를 입력받을 수 있다. -- [ ] 각 진영의 점수를 확인하는 명령어를 입력받을 수 있다. +#### 체스 보드 초기화 + +- [x] 게임 시작시 체스판을 초기화할 수 있다. +- [x] 체스판의 초기상태는 다음과 같다. ``` - move b2 b3 + RNBQKBNR + PPPPPPPP + ........ + ........ + ........ + ........ + pppppppp + rnbqkbnr ``` - - [x] 요구하는 형식의 입력이 들어오지 않으면 예외를 발생시킨다. +- [x] 체스판의 열은 왼쪽부터 a-h까지의 알파벳으로 이루어져 있다. +- [x] 체스판의 행은 밑부터 1-8까지의 숫자로 이루어져 있다. -### 출력 요구사항 +#### 체스 보드 -- [x] 체스판의 상태를 출력할 수 있다. - - [x] 체스판의 초기 상태를 출력할 수 있다 - - [x] 매 턴마다 최신화된 체스판의 상태를 출력할 수 있다 -- [x] 체스판에서 각 진영은 대문자(검은색)와 소문자(흰색)로 출력한다. +- [x] 체스보드 상태를 업데이트 할 수 있다 +- [x] 기물들의 행마법을 검증할 수 있다 +- [x] 기물들을 이동시킬 수 있다. +- [x] 기물들의 위치는 체스보드 범위를 벗어날 수 없다. +- [x] 기물들이 이동할 수 없다면 기물의 위치를 옮기지 않는다 -### 도메인 요구사항 +#### 체스 게임 -- [x] 체스 보드 초기화 - - [x] 게임 시작시 체스판을 초기화할 수 있다. - - [x] 체스판의 초기상태는 다음과 같다. - ``` - RNBQKBNR - PPPPPPPP - ........ - ........ - ........ - ........ - pppppppp - rnbqkbnr - ``` - - [x] 체스판의 열은 왼쪽부터 a-h까지의 알파벳으로 이루어져 있다. - - [x] 체스판의 행은 밑부터 1-8까지의 숫자로 이루어져 있다. - - [x] 기물들은 화이트 또는 블랙의 색깔을 가진다. - - - [x] 기물 이동 - - [x] 기물들의 위치는 체스보드 범위를 벗어날 수 없다. - - [x] 기물들은 각자의 행마법을 따라 이동할 수 있다 - - [x] 룩은 직선으로 이동할 수 있다 - - [x] 비숍은 대각선으로 이동할 수 있다 - - [x] 퀸은 직선/대각선으로 이동할 수 있다 - - [x] 킹은 방향과 무관하게 1칸 이동할 수 있다 - - [x] 나이트는 알파벳 L자 모양으로 앞으로 두칸 이동한 다음 왼쪽, 오른쪽 으로 한칸 움직일 수 있다 - - [x] 폰의 행마법 - - [x] 폰은 초기 상태에서 한칸 또는 두칸씩 전진할 수 있다 - - [x] 폰은 초기 상태에서 움직인 이후에는 한칸씩만 전진할 수 있다 - - [x] 상대 기물이 한칸 대각선에 있다면, 이동할 수 있다 - - - [x] 체스 보드 - - [x] 체스보드 상태를 업데이트 할 수 있다 - - [x] 기물들이 이동할 수 있다면 기물의 위치를 옮긴다 - - [x] 기물들이 이동할 수 없다면 기물의 위치를 옮기지 않는다 - - [x] 도착지에 같은 팀의 기물이 있다면 이동할 수 없다 - - [x] 비숍/룩/퀸은 이동 경로에 다른 기물이 있다면 이동할 수 없다 +- [x] 체스 게임은 백팀의 이동으로 시작해야 한다. +- [x] 기물의 움직임 마다 자신의 턴인지 검증할 수 있어야 한다. +- [ ] 킹이 잡힌 경우 게임을 끝낼 수 있어야 한다. + +#### 점수 계산 - [ ] 체스 프로그램에서 현재까지 남아 있는 말에 따라 점수를 계산할 수 있어야 한다. - - [ ] Queen은 9점 - - [ ] Rook은 5점 - - [ ] Bishop은 3점 - - [ ] Knight는 2.5점 - - [ ] Pawn의 기본 점수는 1점, 같은 세로줄에 같은 색의 폰이 있는 경우는 0.5점으로 계산 - - [ ] King은 점수가 없음 (잡히는 경우 게임 끝) -- [ ] King이 잡히는 경우 경기에서 진다. -- [ ] King이 잡히는 경우 게임이 종료된다. + +``` +Queen은 9점 +Rook은 5점 +Bishop은 3점 +Knight는 2.5점 +Pawn의 기본 점수는 1점, 같은 세로줄에 같은 색의 폰이 있는 경우는 0.5점으로 계산 +King은 점수가 없음 (잡히는 경우 게임 끝) +``` + From 7afc4bc4e349ce1f5c2aa9f3fe9483ad7a185ea6 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 14:29:32 +0900 Subject: [PATCH 11/80] =?UTF-8?q?feat:=20Score=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=EC=86=8D=EC=84=B1=20=EC=A0=95=EC=9D=98=20=EB=B0=8F=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/chess/domain/piece/Score.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/main/java/chess/domain/piece/Score.java diff --git a/src/main/java/chess/domain/piece/Score.java b/src/main/java/chess/domain/piece/Score.java new file mode 100644 index 00000000000..fb8ea23305b --- /dev/null +++ b/src/main/java/chess/domain/piece/Score.java @@ -0,0 +1,13 @@ +package chess.domain.piece; + +public class Score { + private final double value; + + public Score(double value) { + this.value = value; + } + + public double getValue() { + return value; + } +} From 270f24e3565c49370f278c5e361eff04b241a94d Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 14:38:56 +0900 Subject: [PATCH 12/80] =?UTF-8?q?feat:=20=EA=B0=81=20=EA=B8=B0=EB=AC=BC?= =?UTF-8?q?=EB=B3=84=20=EC=A0=90=EC=88=98=20=EB=8B=A4=ED=98=95=EC=84=B1=20?= =?UTF-8?q?=ED=86=B5=ED=95=B4=EC=84=9C=20=20=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/piece/Bishop.java | 7 +++++++ src/main/java/chess/domain/piece/King.java | 6 ++++++ src/main/java/chess/domain/piece/Knight.java | 6 ++++++ src/main/java/chess/domain/piece/Pawn.java | 6 ++++++ src/main/java/chess/domain/piece/Piece.java | 2 ++ src/main/java/chess/domain/piece/Queen.java | 8 ++++++++ src/main/java/chess/domain/piece/Rook.java | 8 ++++++++ 7 files changed, 43 insertions(+) diff --git a/src/main/java/chess/domain/piece/Bishop.java b/src/main/java/chess/domain/piece/Bishop.java index 6d3eb402455..0b951488f56 100644 --- a/src/main/java/chess/domain/piece/Bishop.java +++ b/src/main/java/chess/domain/piece/Bishop.java @@ -4,6 +4,8 @@ import chess.domain.position.Position; public class Bishop extends Piece { + private static final int SCORE_VALUE = 3; + public Bishop(Team team) { super(team); } @@ -17,4 +19,9 @@ boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { return chessBoard.isPathHasObstacle(start.calculateSlidingPath(destination)); } + + @Override + Score score() { + return new Score(SCORE_VALUE); + } } diff --git a/src/main/java/chess/domain/piece/King.java b/src/main/java/chess/domain/piece/King.java index 14f7654e039..021e34d2fca 100644 --- a/src/main/java/chess/domain/piece/King.java +++ b/src/main/java/chess/domain/piece/King.java @@ -5,6 +5,7 @@ public class King extends Piece { private static final int MOVE_DISTANCE = 1; + private static final int SCORE_VALUE = 0; public King(Team team) { super(team); @@ -19,4 +20,9 @@ boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { return false; } + + @Override + Score score() { + return new Score(SCORE_VALUE); + } } diff --git a/src/main/java/chess/domain/piece/Knight.java b/src/main/java/chess/domain/piece/Knight.java index e90549cf69b..a2fea49cdc2 100644 --- a/src/main/java/chess/domain/piece/Knight.java +++ b/src/main/java/chess/domain/piece/Knight.java @@ -5,6 +5,7 @@ public class Knight extends Piece { private static final int L_SHAPE_SQUARED_DISTANCE = 5; + private static final double SCORE_VALUE = 2.5; public Knight(Team team) { super(team); @@ -19,4 +20,9 @@ boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { return false; } + + @Override + Score score() { + return new Score(SCORE_VALUE); + } } diff --git a/src/main/java/chess/domain/piece/Pawn.java b/src/main/java/chess/domain/piece/Pawn.java index 51059120d4f..7d70e0dec06 100644 --- a/src/main/java/chess/domain/piece/Pawn.java +++ b/src/main/java/chess/domain/piece/Pawn.java @@ -7,6 +7,7 @@ public class Pawn extends Piece { private static final int DEFAULT_MOVE_DISTANCE = 1; private static final int INITIAL_MOVE_DISTANCE = 2; + private static final int SCORE_VALUE = 1; public Pawn(Team team) { super(team); @@ -48,4 +49,9 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard } return false; } + + @Override + Score score() { + return new Score(SCORE_VALUE); + } } diff --git a/src/main/java/chess/domain/piece/Piece.java b/src/main/java/chess/domain/piece/Piece.java index 40d2cd2eea0..df66437df1b 100644 --- a/src/main/java/chess/domain/piece/Piece.java +++ b/src/main/java/chess/domain/piece/Piece.java @@ -14,6 +14,8 @@ protected Piece(Team team) { abstract boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard); + abstract Score score(); + public final boolean canMove(Position start, Position destination, ChessBoard chessBoard) { if (start == destination) { return false; diff --git a/src/main/java/chess/domain/piece/Queen.java b/src/main/java/chess/domain/piece/Queen.java index 1951a302098..0044e62505b 100644 --- a/src/main/java/chess/domain/piece/Queen.java +++ b/src/main/java/chess/domain/piece/Queen.java @@ -4,6 +4,9 @@ import chess.domain.position.Position; public class Queen extends Piece { + + public static final int SCORE_VALUE = 9; + public Queen(Team team) { super(team); } @@ -17,4 +20,9 @@ boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { return chessBoard.isPathHasObstacle(start.calculateSlidingPath(destination)); } + + @Override + Score score() { + return new Score(SCORE_VALUE); + } } diff --git a/src/main/java/chess/domain/piece/Rook.java b/src/main/java/chess/domain/piece/Rook.java index 1bef5b3080b..6d67221b3c8 100644 --- a/src/main/java/chess/domain/piece/Rook.java +++ b/src/main/java/chess/domain/piece/Rook.java @@ -4,6 +4,9 @@ import chess.domain.position.Position; public class Rook extends Piece { + + public static final int SCORE_VALUE = 5; + public Rook(Team team) { super(team); } @@ -17,4 +20,9 @@ boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { return chessBoard.isPathHasObstacle(start.calculateSlidingPath(destination)); } + + @Override + Score score() { + return new Score(SCORE_VALUE); + } } From 43a09ac01d039a871071e8aee0879cf7efff6fcb Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 14:50:29 +0900 Subject: [PATCH 13/80] =?UTF-8?q?test:=20=EB=8F=84=EB=A9=94=EC=9D=B8=20?= =?UTF-8?q?=EC=B6=94=EC=83=81=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/chess/domain/piece/PieceTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/chess/domain/piece/PieceTest.java b/src/test/java/chess/domain/piece/PieceTest.java index 57513858507..0bfdd258c5e 100644 --- a/src/test/java/chess/domain/piece/PieceTest.java +++ b/src/test/java/chess/domain/piece/PieceTest.java @@ -29,6 +29,11 @@ boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { return false; } + + @Override + public Score score() { + return new Score(29.3); + } }; whitePiece = new Piece(Team.WHITE) { @@ -41,6 +46,11 @@ boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { return false; } + + @Override + public Score score() { + return new Score(29.3); + } }; } From 013ac29671e5b8e99fb29326f999d3856f4047a1 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 14:51:13 +0900 Subject: [PATCH 14/80] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=EB=81=BC?= =?UTF-8?q?=EB=A6=AC=20=EB=8D=94=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/piece/Score.java | 4 ++++ .../java/chess/domain/piece/ScoreTest.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/test/java/chess/domain/piece/ScoreTest.java diff --git a/src/main/java/chess/domain/piece/Score.java b/src/main/java/chess/domain/piece/Score.java index fb8ea23305b..ef360d60c85 100644 --- a/src/main/java/chess/domain/piece/Score.java +++ b/src/main/java/chess/domain/piece/Score.java @@ -10,4 +10,8 @@ public Score(double value) { public double getValue() { return value; } + + public Score add(Score other) { + return new Score(this.value + other.value); + } } diff --git a/src/test/java/chess/domain/piece/ScoreTest.java b/src/test/java/chess/domain/piece/ScoreTest.java new file mode 100644 index 00000000000..fa64b396d05 --- /dev/null +++ b/src/test/java/chess/domain/piece/ScoreTest.java @@ -0,0 +1,18 @@ +package chess.domain.piece; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class ScoreTest { + @DisplayName("점수끼리 덧셈을 계산할 수 있다") + @Test + void should_CalculateAddition() { + Score score1 = new Score(1.1); + Score score2 = new Score(1.2); + Score added = score1.add(score2); + + assertThat(added.getValue()).isEqualTo(2.3); + } +} From e185bf105464024c6f6b8b2cd2a23eca8df2c65c Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 14:52:18 +0900 Subject: [PATCH 15/80] =?UTF-8?q?fix:=20=EC=A0=90=EC=88=98=20=EA=B0=80?= =?UTF-8?q?=EC=A0=B8=EC=98=A4=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=A0=91=EA=B7=BC=20=EC=A0=9C=EC=96=B4=EC=9E=90=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/piece/Bishop.java | 2 +- src/main/java/chess/domain/piece/King.java | 2 +- src/main/java/chess/domain/piece/Knight.java | 2 +- src/main/java/chess/domain/piece/Pawn.java | 2 +- src/main/java/chess/domain/piece/Piece.java | 2 +- src/main/java/chess/domain/piece/Queen.java | 2 +- src/main/java/chess/domain/piece/Rook.java | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/chess/domain/piece/Bishop.java b/src/main/java/chess/domain/piece/Bishop.java index 0b951488f56..b611a25aa49 100644 --- a/src/main/java/chess/domain/piece/Bishop.java +++ b/src/main/java/chess/domain/piece/Bishop.java @@ -21,7 +21,7 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard } @Override - Score score() { + public Score score() { return new Score(SCORE_VALUE); } } diff --git a/src/main/java/chess/domain/piece/King.java b/src/main/java/chess/domain/piece/King.java index 021e34d2fca..a19661a191d 100644 --- a/src/main/java/chess/domain/piece/King.java +++ b/src/main/java/chess/domain/piece/King.java @@ -22,7 +22,7 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard } @Override - Score score() { + public Score score() { return new Score(SCORE_VALUE); } } diff --git a/src/main/java/chess/domain/piece/Knight.java b/src/main/java/chess/domain/piece/Knight.java index a2fea49cdc2..b16c78b9372 100644 --- a/src/main/java/chess/domain/piece/Knight.java +++ b/src/main/java/chess/domain/piece/Knight.java @@ -22,7 +22,7 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard } @Override - Score score() { + public Score score() { return new Score(SCORE_VALUE); } } diff --git a/src/main/java/chess/domain/piece/Pawn.java b/src/main/java/chess/domain/piece/Pawn.java index 7d70e0dec06..37e900a8db1 100644 --- a/src/main/java/chess/domain/piece/Pawn.java +++ b/src/main/java/chess/domain/piece/Pawn.java @@ -51,7 +51,7 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard } @Override - Score score() { + public Score score() { return new Score(SCORE_VALUE); } } diff --git a/src/main/java/chess/domain/piece/Piece.java b/src/main/java/chess/domain/piece/Piece.java index df66437df1b..c62f63ed318 100644 --- a/src/main/java/chess/domain/piece/Piece.java +++ b/src/main/java/chess/domain/piece/Piece.java @@ -14,7 +14,7 @@ protected Piece(Team team) { abstract boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard); - abstract Score score(); + public abstract Score score(); public final boolean canMove(Position start, Position destination, ChessBoard chessBoard) { if (start == destination) { diff --git a/src/main/java/chess/domain/piece/Queen.java b/src/main/java/chess/domain/piece/Queen.java index 0044e62505b..6ceccda0573 100644 --- a/src/main/java/chess/domain/piece/Queen.java +++ b/src/main/java/chess/domain/piece/Queen.java @@ -22,7 +22,7 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard } @Override - Score score() { + public Score score() { return new Score(SCORE_VALUE); } } diff --git a/src/main/java/chess/domain/piece/Rook.java b/src/main/java/chess/domain/piece/Rook.java index 6d67221b3c8..de3ea80ca79 100644 --- a/src/main/java/chess/domain/piece/Rook.java +++ b/src/main/java/chess/domain/piece/Rook.java @@ -22,7 +22,7 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard } @Override - Score score() { + public Score score() { return new Score(SCORE_VALUE); } } From 243e8b061a9e6f3fd81dc6e1719ad776acdf272b Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 15:27:38 +0900 Subject: [PATCH 16/80] =?UTF-8?q?feat:=20Score=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=EB=8F=99=EB=93=B1=EC=84=B1=20=EB=B0=8F=20=ED=95=B4=EC=8B=9C=20?= =?UTF-8?q?=EC=9E=AC=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/piece/Score.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/chess/domain/piece/Score.java b/src/main/java/chess/domain/piece/Score.java index ef360d60c85..7a7bacd221c 100644 --- a/src/main/java/chess/domain/piece/Score.java +++ b/src/main/java/chess/domain/piece/Score.java @@ -1,5 +1,7 @@ package chess.domain.piece; +import java.util.Objects; + public class Score { private final double value; @@ -14,4 +16,21 @@ public double getValue() { public Score add(Score other) { return new Score(this.value + other.value); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Score score = (Score) o; + return Double.compare(value, score.value) == 0; + } + + @Override + public int hashCode() { + return Objects.hash(value); + } } From 0cdc8872ea7d1502abda86e6f0608650b043a95b Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 15:29:08 +0900 Subject: [PATCH 17/80] =?UTF-8?q?feat:=20=EC=B2=B4=EC=8A=A4=EB=B3=B4?= =?UTF-8?q?=EB=93=9C=EC=97=90=EC=84=9C=20=ED=8C=80=EC=9D=98=20=EA=B8=B0?= =?UTF-8?q?=EB=B3=B8=EC=A0=90=EC=88=98=20=ED=95=A9=EC=9D=84=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=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 --- .../java/chess/domain/board/ChessBoard.java | 9 +++++++ .../chess/domain/board/ChessBoardTest.java | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/main/java/chess/domain/board/ChessBoard.java b/src/main/java/chess/domain/board/ChessBoard.java index 8d01f1b92ce..1d10f7e219c 100644 --- a/src/main/java/chess/domain/board/ChessBoard.java +++ b/src/main/java/chess/domain/board/ChessBoard.java @@ -1,6 +1,7 @@ package chess.domain.board; import chess.domain.piece.Piece; +import chess.domain.piece.Score; import chess.domain.piece.Team; import chess.domain.position.Position; import java.util.List; @@ -51,4 +52,12 @@ public boolean isPathHasObstacle(List path) { public boolean isNoHostilePieceAt(Position position, Team team) { return positionIsEmpty(position) || !findPieceByPosition(position).isOtherTeam(team); } + + public Score calcualteDefaultScore(Team team) { + return board.keySet().stream() + .map(board::get) + .filter(piece -> !piece.isOtherTeam(team)) + .map(Piece::score) + .reduce(new Score(0), Score::add); + } } diff --git a/src/test/java/chess/domain/board/ChessBoardTest.java b/src/test/java/chess/domain/board/ChessBoardTest.java index ed123385ad0..72a7795b7c8 100644 --- a/src/test/java/chess/domain/board/ChessBoardTest.java +++ b/src/test/java/chess/domain/board/ChessBoardTest.java @@ -2,12 +2,18 @@ import static chess.fixture.PositionFixtures.A1; import static chess.fixture.PositionFixtures.A2; +import static chess.fixture.PositionFixtures.A3; +import static chess.fixture.PositionFixtures.A4; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertAll; +import chess.domain.piece.Bishop; import chess.domain.piece.King; +import chess.domain.piece.Knight; import chess.domain.piece.Piece; +import chess.domain.piece.Rook; +import chess.domain.piece.Score; import chess.domain.piece.Team; import chess.domain.position.Position; import java.util.HashMap; @@ -77,4 +83,22 @@ void should_CheckThereIsHostilePiece_When_DestinationIsGiven() { () -> assertThat(chessBoard.isNoHostilePieceAt(A1, Team.BLACK)).isFalse() ); } + + @DisplayName("팀의 기본 점수를 계산할 수 있다") + @Test + void should_CalculateDefaultScore() { + Map positionPiece = new HashMap<>(); + + positionPiece.put(A1, new Bishop(Team.WHITE)); + positionPiece.put(A2, new Knight(Team.WHITE)); + positionPiece.put(A3, new Rook(Team.BLACK)); + positionPiece.put(A4, new Rook(Team.BLACK)); + + ChessBoard chessBoard = new ChessBoard(positionPiece); + + assertAll( + () -> assertThat(chessBoard.calcualteDefaultScore(Team.WHITE)).isEqualTo(new Score(5.5)), + () -> assertThat(chessBoard.calcualteDefaultScore(Team.BLACK)).isEqualTo(new Score(10)) + ); + } } From 47f2d6a370cd0eea3049111018aed09e7c1b3934 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 15:55:48 +0900 Subject: [PATCH 18/80] =?UTF-8?q?feat:=20=ED=8F=B0=EC=9D=B8=EC=A7=80=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8A=94=20?= =?UTF-8?q?=EC=B6=94=EC=83=81=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20=EB=B0=8F=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/chess/domain/piece/Bishop.java | 5 +++++ src/main/java/chess/domain/piece/King.java | 5 +++++ src/main/java/chess/domain/piece/Knight.java | 5 +++++ src/main/java/chess/domain/piece/Pawn.java | 5 +++++ src/main/java/chess/domain/piece/Piece.java | 6 ++++-- src/main/java/chess/domain/piece/Queen.java | 5 +++++ src/main/java/chess/domain/piece/Rook.java | 5 +++++ 7 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main/java/chess/domain/piece/Bishop.java b/src/main/java/chess/domain/piece/Bishop.java index b611a25aa49..f400bd60a0c 100644 --- a/src/main/java/chess/domain/piece/Bishop.java +++ b/src/main/java/chess/domain/piece/Bishop.java @@ -24,4 +24,9 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard public Score score() { return new Score(SCORE_VALUE); } + + @Override + public boolean isPawn() { + return false; + } } diff --git a/src/main/java/chess/domain/piece/King.java b/src/main/java/chess/domain/piece/King.java index a19661a191d..99bc2da22ef 100644 --- a/src/main/java/chess/domain/piece/King.java +++ b/src/main/java/chess/domain/piece/King.java @@ -25,4 +25,9 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard public Score score() { return new Score(SCORE_VALUE); } + + @Override + public boolean isPawn() { + return false; + } } diff --git a/src/main/java/chess/domain/piece/Knight.java b/src/main/java/chess/domain/piece/Knight.java index b16c78b9372..8d439313232 100644 --- a/src/main/java/chess/domain/piece/Knight.java +++ b/src/main/java/chess/domain/piece/Knight.java @@ -25,4 +25,9 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard public Score score() { return new Score(SCORE_VALUE); } + + @Override + public boolean isPawn() { + return false; + } } diff --git a/src/main/java/chess/domain/piece/Pawn.java b/src/main/java/chess/domain/piece/Pawn.java index 37e900a8db1..0c293334e29 100644 --- a/src/main/java/chess/domain/piece/Pawn.java +++ b/src/main/java/chess/domain/piece/Pawn.java @@ -54,4 +54,9 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard public Score score() { return new Score(SCORE_VALUE); } + + @Override + public boolean isPawn() { + return true; + } } diff --git a/src/main/java/chess/domain/piece/Piece.java b/src/main/java/chess/domain/piece/Piece.java index c62f63ed318..8f2354b5ca8 100644 --- a/src/main/java/chess/domain/piece/Piece.java +++ b/src/main/java/chess/domain/piece/Piece.java @@ -10,12 +10,14 @@ protected Piece(Team team) { this.team = team; } + public abstract boolean isPawn(); + + public abstract Score score(); + abstract boolean canNotMoveByItsOwnInPassing(Position start, Position destination); abstract boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard); - public abstract Score score(); - public final boolean canMove(Position start, Position destination, ChessBoard chessBoard) { if (start == destination) { return false; diff --git a/src/main/java/chess/domain/piece/Queen.java b/src/main/java/chess/domain/piece/Queen.java index 6ceccda0573..073ad5033f4 100644 --- a/src/main/java/chess/domain/piece/Queen.java +++ b/src/main/java/chess/domain/piece/Queen.java @@ -25,4 +25,9 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard public Score score() { return new Score(SCORE_VALUE); } + + @Override + public boolean isPawn() { + return false; + } } diff --git a/src/main/java/chess/domain/piece/Rook.java b/src/main/java/chess/domain/piece/Rook.java index de3ea80ca79..9d19cc8855a 100644 --- a/src/main/java/chess/domain/piece/Rook.java +++ b/src/main/java/chess/domain/piece/Rook.java @@ -25,4 +25,9 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard public Score score() { return new Score(SCORE_VALUE); } + + @Override + public boolean isPawn() { + return false; + } } From ebe16a84d11679bc664892c0eb3454277cb5045f Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 16:00:27 +0900 Subject: [PATCH 19/80] =?UTF-8?q?test:=20=EC=B6=94=EC=83=81=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EC=B6=94=EA=B0=80=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=A5=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/chess/domain/piece/PieceTest.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/test/java/chess/domain/piece/PieceTest.java b/src/test/java/chess/domain/piece/PieceTest.java index 0bfdd258c5e..a1d16ff0093 100644 --- a/src/test/java/chess/domain/piece/PieceTest.java +++ b/src/test/java/chess/domain/piece/PieceTest.java @@ -30,6 +30,11 @@ boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard return false; } + @Override + public boolean isPawn() { + return false; + } + @Override public Score score() { return new Score(29.3); @@ -46,7 +51,12 @@ boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { return false; } - + + @Override + public boolean isPawn() { + return false; + } + @Override public Score score() { return new Score(29.3); From 16826b3762029df5a2629dae60ad95c9e3b2a625 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 16:02:09 +0900 Subject: [PATCH 20/80] =?UTF-8?q?feat:=20=ED=8A=B9=EC=A0=95=20=ED=8C=80?= =?UTF-8?q?=EC=9D=B4=20=EC=A3=BC=EC=96=B4=EC=A1=8C=EC=9D=84=20=EB=95=8C=20?= =?UTF-8?q?=EA=B0=99=EC=9D=80=20=ED=8C=8C=EC=9D=BC=EC=97=90=20=EC=9E=88?= =?UTF-8?q?=EB=8A=94=20=ED=8F=B0=EC=9D=98=20=EA=B0=9C=EC=88=98=EB=A5=BC=20?= =?UTF-8?q?=EC=84=B8=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/chess/domain/board/ChessBoard.java | 23 +++++++++++++++++++ .../chess/domain/board/ChessBoardTest.java | 20 ++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/main/java/chess/domain/board/ChessBoard.java b/src/main/java/chess/domain/board/ChessBoard.java index 1d10f7e219c..2e7063eda75 100644 --- a/src/main/java/chess/domain/board/ChessBoard.java +++ b/src/main/java/chess/domain/board/ChessBoard.java @@ -3,7 +3,9 @@ import chess.domain.piece.Piece; import chess.domain.piece.Score; import chess.domain.piece.Team; +import chess.domain.position.File; import chess.domain.position.Position; +import chess.domain.position.Rank; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; @@ -60,4 +62,25 @@ public Score calcualteDefaultScore(Team team) { .map(Piece::score) .reduce(new Score(0), Score::add); } + + public int countSameFilePawn(Team team) { + int total = 0; + for (File file : File.values()) { + int sameTeamPawnCount = 0; + for (Rank rank : Rank.values()) { + Position position = new Position(file, rank); + if (positionIsEmpty(position)) { + continue; + } + Piece piece = findPieceByPosition(position); + if (piece.isPawn() && !piece.isOtherTeam(team)) { + sameTeamPawnCount++; + } + } + if (sameTeamPawnCount > 1) { + total += sameTeamPawnCount; + } + } + return total; + } } diff --git a/src/test/java/chess/domain/board/ChessBoardTest.java b/src/test/java/chess/domain/board/ChessBoardTest.java index 72a7795b7c8..fe80f9cfd79 100644 --- a/src/test/java/chess/domain/board/ChessBoardTest.java +++ b/src/test/java/chess/domain/board/ChessBoardTest.java @@ -4,6 +4,7 @@ import static chess.fixture.PositionFixtures.A2; import static chess.fixture.PositionFixtures.A3; import static chess.fixture.PositionFixtures.A4; +import static chess.fixture.PositionFixtures.B3; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertAll; @@ -11,6 +12,7 @@ import chess.domain.piece.Bishop; import chess.domain.piece.King; import chess.domain.piece.Knight; +import chess.domain.piece.Pawn; import chess.domain.piece.Piece; import chess.domain.piece.Rook; import chess.domain.piece.Score; @@ -101,4 +103,22 @@ void should_CalculateDefaultScore() { () -> assertThat(chessBoard.calcualteDefaultScore(Team.BLACK)).isEqualTo(new Score(10)) ); } + + @DisplayName("세로 라인이 같은 폰이 총 몇개인지 셀 수 있다") + @Test + void should_CountSameFilePawn() { + Map positionPiece = new HashMap<>(); + + positionPiece.put(A1, new Pawn(Team.WHITE)); + positionPiece.put(A2, new Pawn(Team.WHITE)); + positionPiece.put(B3, new Pawn(Team.BLACK)); + positionPiece.put(A4, new Pawn(Team.BLACK)); + + ChessBoard chessBoard = new ChessBoard(positionPiece); + + assertAll( + () -> assertThat(chessBoard.countSameFilePawn(Team.WHITE)).isEqualTo(2), + () -> assertThat(chessBoard.countSameFilePawn(Team.BLACK)).isEqualTo(0) + ); + } } From fd7f0073fdbeaab2418741b575698d68be1e0445 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 16:23:39 +0900 Subject: [PATCH 21/80] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EB=B6=84=EB=A6=AC=20=EB=B0=8F=20=EC=8A=A4=ED=8A=B8=EB=A6=BC?= =?UTF-8?q?=ED=99=9C=EC=9A=A9=20=EC=84=A0=EC=96=B8=ED=98=95=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/chess/domain/board/ChessBoard.java | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/main/java/chess/domain/board/ChessBoard.java b/src/main/java/chess/domain/board/ChessBoard.java index 2e7063eda75..29e617755b0 100644 --- a/src/main/java/chess/domain/board/ChessBoard.java +++ b/src/main/java/chess/domain/board/ChessBoard.java @@ -6,6 +6,7 @@ import chess.domain.position.File; import chess.domain.position.Position; import chess.domain.position.Rank; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; @@ -64,23 +65,23 @@ public Score calcualteDefaultScore(Team team) { } public int countSameFilePawn(Team team) { - int total = 0; - for (File file : File.values()) { - int sameTeamPawnCount = 0; - for (Rank rank : Rank.values()) { - Position position = new Position(file, rank); - if (positionIsEmpty(position)) { - continue; - } - Piece piece = findPieceByPosition(position); - if (piece.isPawn() && !piece.isOtherTeam(team)) { - sameTeamPawnCount++; - } - } - if (sameTeamPawnCount > 1) { - total += sameTeamPawnCount; - } + return Arrays.stream(File.values()) + .mapToInt(file -> countFriendlyPawnAtFile(team, file)) + .sum(); + } + + private int countFriendlyPawnAtFile(Team friendlyTeam, File file) { + int count = (int) Arrays.stream(Rank.values()) + .map(rank -> new Position(file, rank)) + .filter(position -> !this.positionIsEmpty(position)) + .map(this::findPieceByPosition) + .filter(Piece::isPawn) + .filter(piece -> !piece.isOtherTeam(friendlyTeam)) + .count(); + if (count > 1) { + return count; } - return total; + return 0; } + } From 1929dc1fe0bffcec0520d816a5bbd0d6dd6159e4 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 16:24:35 +0900 Subject: [PATCH 22/80] =?UTF-8?q?style:=20=EA=B3=B5=EB=B0=B1=20=EC=BB=A8?= =?UTF-8?q?=EB=B2=A4=EC=85=98=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/board/ChessBoard.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/chess/domain/board/ChessBoard.java b/src/main/java/chess/domain/board/ChessBoard.java index 29e617755b0..7bdbf59fe35 100644 --- a/src/main/java/chess/domain/board/ChessBoard.java +++ b/src/main/java/chess/domain/board/ChessBoard.java @@ -83,5 +83,4 @@ private int countFriendlyPawnAtFile(Team friendlyTeam, File file) { } return 0; } - } From cc667731817ea85b55fabb3b1a674e32ec49fcea Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 16:34:09 +0900 Subject: [PATCH 23/80] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=EB=81=BC?= =?UTF-8?q?=EB=A6=AC=20=EA=B3=B1=EC=85=88=ED=95=A0=20=EC=88=98=20=EC=9E=88?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=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/chess/domain/piece/Score.java | 4 ++++ src/test/java/chess/domain/piece/ScoreTest.java | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/chess/domain/piece/Score.java b/src/main/java/chess/domain/piece/Score.java index 7a7bacd221c..4f390ecc149 100644 --- a/src/main/java/chess/domain/piece/Score.java +++ b/src/main/java/chess/domain/piece/Score.java @@ -17,6 +17,10 @@ public Score add(Score other) { return new Score(this.value + other.value); } + public Score multiply(Score other) { + return new Score(this.value * other.value); + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/test/java/chess/domain/piece/ScoreTest.java b/src/test/java/chess/domain/piece/ScoreTest.java index fa64b396d05..566a5c9b9f1 100644 --- a/src/test/java/chess/domain/piece/ScoreTest.java +++ b/src/test/java/chess/domain/piece/ScoreTest.java @@ -15,4 +15,14 @@ void should_CalculateAddition() { assertThat(added.getValue()).isEqualTo(2.3); } + + @DisplayName("점수끼리 곱셈을 계산할 수 있다") + @Test + void should_CalculateMultiplication() { + Score score1 = new Score(1.1); + Score score2 = new Score(1.2); + Score multiplied = score1.multiply(score2); + + assertThat(multiplied.getValue()).isEqualTo(1.32); + } } From 5d549ce9fa3cb21c035dd9ee19ea8a108549a204 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 16:38:06 +0900 Subject: [PATCH 24/80] =?UTF-8?q?feat:=20=EC=B2=B4=EC=8A=A4=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20=EB=8F=84=EB=A9=94=EC=9D=B8=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EA=B0=81=20=ED=8C=80=EC=9D=98=20=EC=A0=90=EC=88=98=EB=A5=BC=20?= =?UTF-8?q?=EA=B3=84=EC=82=B0=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=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/chess/domain/ChessGame.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/chess/domain/ChessGame.java b/src/main/java/chess/domain/ChessGame.java index b625e113b46..6f597e18b03 100644 --- a/src/main/java/chess/domain/ChessGame.java +++ b/src/main/java/chess/domain/ChessGame.java @@ -2,11 +2,13 @@ import chess.domain.board.ChessBoard; import chess.domain.piece.Piece; +import chess.domain.piece.Score; import chess.domain.piece.Team; import chess.domain.position.Position; public class ChessGame { private static final Team INITIAL_TURN = Team.WHITE; + private static final double PAWN_SCORE_WEIGHT = -0.5; private final ChessBoard chessBoard; private Team turn; @@ -22,6 +24,13 @@ public void move(Position start, Position destiantion) { turn = turn.otherTeam(); } + public Score calculateTeamScore(Team team) { + Score defaultScore = chessBoard.calcualteDefaultScore(team); + int sameFilePawnCount = chessBoard.countSameFilePawn(team); + Score weight = new Score(sameFilePawnCount * PAWN_SCORE_WEIGHT); + return defaultScore.add(weight); + } + public ChessBoard getChessBoard() { return chessBoard; } From 78ad985aa9b52ce6f403415e7042bd20c7886ab2 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 16:45:29 +0900 Subject: [PATCH 25/80] =?UTF-8?q?feat:=20status=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EB=B7=B0=EB=A1=9C=EC=A7=81=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/chess/view/OutputView.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/chess/view/OutputView.java b/src/main/java/chess/view/OutputView.java index 51c0d36e796..7a5d201f910 100644 --- a/src/main/java/chess/view/OutputView.java +++ b/src/main/java/chess/view/OutputView.java @@ -6,6 +6,8 @@ import chess.domain.board.ChessBoard; import chess.domain.piece.Piece; +import chess.domain.piece.Score; +import chess.domain.piece.Team; import chess.domain.position.File; import chess.domain.position.Position; import chess.domain.position.Rank; @@ -25,6 +27,10 @@ public void printChessBoardMessage(ChessBoard chessBoard) { System.out.println(resolveChessBoardMessage(chessBoard)); } + public void printTeamStatusMessage(Team team, Score score) { + System.out.println(resolveTeamStatusMessage(team, score)); + } + private String resolveStartMessage() { return new StringJoiner(LINE_SEPARATOR) .add("체스 게임을 시작합니다.") @@ -54,4 +60,8 @@ private String resolveSquareMessage(ChessBoard chessBoard, Position position) { Piece foundPiece = chessBoard.findPieceByPosition(position); return PieceMessage.messageOf(foundPiece); } + + private String resolveTeamStatusMessage(Team team, Score score) { + return String.format("%s팀: %lf점", team, score.getValue()); + } } From 87816577c2828efa50b862d52794389df5c9dc74 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 16:49:04 +0900 Subject: [PATCH 26/80] =?UTF-8?q?feat:=20status=20=EC=BB=A4=EB=A7=A8?= =?UTF-8?q?=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/command/StatusCommand.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/chess/controller/command/StatusCommand.java diff --git a/src/main/java/chess/controller/command/StatusCommand.java b/src/main/java/chess/controller/command/StatusCommand.java new file mode 100644 index 00000000000..2fa47f6eeee --- /dev/null +++ b/src/main/java/chess/controller/command/StatusCommand.java @@ -0,0 +1,18 @@ +package chess.controller.command; + +import chess.domain.ChessGame; +import chess.domain.piece.Score; +import chess.domain.piece.Team; +import chess.view.OutputView; + +public class StatusCommand implements Command { + + @Override + public ExecuteResult execute(ChessGame chessGame, OutputView outputView) { + Score whiteScore = chessGame.calculateTeamScore(Team.WHITE); + Score blackScore = chessGame.calculateTeamScore(Team.BLACK); + outputView.printTeamStatusMessage(Team.WHITE, whiteScore); + outputView.printTeamStatusMessage(Team.BLACK, blackScore); + return new ExecuteResult(true, true); + } +} From 2a0137153026b71b6e1528729485384d38c62fb0 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 17:12:16 +0900 Subject: [PATCH 27/80] =?UTF-8?q?feat:=20=EC=9E=85=EB=A0=A5=20=EB=B7=B0?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=EC=97=90=20=EC=83=88=EB=A1=9C=EC=9A=B4=20?= =?UTF-8?q?=EB=AA=85=EB=A0=B9=EC=96=B4=20=EC=B6=94=EA=B0=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/chess/view/CommandParser.java | 6 ++++++ src/main/java/chess/view/OutputView.java | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/main/java/chess/view/CommandParser.java b/src/main/java/chess/view/CommandParser.java index e2766bf9463..ba856a1bb5b 100644 --- a/src/main/java/chess/view/CommandParser.java +++ b/src/main/java/chess/view/CommandParser.java @@ -4,6 +4,7 @@ import chess.controller.command.EndCommand; import chess.controller.command.MoveCommand; import chess.controller.command.StartCommand; +import chess.controller.command.StatusCommand; import chess.domain.position.File; import chess.domain.position.Position; import chess.domain.position.Rank; @@ -12,6 +13,8 @@ public class CommandParser { static final String START = "start"; static final String END = "end"; static final String MOVE = "move"; + static final String STATUS = "status"; + private static final int START_POSITION_INDEX = 1; private static final int DESTINATION_POSITION_INDEX = 2; private static final int FILE_INDEX = 0; @@ -27,6 +30,9 @@ public Command parse(String input) { if (input.equals(END)) { return new EndCommand(); } + if (input.equals(STATUS)) { + return new StatusCommand(); + } if (input.startsWith(MOVE)) { String[] split = input.split(" "); String start = split[START_POSITION_INDEX]; diff --git a/src/main/java/chess/view/OutputView.java b/src/main/java/chess/view/OutputView.java index 7a5d201f910..2ae03188369 100644 --- a/src/main/java/chess/view/OutputView.java +++ b/src/main/java/chess/view/OutputView.java @@ -3,6 +3,7 @@ import static chess.view.CommandParser.END; import static chess.view.CommandParser.MOVE; import static chess.view.CommandParser.START; +import static chess.view.CommandParser.STATUS; import chess.domain.board.ChessBoard; import chess.domain.piece.Piece; @@ -36,6 +37,7 @@ private String resolveStartMessage() { .add("체스 게임을 시작합니다.") .add(String.format("> 게임 시작 : %s", START)) .add(String.format("> 게임 종료 : %s", END)) + .add(String.format("> 점수 출력 : %s", STATUS)) .add(String.format("> 게임 이동 : %s source위치 target위치 - 예. %s b2 b3", MOVE, MOVE)) .toString(); } From ca9b473ff6606160dd042321075357e25b8eaa6f Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 17:15:08 +0900 Subject: [PATCH 28/80] =?UTF-8?q?fix:=20double=20=ED=83=80=EC=9E=85?= =?UTF-8?q?=EC=9D=84=20formatting=ED=95=A0=20=EB=95=8C=20=EC=84=9C?= =?UTF-8?q?=EC=8B=9D=EC=A7=80=EC=A0=95=EC=9E=90=20=EB=AC=B8=EB=B2=95?= =?UTF-8?q?=EC=97=90=20=EB=A7=9E=EA=B2=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/view/OutputView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/chess/view/OutputView.java b/src/main/java/chess/view/OutputView.java index 2ae03188369..cb14c1292f5 100644 --- a/src/main/java/chess/view/OutputView.java +++ b/src/main/java/chess/view/OutputView.java @@ -64,6 +64,6 @@ private String resolveSquareMessage(ChessBoard chessBoard, Position position) { } private String resolveTeamStatusMessage(Team team, Score score) { - return String.format("%s팀: %lf점", team, score.getValue()); + return String.format("%s팀: %.1f점", team, score.getValue()); } } From 21df77ec75ac5372b414603a38c317dbe66967c4 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 17:39:22 +0900 Subject: [PATCH 29/80] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=EA=B0=84=20?= =?UTF-8?q?=EB=B9=84=EA=B5=90=20=EA=B8=B0=EB=8A=A5=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/chess/domain/piece/Score.java | 8 ++++++++ .../java/chess/domain/piece/ScoreTest.java | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/main/java/chess/domain/piece/Score.java b/src/main/java/chess/domain/piece/Score.java index 4f390ecc149..a497fdc5e4b 100644 --- a/src/main/java/chess/domain/piece/Score.java +++ b/src/main/java/chess/domain/piece/Score.java @@ -21,6 +21,14 @@ public Score multiply(Score other) { return new Score(this.value * other.value); } + public boolean isAbove(Score other) { + return this.value > other.value; + } + + public boolean isBelow(Score other) { + return this.value < other.value; + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/test/java/chess/domain/piece/ScoreTest.java b/src/test/java/chess/domain/piece/ScoreTest.java index 566a5c9b9f1..e43a079531c 100644 --- a/src/test/java/chess/domain/piece/ScoreTest.java +++ b/src/test/java/chess/domain/piece/ScoreTest.java @@ -25,4 +25,22 @@ void should_CalculateMultiplication() { assertThat(multiplied.getValue()).isEqualTo(1.32); } + + @DisplayName("특정 점수가 더 높은지 계산할 수 있다") + @Test + void should_ScoreCouldCheckIsAbove_When_OtherScoreIsGiven() { + Score higher = new Score(1.3); + Score lower = new Score(1.2); + + assertThat(higher.isAbove(lower)).isTrue(); + } + + @DisplayName("특정 점수가 더 낮은지 계산할 수 있다") + @Test + void should_ScoreCouldCheckIsBelow_When_OtherScoreIsGiven() { + Score higher = new Score(1.3); + Score lower = new Score(1.2); + + assertThat(lower.isBelow(higher)).isTrue(); + } } From 6bd2cd18f7d2388487cf03fc85ae1a881266fe1f Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 17:41:41 +0900 Subject: [PATCH 30/80] =?UTF-8?q?feat:=20=EC=9D=B4=EA=B8=B4=20=EC=A7=84?= =?UTF-8?q?=EC=98=81=EC=9D=84=20=EA=B3=84=EC=82=B0=20=EB=B0=8F=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/controller/command/StatusCommand.java | 11 +++++++++-- src/main/java/chess/view/OutputView.java | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/main/java/chess/controller/command/StatusCommand.java b/src/main/java/chess/controller/command/StatusCommand.java index 2fa47f6eeee..3a8c39dac23 100644 --- a/src/main/java/chess/controller/command/StatusCommand.java +++ b/src/main/java/chess/controller/command/StatusCommand.java @@ -11,8 +11,15 @@ public class StatusCommand implements Command { public ExecuteResult execute(ChessGame chessGame, OutputView outputView) { Score whiteScore = chessGame.calculateTeamScore(Team.WHITE); Score blackScore = chessGame.calculateTeamScore(Team.BLACK); - outputView.printTeamStatusMessage(Team.WHITE, whiteScore); - outputView.printTeamStatusMessage(Team.BLACK, blackScore); + Team winner = selectWinnerTeam(whiteScore, blackScore); + outputView.printStatusMessage(whiteScore, blackScore, winner); return new ExecuteResult(true, true); } + + private Team selectWinnerTeam(Score whiteScore, Score blackScore) { + if (whiteScore.isAbove(blackScore)) { + return Team.WHITE; + } + return Team.BLACK; + } } diff --git a/src/main/java/chess/view/OutputView.java b/src/main/java/chess/view/OutputView.java index cb14c1292f5..d40174ff4d9 100644 --- a/src/main/java/chess/view/OutputView.java +++ b/src/main/java/chess/view/OutputView.java @@ -28,8 +28,8 @@ public void printChessBoardMessage(ChessBoard chessBoard) { System.out.println(resolveChessBoardMessage(chessBoard)); } - public void printTeamStatusMessage(Team team, Score score) { - System.out.println(resolveTeamStatusMessage(team, score)); + public void printStatusMessage(Score whiteScore, Score blackScore, Team winner) { + System.out.println(resolveStatusMessage(whiteScore, blackScore, winner)); } private String resolveStartMessage() { @@ -63,7 +63,19 @@ private String resolveSquareMessage(ChessBoard chessBoard, Position position) { return PieceMessage.messageOf(foundPiece); } + private String resolveStatusMessage(Score whiteScore, Score blackScore, Team winner) { + return new StringJoiner(LINE_SEPARATOR) + .add(resolveTeamStatusMessage(Team.WHITE, whiteScore)) + .add(resolveTeamStatusMessage(Team.BLACK, blackScore)) + .add(resolveStatusWinnerMessage(winner)) + .toString(); + } + private String resolveTeamStatusMessage(Team team, Score score) { return String.format("%s팀: %.1f점", team, score.getValue()); } + + private String resolveStatusWinnerMessage(Team winner) { + return String.format("현 시점 기물 점수 승부 %s 승리"); + } } From 03702e7b59a16c126228ba1dfeb961403d02d357 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 17:43:15 +0900 Subject: [PATCH 31/80] =?UTF-8?q?fix:=20String=20format=20=EC=9D=B8?= =?UTF-8?q?=EC=9E=90=20=EB=84=98=EA=B2=A8=EC=A3=BC=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8D=98=20=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/view/OutputView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/chess/view/OutputView.java b/src/main/java/chess/view/OutputView.java index d40174ff4d9..7eca0d44c5f 100644 --- a/src/main/java/chess/view/OutputView.java +++ b/src/main/java/chess/view/OutputView.java @@ -76,6 +76,6 @@ private String resolveTeamStatusMessage(Team team, Score score) { } private String resolveStatusWinnerMessage(Team winner) { - return String.format("현 시점 기물 점수 승부 %s 승리"); + return String.format("현 시점 기물 점수 승부 %s 승리", winner); } } From 5a49e74ff24bd983b44269b99a4b46d23579f26d Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 17:45:01 +0900 Subject: [PATCH 32/80] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=AA=A9=EB=A1=9D=20=EC=B5=9C=EC=8B=A0=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d71aabf348d..20ae1066451 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ - [x] 체스 게임을 시작하는 명령어를 입력받을 수 있다. - [x] 체스 게임을 종료하는 명령어를 입력받을 수 있다. - [x] 체스 말을 이동하는 명령어를 입력받을 수 있다. -- [ ] 각 진영의 점수를 확인하는 명령어를 입력받을 수 있다. +- [x] 각 진영의 점수를 확인하는 명령어를 입력받을 수 있다. - [x] 명령어 입력시 형식에 맞는 입력이 들어오지 않으면 예외를 발생시킨다. ### 출력 요구사항 @@ -67,7 +67,7 @@ #### 점수 계산 -- [ ] 체스 프로그램에서 현재까지 남아 있는 말에 따라 점수를 계산할 수 있어야 한다. +- [x] 체스 프로그램에서 현재까지 남아 있는 말에 따라 점수를 계산할 수 있어야 한다. ``` Queen은 9점 From 2e9fabc1469c5f37013f95d19cc46f00448a0c63 Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 18:54:15 +0900 Subject: [PATCH 33/80] =?UTF-8?q?feat:=20=ED=8A=B9=EC=A0=95=20=EA=B8=B0?= =?UTF-8?q?=EB=AC=BC=EC=9D=B4=20=ED=82=B9=EC=9D=B8=EC=A7=80=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=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/chess/domain/piece/Bishop.java | 5 +++++ src/main/java/chess/domain/piece/King.java | 5 +++++ src/main/java/chess/domain/piece/Knight.java | 5 +++++ src/main/java/chess/domain/piece/Pawn.java | 5 +++++ src/main/java/chess/domain/piece/Piece.java | 2 ++ src/main/java/chess/domain/piece/Queen.java | 5 +++++ src/main/java/chess/domain/piece/Rook.java | 5 +++++ 7 files changed, 32 insertions(+) diff --git a/src/main/java/chess/domain/piece/Bishop.java b/src/main/java/chess/domain/piece/Bishop.java index f400bd60a0c..5d75da7cc13 100644 --- a/src/main/java/chess/domain/piece/Bishop.java +++ b/src/main/java/chess/domain/piece/Bishop.java @@ -29,4 +29,9 @@ public Score score() { public boolean isPawn() { return false; } + + @Override + public boolean isKing() { + return false; + } } diff --git a/src/main/java/chess/domain/piece/King.java b/src/main/java/chess/domain/piece/King.java index 99bc2da22ef..eb2467c9486 100644 --- a/src/main/java/chess/domain/piece/King.java +++ b/src/main/java/chess/domain/piece/King.java @@ -30,4 +30,9 @@ public Score score() { public boolean isPawn() { return false; } + + @Override + public boolean isKing() { + return true; + } } diff --git a/src/main/java/chess/domain/piece/Knight.java b/src/main/java/chess/domain/piece/Knight.java index 8d439313232..0d4ed5bec15 100644 --- a/src/main/java/chess/domain/piece/Knight.java +++ b/src/main/java/chess/domain/piece/Knight.java @@ -30,4 +30,9 @@ public Score score() { public boolean isPawn() { return false; } + + @Override + public boolean isKing() { + return false; + } } diff --git a/src/main/java/chess/domain/piece/Pawn.java b/src/main/java/chess/domain/piece/Pawn.java index 0c293334e29..5b8080250be 100644 --- a/src/main/java/chess/domain/piece/Pawn.java +++ b/src/main/java/chess/domain/piece/Pawn.java @@ -59,4 +59,9 @@ public Score score() { public boolean isPawn() { return true; } + + @Override + public boolean isKing() { + return false; + } } diff --git a/src/main/java/chess/domain/piece/Piece.java b/src/main/java/chess/domain/piece/Piece.java index 8f2354b5ca8..ea18dd17c81 100644 --- a/src/main/java/chess/domain/piece/Piece.java +++ b/src/main/java/chess/domain/piece/Piece.java @@ -12,6 +12,8 @@ protected Piece(Team team) { public abstract boolean isPawn(); + public abstract boolean isKing(); + public abstract Score score(); abstract boolean canNotMoveByItsOwnInPassing(Position start, Position destination); diff --git a/src/main/java/chess/domain/piece/Queen.java b/src/main/java/chess/domain/piece/Queen.java index 073ad5033f4..57196df94dd 100644 --- a/src/main/java/chess/domain/piece/Queen.java +++ b/src/main/java/chess/domain/piece/Queen.java @@ -30,4 +30,9 @@ public Score score() { public boolean isPawn() { return false; } + + @Override + public boolean isKing() { + return false; + } } diff --git a/src/main/java/chess/domain/piece/Rook.java b/src/main/java/chess/domain/piece/Rook.java index 9d19cc8855a..1544ec18b9b 100644 --- a/src/main/java/chess/domain/piece/Rook.java +++ b/src/main/java/chess/domain/piece/Rook.java @@ -30,4 +30,9 @@ public Score score() { public boolean isPawn() { return false; } + + @Override + public boolean isKing() { + return false; + } } From ed7939efd5f3b48a6a471d92eba92655edeecb5b Mon Sep 17 00:00:00 2001 From: libienz Date: Wed, 27 Mar 2024 19:57:48 +0900 Subject: [PATCH 34/80] =?UTF-8?q?feat:=20=ED=82=B9=EC=9D=B4=20=EC=9E=A1?= =?UTF-8?q?=ED=9E=88=EB=A9=B4=20=EA=B2=8C=EC=9E=84=EC=9D=B4=20=EC=A2=85?= =?UTF-8?q?=EB=A3=8C=EB=90=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=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 --- .../chess/controller/ChessGameController.java | 2 +- src/main/java/chess/domain/ChessGame.java | 4 +++ .../java/chess/domain/board/ChessBoard.java | 20 ++++++++++++++ .../chess/domain/board/ChessBoardTest.java | 26 +++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/main/java/chess/controller/ChessGameController.java b/src/main/java/chess/controller/ChessGameController.java index 6161e18bd2a..5e60f7bdece 100644 --- a/src/main/java/chess/controller/ChessGameController.java +++ b/src/main/java/chess/controller/ChessGameController.java @@ -26,7 +26,7 @@ public void run() { Command command = inputView.readCommand(); result = command.execute(chessGame, outputView); } - while (result.isSuccess() && result.isNeedNextCommand()); + while (result.isSuccess() && result.isNeedNextCommand() && chessGame.isNotEnd()); } private ChessGame initializeChessGame() { diff --git a/src/main/java/chess/domain/ChessGame.java b/src/main/java/chess/domain/ChessGame.java index 6f597e18b03..95d86cee8e9 100644 --- a/src/main/java/chess/domain/ChessGame.java +++ b/src/main/java/chess/domain/ChessGame.java @@ -31,6 +31,10 @@ public Score calculateTeamScore(Team team) { return defaultScore.add(weight); } + public boolean isNotEnd() { + return chessBoard.isBlackKingAlive() && chessBoard.isWhiteKingAlive(); + } + public ChessBoard getChessBoard() { return chessBoard; } diff --git a/src/main/java/chess/domain/board/ChessBoard.java b/src/main/java/chess/domain/board/ChessBoard.java index 7bdbf59fe35..4e090242294 100644 --- a/src/main/java/chess/domain/board/ChessBoard.java +++ b/src/main/java/chess/domain/board/ChessBoard.java @@ -70,6 +70,26 @@ public int countSameFilePawn(Team team) { .sum(); } + public boolean isBlackKingAlive() { + return Arrays.stream(File.values()) + .flatMap(file -> Arrays.stream(Rank.values()) + .map(rank -> new Position(file, rank))) + .filter(position -> !this.positionIsEmpty(position)) + .map(this::findPieceByPosition) + .filter(Piece::isKing) + .anyMatch(Piece::isBlackTeam); + } + + public boolean isWhiteKingAlive() { + return Arrays.stream(File.values()) + .flatMap(file -> Arrays.stream(Rank.values()) + .map(rank -> new Position(file, rank))) + .filter(position -> !this.positionIsEmpty(position)) + .map(this::findPieceByPosition) + .filter(Piece::isKing) + .anyMatch(piece -> piece.isOtherTeam(Team.BLACK)); + } + private int countFriendlyPawnAtFile(Team friendlyTeam, File file) { int count = (int) Arrays.stream(Rank.values()) .map(rank -> new Position(file, rank)) diff --git a/src/test/java/chess/domain/board/ChessBoardTest.java b/src/test/java/chess/domain/board/ChessBoardTest.java index fe80f9cfd79..a51054357a7 100644 --- a/src/test/java/chess/domain/board/ChessBoardTest.java +++ b/src/test/java/chess/domain/board/ChessBoardTest.java @@ -121,4 +121,30 @@ void should_CountSameFilePawn() { () -> assertThat(chessBoard.countSameFilePawn(Team.BLACK)).isEqualTo(0) ); } + + @DisplayName("흑팀의 킹이 살아있는지 확인할 수 있다") + @Test + void should_CheckBlackKingIsAlive() { + Map positionPiece = new HashMap<>(); + + positionPiece.put(A1, new King(Team.WHITE)); + positionPiece.put(B3, new King(Team.BLACK)); + + ChessBoard chessBoard = new ChessBoard(positionPiece); + + assertThat(chessBoard.isBlackKingAlive()).isTrue(); + } + + @DisplayName("백팀의 킹이 살아있는지 확인할 수 있다") + @Test + void should_CheckWhiteKingIsAlive() { + Map positionPiece = new HashMap<>(); + + positionPiece.put(A1, new King(Team.WHITE)); + positionPiece.put(B3, new King(Team.BLACK)); + + ChessBoard chessBoard = new ChessBoard(positionPiece); + + assertThat(chessBoard.isWhiteKingAlive()).isTrue(); + } } From df94333edb795bf691f305a6dc094c282e4873a4 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 28 Mar 2024 11:48:26 +0900 Subject: [PATCH 35/80] =?UTF-8?q?docs:=204=EB=8B=A8=EA=B3=84=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 20ae1066451..1ec10432b64 100644 --- a/README.md +++ b/README.md @@ -78,4 +78,7 @@ Pawn의 기본 점수는 1점, 같은 세로줄에 같은 색의 폰이 있는 King은 점수가 없음 (잡히는 경우 게임 끝) ``` +#### 이어 하기 + +- [ ] 애플리케이션을 재시작하더라도 이전에 하던 체스 게임을 다시 시작할 수 있어야 한다. From 5219281429db96dcf06a92391c540b13462e6b1e Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 28 Mar 2024 13:31:02 +0900 Subject: [PATCH 36/80] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=97=90=20=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20=EC=9D=B5?= =?UTF-8?q?=EB=AA=85=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=A0=95=EC=9D=98=20?= =?UTF-8?q?=EB=B6=80=EB=B6=84=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/chess/domain/piece/PieceTest.java | 64 ++++--------------- 1 file changed, 11 insertions(+), 53 deletions(-) diff --git a/src/test/java/chess/domain/piece/PieceTest.java b/src/test/java/chess/domain/piece/PieceTest.java index a1d16ff0093..0aa8e5b55ba 100644 --- a/src/test/java/chess/domain/piece/PieceTest.java +++ b/src/test/java/chess/domain/piece/PieceTest.java @@ -8,65 +8,17 @@ import chess.fixture.PositionFixtures; import java.util.HashMap; import java.util.Map; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; class PieceTest { - private Piece blackPiece; - private Piece whitePiece; - - - @BeforeEach - void setUp() { - blackPiece = new Piece(Team.BLACK) { - @Override - boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { - return false; - } - - @Override - boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { - return false; - } - - @Override - public boolean isPawn() { - return false; - } - - @Override - public Score score() { - return new Score(29.3); - } - }; - - whitePiece = new Piece(Team.WHITE) { - @Override - boolean canNotMoveByItsOwnInPassing(Position start, Position destination) { - return false; - } - - @Override - boolean canNotMoveByBoardStatus(Position start, Position destination, ChessBoard chessBoard) { - return false; - } - - @Override - public boolean isPawn() { - return false; - } - - @Override - public Score score() { - return new Score(29.3); - } - }; - } @DisplayName("기물의 팀이 흑팀인지 확인할 수 있다") @Test void should_CheckPieceIsBlackTeam() { + Piece blackPiece = new King(Team.BLACK); + Piece whitePiece = new King(Team.WHITE); + assertAll( () -> assertThat(blackPiece.isBlackTeam()).isTrue(), () -> assertThat(whitePiece.isBlackTeam()).isFalse() @@ -76,15 +28,19 @@ void should_CheckPieceIsBlackTeam() { @DisplayName("기물간 서로 같은 팀인지 다른 팀인지 확인할 수 있다") @Test void should_ComparePieceTeam() { + Piece blackPiece = new King(Team.BLACK); + Piece whitePiece = new King(Team.WHITE); assertAll( () -> assertThat(blackPiece.isOtherTeam(Team.WHITE)).isTrue(), () -> assertThat(whitePiece.isOtherTeam(Team.WHITE)).isFalse() ); } - @DisplayName("모든 기물은 시작위치와 같은 곳으로 이동하지 못한다") + @DisplayName("기물은 시작위치와 같은 곳으로 이동하지 못한다") @Test void should_AllPieceCanNotMove_When_StartAndDestinationIsSame() { + Piece blackPiece = new King(Team.BLACK); + Piece whitePiece = new King(Team.WHITE); Map board = new HashMap<>(); board.put(PositionFixtures.A1, whitePiece); ChessBoard chessBoard = new ChessBoard(board); @@ -93,9 +49,11 @@ void should_AllPieceCanNotMove_When_StartAndDestinationIsSame() { assertThat(canMoveSamePosition).isFalse(); } - @DisplayName("모든 기물은 도착지에 아군이 있는 경우 이동하지 못한다") + @DisplayName("기물은 도착지에 아군이 있는 경우 이동하지 못한다") @Test void should_AllPieceCanNotMove_When_FriendlyPieceAtDestination() { + Piece blackPiece = new King(Team.BLACK); + Piece whitePiece = new King(Team.WHITE); Map board = new HashMap<>(); board.put(PositionFixtures.A1, whitePiece); board.put(PositionFixtures.A2, new King(Team.WHITE)); From 92e8d1015d60357b80dffdac442523ebeac47f07 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 28 Mar 2024 16:18:31 +0900 Subject: [PATCH 37/80] =?UTF-8?q?chore:=20DB=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 ++ docker/docker-compose.yml | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 docker/docker-compose.yml diff --git a/build.gradle b/build.gradle index 3697236c6fb..20ad08a5a5e 100644 --- a/build.gradle +++ b/build.gradle @@ -13,6 +13,8 @@ dependencies { testImplementation platform('org.assertj:assertj-bom:3.25.1') testImplementation('org.junit.jupiter:junit-jupiter') testImplementation('org.assertj:assertj-core') + + runtimeOnly("com.mysql:mysql-connector-j:8.3.0") } java { diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 00000000000..558a1d5a53f --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,18 @@ +version: "3.9" +services: + db: + image: mysql:8.0.28 + platform: linux/x86_64 + restart: always + ports: + - "13306:3306" + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: chess + MYSQL_USER: user + MYSQL_PASSWORD: password + TZ: Asia/Seoul + volumes: + - ./db/mysql/data:/var/lib/mysql + - ./db/mysql/config:/etc/mysql/conf.d + - ./db/mysql/init:/docker-entrypoint-initdb.d From c079e9656569d0e1516f39afbfbe97b8cd754bc1 Mon Sep 17 00:00:00 2001 From: libienz Date: Thu, 28 Mar 2024 16:47:31 +0900 Subject: [PATCH 38/80] =?UTF-8?q?feat:=20Connection=20=EA=B0=80=EC=A0=B8?= =?UTF-8?q?=EC=98=A4=EB=8A=94=20=EA=B4=80=EB=A6=AC=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/repository/ConnectionManager.java | 23 +++++++++++++++++++ .../repository/ConnectionManagerTest.java | 21 +++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/main/java/chess/repository/ConnectionManager.java create mode 100644 src/test/java/chess/repository/ConnectionManagerTest.java diff --git a/src/main/java/chess/repository/ConnectionManager.java b/src/main/java/chess/repository/ConnectionManager.java new file mode 100644 index 00000000000..aa591180a88 --- /dev/null +++ b/src/main/java/chess/repository/ConnectionManager.java @@ -0,0 +1,23 @@ +package chess.repository; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class ConnectionManager { + private static final String SERVER = "localhost:13306"; + private static final String DATABASE = "chess"; + private static final String OPTION = "?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; + private static final String USERNAME = "root"; + private static final String PASSWORD = "root"; + + public Connection getConnection() { + try { + return DriverManager.getConnection("jdbc:mysql://" + SERVER + "/" + DATABASE + OPTION, USERNAME, PASSWORD); + } catch (final SQLException e) { + System.err.println("DB 연결 오류:" + e.getMessage()); + e.printStackTrace(); + return null; + } + } +} diff --git a/src/test/java/chess/repository/ConnectionManagerTest.java b/src/test/java/chess/repository/ConnectionManagerTest.java new file mode 100644 index 00000000000..f1bb49107f0 --- /dev/null +++ b/src/test/java/chess/repository/ConnectionManagerTest.java @@ -0,0 +1,21 @@ +package chess.repository; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import java.sql.SQLException; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class ConnectionManagerTest { + private final ConnectionManager connectionManager = new ConnectionManager(); + + @DisplayName("DB 커넥션을 성공적으로 얻어올 수 있는지 테스트") + @Test + void should_ConnectionManagerCouldGetConnection() { + try (final var connection = connectionManager.getConnection()) { + assertThat(connection).isNotNull(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} From e6f0d07fc350ea1166eca2efb35961b06b797c4d Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 29 Mar 2024 23:23:04 +0900 Subject: [PATCH 39/80] =?UTF-8?q?feat:=20=EA=B8=B0=EB=AC=BC=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EA=B2=8C=ED=84=B0=20=ED=94=84=EB=A1=9C=ED=8D=BC?= =?UTF-8?q?=ED=8B=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/piece/Piece.java | 4 ++++ src/main/java/chess/domain/position/Position.java | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/main/java/chess/domain/piece/Piece.java b/src/main/java/chess/domain/piece/Piece.java index ea18dd17c81..ad364667da3 100644 --- a/src/main/java/chess/domain/piece/Piece.java +++ b/src/main/java/chess/domain/piece/Piece.java @@ -53,4 +53,8 @@ public boolean isBlackTeam() { public boolean isOtherTeam(Team other) { return this.team != other; } + + public Team getTeam() { + return team; + } } diff --git a/src/main/java/chess/domain/position/Position.java b/src/main/java/chess/domain/position/Position.java index 2bcc3db3aa2..641ece2fc28 100644 --- a/src/main/java/chess/domain/position/Position.java +++ b/src/main/java/chess/domain/position/Position.java @@ -95,6 +95,14 @@ private Position moveOneSpace(Direction direction) { return new Position(movedFile, movedRank); } + public File getFile() { + return file; + } + + public Rank getRank() { + return rank; + } + @Override public boolean equals(Object o) { if (this == o) { From 6c386fd75b22666dc99a1c313dc59c62843e3be0 Mon Sep 17 00:00:00 2001 From: libienz Date: Fri, 29 Mar 2024 23:43:46 +0900 Subject: [PATCH 40/80] =?UTF-8?q?feat:=20ChessGame=20=ED=84=B4=20=EA=B2=8C?= =?UTF-8?q?=ED=84=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/ChessGame.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/chess/domain/ChessGame.java b/src/main/java/chess/domain/ChessGame.java index 95d86cee8e9..6381c61b65c 100644 --- a/src/main/java/chess/domain/ChessGame.java +++ b/src/main/java/chess/domain/ChessGame.java @@ -39,6 +39,10 @@ public ChessBoard getChessBoard() { return chessBoard; } + public Team getTurn() { + return turn; + } + private void validateTurn(Piece piece) { if (piece.isOtherTeam(turn)) { throw new IllegalArgumentException(turn + "의 차례입니다"); From 74108b02654dae9baae30980e1682969de161556 Mon Sep 17 00:00:00 2001 From: libienz Date: Sat, 30 Mar 2024 00:59:51 +0900 Subject: [PATCH 41/80] =?UTF-8?q?feat:=20=ED=84=B4=EC=9D=84=20=EC=A1=B0?= =?UTF-8?q?=EC=9E=91=ED=95=98=EB=8A=94=20=EC=B2=B4=EC=8A=A4=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20=EC=83=9D=EC=84=B1=EC=9E=90=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/ChessGame.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/chess/domain/ChessGame.java b/src/main/java/chess/domain/ChessGame.java index 6381c61b65c..28df13f70c5 100644 --- a/src/main/java/chess/domain/ChessGame.java +++ b/src/main/java/chess/domain/ChessGame.java @@ -14,8 +14,12 @@ public class ChessGame { private Team turn; public ChessGame(ChessBoard chessBoard) { + this(chessBoard, INITIAL_TURN); + } + + public ChessGame(ChessBoard chessBoard, Team turn) { this.chessBoard = chessBoard; - turn = INITIAL_TURN; + this.turn = turn; } public void move(Position start, Position destiantion) { From fb2c817bd89cd59fa7512e42122346fbad837c10 Mon Sep 17 00:00:00 2001 From: libienz Date: Sat, 30 Mar 2024 01:03:58 +0900 Subject: [PATCH 42/80] =?UTF-8?q?feat:=20=ED=94=BC=EC=8A=A4=20=EB=B0=B0?= =?UTF-8?q?=EC=B9=98=20=EC=A0=95=EB=B3=B4=20=EC=A0=84=EB=8B=AC=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/chess/dto/PiecePlacementDto.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/chess/dto/PiecePlacementDto.java diff --git a/src/main/java/chess/dto/PiecePlacementDto.java b/src/main/java/chess/dto/PiecePlacementDto.java new file mode 100644 index 00000000000..6151d2d72fe --- /dev/null +++ b/src/main/java/chess/dto/PiecePlacementDto.java @@ -0,0 +1,36 @@ +package chess.dto; + +import chess.domain.piece.Piece; +import chess.domain.position.Position; +import chess.repository.mapper.ValueMapper; + +public class PiecePlacementDto { + private final String position; + private final String team; + private final String type; + + public PiecePlacementDto(String position, String team, String type) { + this.position = position; + this.team = team; + this.type = type; + } + + public static PiecePlacementDto of(Piece piece, Position position) { + return new PiecePlacementDto( + ValueMapper.mapPositionToString(position), + ValueMapper.mapTeamToString(piece.getTeam()), + ValueMapper.mapPieceTypeToString(piece)); + } + + public String getPosition() { + return position; + } + + public String getTeam() { + return team; + } + + public String getType() { + return type; + } +} From 9f787d642378c5aa09fc0f0ba8dc333b215761f8 Mon Sep 17 00:00:00 2001 From: libienz Date: Sat, 30 Mar 2024 01:04:38 +0900 Subject: [PATCH 43/80] =?UTF-8?q?feat:=20=EB=8F=84=EB=A9=94=EC=9D=B8=20<->?= =?UTF-8?q?=20DB=20value=20=EB=B3=80=ED=99=98=20=EB=A7=A4=ED=8D=BC=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/repository/mapper/DomainMapper.java | 79 +++++++++++++++++++ .../chess/repository/mapper/ValueMapper.java | 51 ++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 src/main/java/chess/repository/mapper/DomainMapper.java create mode 100644 src/main/java/chess/repository/mapper/ValueMapper.java diff --git a/src/main/java/chess/repository/mapper/DomainMapper.java b/src/main/java/chess/repository/mapper/DomainMapper.java new file mode 100644 index 00000000000..4911520722a --- /dev/null +++ b/src/main/java/chess/repository/mapper/DomainMapper.java @@ -0,0 +1,79 @@ +package chess.repository.mapper; + +import chess.domain.board.ChessBoard; +import chess.domain.piece.Bishop; +import chess.domain.piece.King; +import chess.domain.piece.Knight; +import chess.domain.piece.Pawn; +import chess.domain.piece.Piece; +import chess.domain.piece.Queen; +import chess.domain.piece.Rook; +import chess.domain.piece.Team; +import chess.domain.position.File; +import chess.domain.position.Position; +import chess.domain.position.Rank; +import chess.dto.PiecePlacementDto; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DomainMapper { + private static final int FILE_INDEX = 0; + private static final int RANK_INDEX = 1; + + public static Team mapToTurn(String value) { + if (value.equals("WHITE")) { + return Team.WHITE; + } + return Team.BLACK; + } + + public static ChessBoard mapToBoard(List piecePlacements) { + Map positionPiece = new HashMap<>(); + + for (PiecePlacementDto piecePlacementDto : piecePlacements) { + Position position = mapToPosition(piecePlacementDto.getPosition()); + Team team = mapToTeam(piecePlacementDto.getTeam()); + Piece piece = mapToPiece(piecePlacementDto.getType(), team); + + positionPiece.put(position, piece); + } + + return new ChessBoard(positionPiece); + } + + private static Piece mapToPiece(String pieceTypeValue, Team team) { + if ("pawn".equals(pieceTypeValue)) { + return new Pawn(team); + } + if ("knight".equals(pieceTypeValue)) { + return new Knight(team); + } + if ("bishop".equals(pieceTypeValue)) { + return new Bishop(team); + } + if ("rook".equals(pieceTypeValue)) { + return new Rook(team); + } + if ("queen".equals(pieceTypeValue)) { + return new Queen(team); + } + if ("king".equals(pieceTypeValue)) { + return new King(team); + } + throw new IllegalArgumentException("Invalid piece type: " + pieceTypeValue); + } + + private static Position mapToPosition(String value) { + File file = File.from(value.charAt(FILE_INDEX) - 'A'); + Rank rank = Rank.from(8 - (value.charAt(RANK_INDEX) - '0')); + return new Position(file, rank); + } + + private static Team mapToTeam(String value) { + if (value.equals("WHITE")) { + return Team.WHITE; + } + return Team.BLACK; + } +} diff --git a/src/main/java/chess/repository/mapper/ValueMapper.java b/src/main/java/chess/repository/mapper/ValueMapper.java new file mode 100644 index 00000000000..0f5c8f17c40 --- /dev/null +++ b/src/main/java/chess/repository/mapper/ValueMapper.java @@ -0,0 +1,51 @@ +package chess.repository.mapper; + +import chess.domain.piece.Bishop; +import chess.domain.piece.King; +import chess.domain.piece.Knight; +import chess.domain.piece.Pawn; +import chess.domain.piece.Piece; +import chess.domain.piece.Queen; +import chess.domain.piece.Rook; +import chess.domain.piece.Team; +import chess.domain.position.File; +import chess.domain.position.Position; +import chess.domain.position.Rank; + +public class ValueMapper { + private ValueMapper() { + } + + public static String mapPositionToString(Position position) { + File file = position.getFile(); + Rank rank = position.getRank(); + return file.name() + rank.name(); + } + + public static String mapTeamToString(Team team) { + return team.name(); + } + + public static String mapPieceTypeToString(Piece piece) { + if (piece instanceof Pawn) { + return "pawn"; + } + if (piece instanceof Rook) { + return "rook"; + } + if (piece instanceof Knight) { + return "knight"; + } + if (piece instanceof Bishop) { + return "bishop"; + } + if (piece instanceof Queen) { + return "queen"; + } + if (piece instanceof King) { + return "king"; + } + throw new IllegalArgumentException("알 수 없는 피스 타입입니다"); + } + +} From 30d43b5e4fee21b307407a760ffde997c703bcf5 Mon Sep 17 00:00:00 2001 From: libienz Date: Sat, 30 Mar 2024 01:05:06 +0900 Subject: [PATCH 44/80] =?UTF-8?q?feat:=20=EA=B8=B0=EB=AC=BC=20=EB=B0=B0?= =?UTF-8?q?=EC=B9=98=20=EC=A0=95=EB=B3=B4=20=EC=A0=80=EC=9E=A5=EC=86=8C=20?= =?UTF-8?q?dao=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/repository/PieceRepository.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/main/java/chess/repository/PieceRepository.java diff --git a/src/main/java/chess/repository/PieceRepository.java b/src/main/java/chess/repository/PieceRepository.java new file mode 100644 index 00000000000..9f2643ce949 --- /dev/null +++ b/src/main/java/chess/repository/PieceRepository.java @@ -0,0 +1,59 @@ +package chess.repository; + +import chess.dto.PiecePlacementDto; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +public class PieceRepository { + private static final String TABLE_NAME = "pieces"; + + private final ConnectionManager connectionManager; + + public PieceRepository(ConnectionManager connectionManager) { + this.connectionManager = connectionManager; + } + + public void savePiece(PiecePlacementDto piecePlacementDto) throws SQLException { + String query = String.format("INSERT INTO %s VALUES(?, ?, ?", TABLE_NAME); + + Connection connection = connectionManager.getConnection(); + PreparedStatement pstmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); + pstmt.setString(1, piecePlacementDto.getPosition()); + pstmt.setString(2, piecePlacementDto.getTeam()); + pstmt.setString(3, piecePlacementDto.getType()); + + pstmt.execute(); + } + + public List findPieces() throws SQLException { + String query = String.format("SELECT * FROM %s", TABLE_NAME); + Connection connection = connectionManager.getConnection(); + PreparedStatement pstmt = connection.prepareStatement(query); + + ResultSet resultSet = pstmt.executeQuery(); + List result = new ArrayList<>(); + while (resultSet.next()) { + long id = resultSet.getLong("id"); + String position = resultSet.getString("position"); + String team = resultSet.getString("team"); + String type = resultSet.getString("type"); + + result.add(new PiecePlacementDto(position, team, type)); + } + + return result; + } + + public void deleteAll() throws SQLException { + String query = String.format("DELETE FROM %s", TABLE_NAME); + + Connection connection = connectionManager.getConnection(); + PreparedStatement pstmt = connection.prepareStatement(query); + pstmt.executeUpdate(); + } +} From dcda05df16444a04330305b0d499de2ff7280383 Mon Sep 17 00:00:00 2001 From: libienz Date: Sat, 30 Mar 2024 01:05:30 +0900 Subject: [PATCH 45/80] =?UTF-8?q?feat:=20=ED=84=B4=20=EC=A0=95=EB=B3=B4=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/chess/repository/TurnRepository.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/chess/repository/TurnRepository.java diff --git a/src/main/java/chess/repository/TurnRepository.java b/src/main/java/chess/repository/TurnRepository.java new file mode 100644 index 00000000000..6f476888e09 --- /dev/null +++ b/src/main/java/chess/repository/TurnRepository.java @@ -0,0 +1,48 @@ +package chess.repository; + +import chess.domain.piece.Team; +import chess.repository.mapper.DomainMapper; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class TurnRepository { + private static final String TABLE_NAME = "turn"; + + private final ConnectionManager connectionManager; + + public TurnRepository(ConnectionManager connectionManager) { + this.connectionManager = connectionManager; + } + + public void saveTurn(Team turn) throws SQLException { + String query = String.format("INSERT INTO %s VALUES(?", TABLE_NAME); + + Connection connection = connectionManager.getConnection(); + PreparedStatement pstmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); + pstmt.setString(1, turn.name()); + pstmt.execute(); + } + + public Team findCurrentTurn() throws SQLException { + String query = String.format("SELECT * from %s WHERE ID = 1", TABLE_NAME); + + Connection connection = connectionManager.getConnection(); + PreparedStatement pstmt = connection.prepareStatement(query); + + ResultSet resultSet = pstmt.executeQuery(); + String value = resultSet.getString(1); + + return DomainMapper.mapToTurn(value); + } + + public void deleteAll() throws SQLException { + String query = String.format("DELETE FROM %s", TABLE_NAME); + + Connection connection = connectionManager.getConnection(); + PreparedStatement pstmt = connection.prepareStatement(query); + pstmt.executeUpdate(); + } +} From 09fec550b480e84a6ee3a180f5f708faa50a78ba Mon Sep 17 00:00:00 2001 From: libienz Date: Sat, 30 Mar 2024 01:05:50 +0900 Subject: [PATCH 46/80] =?UTF-8?q?feat:=20=EC=B2=B4=EC=8A=A4=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20=EC=A0=80=EC=9E=A5=20=EB=B0=8F=20=EB=A1=9C=EB=93=9C?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/chess/service/ChessGameService.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/chess/service/ChessGameService.java diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java new file mode 100644 index 00000000000..48db3614d02 --- /dev/null +++ b/src/main/java/chess/service/ChessGameService.java @@ -0,0 +1,48 @@ +package chess.service; + +import chess.domain.ChessGame; +import chess.domain.board.ChessBoard; +import chess.domain.piece.Piece; +import chess.domain.piece.Team; +import chess.domain.position.Position; +import chess.dto.PiecePlacementDto; +import chess.repository.PieceRepository; +import chess.repository.TurnRepository; +import chess.repository.mapper.DomainMapper; +import java.sql.SQLException; +import java.util.List; +import java.util.Map; + +public class ChessGameService { + private final PieceRepository pieceRepository; + private final TurnRepository turnRepository; + + public ChessGameService(PieceRepository pieceRepository, TurnRepository turnRepository) { + this.pieceRepository = pieceRepository; + this.turnRepository = turnRepository; + } + + public void saveChessGame(ChessGame chessGame) throws SQLException { + deleteSavedChessGame(); + ChessBoard chessBoard = chessGame.getChessBoard(); + Map board = chessBoard.getBoard(); + + for (Position position : board.keySet()) { + Piece piece = board.get(position); + pieceRepository.savePiece(PiecePlacementDto.of(piece, position)); + } + turnRepository.saveTurn(chessGame.getTurn()); + } + + private void deleteSavedChessGame() throws SQLException { + pieceRepository.deleteAll(); + } + + public ChessGame loadChessGame() throws SQLException { + List pieces = pieceRepository.findPieces(); + ChessBoard chessBoard = DomainMapper.mapToBoard(pieces); + Team currentTurn = turnRepository.findCurrentTurn(); + + return new ChessGame(chessBoard, currentTurn); + } +} From 3b201cf4bd1a70f2399d5df9588adc7071a16782 Mon Sep 17 00:00:00 2001 From: libienz Date: Sat, 30 Mar 2024 01:11:36 +0900 Subject: [PATCH 47/80] =?UTF-8?q?feat:=20=EC=B2=B4=EC=8A=A4=EB=B3=B4?= =?UTF-8?q?=EB=93=9C=20=EB=A7=B5=20=EA=B2=8C=ED=84=B0=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=ED=8D=BC=ED=8B=B0=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/chess/domain/board/ChessBoard.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/chess/domain/board/ChessBoard.java b/src/main/java/chess/domain/board/ChessBoard.java index 4e090242294..7ec8dd4351c 100644 --- a/src/main/java/chess/domain/board/ChessBoard.java +++ b/src/main/java/chess/domain/board/ChessBoard.java @@ -103,4 +103,8 @@ private int countFriendlyPawnAtFile(Team friendlyTeam, File file) { } return 0; } + + public Map getBoard() { + return board; + } } From 8ba88ffcc1bb535ff27b22e7ac5ae26e98afe8b0 Mon Sep 17 00:00:00 2001 From: libienz Date: Sat, 30 Mar 2024 01:12:17 +0900 Subject: [PATCH 48/80] =?UTF-8?q?refactor:=20=EC=98=81=EC=86=8D=EC=84=B1?= =?UTF-8?q?=20=EB=A0=88=EC=9D=B4=EC=96=B4=20try=20with=20resources?= =?UTF-8?q?=EB=A5=BC=20=ED=99=9C=EC=9A=A9=ED=95=98=EC=97=AC=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EB=A5=BC=20=EC=B2=98=EB=A6=AC=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/repository/PieceRepository.java | 59 +++++++++++-------- .../java/chess/repository/TurnRepository.java | 50 ++++++++++------ 2 files changed, 69 insertions(+), 40 deletions(-) diff --git a/src/main/java/chess/repository/PieceRepository.java b/src/main/java/chess/repository/PieceRepository.java index 9f2643ce949..a742562ec54 100644 --- a/src/main/java/chess/repository/PieceRepository.java +++ b/src/main/java/chess/repository/PieceRepository.java @@ -18,42 +18,55 @@ public PieceRepository(ConnectionManager connectionManager) { this.connectionManager = connectionManager; } - public void savePiece(PiecePlacementDto piecePlacementDto) throws SQLException { + public void savePiece(PiecePlacementDto piecePlacementDto) { String query = String.format("INSERT INTO %s VALUES(?, ?, ?", TABLE_NAME); - Connection connection = connectionManager.getConnection(); - PreparedStatement pstmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); - pstmt.setString(1, piecePlacementDto.getPosition()); - pstmt.setString(2, piecePlacementDto.getTeam()); - pstmt.setString(3, piecePlacementDto.getType()); + try (Connection connection = connectionManager.getConnection(); + PreparedStatement pstmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { - pstmt.execute(); + pstmt.setString(1, piecePlacementDto.getPosition()); + pstmt.setString(2, piecePlacementDto.getTeam()); + pstmt.setString(3, piecePlacementDto.getType()); + + pstmt.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } } - public List findPieces() throws SQLException { + public List findPieces() { String query = String.format("SELECT * FROM %s", TABLE_NAME); - Connection connection = connectionManager.getConnection(); - PreparedStatement pstmt = connection.prepareStatement(query); - ResultSet resultSet = pstmt.executeQuery(); List result = new ArrayList<>(); - while (resultSet.next()) { - long id = resultSet.getLong("id"); - String position = resultSet.getString("position"); - String team = resultSet.getString("team"); - String type = resultSet.getString("type"); - result.add(new PiecePlacementDto(position, team, type)); - } + try (Connection connection = connectionManager.getConnection(); + PreparedStatement pstmt = connection.prepareStatement(query); + ResultSet resultSet = pstmt.executeQuery()) { + + while (resultSet.next()) { + long id = resultSet.getLong("id"); + String position = resultSet.getString("position"); + String team = resultSet.getString("team"); + String type = resultSet.getString("type"); - return result; + result.add(new PiecePlacementDto(position, team, type)); + } + return result; + } catch (SQLException e) { + e.printStackTrace(); + } + throw new RuntimeException("기물을 가져오는 과정에서 오류가 발생했습니다"); } - public void deleteAll() throws SQLException { + public void deleteAll() { String query = String.format("DELETE FROM %s", TABLE_NAME); - Connection connection = connectionManager.getConnection(); - PreparedStatement pstmt = connection.prepareStatement(query); - pstmt.executeUpdate(); + try (Connection connection = connectionManager.getConnection(); + PreparedStatement pstmt = connection.prepareStatement(query)) { + + pstmt.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } } } diff --git a/src/main/java/chess/repository/TurnRepository.java b/src/main/java/chess/repository/TurnRepository.java index 6f476888e09..886bacaf495 100644 --- a/src/main/java/chess/repository/TurnRepository.java +++ b/src/main/java/chess/repository/TurnRepository.java @@ -17,32 +17,48 @@ public TurnRepository(ConnectionManager connectionManager) { this.connectionManager = connectionManager; } - public void saveTurn(Team turn) throws SQLException { - String query = String.format("INSERT INTO %s VALUES(?", TABLE_NAME); + public void saveTurn(Team turn) { + String query = String.format("INSERT INTO %s VALUES(?)", TABLE_NAME); - Connection connection = connectionManager.getConnection(); - PreparedStatement pstmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); - pstmt.setString(1, turn.name()); - pstmt.execute(); + try (Connection connection = connectionManager.getConnection(); + PreparedStatement pstmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { + + pstmt.setString(1, turn.name()); + pstmt.execute(); + + } catch (SQLException e) { + e.printStackTrace(); + } } - public Team findCurrentTurn() throws SQLException { - String query = String.format("SELECT * from %s WHERE ID = 1", TABLE_NAME); + public Team findCurrentTurn() { + String query = String.format("SELECT * FROM %s WHERE ID = 1", TABLE_NAME); - Connection connection = connectionManager.getConnection(); - PreparedStatement pstmt = connection.prepareStatement(query); + try (Connection connection = connectionManager.getConnection(); + PreparedStatement pstmt = connection.prepareStatement(query); + ResultSet resultSet = pstmt.executeQuery()) { - ResultSet resultSet = pstmt.executeQuery(); - String value = resultSet.getString(1); + if (resultSet.next()) { + String value = resultSet.getString(1); + return DomainMapper.mapToTurn(value); + } - return DomainMapper.mapToTurn(value); + } catch (SQLException e) { + e.printStackTrace(); + } + throw new RuntimeException("턴을 가져오는 과정에서 오류가 발생했습니다"); } - public void deleteAll() throws SQLException { + public void deleteAll() { String query = String.format("DELETE FROM %s", TABLE_NAME); - Connection connection = connectionManager.getConnection(); - PreparedStatement pstmt = connection.prepareStatement(query); - pstmt.executeUpdate(); + try (Connection connection = connectionManager.getConnection(); + PreparedStatement pstmt = connection.prepareStatement(query)) { + + pstmt.executeUpdate(); + + } catch (SQLException e) { + e.printStackTrace(); + } } } From f39f6f10add4dffa67ba3e8b416573028ffcb8d4 Mon Sep 17 00:00:00 2001 From: libienz Date: Sat, 30 Mar 2024 02:02:58 +0900 Subject: [PATCH 49/80] =?UTF-8?q?refactor:=20=EC=98=81=EC=86=8D=ED=99=94?= =?UTF-8?q?=20=EB=A0=88=EC=9D=B4=EC=96=B4=20=EC=98=B5=EC=85=94=EB=84=90=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/repository/PieceRepository.java | 9 ++++++--- src/main/java/chess/repository/TurnRepository.java | 8 ++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/chess/repository/PieceRepository.java b/src/main/java/chess/repository/PieceRepository.java index a742562ec54..adf51da2f5e 100644 --- a/src/main/java/chess/repository/PieceRepository.java +++ b/src/main/java/chess/repository/PieceRepository.java @@ -8,6 +8,7 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.List; +import java.util.Optional; public class PieceRepository { private static final String TABLE_NAME = "pieces"; @@ -34,7 +35,7 @@ public void savePiece(PiecePlacementDto piecePlacementDto) { } } - public List findPieces() { + public Optional> findPieces() { String query = String.format("SELECT * FROM %s", TABLE_NAME); List result = new ArrayList<>(); @@ -51,11 +52,13 @@ public List findPieces() { result.add(new PiecePlacementDto(position, team, type)); } - return result; + if (!result.isEmpty()) { + return Optional.of(result); + } } catch (SQLException e) { e.printStackTrace(); } - throw new RuntimeException("기물을 가져오는 과정에서 오류가 발생했습니다"); + return Optional.empty(); } public void deleteAll() { diff --git a/src/main/java/chess/repository/TurnRepository.java b/src/main/java/chess/repository/TurnRepository.java index 886bacaf495..b3358ac270f 100644 --- a/src/main/java/chess/repository/TurnRepository.java +++ b/src/main/java/chess/repository/TurnRepository.java @@ -7,6 +7,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.Optional; public class TurnRepository { private static final String TABLE_NAME = "turn"; @@ -31,7 +32,7 @@ public void saveTurn(Team turn) { } } - public Team findCurrentTurn() { + public Optional findCurrentTurn() { String query = String.format("SELECT * FROM %s WHERE ID = 1", TABLE_NAME); try (Connection connection = connectionManager.getConnection(); @@ -40,13 +41,12 @@ public Team findCurrentTurn() { if (resultSet.next()) { String value = resultSet.getString(1); - return DomainMapper.mapToTurn(value); + return Optional.of(DomainMapper.mapToTurn(value)); } - } catch (SQLException e) { e.printStackTrace(); } - throw new RuntimeException("턴을 가져오는 과정에서 오류가 발생했습니다"); + return Optional.empty(); } public void deleteAll() { From 54e348d43f77e69726e974bb310ca21b1d19e447 Mon Sep 17 00:00:00 2001 From: libienz Date: Sat, 30 Mar 2024 02:32:34 +0900 Subject: [PATCH 50/80] =?UTF-8?q?feat:=20DB=20=EC=A0=80=EC=9E=A5=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=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/chess/ChessApplication.java | 9 ++++++- .../chess/controller/ChessGameController.java | 10 ++++++-- src/main/java/chess/domain/position/Rank.java | 24 ++++++++++++------- .../chess/repository/PieceRepository.java | 2 +- .../java/chess/repository/TurnRepository.java | 4 ++-- .../chess/repository/mapper/DomainMapper.java | 2 +- .../chess/repository/mapper/ValueMapper.java | 2 +- .../java/chess/service/ChessGameService.java | 19 ++++++++++----- 8 files changed, 49 insertions(+), 23 deletions(-) diff --git a/src/main/java/chess/ChessApplication.java b/src/main/java/chess/ChessApplication.java index cbba067f7f9..16b30cb9178 100644 --- a/src/main/java/chess/ChessApplication.java +++ b/src/main/java/chess/ChessApplication.java @@ -1,6 +1,10 @@ package chess; import chess.controller.ChessGameController; +import chess.repository.ConnectionManager; +import chess.repository.PieceRepository; +import chess.repository.TurnRepository; +import chess.service.ChessGameService; import chess.view.CommandParser; import chess.view.InputView; import chess.view.OutputView; @@ -9,8 +13,11 @@ public class ChessApplication { public static void main(String[] args) { InputView inputView = new InputView(new CommandParser()); OutputView outputView = new OutputView(); + PieceRepository pieceRepository = new PieceRepository(new ConnectionManager()); + TurnRepository turnRepository = new TurnRepository(new ConnectionManager()); + ChessGameService chessGameService = new ChessGameService(pieceRepository, turnRepository); - ChessGameController controller = new ChessGameController(inputView, outputView); + ChessGameController controller = new ChessGameController(inputView, outputView, chessGameService); controller.run(); } } diff --git a/src/main/java/chess/controller/ChessGameController.java b/src/main/java/chess/controller/ChessGameController.java index 5e60f7bdece..7773679bcf7 100644 --- a/src/main/java/chess/controller/ChessGameController.java +++ b/src/main/java/chess/controller/ChessGameController.java @@ -5,26 +5,32 @@ import chess.domain.ChessGame; import chess.domain.board.ChessBoard; import chess.domain.board.ChessBoardCreator; +import chess.service.ChessGameService; import chess.view.InputView; import chess.view.OutputView; public class ChessGameController { private final InputView inputView; private final OutputView outputView; + private final ChessGameService chessGameService; - public ChessGameController(InputView inputView, OutputView outputView) { + public ChessGameController(InputView inputView, OutputView outputView, ChessGameService chessGameService) { this.inputView = inputView; this.outputView = outputView; + this.chessGameService = chessGameService; } public void run() { outputView.printStartMessage(); ChessGame chessGame = initializeChessGame(); - + if (chessGameService.isChessGameInProgress()) { + chessGame = chessGameService.loadChessGame(); + } ExecuteResult result; do { Command command = inputView.readCommand(); result = command.execute(chessGame, outputView); + chessGameService.saveChessGame(chessGame); } while (result.isSuccess() && result.isNeedNextCommand() && chessGame.isNotEnd()); } diff --git a/src/main/java/chess/domain/position/Rank.java b/src/main/java/chess/domain/position/Rank.java index 6012227fb57..d6730f0d00f 100644 --- a/src/main/java/chess/domain/position/Rank.java +++ b/src/main/java/chess/domain/position/Rank.java @@ -4,20 +4,22 @@ import java.util.NoSuchElementException; public enum Rank { - EIGHT(0), - SEVEN(1), - SIX(2), - FIVE(3), - FOUR(4), - THREE(5), - TWO(6), - ONE(7); + EIGHT(0, 8), + SEVEN(1, 7), + SIX(2, 6), + FIVE(3, 5), + FOUR(4, 4), + THREE(5, 3), + TWO(6, 2), + ONE(7, 1); private final int rowNumber; + private final int rankNumber; - Rank(int rowNumber) { + Rank(int rowNumber, int rankNumber) { this.rowNumber = rowNumber; + this.rankNumber = rankNumber; } public static Rank from(int rowNumber) { @@ -46,4 +48,8 @@ public boolean isBelow(Rank other) { public Rank move(int weight) { return from(rowNumber + weight); } + + public int getRankNumber() { + return rankNumber; + } } diff --git a/src/main/java/chess/repository/PieceRepository.java b/src/main/java/chess/repository/PieceRepository.java index adf51da2f5e..f7d1bca0511 100644 --- a/src/main/java/chess/repository/PieceRepository.java +++ b/src/main/java/chess/repository/PieceRepository.java @@ -20,7 +20,7 @@ public PieceRepository(ConnectionManager connectionManager) { } public void savePiece(PiecePlacementDto piecePlacementDto) { - String query = String.format("INSERT INTO %s VALUES(?, ?, ?", TABLE_NAME); + String query = String.format("INSERT INTO %s (position, team, type) VALUES (?, ?, ?)", TABLE_NAME); try (Connection connection = connectionManager.getConnection(); PreparedStatement pstmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { diff --git a/src/main/java/chess/repository/TurnRepository.java b/src/main/java/chess/repository/TurnRepository.java index b3358ac270f..a9eaafefa71 100644 --- a/src/main/java/chess/repository/TurnRepository.java +++ b/src/main/java/chess/repository/TurnRepository.java @@ -19,7 +19,7 @@ public TurnRepository(ConnectionManager connectionManager) { } public void saveTurn(Team turn) { - String query = String.format("INSERT INTO %s VALUES(?)", TABLE_NAME); + String query = String.format("INSERT INTO %s (turn) VALUES(?)", TABLE_NAME); try (Connection connection = connectionManager.getConnection(); PreparedStatement pstmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { @@ -33,7 +33,7 @@ public void saveTurn(Team turn) { } public Optional findCurrentTurn() { - String query = String.format("SELECT * FROM %s WHERE ID = 1", TABLE_NAME); + String query = String.format("SELECT * FROM %s", TABLE_NAME); try (Connection connection = connectionManager.getConnection(); PreparedStatement pstmt = connection.prepareStatement(query); diff --git a/src/main/java/chess/repository/mapper/DomainMapper.java b/src/main/java/chess/repository/mapper/DomainMapper.java index 4911520722a..104857f6b60 100644 --- a/src/main/java/chess/repository/mapper/DomainMapper.java +++ b/src/main/java/chess/repository/mapper/DomainMapper.java @@ -65,7 +65,7 @@ private static Piece mapToPiece(String pieceTypeValue, Team team) { } private static Position mapToPosition(String value) { - File file = File.from(value.charAt(FILE_INDEX) - 'A'); + File file = File.from(value.charAt(FILE_INDEX) - 'a'); Rank rank = Rank.from(8 - (value.charAt(RANK_INDEX) - '0')); return new Position(file, rank); } diff --git a/src/main/java/chess/repository/mapper/ValueMapper.java b/src/main/java/chess/repository/mapper/ValueMapper.java index 0f5c8f17c40..2110b2ddd74 100644 --- a/src/main/java/chess/repository/mapper/ValueMapper.java +++ b/src/main/java/chess/repository/mapper/ValueMapper.java @@ -19,7 +19,7 @@ private ValueMapper() { public static String mapPositionToString(Position position) { File file = position.getFile(); Rank rank = position.getRank(); - return file.name() + rank.name(); + return file.name().toLowerCase() + rank.getRankNumber(); } public static String mapTeamToString(Team team) { diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index 48db3614d02..b8b29f4547a 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -9,7 +9,6 @@ import chess.repository.PieceRepository; import chess.repository.TurnRepository; import chess.repository.mapper.DomainMapper; -import java.sql.SQLException; import java.util.List; import java.util.Map; @@ -22,7 +21,7 @@ public ChessGameService(PieceRepository pieceRepository, TurnRepository turnRepo this.turnRepository = turnRepository; } - public void saveChessGame(ChessGame chessGame) throws SQLException { + public void saveChessGame(ChessGame chessGame) { deleteSavedChessGame(); ChessBoard chessBoard = chessGame.getChessBoard(); Map board = chessBoard.getBoard(); @@ -34,15 +33,23 @@ public void saveChessGame(ChessGame chessGame) throws SQLException { turnRepository.saveTurn(chessGame.getTurn()); } - private void deleteSavedChessGame() throws SQLException { + private void deleteSavedChessGame() { pieceRepository.deleteAll(); + turnRepository.deleteAll(); } - public ChessGame loadChessGame() throws SQLException { - List pieces = pieceRepository.findPieces(); + public ChessGame loadChessGame() { + List pieces = pieceRepository.findPieces().get(); + Team currentTurn = turnRepository.findCurrentTurn().get(); ChessBoard chessBoard = DomainMapper.mapToBoard(pieces); - Team currentTurn = turnRepository.findCurrentTurn(); return new ChessGame(chessBoard, currentTurn); } + + public boolean isChessGameInProgress() { + if (pieceRepository.findPieces().isEmpty()) { + return false; + } + return true; + } } From 20cf64fc7daccafde0821ddba0876fd3e20cc39a Mon Sep 17 00:00:00 2001 From: libienz Date: Sat, 30 Mar 2024 02:46:44 +0900 Subject: [PATCH 51/80] =?UTF-8?q?fix:=20=ED=84=B4=EC=9D=84=20db=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EA=B0=80=EC=A0=B8=EC=98=AC=20=EB=95=8C=20=EC=9E=98?= =?UTF-8?q?=EB=AA=BB=20=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8A=94=20=ED=98=84?= =?UTF-8?q?=EC=83=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/controller/ChessGameController.java | 3 +-- src/main/java/chess/controller/command/Command.java | 3 ++- src/main/java/chess/controller/command/EndCommand.java | 4 +++- src/main/java/chess/controller/command/MoveCommand.java | 4 +++- src/main/java/chess/controller/command/StartCommand.java | 3 ++- src/main/java/chess/controller/command/StatusCommand.java | 3 ++- src/main/java/chess/repository/TurnRepository.java | 2 +- 7 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/chess/controller/ChessGameController.java b/src/main/java/chess/controller/ChessGameController.java index 7773679bcf7..9cf84542ef5 100644 --- a/src/main/java/chess/controller/ChessGameController.java +++ b/src/main/java/chess/controller/ChessGameController.java @@ -29,8 +29,7 @@ public void run() { ExecuteResult result; do { Command command = inputView.readCommand(); - result = command.execute(chessGame, outputView); - chessGameService.saveChessGame(chessGame); + result = command.execute(chessGameService, chessGame, outputView); } while (result.isSuccess() && result.isNeedNextCommand() && chessGame.isNotEnd()); } diff --git a/src/main/java/chess/controller/command/Command.java b/src/main/java/chess/controller/command/Command.java index bd076ac4ff3..025a2b1a52c 100644 --- a/src/main/java/chess/controller/command/Command.java +++ b/src/main/java/chess/controller/command/Command.java @@ -1,8 +1,9 @@ package chess.controller.command; import chess.domain.ChessGame; +import chess.service.ChessGameService; import chess.view.OutputView; public interface Command { - ExecuteResult execute(ChessGame chessGame, OutputView outputView); + ExecuteResult execute(ChessGameService chessGameService, ChessGame chessGame, OutputView outputView); } diff --git a/src/main/java/chess/controller/command/EndCommand.java b/src/main/java/chess/controller/command/EndCommand.java index db380f46762..bc10f414f3b 100644 --- a/src/main/java/chess/controller/command/EndCommand.java +++ b/src/main/java/chess/controller/command/EndCommand.java @@ -1,11 +1,13 @@ package chess.controller.command; import chess.domain.ChessGame; +import chess.service.ChessGameService; import chess.view.OutputView; public class EndCommand implements Command { @Override - public ExecuteResult execute(ChessGame chessGame, OutputView outputView) { + public ExecuteResult execute(ChessGameService chessGameService, ChessGame chessGame, OutputView outputView) { + chessGameService.saveChessGame(chessGame); return new ExecuteResult(true, false); } } diff --git a/src/main/java/chess/controller/command/MoveCommand.java b/src/main/java/chess/controller/command/MoveCommand.java index 6434c6fae89..70317cb308d 100644 --- a/src/main/java/chess/controller/command/MoveCommand.java +++ b/src/main/java/chess/controller/command/MoveCommand.java @@ -2,6 +2,7 @@ import chess.domain.ChessGame; import chess.domain.position.Position; +import chess.service.ChessGameService; import chess.view.OutputView; public class MoveCommand implements Command { @@ -14,8 +15,9 @@ public MoveCommand(Position start, Position destination) { } @Override - public ExecuteResult execute(ChessGame chessGame, OutputView outputView) { + public ExecuteResult execute(ChessGameService chessGameService, ChessGame chessGame, OutputView outputView) { chessGame.move(start, destination); + chessGameService.saveChessGame(chessGame); outputView.printChessBoardMessage(chessGame.getChessBoard()); return new ExecuteResult(true, true); } diff --git a/src/main/java/chess/controller/command/StartCommand.java b/src/main/java/chess/controller/command/StartCommand.java index e52a95952e6..eef9f33a507 100644 --- a/src/main/java/chess/controller/command/StartCommand.java +++ b/src/main/java/chess/controller/command/StartCommand.java @@ -1,11 +1,12 @@ package chess.controller.command; import chess.domain.ChessGame; +import chess.service.ChessGameService; import chess.view.OutputView; public class StartCommand implements Command { @Override - public ExecuteResult execute(ChessGame chessGame, OutputView outputView) { + public ExecuteResult execute(ChessGameService chessGameService, ChessGame chessGame, OutputView outputView) { outputView.printChessBoardMessage(chessGame.getChessBoard()); return new ExecuteResult(true, true); } diff --git a/src/main/java/chess/controller/command/StatusCommand.java b/src/main/java/chess/controller/command/StatusCommand.java index 3a8c39dac23..8b0c6e306d7 100644 --- a/src/main/java/chess/controller/command/StatusCommand.java +++ b/src/main/java/chess/controller/command/StatusCommand.java @@ -3,12 +3,13 @@ import chess.domain.ChessGame; import chess.domain.piece.Score; import chess.domain.piece.Team; +import chess.service.ChessGameService; import chess.view.OutputView; public class StatusCommand implements Command { @Override - public ExecuteResult execute(ChessGame chessGame, OutputView outputView) { + public ExecuteResult execute(ChessGameService chessGameService, ChessGame chessGame, OutputView outputView) { Score whiteScore = chessGame.calculateTeamScore(Team.WHITE); Score blackScore = chessGame.calculateTeamScore(Team.BLACK); Team winner = selectWinnerTeam(whiteScore, blackScore); diff --git a/src/main/java/chess/repository/TurnRepository.java b/src/main/java/chess/repository/TurnRepository.java index a9eaafefa71..dead6d6ca03 100644 --- a/src/main/java/chess/repository/TurnRepository.java +++ b/src/main/java/chess/repository/TurnRepository.java @@ -40,7 +40,7 @@ public Optional findCurrentTurn() { ResultSet resultSet = pstmt.executeQuery()) { if (resultSet.next()) { - String value = resultSet.getString(1); + String value = resultSet.getString(2); return Optional.of(DomainMapper.mapToTurn(value)); } } catch (SQLException e) { From 75d9ebcccb4e2faf9759a68053711df6e0215746 Mon Sep 17 00:00:00 2001 From: libienz Date: Sat, 30 Mar 2024 02:47:15 +0900 Subject: [PATCH 52/80] =?UTF-8?q?feat:=20=ED=85=8C=EC=9D=B4=EB=B8=94=20?= =?UTF-8?q?=EC=8A=A4=ED=82=A4=EB=A7=88=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/sql/piece.sql | 9 +++++++++ src/main/resources/sql/turn.sql | 7 +++++++ 2 files changed, 16 insertions(+) create mode 100644 src/main/resources/sql/piece.sql create mode 100644 src/main/resources/sql/turn.sql diff --git a/src/main/resources/sql/piece.sql b/src/main/resources/sql/piece.sql new file mode 100644 index 00000000000..3255140d923 --- /dev/null +++ b/src/main/resources/sql/piece.sql @@ -0,0 +1,9 @@ +CREATE TABLE pieces +( + id BIGINT NOT NULL AUTO_INCREMENT, + position VARCHAR(4), + team VARCHAR(8), + type VARCHAR(8), + + PRIMARY KEY (id) +); diff --git a/src/main/resources/sql/turn.sql b/src/main/resources/sql/turn.sql new file mode 100644 index 00000000000..439ce8d53b2 --- /dev/null +++ b/src/main/resources/sql/turn.sql @@ -0,0 +1,7 @@ +CREATE TABLE turn +( + id BIGINT NOT NULL AUTO_INCREMENT, + turn VARCHAR(8) NOT NULL, + + PRIMARY KEY (id) +); From f3be8e8b2d5e17b5b406de686aac3896e8e4dca3 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 13:00:30 +0900 Subject: [PATCH 53/80] =?UTF-8?q?refactor:=20=EC=A7=84=ED=96=89=EC=A4=91?= =?UTF-8?q?=EC=9D=B8=20=EA=B2=8C=EC=9E=84=EC=9D=B4=20=EC=9E=88=EB=8A=94=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=EC=99=80=20=EC=95=84=EB=8B=8C=20=EA=B2=BD?= =?UTF-8?q?=EC=9A=B0=EC=9D=98=20=EB=B6=84=EA=B8=B0=EB=A5=BC=20=EC=BB=A8?= =?UTF-8?q?=ED=8A=B8=EB=A1=A4=EB=9F=AC=EA=B0=80=20=EB=AA=A8=EB=A5=B4?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/controller/ChessGameController.java | 6 ++---- .../java/chess/service/ChessGameService.java | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/chess/controller/ChessGameController.java b/src/main/java/chess/controller/ChessGameController.java index 9cf84542ef5..734eeeb2128 100644 --- a/src/main/java/chess/controller/ChessGameController.java +++ b/src/main/java/chess/controller/ChessGameController.java @@ -22,10 +22,8 @@ public ChessGameController(InputView inputView, OutputView outputView, ChessGame public void run() { outputView.printStartMessage(); - ChessGame chessGame = initializeChessGame(); - if (chessGameService.isChessGameInProgress()) { - chessGame = chessGameService.loadChessGame(); - } + ChessGame chessGame = chessGameService.startChessGame(); + ExecuteResult result; do { Command command = inputView.readCommand(); diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index b8b29f4547a..2742f948a69 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -2,6 +2,7 @@ import chess.domain.ChessGame; import chess.domain.board.ChessBoard; +import chess.domain.board.ChessBoardCreator; import chess.domain.piece.Piece; import chess.domain.piece.Team; import chess.domain.position.Position; @@ -21,6 +22,13 @@ public ChessGameService(PieceRepository pieceRepository, TurnRepository turnRepo this.turnRepository = turnRepository; } + public ChessGame startChessGame() { + if (isChessGameInProgress()) { + return loadChessGame(); + } + return createNewChessGame(); + } + public void saveChessGame(ChessGame chessGame) { deleteSavedChessGame(); ChessBoard chessBoard = chessGame.getChessBoard(); @@ -46,10 +54,16 @@ public ChessGame loadChessGame() { return new ChessGame(chessBoard, currentTurn); } - public boolean isChessGameInProgress() { + private boolean isChessGameInProgress() { if (pieceRepository.findPieces().isEmpty()) { return false; } return true; } + + private ChessGame createNewChessGame() { + ChessBoardCreator chessBoardCreator = new ChessBoardCreator(); + ChessBoard chessBoard = chessBoardCreator.create(); + return new ChessGame(chessBoard); + } } From 44b50b64b811ae8a9fec17a11e9cc9c624b6917c Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 13:27:39 +0900 Subject: [PATCH 54/80] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/controller/ChessGameController.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/main/java/chess/controller/ChessGameController.java b/src/main/java/chess/controller/ChessGameController.java index 734eeeb2128..05a86607ee9 100644 --- a/src/main/java/chess/controller/ChessGameController.java +++ b/src/main/java/chess/controller/ChessGameController.java @@ -3,8 +3,6 @@ import chess.controller.command.Command; import chess.controller.command.ExecuteResult; import chess.domain.ChessGame; -import chess.domain.board.ChessBoard; -import chess.domain.board.ChessBoardCreator; import chess.service.ChessGameService; import chess.view.InputView; import chess.view.OutputView; @@ -31,10 +29,4 @@ public void run() { } while (result.isSuccess() && result.isNeedNextCommand() && chessGame.isNotEnd()); } - - private ChessGame initializeChessGame() { - ChessBoardCreator chessBoardCreator = new ChessBoardCreator(); - ChessBoard chessBoard = chessBoardCreator.create(); - return new ChessGame(chessBoard); - } } From 65af47fe8c9b75f4e0254ae84e389001c84092ae Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 14:01:56 +0900 Subject: [PATCH 55/80] =?UTF-8?q?feat:=20=EC=A0=90=EC=88=98=20=ED=98=84?= =?UTF-8?q?=ED=99=A9=20DTO=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/chess/dto/ScoreStatusDto.java | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/chess/dto/ScoreStatusDto.java diff --git a/src/main/java/chess/dto/ScoreStatusDto.java b/src/main/java/chess/dto/ScoreStatusDto.java new file mode 100644 index 00000000000..03f912a3669 --- /dev/null +++ b/src/main/java/chess/dto/ScoreStatusDto.java @@ -0,0 +1,32 @@ +package chess.dto; + +import chess.domain.piece.Score; +import chess.domain.piece.Team; + +public class ScoreStatusDto { + private final double whiteTeamScore; + private final double blackTeamScore; + private final String winnerTeam; + + public ScoreStatusDto(double whiteTeamScore, double blackTeamScore, String winnerTeam) { + this.whiteTeamScore = whiteTeamScore; + this.blackTeamScore = blackTeamScore; + this.winnerTeam = winnerTeam; + } + + public static ScoreStatusDto of(Score whiteTeamScore, Score blackTeamScore, Team winnerTeam) { + return new ScoreStatusDto(whiteTeamScore.getValue(), blackTeamScore.getValue(), winnerTeam.name()); + } + + public double getWhiteTeamScore() { + return whiteTeamScore; + } + + public double getBlackTeamScore() { + return blackTeamScore; + } + + public String getWinnerTeam() { + return winnerTeam; + } +} From 083ca2eb1b5264cb46cfe618e9bbe62db66a96aa Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 14:20:12 +0900 Subject: [PATCH 56/80] =?UTF-8?q?refactor:=20=EC=BB=A8=ED=8A=B8=EB=A1=A4?= =?UTF-8?q?=EB=9F=AC=EA=B0=80=20=EC=84=9C=EB=B9=84=EC=8A=A4=20=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=EC=96=B4=EB=A7=8C=20=EC=9D=98=EC=A1=B4=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/controller/ChessGameController.java | 7 ++-- .../chess/controller/command/Command.java | 3 +- .../chess/controller/command/EndCommand.java | 4 +-- .../chess/controller/command/MoveCommand.java | 6 ++-- .../controller/command/StartCommand.java | 4 ++- .../controller/command/StatusCommand.java | 19 +++-------- .../java/chess/service/ChessGameService.java | 34 ++++++++++++++++--- src/main/java/chess/view/OutputView.java | 21 ++++++------ 8 files changed, 54 insertions(+), 44 deletions(-) diff --git a/src/main/java/chess/controller/ChessGameController.java b/src/main/java/chess/controller/ChessGameController.java index 05a86607ee9..b33dda8dbaa 100644 --- a/src/main/java/chess/controller/ChessGameController.java +++ b/src/main/java/chess/controller/ChessGameController.java @@ -2,7 +2,6 @@ import chess.controller.command.Command; import chess.controller.command.ExecuteResult; -import chess.domain.ChessGame; import chess.service.ChessGameService; import chess.view.InputView; import chess.view.OutputView; @@ -20,13 +19,13 @@ public ChessGameController(InputView inputView, OutputView outputView, ChessGame public void run() { outputView.printStartMessage(); - ChessGame chessGame = chessGameService.startChessGame(); + chessGameService.startChessGame(); ExecuteResult result; do { Command command = inputView.readCommand(); - result = command.execute(chessGameService, chessGame, outputView); + result = command.execute(chessGameService, outputView); } - while (result.isSuccess() && result.isNeedNextCommand() && chessGame.isNotEnd()); + while (result.isSuccess() && result.isNeedNextCommand()); } } diff --git a/src/main/java/chess/controller/command/Command.java b/src/main/java/chess/controller/command/Command.java index 025a2b1a52c..1dc2a4aaaa6 100644 --- a/src/main/java/chess/controller/command/Command.java +++ b/src/main/java/chess/controller/command/Command.java @@ -1,9 +1,8 @@ package chess.controller.command; -import chess.domain.ChessGame; import chess.service.ChessGameService; import chess.view.OutputView; public interface Command { - ExecuteResult execute(ChessGameService chessGameService, ChessGame chessGame, OutputView outputView); + ExecuteResult execute(ChessGameService chessGameService, OutputView outputView); } diff --git a/src/main/java/chess/controller/command/EndCommand.java b/src/main/java/chess/controller/command/EndCommand.java index bc10f414f3b..f23eacb5330 100644 --- a/src/main/java/chess/controller/command/EndCommand.java +++ b/src/main/java/chess/controller/command/EndCommand.java @@ -1,13 +1,11 @@ package chess.controller.command; -import chess.domain.ChessGame; import chess.service.ChessGameService; import chess.view.OutputView; public class EndCommand implements Command { @Override - public ExecuteResult execute(ChessGameService chessGameService, ChessGame chessGame, OutputView outputView) { - chessGameService.saveChessGame(chessGame); + public ExecuteResult execute(ChessGameService chessGameService, OutputView outputView) { return new ExecuteResult(true, false); } } diff --git a/src/main/java/chess/controller/command/MoveCommand.java b/src/main/java/chess/controller/command/MoveCommand.java index 70317cb308d..bcc55e7ae00 100644 --- a/src/main/java/chess/controller/command/MoveCommand.java +++ b/src/main/java/chess/controller/command/MoveCommand.java @@ -15,9 +15,9 @@ public MoveCommand(Position start, Position destination) { } @Override - public ExecuteResult execute(ChessGameService chessGameService, ChessGame chessGame, OutputView outputView) { - chessGame.move(start, destination); - chessGameService.saveChessGame(chessGame); + public ExecuteResult execute(ChessGameService chessGameService, OutputView outputView) { + chessGameService.movePiece(start, destination); + ChessGame chessGame = chessGameService.loadChessGame(); outputView.printChessBoardMessage(chessGame.getChessBoard()); return new ExecuteResult(true, true); } diff --git a/src/main/java/chess/controller/command/StartCommand.java b/src/main/java/chess/controller/command/StartCommand.java index eef9f33a507..d6fd0a5fcf6 100644 --- a/src/main/java/chess/controller/command/StartCommand.java +++ b/src/main/java/chess/controller/command/StartCommand.java @@ -6,7 +6,9 @@ public class StartCommand implements Command { @Override - public ExecuteResult execute(ChessGameService chessGameService, ChessGame chessGame, OutputView outputView) { + public ExecuteResult execute(ChessGameService chessGameService, OutputView outputView) { + chessGameService.startChessGame(); + ChessGame chessGame = chessGameService.loadChessGame(); outputView.printChessBoardMessage(chessGame.getChessBoard()); return new ExecuteResult(true, true); } diff --git a/src/main/java/chess/controller/command/StatusCommand.java b/src/main/java/chess/controller/command/StatusCommand.java index 8b0c6e306d7..271ac782209 100644 --- a/src/main/java/chess/controller/command/StatusCommand.java +++ b/src/main/java/chess/controller/command/StatusCommand.java @@ -1,26 +1,15 @@ package chess.controller.command; -import chess.domain.ChessGame; -import chess.domain.piece.Score; -import chess.domain.piece.Team; +import chess.dto.ScoreStatusDto; import chess.service.ChessGameService; import chess.view.OutputView; public class StatusCommand implements Command { @Override - public ExecuteResult execute(ChessGameService chessGameService, ChessGame chessGame, OutputView outputView) { - Score whiteScore = chessGame.calculateTeamScore(Team.WHITE); - Score blackScore = chessGame.calculateTeamScore(Team.BLACK); - Team winner = selectWinnerTeam(whiteScore, blackScore); - outputView.printStatusMessage(whiteScore, blackScore, winner); + public ExecuteResult execute(ChessGameService chessGameService, OutputView outputView) { + ScoreStatusDto scoreStatusDto = chessGameService.calculateScoreStatus(); + outputView.printStatusMessage(scoreStatusDto); return new ExecuteResult(true, true); } - - private Team selectWinnerTeam(Score whiteScore, Score blackScore) { - if (whiteScore.isAbove(blackScore)) { - return Team.WHITE; - } - return Team.BLACK; - } } diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index 2742f948a69..e78819d65c2 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -4,9 +4,11 @@ import chess.domain.board.ChessBoard; import chess.domain.board.ChessBoardCreator; import chess.domain.piece.Piece; +import chess.domain.piece.Score; import chess.domain.piece.Team; import chess.domain.position.Position; import chess.dto.PiecePlacementDto; +import chess.dto.ScoreStatusDto; import chess.repository.PieceRepository; import chess.repository.TurnRepository; import chess.repository.mapper.DomainMapper; @@ -22,11 +24,11 @@ public ChessGameService(PieceRepository pieceRepository, TurnRepository turnRepo this.turnRepository = turnRepository; } - public ChessGame startChessGame() { + public void startChessGame() { if (isChessGameInProgress()) { - return loadChessGame(); + return; } - return createNewChessGame(); + createNewChessGame(); } public void saveChessGame(ChessGame chessGame) { @@ -41,6 +43,20 @@ public void saveChessGame(ChessGame chessGame) { turnRepository.saveTurn(chessGame.getTurn()); } + public void movePiece(Position start, Position destination) { + ChessGame chessGame = loadChessGame(); + chessGame.move(start, destination); + saveChessGame(chessGame); + } + + public ScoreStatusDto calculateScoreStatus() { + ChessGame chessGame = loadChessGame(); + Score blackScore = chessGame.calculateTeamScore(Team.BLACK); + Score whiteScore = chessGame.calculateTeamScore(Team.WHITE); + Team winnerTeam = judgeWinnerTeam(whiteScore, blackScore); + return ScoreStatusDto.of(whiteScore, blackScore, winnerTeam); + } + private void deleteSavedChessGame() { pieceRepository.deleteAll(); turnRepository.deleteAll(); @@ -61,9 +77,17 @@ private boolean isChessGameInProgress() { return true; } - private ChessGame createNewChessGame() { + //체스 게임 도메인으로 응집 + private void createNewChessGame() { ChessBoardCreator chessBoardCreator = new ChessBoardCreator(); ChessBoard chessBoard = chessBoardCreator.create(); - return new ChessGame(chessBoard); + saveChessGame(new ChessGame(chessBoard)); + } + + private Team judgeWinnerTeam(Score whiteTeamScore, Score blackTeamScore) { + if (whiteTeamScore.isAbove(blackTeamScore)) { + return Team.WHITE; + } + return Team.BLACK; } } diff --git a/src/main/java/chess/view/OutputView.java b/src/main/java/chess/view/OutputView.java index 7eca0d44c5f..58116956d6d 100644 --- a/src/main/java/chess/view/OutputView.java +++ b/src/main/java/chess/view/OutputView.java @@ -7,11 +7,10 @@ import chess.domain.board.ChessBoard; import chess.domain.piece.Piece; -import chess.domain.piece.Score; -import chess.domain.piece.Team; import chess.domain.position.File; import chess.domain.position.Position; import chess.domain.position.Rank; +import chess.dto.ScoreStatusDto; import java.util.Arrays; import java.util.StringJoiner; import java.util.stream.Collectors; @@ -28,8 +27,8 @@ public void printChessBoardMessage(ChessBoard chessBoard) { System.out.println(resolveChessBoardMessage(chessBoard)); } - public void printStatusMessage(Score whiteScore, Score blackScore, Team winner) { - System.out.println(resolveStatusMessage(whiteScore, blackScore, winner)); + public void printStatusMessage(ScoreStatusDto scoreStatusDto) { + System.out.println(resolveStatusMessage(scoreStatusDto)); } private String resolveStartMessage() { @@ -63,19 +62,19 @@ private String resolveSquareMessage(ChessBoard chessBoard, Position position) { return PieceMessage.messageOf(foundPiece); } - private String resolveStatusMessage(Score whiteScore, Score blackScore, Team winner) { + private String resolveStatusMessage(ScoreStatusDto scoreStatusDto) { return new StringJoiner(LINE_SEPARATOR) - .add(resolveTeamStatusMessage(Team.WHITE, whiteScore)) - .add(resolveTeamStatusMessage(Team.BLACK, blackScore)) - .add(resolveStatusWinnerMessage(winner)) + .add(resolveTeamStatusMessage("백", scoreStatusDto.getWhiteTeamScore())) + .add(resolveTeamStatusMessage("흑", scoreStatusDto.getBlackTeamScore())) + .add(resolveStatusWinnerMessage(scoreStatusDto.getWinnerTeam())) .toString(); } - private String resolveTeamStatusMessage(Team team, Score score) { - return String.format("%s팀: %.1f점", team, score.getValue()); + private String resolveTeamStatusMessage(String team, double score) { + return String.format("%s팀: %.1f점", team, score); } - private String resolveStatusWinnerMessage(Team winner) { + private String resolveStatusWinnerMessage(String winner) { return String.format("현 시점 기물 점수 승부 %s 승리", winner); } } From 4e19ad84ebeae45c689776aab1f55c9f938f2aab Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 14:22:20 +0900 Subject: [PATCH 57/80] =?UTF-8?q?refactor:=20=EA=B2=8C=EC=9E=84=20?= =?UTF-8?q?=EC=A2=85=EB=A3=8C=20=EC=97=AC=EB=B6=80=EB=A5=BC=20=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=EC=97=90=20=EB=AC=BB=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/controller/ChessGameController.java | 2 +- src/main/java/chess/service/ChessGameService.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/chess/controller/ChessGameController.java b/src/main/java/chess/controller/ChessGameController.java index b33dda8dbaa..84fd4254ab0 100644 --- a/src/main/java/chess/controller/ChessGameController.java +++ b/src/main/java/chess/controller/ChessGameController.java @@ -26,6 +26,6 @@ public void run() { Command command = inputView.readCommand(); result = command.execute(chessGameService, outputView); } - while (result.isSuccess() && result.isNeedNextCommand()); + while (result.isSuccess() && result.isNeedNextCommand() && chessGameService.isChessGameNotEnd()); } } diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index e78819d65c2..aef90ebad11 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -70,6 +70,11 @@ public ChessGame loadChessGame() { return new ChessGame(chessBoard, currentTurn); } + public boolean isChessGameNotEnd() { + ChessGame chessGame = loadChessGame(); + return chessGame.isNotEnd(); + } + private boolean isChessGameInProgress() { if (pieceRepository.findPieces().isEmpty()) { return false; From 3cfa82050f888afa1005ee769420e450baa389f6 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 14:25:06 +0900 Subject: [PATCH 58/80] =?UTF-8?q?feat:=20=EC=83=88=EB=A1=9C=EC=9A=B4=20?= =?UTF-8?q?=EC=B2=B4=EC=8A=A4=20=EA=B2=8C=EC=9E=84=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=B2=B4=EC=8A=A4=20=EA=B2=8C=EC=9E=84=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=20=EB=A1=9C=EC=A7=81=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=9D=91=EC=A7=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/ChessGame.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/chess/domain/ChessGame.java b/src/main/java/chess/domain/ChessGame.java index 28df13f70c5..2cf6345e224 100644 --- a/src/main/java/chess/domain/ChessGame.java +++ b/src/main/java/chess/domain/ChessGame.java @@ -1,6 +1,7 @@ package chess.domain; import chess.domain.board.ChessBoard; +import chess.domain.board.ChessBoardCreator; import chess.domain.piece.Piece; import chess.domain.piece.Score; import chess.domain.piece.Team; @@ -22,6 +23,12 @@ public ChessGame(ChessBoard chessBoard, Team turn) { this.turn = turn; } + public static ChessGame createNewChessGame() { + ChessBoardCreator chessBoardCreator = new ChessBoardCreator(); + ChessBoard chessBoard = chessBoardCreator.create(); + return new ChessGame(chessBoard); + } + public void move(Position start, Position destiantion) { validateTurn(chessBoard.findPieceByPosition(start)); chessBoard.move(start, destiantion); From 2db4eb57086aa086d52ef3cd113f7f2abdb348b1 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 14:31:26 +0900 Subject: [PATCH 59/80] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EB=B0=B0=EC=B9=98=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/chess/service/ChessGameService.java | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index aef90ebad11..b2123ee683b 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -2,7 +2,6 @@ import chess.domain.ChessGame; import chess.domain.board.ChessBoard; -import chess.domain.board.ChessBoardCreator; import chess.domain.piece.Piece; import chess.domain.piece.Score; import chess.domain.piece.Team; @@ -31,18 +30,6 @@ public void startChessGame() { createNewChessGame(); } - public void saveChessGame(ChessGame chessGame) { - deleteSavedChessGame(); - ChessBoard chessBoard = chessGame.getChessBoard(); - Map board = chessBoard.getBoard(); - - for (Position position : board.keySet()) { - Piece piece = board.get(position); - pieceRepository.savePiece(PiecePlacementDto.of(piece, position)); - } - turnRepository.saveTurn(chessGame.getTurn()); - } - public void movePiece(Position start, Position destination) { ChessGame chessGame = loadChessGame(); chessGame.move(start, destination); @@ -57,11 +44,6 @@ public ScoreStatusDto calculateScoreStatus() { return ScoreStatusDto.of(whiteScore, blackScore, winnerTeam); } - private void deleteSavedChessGame() { - pieceRepository.deleteAll(); - turnRepository.deleteAll(); - } - public ChessGame loadChessGame() { List pieces = pieceRepository.findPieces().get(); Team currentTurn = turnRepository.findCurrentTurn().get(); @@ -75,6 +57,28 @@ public boolean isChessGameNotEnd() { return chessGame.isNotEnd(); } + private void createNewChessGame() { + ChessGame newChessGame = ChessGame.createNewChessGame(); + saveChessGame(newChessGame); + } + + private void saveChessGame(ChessGame chessGame) { + deleteSavedChessGame(); + ChessBoard chessBoard = chessGame.getChessBoard(); + Map board = chessBoard.getBoard(); + + for (Position position : board.keySet()) { + Piece piece = board.get(position); + pieceRepository.savePiece(PiecePlacementDto.of(piece, position)); + } + turnRepository.saveTurn(chessGame.getTurn()); + } + + private void deleteSavedChessGame() { + pieceRepository.deleteAll(); + turnRepository.deleteAll(); + } + private boolean isChessGameInProgress() { if (pieceRepository.findPieces().isEmpty()) { return false; @@ -82,13 +86,6 @@ private boolean isChessGameInProgress() { return true; } - //체스 게임 도메인으로 응집 - private void createNewChessGame() { - ChessBoardCreator chessBoardCreator = new ChessBoardCreator(); - ChessBoard chessBoard = chessBoardCreator.create(); - saveChessGame(new ChessGame(chessBoard)); - } - private Team judgeWinnerTeam(Score whiteTeamScore, Score blackTeamScore) { if (whiteTeamScore.isAbove(blackTeamScore)) { return Team.WHITE; From c86106315eb688e773aa9f945e7d3e5a7a6fd677 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 14:42:13 +0900 Subject: [PATCH 60/80] =?UTF-8?q?refactor:=20=EC=A1=B0=EA=B1=B4=EB=AC=B8?= =?UTF-8?q?=20simplify=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/service/ChessGameService.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index b2123ee683b..044f42d776a 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -80,10 +80,7 @@ private void deleteSavedChessGame() { } private boolean isChessGameInProgress() { - if (pieceRepository.findPieces().isEmpty()) { - return false; - } - return true; + return pieceRepository.findPieces().isPresent(); } private Team judgeWinnerTeam(Score whiteTeamScore, Score blackTeamScore) { From d9dfcb22c41b987bfcad73b1c746301eaded803f Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 14:56:54 +0900 Subject: [PATCH 61/80] =?UTF-8?q?feat:=20File=EC=9D=98=20=EC=97=B4=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EA=B2=8C=ED=84=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/position/File.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/chess/domain/position/File.java b/src/main/java/chess/domain/position/File.java index 9f009e64686..d1e78aeae64 100644 --- a/src/main/java/chess/domain/position/File.java +++ b/src/main/java/chess/domain/position/File.java @@ -41,4 +41,8 @@ public boolean isFurtherRightThan(File other) { public File move(int weight) { return from(columnNumber + weight); } + + public int getColumnNumber() { + return columnNumber; + } } From 3fe6cf208011f473c7f752918c027a406f7f9848 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 15:35:38 +0900 Subject: [PATCH 62/80] =?UTF-8?q?feat:=20BoardSnapShotDto=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/chess/dto/BoardSnapShotDto.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/main/java/chess/dto/BoardSnapShotDto.java diff --git a/src/main/java/chess/dto/BoardSnapShotDto.java b/src/main/java/chess/dto/BoardSnapShotDto.java new file mode 100644 index 00000000000..e9484c001d0 --- /dev/null +++ b/src/main/java/chess/dto/BoardSnapShotDto.java @@ -0,0 +1,40 @@ +package chess.dto; + +import chess.domain.board.ChessBoard; +import chess.domain.position.File; +import chess.domain.position.Position; +import chess.domain.position.Rank; +import chess.view.PieceMessage; + +public class BoardSnapShotDto { + private final String[][] boardSnapShot; + + public BoardSnapShotDto(String[][] boardSnapShot) { + this.boardSnapShot = boardSnapShot; + } + + public static BoardSnapShotDto from(ChessBoard chessBoard) { + String[][] boardSnapShot = new String[Rank.values().length][File.values().length]; + + for (Rank rank : Rank.values()) { + for (File file : File.values()) { + Position position = new Position(file, rank); + int rankNumber = position.getRank().getRankNumber(); + int columnNumber = position.getFile().getColumnNumber(); + boardSnapShot[rankNumber][columnNumber] = squareSnapshot(chessBoard, position); + } + } + return new BoardSnapShotDto(boardSnapShot); + } + + private static String squareSnapshot(ChessBoard chessBoard, Position position) { + if (chessBoard.positionIsEmpty(position)) { + return "."; + } + return PieceMessage.messageOf(chessBoard.findPieceByPosition(position)); + } + + public String[][] getBoardSnapShot() { + return boardSnapShot; + } +} From 1e26dc3b449223d6e6791ca3c419f1792829926b Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 15:51:34 +0900 Subject: [PATCH 63/80] =?UTF-8?q?feat:=20=EB=B3=B4=EB=93=9C=20=EC=83=81?= =?UTF-8?q?=ED=83=9C=20=EC=8A=A4=EB=83=85=EC=83=B7=20DTO=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/chess/dto/BoardSnapShotDto.java | 30 +++++++++++-------- src/main/java/chess/dto/RankSnapShotDto.java | 15 ++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 src/main/java/chess/dto/RankSnapShotDto.java diff --git a/src/main/java/chess/dto/BoardSnapShotDto.java b/src/main/java/chess/dto/BoardSnapShotDto.java index e9484c001d0..a17bf81190b 100644 --- a/src/main/java/chess/dto/BoardSnapShotDto.java +++ b/src/main/java/chess/dto/BoardSnapShotDto.java @@ -5,26 +5,32 @@ import chess.domain.position.Position; import chess.domain.position.Rank; import chess.view.PieceMessage; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; public class BoardSnapShotDto { - private final String[][] boardSnapShot; + private final List boardSnapShot; - public BoardSnapShotDto(String[][] boardSnapShot) { + public BoardSnapShotDto(List boardSnapShot) { this.boardSnapShot = boardSnapShot; } public static BoardSnapShotDto from(ChessBoard chessBoard) { - String[][] boardSnapShot = new String[Rank.values().length][File.values().length]; + List boardSnapShot = Arrays.stream(Rank.values()) + .map(rank -> rankSnapShot(chessBoard, rank)) + .collect(Collectors.toList()); + return new BoardSnapShotDto(boardSnapShot); + } - for (Rank rank : Rank.values()) { - for (File file : File.values()) { - Position position = new Position(file, rank); - int rankNumber = position.getRank().getRankNumber(); - int columnNumber = position.getFile().getColumnNumber(); - boardSnapShot[rankNumber][columnNumber] = squareSnapshot(chessBoard, position); - } + private static RankSnapShotDto rankSnapShot(ChessBoard chessBoard, Rank rank) { + List rankSnapShot = new ArrayList<>(); + for (File file : File.values()) { + Position position = new Position(file, rank); + rankSnapShot.add(squareSnapshot(chessBoard, position)); } - return new BoardSnapShotDto(boardSnapShot); + return new RankSnapShotDto(rankSnapShot); } private static String squareSnapshot(ChessBoard chessBoard, Position position) { @@ -34,7 +40,7 @@ private static String squareSnapshot(ChessBoard chessBoard, Position position) { return PieceMessage.messageOf(chessBoard.findPieceByPosition(position)); } - public String[][] getBoardSnapShot() { + public List getBoardSnapShot() { return boardSnapShot; } } diff --git a/src/main/java/chess/dto/RankSnapShotDto.java b/src/main/java/chess/dto/RankSnapShotDto.java new file mode 100644 index 00000000000..85b3084d705 --- /dev/null +++ b/src/main/java/chess/dto/RankSnapShotDto.java @@ -0,0 +1,15 @@ +package chess.dto; + +import java.util.List; + +public class RankSnapShotDto { + private final List rank; + + public RankSnapShotDto(List rank) { + this.rank = rank; + } + + public List getRank() { + return rank; + } +} From f960abaa855b462b1ed6d1664382d0f5346d539f Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 15:52:00 +0900 Subject: [PATCH 64/80] =?UTF-8?q?feat:=20=EC=8A=A4=EB=83=85=EC=83=B7=20?= =?UTF-8?q?=EA=B8=B0=EB=B0=98=20=EC=B2=B4=EC=8A=A4=20=EB=B3=B4=EB=93=9C=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/view/OutputView.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/chess/view/OutputView.java b/src/main/java/chess/view/OutputView.java index 58116956d6d..a229106b547 100644 --- a/src/main/java/chess/view/OutputView.java +++ b/src/main/java/chess/view/OutputView.java @@ -10,8 +10,11 @@ import chess.domain.position.File; import chess.domain.position.Position; import chess.domain.position.Rank; +import chess.dto.BoardSnapShotDto; +import chess.dto.RankSnapShotDto; import chess.dto.ScoreStatusDto; import java.util.Arrays; +import java.util.List; import java.util.StringJoiner; import java.util.stream.Collectors; @@ -27,6 +30,22 @@ public void printChessBoardMessage(ChessBoard chessBoard) { System.out.println(resolveChessBoardMessage(chessBoard)); } + public void printChessBoardMessage(BoardSnapShotDto boardSnapshotDto) { + System.out.println(resolveBoardSnapshotMessage(boardSnapshotDto)); + } + + private String resolveBoardSnapshotMessage(BoardSnapShotDto boardSnapshot) { + List boardSnapShot = boardSnapshot.getBoardSnapShot(); + return boardSnapShot.stream() + .map(this::resolveRankSnapshotMessage) + .collect(Collectors.joining(LINE_SEPARATOR)); + } + + private String resolveRankSnapshotMessage(RankSnapShotDto rankSnapShotDto) { + List rank = rankSnapShotDto.getRank(); + return String.join("", rank); + } + public void printStatusMessage(ScoreStatusDto scoreStatusDto) { System.out.println(resolveStatusMessage(scoreStatusDto)); } From de67de51b450a761440e6e7309f3d1cbeab9a0ca Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 15:57:29 +0900 Subject: [PATCH 65/80] =?UTF-8?q?refactor:=20=EC=84=9C=EB=B9=84=EC=8A=A4?= =?UTF-8?q?=20=EB=A0=88=EC=9D=B4=EC=96=B4=EA=B0=80=20DTO=EB=A5=BC=20?= =?UTF-8?q?=EB=B0=98=ED=99=98=ED=95=98=EC=97=AC=20=EA=B2=B0=EA=B3=BC?= =?UTF-8?q?=EB=A5=BC=20=EC=B0=B8=EC=A1=B0=ED=95=A0=20=EC=88=98=20=EC=9E=88?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/controller/command/MoveCommand.java | 7 +++---- src/main/java/chess/controller/command/StartCommand.java | 7 +++---- src/main/java/chess/service/ChessGameService.java | 9 ++++++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/chess/controller/command/MoveCommand.java b/src/main/java/chess/controller/command/MoveCommand.java index bcc55e7ae00..520c5d9ceb9 100644 --- a/src/main/java/chess/controller/command/MoveCommand.java +++ b/src/main/java/chess/controller/command/MoveCommand.java @@ -1,7 +1,7 @@ package chess.controller.command; -import chess.domain.ChessGame; import chess.domain.position.Position; +import chess.dto.BoardSnapShotDto; import chess.service.ChessGameService; import chess.view.OutputView; @@ -16,9 +16,8 @@ public MoveCommand(Position start, Position destination) { @Override public ExecuteResult execute(ChessGameService chessGameService, OutputView outputView) { - chessGameService.movePiece(start, destination); - ChessGame chessGame = chessGameService.loadChessGame(); - outputView.printChessBoardMessage(chessGame.getChessBoard()); + BoardSnapShotDto boardSnapShotDto = chessGameService.movePiece(start, destination); + outputView.printChessBoardMessage(boardSnapShotDto); return new ExecuteResult(true, true); } } diff --git a/src/main/java/chess/controller/command/StartCommand.java b/src/main/java/chess/controller/command/StartCommand.java index d6fd0a5fcf6..5ecba7c6b48 100644 --- a/src/main/java/chess/controller/command/StartCommand.java +++ b/src/main/java/chess/controller/command/StartCommand.java @@ -1,15 +1,14 @@ package chess.controller.command; -import chess.domain.ChessGame; +import chess.dto.BoardSnapShotDto; import chess.service.ChessGameService; import chess.view.OutputView; public class StartCommand implements Command { @Override public ExecuteResult execute(ChessGameService chessGameService, OutputView outputView) { - chessGameService.startChessGame(); - ChessGame chessGame = chessGameService.loadChessGame(); - outputView.printChessBoardMessage(chessGame.getChessBoard()); + BoardSnapShotDto boardSnapShotDto = chessGameService.startChessGame(); + outputView.printChessBoardMessage(boardSnapShotDto); return new ExecuteResult(true, true); } } diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index 044f42d776a..8e7d3ceaaa1 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -6,6 +6,7 @@ import chess.domain.piece.Score; import chess.domain.piece.Team; import chess.domain.position.Position; +import chess.dto.BoardSnapShotDto; import chess.dto.PiecePlacementDto; import chess.dto.ScoreStatusDto; import chess.repository.PieceRepository; @@ -23,17 +24,19 @@ public ChessGameService(PieceRepository pieceRepository, TurnRepository turnRepo this.turnRepository = turnRepository; } - public void startChessGame() { + public BoardSnapShotDto startChessGame() { if (isChessGameInProgress()) { - return; + return BoardSnapShotDto.from(loadChessGame().getChessBoard()); } createNewChessGame(); + return BoardSnapShotDto.from(loadChessGame().getChessBoard()); } - public void movePiece(Position start, Position destination) { + public BoardSnapShotDto movePiece(Position start, Position destination) { ChessGame chessGame = loadChessGame(); chessGame.move(start, destination); saveChessGame(chessGame); + return BoardSnapShotDto.from(chessGame.getChessBoard()); } public ScoreStatusDto calculateScoreStatus() { From 5462f0c6962af13ad91abf5f24761213b3ac9a60 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 15:58:19 +0900 Subject: [PATCH 66/80] =?UTF-8?q?refactor:=20=EB=B7=B0=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=20=EC=82=AC=EC=9A=A9=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=A0=9C=EA=B1=B0=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0=20=EB=B0=8F=20=EB=B0=B0=EC=B9=98=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/view/OutputView.java | 45 ++++-------------------- 1 file changed, 7 insertions(+), 38 deletions(-) diff --git a/src/main/java/chess/view/OutputView.java b/src/main/java/chess/view/OutputView.java index a229106b547..001c6a13847 100644 --- a/src/main/java/chess/view/OutputView.java +++ b/src/main/java/chess/view/OutputView.java @@ -5,15 +5,9 @@ import static chess.view.CommandParser.START; import static chess.view.CommandParser.STATUS; -import chess.domain.board.ChessBoard; -import chess.domain.piece.Piece; -import chess.domain.position.File; -import chess.domain.position.Position; -import chess.domain.position.Rank; import chess.dto.BoardSnapShotDto; import chess.dto.RankSnapShotDto; import chess.dto.ScoreStatusDto; -import java.util.Arrays; import java.util.List; import java.util.StringJoiner; import java.util.stream.Collectors; @@ -26,26 +20,10 @@ public void printStartMessage() { System.out.println(resolveStartMessage()); } - public void printChessBoardMessage(ChessBoard chessBoard) { - System.out.println(resolveChessBoardMessage(chessBoard)); - } - public void printChessBoardMessage(BoardSnapShotDto boardSnapshotDto) { System.out.println(resolveBoardSnapshotMessage(boardSnapshotDto)); } - private String resolveBoardSnapshotMessage(BoardSnapShotDto boardSnapshot) { - List boardSnapShot = boardSnapshot.getBoardSnapShot(); - return boardSnapShot.stream() - .map(this::resolveRankSnapshotMessage) - .collect(Collectors.joining(LINE_SEPARATOR)); - } - - private String resolveRankSnapshotMessage(RankSnapShotDto rankSnapShotDto) { - List rank = rankSnapShotDto.getRank(); - return String.join("", rank); - } - public void printStatusMessage(ScoreStatusDto scoreStatusDto) { System.out.println(resolveStatusMessage(scoreStatusDto)); } @@ -60,25 +38,16 @@ private String resolveStartMessage() { .toString(); } - private String resolveChessBoardMessage(ChessBoard chessBoard) { - return Arrays.stream(Rank.values()) - .map(rank -> resolveRankMessage(chessBoard, rank)) + private String resolveBoardSnapshotMessage(BoardSnapShotDto boardSnapshot) { + List boardSnapShot = boardSnapshot.getBoardSnapShot(); + return boardSnapShot.stream() + .map(this::resolveRankSnapshotMessage) .collect(Collectors.joining(LINE_SEPARATOR)); } - private String resolveRankMessage(ChessBoard chessBoard, Rank rank) { - return Arrays.stream(File.values()) - .map(file -> new Position(file, rank)) - .map(position -> resolveSquareMessage(chessBoard, position)) - .collect(Collectors.joining()); - } - - private String resolveSquareMessage(ChessBoard chessBoard, Position position) { - if (chessBoard.positionIsEmpty(position)) { - return POSITION_EMPTY_MESSAGE; - } - Piece foundPiece = chessBoard.findPieceByPosition(position); - return PieceMessage.messageOf(foundPiece); + private String resolveRankSnapshotMessage(RankSnapShotDto rankSnapShotDto) { + List rank = rankSnapShotDto.getRank(); + return String.join("", rank); } private String resolveStatusMessage(ScoreStatusDto scoreStatusDto) { From 661203ab985bb07d98def8c8ca8a2d38db974591 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 15:59:22 +0900 Subject: [PATCH 67/80] =?UTF-8?q?rename:=20DTO=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=9D=B4=EB=A6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/controller/command/MoveCommand.java | 6 +++--- .../chess/controller/command/StartCommand.java | 6 +++--- .../{BoardSnapShotDto.java => BoardDto.java} | 18 +++++++++--------- .../dto/{RankSnapShotDto.java => RankDto.java} | 4 ++-- .../java/chess/service/ChessGameService.java | 12 ++++++------ src/main/java/chess/view/OutputView.java | 14 +++++++------- 6 files changed, 30 insertions(+), 30 deletions(-) rename src/main/java/chess/dto/{BoardSnapShotDto.java => BoardDto.java} (65%) rename src/main/java/chess/dto/{RankSnapShotDto.java => RankDto.java} (69%) diff --git a/src/main/java/chess/controller/command/MoveCommand.java b/src/main/java/chess/controller/command/MoveCommand.java index 520c5d9ceb9..b3dbae31b3f 100644 --- a/src/main/java/chess/controller/command/MoveCommand.java +++ b/src/main/java/chess/controller/command/MoveCommand.java @@ -1,7 +1,7 @@ package chess.controller.command; import chess.domain.position.Position; -import chess.dto.BoardSnapShotDto; +import chess.dto.BoardDto; import chess.service.ChessGameService; import chess.view.OutputView; @@ -16,8 +16,8 @@ public MoveCommand(Position start, Position destination) { @Override public ExecuteResult execute(ChessGameService chessGameService, OutputView outputView) { - BoardSnapShotDto boardSnapShotDto = chessGameService.movePiece(start, destination); - outputView.printChessBoardMessage(boardSnapShotDto); + BoardDto boardDto = chessGameService.movePiece(start, destination); + outputView.printChessBoardMessage(boardDto); return new ExecuteResult(true, true); } } diff --git a/src/main/java/chess/controller/command/StartCommand.java b/src/main/java/chess/controller/command/StartCommand.java index 5ecba7c6b48..270a7610ed0 100644 --- a/src/main/java/chess/controller/command/StartCommand.java +++ b/src/main/java/chess/controller/command/StartCommand.java @@ -1,14 +1,14 @@ package chess.controller.command; -import chess.dto.BoardSnapShotDto; +import chess.dto.BoardDto; import chess.service.ChessGameService; import chess.view.OutputView; public class StartCommand implements Command { @Override public ExecuteResult execute(ChessGameService chessGameService, OutputView outputView) { - BoardSnapShotDto boardSnapShotDto = chessGameService.startChessGame(); - outputView.printChessBoardMessage(boardSnapShotDto); + BoardDto boardDto = chessGameService.startChessGame(); + outputView.printChessBoardMessage(boardDto); return new ExecuteResult(true, true); } } diff --git a/src/main/java/chess/dto/BoardSnapShotDto.java b/src/main/java/chess/dto/BoardDto.java similarity index 65% rename from src/main/java/chess/dto/BoardSnapShotDto.java rename to src/main/java/chess/dto/BoardDto.java index a17bf81190b..304ef03d1c9 100644 --- a/src/main/java/chess/dto/BoardSnapShotDto.java +++ b/src/main/java/chess/dto/BoardDto.java @@ -10,27 +10,27 @@ import java.util.List; import java.util.stream.Collectors; -public class BoardSnapShotDto { - private final List boardSnapShot; +public class BoardDto { + private final List boardSnapShot; - public BoardSnapShotDto(List boardSnapShot) { + public BoardDto(List boardSnapShot) { this.boardSnapShot = boardSnapShot; } - public static BoardSnapShotDto from(ChessBoard chessBoard) { - List boardSnapShot = Arrays.stream(Rank.values()) + public static BoardDto from(ChessBoard chessBoard) { + List boardSnapShot = Arrays.stream(Rank.values()) .map(rank -> rankSnapShot(chessBoard, rank)) .collect(Collectors.toList()); - return new BoardSnapShotDto(boardSnapShot); + return new BoardDto(boardSnapShot); } - private static RankSnapShotDto rankSnapShot(ChessBoard chessBoard, Rank rank) { + private static RankDto rankSnapShot(ChessBoard chessBoard, Rank rank) { List rankSnapShot = new ArrayList<>(); for (File file : File.values()) { Position position = new Position(file, rank); rankSnapShot.add(squareSnapshot(chessBoard, position)); } - return new RankSnapShotDto(rankSnapShot); + return new RankDto(rankSnapShot); } private static String squareSnapshot(ChessBoard chessBoard, Position position) { @@ -40,7 +40,7 @@ private static String squareSnapshot(ChessBoard chessBoard, Position position) { return PieceMessage.messageOf(chessBoard.findPieceByPosition(position)); } - public List getBoardSnapShot() { + public List getBoardSnapShot() { return boardSnapShot; } } diff --git a/src/main/java/chess/dto/RankSnapShotDto.java b/src/main/java/chess/dto/RankDto.java similarity index 69% rename from src/main/java/chess/dto/RankSnapShotDto.java rename to src/main/java/chess/dto/RankDto.java index 85b3084d705..82a549cfde7 100644 --- a/src/main/java/chess/dto/RankSnapShotDto.java +++ b/src/main/java/chess/dto/RankDto.java @@ -2,10 +2,10 @@ import java.util.List; -public class RankSnapShotDto { +public class RankDto { private final List rank; - public RankSnapShotDto(List rank) { + public RankDto(List rank) { this.rank = rank; } diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index 8e7d3ceaaa1..f653bc9ce34 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -6,7 +6,7 @@ import chess.domain.piece.Score; import chess.domain.piece.Team; import chess.domain.position.Position; -import chess.dto.BoardSnapShotDto; +import chess.dto.BoardDto; import chess.dto.PiecePlacementDto; import chess.dto.ScoreStatusDto; import chess.repository.PieceRepository; @@ -24,19 +24,19 @@ public ChessGameService(PieceRepository pieceRepository, TurnRepository turnRepo this.turnRepository = turnRepository; } - public BoardSnapShotDto startChessGame() { + public BoardDto startChessGame() { if (isChessGameInProgress()) { - return BoardSnapShotDto.from(loadChessGame().getChessBoard()); + return BoardDto.from(loadChessGame().getChessBoard()); } createNewChessGame(); - return BoardSnapShotDto.from(loadChessGame().getChessBoard()); + return BoardDto.from(loadChessGame().getChessBoard()); } - public BoardSnapShotDto movePiece(Position start, Position destination) { + public BoardDto movePiece(Position start, Position destination) { ChessGame chessGame = loadChessGame(); chessGame.move(start, destination); saveChessGame(chessGame); - return BoardSnapShotDto.from(chessGame.getChessBoard()); + return BoardDto.from(chessGame.getChessBoard()); } public ScoreStatusDto calculateScoreStatus() { diff --git a/src/main/java/chess/view/OutputView.java b/src/main/java/chess/view/OutputView.java index 001c6a13847..c4891783ef6 100644 --- a/src/main/java/chess/view/OutputView.java +++ b/src/main/java/chess/view/OutputView.java @@ -5,8 +5,8 @@ import static chess.view.CommandParser.START; import static chess.view.CommandParser.STATUS; -import chess.dto.BoardSnapShotDto; -import chess.dto.RankSnapShotDto; +import chess.dto.BoardDto; +import chess.dto.RankDto; import chess.dto.ScoreStatusDto; import java.util.List; import java.util.StringJoiner; @@ -20,7 +20,7 @@ public void printStartMessage() { System.out.println(resolveStartMessage()); } - public void printChessBoardMessage(BoardSnapShotDto boardSnapshotDto) { + public void printChessBoardMessage(BoardDto boardSnapshotDto) { System.out.println(resolveBoardSnapshotMessage(boardSnapshotDto)); } @@ -38,15 +38,15 @@ private String resolveStartMessage() { .toString(); } - private String resolveBoardSnapshotMessage(BoardSnapShotDto boardSnapshot) { - List boardSnapShot = boardSnapshot.getBoardSnapShot(); + private String resolveBoardSnapshotMessage(BoardDto boardSnapshot) { + List boardSnapShot = boardSnapshot.getBoardSnapShot(); return boardSnapShot.stream() .map(this::resolveRankSnapshotMessage) .collect(Collectors.joining(LINE_SEPARATOR)); } - private String resolveRankSnapshotMessage(RankSnapShotDto rankSnapShotDto) { - List rank = rankSnapShotDto.getRank(); + private String resolveRankSnapshotMessage(RankDto rankDto) { + List rank = rankDto.getRank(); return String.join("", rank); } From 21b7459fdcebc2814b51c7075c97ccc2e44b950c Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 16:16:49 +0900 Subject: [PATCH 68/80] =?UTF-8?q?refactor:=20=EC=97=AC=EB=9F=AC=EA=B0=9C?= =?UTF-8?q?=EC=9D=98=20=EA=B8=B0=EB=AC=BC=20=EC=A0=80=EC=9E=A5=EC=8B=9C=20?= =?UTF-8?q?=ED=95=98=EB=82=98=EC=9D=98=20=EC=BB=A4=EB=84=A5=EC=85=98=20?= =?UTF-8?q?=EC=9D=B4=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/chess/repository/PieceRepository.java | 16 ++++++++++------ .../java/chess/service/ChessGameService.java | 9 ++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/chess/repository/PieceRepository.java b/src/main/java/chess/repository/PieceRepository.java index f7d1bca0511..ef0fb72be3a 100644 --- a/src/main/java/chess/repository/PieceRepository.java +++ b/src/main/java/chess/repository/PieceRepository.java @@ -5,7 +5,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -19,16 +18,21 @@ public PieceRepository(ConnectionManager connectionManager) { this.connectionManager = connectionManager; } - public void savePiece(PiecePlacementDto piecePlacementDto) { - String query = String.format("INSERT INTO %s (position, team, type) VALUES (?, ?, ?)", TABLE_NAME); + public void savePieces(List pieces) { + try (Connection connection = connectionManager.getConnection()) { + pieces.forEach(piece -> savePiece(piece, connection)); + } catch (SQLException e) { + e.printStackTrace(); + } + } - try (Connection connection = connectionManager.getConnection(); - PreparedStatement pstmt = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { + private void savePiece(PiecePlacementDto piecePlacementDto, Connection connection) { + String query = String.format("INSERT INTO %s (position, team, type) VALUES (?, ?, ?)", TABLE_NAME); + try (PreparedStatement pstmt = connection.prepareStatement(query)) { pstmt.setString(1, piecePlacementDto.getPosition()); pstmt.setString(2, piecePlacementDto.getTeam()); pstmt.setString(3, piecePlacementDto.getType()); - pstmt.execute(); } catch (SQLException e) { e.printStackTrace(); diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index f653bc9ce34..7a220aecf56 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -69,11 +69,10 @@ private void saveChessGame(ChessGame chessGame) { deleteSavedChessGame(); ChessBoard chessBoard = chessGame.getChessBoard(); Map board = chessBoard.getBoard(); - - for (Position position : board.keySet()) { - Piece piece = board.get(position); - pieceRepository.savePiece(PiecePlacementDto.of(piece, position)); - } + List pieces = board.keySet().stream() + .map(position -> PiecePlacementDto.of(board.get(position), position)) + .toList(); + pieceRepository.savePieces(pieces); turnRepository.saveTurn(chessGame.getTurn()); } From a6d4aff14057ef7e27bf657c391aa953225ea4b4 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 16:18:01 +0900 Subject: [PATCH 69/80] =?UTF-8?q?rename:=20DTO=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{PiecePlacementDto.java => PieceDto.java} | 8 ++++---- .../java/chess/repository/PieceRepository.java | 18 +++++++++--------- .../chess/repository/mapper/DomainMapper.java | 12 ++++++------ .../java/chess/service/ChessGameService.java | 8 ++++---- 4 files changed, 23 insertions(+), 23 deletions(-) rename src/main/java/chess/dto/{PiecePlacementDto.java => PieceDto.java} (76%) diff --git a/src/main/java/chess/dto/PiecePlacementDto.java b/src/main/java/chess/dto/PieceDto.java similarity index 76% rename from src/main/java/chess/dto/PiecePlacementDto.java rename to src/main/java/chess/dto/PieceDto.java index 6151d2d72fe..59f4aca5390 100644 --- a/src/main/java/chess/dto/PiecePlacementDto.java +++ b/src/main/java/chess/dto/PieceDto.java @@ -4,19 +4,19 @@ import chess.domain.position.Position; import chess.repository.mapper.ValueMapper; -public class PiecePlacementDto { +public class PieceDto { private final String position; private final String team; private final String type; - public PiecePlacementDto(String position, String team, String type) { + public PieceDto(String position, String team, String type) { this.position = position; this.team = team; this.type = type; } - public static PiecePlacementDto of(Piece piece, Position position) { - return new PiecePlacementDto( + public static PieceDto of(Piece piece, Position position) { + return new PieceDto( ValueMapper.mapPositionToString(position), ValueMapper.mapTeamToString(piece.getTeam()), ValueMapper.mapPieceTypeToString(piece)); diff --git a/src/main/java/chess/repository/PieceRepository.java b/src/main/java/chess/repository/PieceRepository.java index ef0fb72be3a..326603cbbff 100644 --- a/src/main/java/chess/repository/PieceRepository.java +++ b/src/main/java/chess/repository/PieceRepository.java @@ -1,6 +1,6 @@ package chess.repository; -import chess.dto.PiecePlacementDto; +import chess.dto.PieceDto; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -18,7 +18,7 @@ public PieceRepository(ConnectionManager connectionManager) { this.connectionManager = connectionManager; } - public void savePieces(List pieces) { + public void savePieces(List pieces) { try (Connection connection = connectionManager.getConnection()) { pieces.forEach(piece -> savePiece(piece, connection)); } catch (SQLException e) { @@ -26,23 +26,23 @@ public void savePieces(List pieces) { } } - private void savePiece(PiecePlacementDto piecePlacementDto, Connection connection) { + private void savePiece(PieceDto pieceDto, Connection connection) { String query = String.format("INSERT INTO %s (position, team, type) VALUES (?, ?, ?)", TABLE_NAME); try (PreparedStatement pstmt = connection.prepareStatement(query)) { - pstmt.setString(1, piecePlacementDto.getPosition()); - pstmt.setString(2, piecePlacementDto.getTeam()); - pstmt.setString(3, piecePlacementDto.getType()); + pstmt.setString(1, pieceDto.getPosition()); + pstmt.setString(2, pieceDto.getTeam()); + pstmt.setString(3, pieceDto.getType()); pstmt.execute(); } catch (SQLException e) { e.printStackTrace(); } } - public Optional> findPieces() { + public Optional> findPieces() { String query = String.format("SELECT * FROM %s", TABLE_NAME); - List result = new ArrayList<>(); + List result = new ArrayList<>(); try (Connection connection = connectionManager.getConnection(); PreparedStatement pstmt = connection.prepareStatement(query); @@ -54,7 +54,7 @@ public Optional> findPieces() { String team = resultSet.getString("team"); String type = resultSet.getString("type"); - result.add(new PiecePlacementDto(position, team, type)); + result.add(new PieceDto(position, team, type)); } if (!result.isEmpty()) { return Optional.of(result); diff --git a/src/main/java/chess/repository/mapper/DomainMapper.java b/src/main/java/chess/repository/mapper/DomainMapper.java index 104857f6b60..b334eaa7ce2 100644 --- a/src/main/java/chess/repository/mapper/DomainMapper.java +++ b/src/main/java/chess/repository/mapper/DomainMapper.java @@ -12,7 +12,7 @@ import chess.domain.position.File; import chess.domain.position.Position; import chess.domain.position.Rank; -import chess.dto.PiecePlacementDto; +import chess.dto.PieceDto; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -28,13 +28,13 @@ public static Team mapToTurn(String value) { return Team.BLACK; } - public static ChessBoard mapToBoard(List piecePlacements) { + public static ChessBoard mapToBoard(List piecePlacements) { Map positionPiece = new HashMap<>(); - for (PiecePlacementDto piecePlacementDto : piecePlacements) { - Position position = mapToPosition(piecePlacementDto.getPosition()); - Team team = mapToTeam(piecePlacementDto.getTeam()); - Piece piece = mapToPiece(piecePlacementDto.getType(), team); + for (PieceDto pieceDto : piecePlacements) { + Position position = mapToPosition(pieceDto.getPosition()); + Team team = mapToTeam(pieceDto.getTeam()); + Piece piece = mapToPiece(pieceDto.getType(), team); positionPiece.put(position, piece); } diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index 7a220aecf56..4e83439342b 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -7,7 +7,7 @@ import chess.domain.piece.Team; import chess.domain.position.Position; import chess.dto.BoardDto; -import chess.dto.PiecePlacementDto; +import chess.dto.PieceDto; import chess.dto.ScoreStatusDto; import chess.repository.PieceRepository; import chess.repository.TurnRepository; @@ -48,7 +48,7 @@ public ScoreStatusDto calculateScoreStatus() { } public ChessGame loadChessGame() { - List pieces = pieceRepository.findPieces().get(); + List pieces = pieceRepository.findPieces().get(); Team currentTurn = turnRepository.findCurrentTurn().get(); ChessBoard chessBoard = DomainMapper.mapToBoard(pieces); @@ -69,8 +69,8 @@ private void saveChessGame(ChessGame chessGame) { deleteSavedChessGame(); ChessBoard chessBoard = chessGame.getChessBoard(); Map board = chessBoard.getBoard(); - List pieces = board.keySet().stream() - .map(position -> PiecePlacementDto.of(board.get(position), position)) + List pieces = board.keySet().stream() + .map(position -> PieceDto.of(board.get(position), position)) .toList(); pieceRepository.savePieces(pieces); turnRepository.saveTurn(chessGame.getTurn()); From 02ebd2b8124feac03552d1e6529699c531fa34bc Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 16:23:40 +0900 Subject: [PATCH 70/80] =?UTF-8?q?refactor:=20DTO=20=EC=9D=BC=EA=B8=89=20?= =?UTF-8?q?=EC=BB=AC=EB=A0=89=EC=85=98=20=EC=B6=94=EA=B0=80=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=B0=8F=20=EC=83=9D=EC=84=B1=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=20=EC=9D=91=EC=A7=91=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/dto/PiecesDto.java | 27 +++++++++++++++++++ .../chess/repository/PieceRepository.java | 5 ++-- .../java/chess/service/ChessGameService.java | 10 +++---- 3 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 src/main/java/chess/dto/PiecesDto.java diff --git a/src/main/java/chess/dto/PiecesDto.java b/src/main/java/chess/dto/PiecesDto.java new file mode 100644 index 00000000000..4278e9ba637 --- /dev/null +++ b/src/main/java/chess/dto/PiecesDto.java @@ -0,0 +1,27 @@ +package chess.dto; + +import chess.domain.board.ChessBoard; +import chess.domain.piece.Piece; +import chess.domain.position.Position; +import java.util.List; +import java.util.Map; + +public class PiecesDto { + private final List pieces; + + private PiecesDto(List pieces) { + this.pieces = pieces; + } + + public static PiecesDto from(ChessBoard chessBoard) { + Map board = chessBoard.getBoard(); + List pieces = board.keySet().stream() + .map(position -> PieceDto.of(board.get(position), position)) + .toList(); + return new PiecesDto(pieces); + } + + public List getPieces() { + return pieces; + } +} diff --git a/src/main/java/chess/repository/PieceRepository.java b/src/main/java/chess/repository/PieceRepository.java index 326603cbbff..f5b05b49959 100644 --- a/src/main/java/chess/repository/PieceRepository.java +++ b/src/main/java/chess/repository/PieceRepository.java @@ -1,6 +1,7 @@ package chess.repository; import chess.dto.PieceDto; +import chess.dto.PiecesDto; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -18,9 +19,9 @@ public PieceRepository(ConnectionManager connectionManager) { this.connectionManager = connectionManager; } - public void savePieces(List pieces) { + public void savePieces(PiecesDto pieces) { try (Connection connection = connectionManager.getConnection()) { - pieces.forEach(piece -> savePiece(piece, connection)); + pieces.getPieces().forEach(piece -> savePiece(piece, connection)); } catch (SQLException e) { e.printStackTrace(); } diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index 4e83439342b..a618dc4fce3 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -2,18 +2,17 @@ import chess.domain.ChessGame; import chess.domain.board.ChessBoard; -import chess.domain.piece.Piece; import chess.domain.piece.Score; import chess.domain.piece.Team; import chess.domain.position.Position; import chess.dto.BoardDto; import chess.dto.PieceDto; +import chess.dto.PiecesDto; import chess.dto.ScoreStatusDto; import chess.repository.PieceRepository; import chess.repository.TurnRepository; import chess.repository.mapper.DomainMapper; import java.util.List; -import java.util.Map; public class ChessGameService { private final PieceRepository pieceRepository; @@ -68,11 +67,8 @@ private void createNewChessGame() { private void saveChessGame(ChessGame chessGame) { deleteSavedChessGame(); ChessBoard chessBoard = chessGame.getChessBoard(); - Map board = chessBoard.getBoard(); - List pieces = board.keySet().stream() - .map(position -> PieceDto.of(board.get(position), position)) - .toList(); - pieceRepository.savePieces(pieces); + PiecesDto piecesDto = PiecesDto.from(chessBoard); + pieceRepository.savePieces(piecesDto); turnRepository.saveTurn(chessGame.getTurn()); } From f2b0d15bd4ede4387eb28b6cba08ddc6407c5b6e Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 16:27:43 +0900 Subject: [PATCH 71/80] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EC=9D=B8=EB=9D=BC=EC=9D=B8=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/service/ChessGameService.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index a618dc4fce3..1ae94c99201 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -27,8 +27,7 @@ public BoardDto startChessGame() { if (isChessGameInProgress()) { return BoardDto.from(loadChessGame().getChessBoard()); } - createNewChessGame(); - return BoardDto.from(loadChessGame().getChessBoard()); + return createNewChessGame(); } public BoardDto movePiece(Position start, Position destination) { @@ -50,18 +49,17 @@ public ChessGame loadChessGame() { List pieces = pieceRepository.findPieces().get(); Team currentTurn = turnRepository.findCurrentTurn().get(); ChessBoard chessBoard = DomainMapper.mapToBoard(pieces); - return new ChessGame(chessBoard, currentTurn); } public boolean isChessGameNotEnd() { - ChessGame chessGame = loadChessGame(); - return chessGame.isNotEnd(); + return loadChessGame().isNotEnd(); } - private void createNewChessGame() { + private BoardDto createNewChessGame() { ChessGame newChessGame = ChessGame.createNewChessGame(); saveChessGame(newChessGame); + return BoardDto.from(newChessGame.getChessBoard()); } private void saveChessGame(ChessGame chessGame) { From 9c32053996f6d3b5f8eda855ac193ced97eeac24 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 16:32:19 +0900 Subject: [PATCH 72/80] =?UTF-8?q?refactor:=20DTO=EC=9D=BC=EA=B8=89=20?= =?UTF-8?q?=EC=BB=AC=EB=A0=89=EC=85=98=EC=9D=84=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/dto/PiecesDto.java | 2 +- src/main/java/chess/repository/PieceRepository.java | 4 ++-- src/main/java/chess/repository/mapper/DomainMapper.java | 6 +++--- src/main/java/chess/service/ChessGameService.java | 4 +--- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/chess/dto/PiecesDto.java b/src/main/java/chess/dto/PiecesDto.java index 4278e9ba637..f4015854210 100644 --- a/src/main/java/chess/dto/PiecesDto.java +++ b/src/main/java/chess/dto/PiecesDto.java @@ -9,7 +9,7 @@ public class PiecesDto { private final List pieces; - private PiecesDto(List pieces) { + public PiecesDto(List pieces) { this.pieces = pieces; } diff --git a/src/main/java/chess/repository/PieceRepository.java b/src/main/java/chess/repository/PieceRepository.java index f5b05b49959..ddad70fdf8a 100644 --- a/src/main/java/chess/repository/PieceRepository.java +++ b/src/main/java/chess/repository/PieceRepository.java @@ -40,7 +40,7 @@ private void savePiece(PieceDto pieceDto, Connection connection) { } } - public Optional> findPieces() { + public Optional findPieces() { String query = String.format("SELECT * FROM %s", TABLE_NAME); List result = new ArrayList<>(); @@ -58,7 +58,7 @@ public Optional> findPieces() { result.add(new PieceDto(position, team, type)); } if (!result.isEmpty()) { - return Optional.of(result); + return Optional.of(new PiecesDto(result)); } } catch (SQLException e) { e.printStackTrace(); diff --git a/src/main/java/chess/repository/mapper/DomainMapper.java b/src/main/java/chess/repository/mapper/DomainMapper.java index b334eaa7ce2..a34ea587da1 100644 --- a/src/main/java/chess/repository/mapper/DomainMapper.java +++ b/src/main/java/chess/repository/mapper/DomainMapper.java @@ -13,8 +13,8 @@ import chess.domain.position.Position; import chess.domain.position.Rank; import chess.dto.PieceDto; +import chess.dto.PiecesDto; import java.util.HashMap; -import java.util.List; import java.util.Map; public class DomainMapper { @@ -28,10 +28,10 @@ public static Team mapToTurn(String value) { return Team.BLACK; } - public static ChessBoard mapToBoard(List piecePlacements) { + public static ChessBoard mapToBoard(PiecesDto piecePlacements) { Map positionPiece = new HashMap<>(); - for (PieceDto pieceDto : piecePlacements) { + for (PieceDto pieceDto : piecePlacements.getPieces()) { Position position = mapToPosition(pieceDto.getPosition()); Team team = mapToTeam(pieceDto.getTeam()); Piece piece = mapToPiece(pieceDto.getType(), team); diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index 1ae94c99201..d121459b23b 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -6,13 +6,11 @@ import chess.domain.piece.Team; import chess.domain.position.Position; import chess.dto.BoardDto; -import chess.dto.PieceDto; import chess.dto.PiecesDto; import chess.dto.ScoreStatusDto; import chess.repository.PieceRepository; import chess.repository.TurnRepository; import chess.repository.mapper.DomainMapper; -import java.util.List; public class ChessGameService { private final PieceRepository pieceRepository; @@ -46,7 +44,7 @@ public ScoreStatusDto calculateScoreStatus() { } public ChessGame loadChessGame() { - List pieces = pieceRepository.findPieces().get(); + PiecesDto pieces = pieceRepository.findPieces().get(); Team currentTurn = turnRepository.findCurrentTurn().get(); ChessBoard chessBoard = DomainMapper.mapToBoard(pieces); return new ChessGame(chessBoard, currentTurn); From 66ac4b2375e8cd51b4b5ff589c2217a80c2eb017 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 16:50:10 +0900 Subject: [PATCH 73/80] =?UTF-8?q?test:=20=EC=A0=90=EC=88=98=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=20=EB=A1=9C=EC=A7=81=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/chess/domain/ChessGameTest.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/test/java/chess/domain/ChessGameTest.java b/src/test/java/chess/domain/ChessGameTest.java index ad0f2f3c2a9..82f837adda3 100644 --- a/src/test/java/chess/domain/ChessGameTest.java +++ b/src/test/java/chess/domain/ChessGameTest.java @@ -2,10 +2,15 @@ import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import chess.domain.board.ChessBoard; import chess.domain.piece.King; +import chess.domain.piece.Knight; +import chess.domain.piece.Pawn; import chess.domain.piece.Piece; +import chess.domain.piece.Rook; +import chess.domain.piece.Score; import chess.domain.piece.Team; import chess.domain.position.Position; import chess.fixture.PositionFixtures; @@ -40,4 +45,43 @@ void should_ThrowException_When_IllegalTurnMove() { assertThatThrownBy(() -> chessGame.move(PositionFixtures.A8, PositionFixtures.A7)) .isInstanceOf(IllegalArgumentException.class); } + + @DisplayName("배치된 기물에 맞추어 팀 점수를 계산할 수 있다") + @Test + void should_CalculateTeamScore() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, new Knight(Team.WHITE)); + board.put(PositionFixtures.A8, new Rook(Team.WHITE)); + + ChessBoard chessBoard = new ChessBoard(board); + ChessGame chessGame = new ChessGame(chessBoard); + + assertThat(chessGame.calculateTeamScore(Team.WHITE)).isEqualTo(new Score(7.5)); + } + + @DisplayName("폰은 열이 겹치지 않는다면 1점으로 계산한다") + @Test + void should_CalculatePawnScoreAsOne_When_NoSameFilePawn() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, new Pawn(Team.WHITE)); + board.put(PositionFixtures.B1, new Pawn(Team.WHITE)); + + ChessBoard chessBoard = new ChessBoard(board); + ChessGame chessGame = new ChessGame(chessBoard); + + assertThat(chessGame.calculateTeamScore(Team.WHITE)).isEqualTo(new Score(2)); + } + + @DisplayName("폰은 열이 겹치지 않는다면 1점으로 계산한다") + @Test + void should_CalculatePawnScoreAsHalf_When_SameFilePawnExist() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, new Pawn(Team.WHITE)); + board.put(PositionFixtures.A2, new Pawn(Team.WHITE)); + + ChessBoard chessBoard = new ChessBoard(board); + ChessGame chessGame = new ChessGame(chessBoard); + + assertThat(chessGame.calculateTeamScore(Team.WHITE)).isEqualTo(new Score(1)); + } } From 059b37b69141250b95fc273479aefb30db3b9959 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 16:53:00 +0900 Subject: [PATCH 74/80] =?UTF-8?q?refactor:=20=EC=B2=B4=EC=8A=A4=20?= =?UTF-8?q?=EA=B2=8C=EC=9E=84=20=EC=A0=90=EC=88=98=20=EB=B9=84=EA=B5=90=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=B2=B4=EC=8A=A4=EA=B2=8C=EC=9E=84=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=EC=95=88=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=91=EC=A7=91=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/domain/ChessGame.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/chess/domain/ChessGame.java b/src/main/java/chess/domain/ChessGame.java index 2cf6345e224..b64a54edad8 100644 --- a/src/main/java/chess/domain/ChessGame.java +++ b/src/main/java/chess/domain/ChessGame.java @@ -42,6 +42,15 @@ public Score calculateTeamScore(Team team) { return defaultScore.add(weight); } + public Team selectHigherScoreTeam() { + Score blackScore = calculateTeamScore(Team.BLACK); + Score whiteScore = calculateTeamScore(Team.WHITE); + if (whiteScore.isAbove(blackScore)) { + return Team.WHITE; + } + return Team.BLACK; + } + public boolean isNotEnd() { return chessBoard.isBlackKingAlive() && chessBoard.isWhiteKingAlive(); } From 84a40432ed6f9b55265f2640e40f530325744ac2 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 17:03:40 +0900 Subject: [PATCH 75/80] =?UTF-8?q?test:=20=EC=A0=90=EC=88=98=20=EB=B9=84?= =?UTF-8?q?=EA=B5=90=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/chess/domain/ChessGameTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/java/chess/domain/ChessGameTest.java b/src/test/java/chess/domain/ChessGameTest.java index 82f837adda3..1a9d45cb032 100644 --- a/src/test/java/chess/domain/ChessGameTest.java +++ b/src/test/java/chess/domain/ChessGameTest.java @@ -84,4 +84,21 @@ void should_CalculatePawnScoreAsHalf_When_SameFilePawnExist() { assertThat(chessGame.calculateTeamScore(Team.WHITE)).isEqualTo(new Score(1)); } + + @DisplayName("체스 게임 도메인은 점수가 더 높은 팀을 가려낼 수 있다") + @Test + void should_ChessGameCanSelectHigherScoreTeam() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, new Pawn(Team.WHITE)); + board.put(PositionFixtures.A2, new Pawn(Team.WHITE)); + + board.put(PositionFixtures.A3, new Knight(Team.BLACK)); + board.put(PositionFixtures.A8, new Rook(Team.BLACK)); + + ChessBoard chessBoard = new ChessBoard(board); + ChessGame chessGame = new ChessGame(chessBoard); + + assertThat(chessGame.selectHigherScoreTeam()).isEqualTo(Team.BLACK); + } } + From 7c2e131b5188be2cbf6a7b1dd4a5a934dbfb29b0 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 17:04:33 +0900 Subject: [PATCH 76/80] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/service/ChessGameService.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/chess/service/ChessGameService.java b/src/main/java/chess/service/ChessGameService.java index d121459b23b..16f188563dd 100644 --- a/src/main/java/chess/service/ChessGameService.java +++ b/src/main/java/chess/service/ChessGameService.java @@ -39,8 +39,7 @@ public ScoreStatusDto calculateScoreStatus() { ChessGame chessGame = loadChessGame(); Score blackScore = chessGame.calculateTeamScore(Team.BLACK); Score whiteScore = chessGame.calculateTeamScore(Team.WHITE); - Team winnerTeam = judgeWinnerTeam(whiteScore, blackScore); - return ScoreStatusDto.of(whiteScore, blackScore, winnerTeam); + return ScoreStatusDto.of(whiteScore, blackScore, chessGame.selectHigherScoreTeam()); } public ChessGame loadChessGame() { @@ -76,11 +75,4 @@ private void deleteSavedChessGame() { private boolean isChessGameInProgress() { return pieceRepository.findPieces().isPresent(); } - - private Team judgeWinnerTeam(Score whiteTeamScore, Score blackTeamScore) { - if (whiteTeamScore.isAbove(blackTeamScore)) { - return Team.WHITE; - } - return Team.BLACK; - } } From 0b5b6ecb6d55f585a101c3d7dae1a7d5c1eab018 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 18:56:24 +0900 Subject: [PATCH 77/80] =?UTF-8?q?refactor:=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=83=81=ED=99=A9=20=EB=B0=9C=EC=83=9D=20=EC=8B=9C=20=ED=99=95?= =?UTF-8?q?=EC=8B=A4=ED=9E=88=20=EC=B2=98=EB=A6=AC=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/repository/ConnectionManager.java | 4 +--- src/main/java/chess/repository/PieceRepository.java | 8 ++++---- src/main/java/chess/repository/TurnRepository.java | 6 +++--- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/chess/repository/ConnectionManager.java b/src/main/java/chess/repository/ConnectionManager.java index aa591180a88..149f9d2a7c3 100644 --- a/src/main/java/chess/repository/ConnectionManager.java +++ b/src/main/java/chess/repository/ConnectionManager.java @@ -15,9 +15,7 @@ public Connection getConnection() { try { return DriverManager.getConnection("jdbc:mysql://" + SERVER + "/" + DATABASE + OPTION, USERNAME, PASSWORD); } catch (final SQLException e) { - System.err.println("DB 연결 오류:" + e.getMessage()); - e.printStackTrace(); - return null; + throw new RuntimeException("DB 연결 오류"); } } } diff --git a/src/main/java/chess/repository/PieceRepository.java b/src/main/java/chess/repository/PieceRepository.java index ddad70fdf8a..e4df2facf45 100644 --- a/src/main/java/chess/repository/PieceRepository.java +++ b/src/main/java/chess/repository/PieceRepository.java @@ -23,7 +23,7 @@ public void savePieces(PiecesDto pieces) { try (Connection connection = connectionManager.getConnection()) { pieces.getPieces().forEach(piece -> savePiece(piece, connection)); } catch (SQLException e) { - e.printStackTrace(); + throw new RuntimeException("기물 저장 과정 중 오류 발생"); } } @@ -36,7 +36,7 @@ private void savePiece(PieceDto pieceDto, Connection connection) { pstmt.setString(3, pieceDto.getType()); pstmt.execute(); } catch (SQLException e) { - e.printStackTrace(); + throw new RuntimeException("기물 저장 과정 중 오류 발생"); } } @@ -61,7 +61,7 @@ public Optional findPieces() { return Optional.of(new PiecesDto(result)); } } catch (SQLException e) { - e.printStackTrace(); + throw new RuntimeException("기물 조회 과정 중 오류 발생"); } return Optional.empty(); } @@ -74,7 +74,7 @@ public void deleteAll() { pstmt.executeUpdate(); } catch (SQLException e) { - e.printStackTrace(); + throw new RuntimeException("기물 조회 과정 중 오류 발생"); } } } diff --git a/src/main/java/chess/repository/TurnRepository.java b/src/main/java/chess/repository/TurnRepository.java index dead6d6ca03..fd7b2c29ef5 100644 --- a/src/main/java/chess/repository/TurnRepository.java +++ b/src/main/java/chess/repository/TurnRepository.java @@ -28,7 +28,7 @@ public void saveTurn(Team turn) { pstmt.execute(); } catch (SQLException e) { - e.printStackTrace(); + throw new RuntimeException("턴 저장 과정 중 오류 발생"); } } @@ -44,7 +44,7 @@ public Optional findCurrentTurn() { return Optional.of(DomainMapper.mapToTurn(value)); } } catch (SQLException e) { - e.printStackTrace(); + throw new RuntimeException("턴 조회 과정 중 오류 발생"); } return Optional.empty(); } @@ -58,7 +58,7 @@ public void deleteAll() { pstmt.executeUpdate(); } catch (SQLException e) { - e.printStackTrace(); + throw new RuntimeException("턴 삭세 과정 중 오류 발생"); } } } From 6cb54feed86d6ffc9a4b7569dc92c70735f2c579 Mon Sep 17 00:00:00 2001 From: libienz Date: Sun, 31 Mar 2024 18:57:42 +0900 Subject: [PATCH 78/80] =?UTF-8?q?refactor:=20=EB=AF=B8=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=20=EB=B3=80=EC=88=98=20=EC=A0=9C=EA=B1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chess/repository/PieceRepository.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/chess/repository/PieceRepository.java b/src/main/java/chess/repository/PieceRepository.java index e4df2facf45..b598741ee02 100644 --- a/src/main/java/chess/repository/PieceRepository.java +++ b/src/main/java/chess/repository/PieceRepository.java @@ -50,7 +50,6 @@ public Optional findPieces() { ResultSet resultSet = pstmt.executeQuery()) { while (resultSet.next()) { - long id = resultSet.getLong("id"); String position = resultSet.getString("position"); String team = resultSet.getString("team"); String type = resultSet.getString("type"); From 27c723bcad71abb219522fb7d20d9943d826398b Mon Sep 17 00:00:00 2001 From: libienz Date: Mon, 1 Apr 2024 17:24:29 +0900 Subject: [PATCH 79/80] =?UTF-8?q?chore:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=AA=A9=20=EB=9D=BC=EC=9D=B4=EB=B8=8C=EB=9F=AC=EB=A6=AC=20?= =?UTF-8?q?=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 20ad08a5a5e..041505beca4 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,8 @@ dependencies { testImplementation platform('org.assertj:assertj-bom:3.25.1') testImplementation('org.junit.jupiter:junit-jupiter') testImplementation('org.assertj:assertj-core') - + testImplementation 'org.mockito:mockito-core:3.6.28' + runtimeOnly("com.mysql:mysql-connector-j:8.3.0") } From 8e978c40fc6ca1de02a010632214dcc826b64c1d Mon Sep 17 00:00:00 2001 From: libienz Date: Mon, 1 Apr 2024 17:24:42 +0900 Subject: [PATCH 80/80] =?UTF-8?q?test:=20=EC=84=9C=EB=B9=84=EC=8A=A4=20?= =?UTF-8?q?=EB=A0=88=EC=9D=B4=EC=96=B4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/service/ChessGameServiceTest.java | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 src/test/java/chess/service/ChessGameServiceTest.java diff --git a/src/test/java/chess/service/ChessGameServiceTest.java b/src/test/java/chess/service/ChessGameServiceTest.java new file mode 100644 index 00000000000..0dd244b6c8c --- /dev/null +++ b/src/test/java/chess/service/ChessGameServiceTest.java @@ -0,0 +1,136 @@ +package chess.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.mockito.Mockito.when; + +import chess.domain.ChessGame; +import chess.domain.board.ChessBoard; +import chess.domain.piece.King; +import chess.domain.piece.Pawn; +import chess.domain.piece.Piece; +import chess.domain.piece.Team; +import chess.domain.position.Position; +import chess.dto.BoardDto; +import chess.dto.PiecesDto; +import chess.dto.RankDto; +import chess.dto.ScoreStatusDto; +import chess.fixture.PositionFixtures; +import chess.repository.PieceRepository; +import chess.repository.TurnRepository; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +class ChessGameServiceTest { + private ChessGameService chessGameService; + @Mock + private PieceRepository pieceRepository; + @Mock + private TurnRepository turnRepository; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + chessGameService = new ChessGameService(pieceRepository, turnRepository); + } + + @DisplayName("게임 시작 시 이미 진행되고 있는 게임이 존재하는 경우 이어서 진행된다") + @Test + void should_StartExistingGame_When_GameIsInProgress() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, new Pawn(Team.WHITE)); + ChessBoard chessBoard = new ChessBoard(board); + + when(pieceRepository.findPieces()).thenReturn(Optional.of(PiecesDto.from(chessBoard))); + when(turnRepository.findCurrentTurn()).thenReturn(Optional.of(Team.WHITE)); + + BoardDto boardDto = chessGameService.startChessGame(); + List boardSnapShot = boardDto.getBoardSnapShot(); + List rank1 = boardSnapShot.get(7).getRank(); + assertThat(rank1.get(0)).isEqualTo("p"); + } + + @DisplayName("게임 시작 시 이미 진행되고 있는 게임이 없는 경우 새로운 게임이 진행된다") + @Test + void should_StartNewGame_When_ThereIsNoGameInProgress() { + when(pieceRepository.findPieces()).thenReturn(Optional.empty()); + when(turnRepository.findCurrentTurn()).thenReturn(Optional.empty()); + + BoardDto boardDto = chessGameService.startChessGame(); + List boardSnapShot = boardDto.getBoardSnapShot(); + List rank2 = boardSnapShot.get(6).getRank(); + assertThat(rank2).containsExactly("p", "p", "p", "p", "p", "p", "p", "p"); + } + + @DisplayName("움직임을 요청 받으면 저장되어있는 배치에서 움직임을 검증, 움직임을 수행한다") + @Test + void should_MoveBySavedPlacement_When_MoveCallArrive() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, new Pawn(Team.WHITE)); + ChessBoard chessBoard = new ChessBoard(board); + + when(pieceRepository.findPieces()).thenReturn(Optional.of(PiecesDto.from(chessBoard))); + when(turnRepository.findCurrentTurn()).thenReturn(Optional.of(Team.WHITE)); + BoardDto boardDto = chessGameService.movePiece(PositionFixtures.A1, PositionFixtures.A2); + String a2 = boardDto.getBoardSnapShot().get(6).getRank().get(0); + assertThat(a2).isEqualTo("p"); + } + + @DisplayName("저장된 보드 상태를 기반으로 점수를 계산할 수 있다") + @Test + void should_CalculateScoreBySavedStatus() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, new Pawn(Team.WHITE)); + ChessBoard chessBoard = new ChessBoard(board); + + when(pieceRepository.findPieces()).thenReturn(Optional.of(PiecesDto.from(chessBoard))); + when(turnRepository.findCurrentTurn()).thenReturn(Optional.of(Team.WHITE)); + + ScoreStatusDto scoreStatusDto = chessGameService.calculateScoreStatus(); + assertAll( + () -> assertThat(scoreStatusDto.getBlackTeamScore()).isEqualTo(0), + () -> assertThat(scoreStatusDto.getWhiteTeamScore()).isEqualTo(1), + () -> assertThat(scoreStatusDto.getWinnerTeam()).isEqualTo("WHITE") + ); + } + + @DisplayName("저장된 게임을 로드할 수 있다") + @Test + void should_LoadInProgressGame() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, new King(Team.WHITE)); + board.put(PositionFixtures.A2, new King(Team.BLACK)); + ChessBoard chessBoard = new ChessBoard(board); + + when(pieceRepository.findPieces()).thenReturn(Optional.of(PiecesDto.from(chessBoard))); + when(turnRepository.findCurrentTurn()).thenReturn(Optional.of(Team.WHITE)); + + ChessGame chessGame = chessGameService.loadChessGame(); + assertAll( + () -> assertThat(chessGame.getTurn()).isEqualTo(Team.WHITE), + () -> assertThat(chessGame.getChessBoard().positionIsEmpty(PositionFixtures.A1)).isFalse(), + () -> assertThat(chessGame.getChessBoard().positionIsEmpty(PositionFixtures.A2)).isFalse() + ); + } + + @DisplayName("저장된 게임이 끝난 게임인지 확인할 수 있다") + @Test + void should_CheckSavedGameIsEnd() { + Map board = new HashMap<>(); + board.put(PositionFixtures.A1, new King(Team.BLACK)); + ChessBoard chessBoard = new ChessBoard(board); + + when(pieceRepository.findPieces()).thenReturn(Optional.of(PiecesDto.from(chessBoard))); + when(turnRepository.findCurrentTurn()).thenReturn(Optional.of(Team.WHITE)); + + boolean chessGameNotEnd = chessGameService.isChessGameNotEnd(); + assertThat(chessGameNotEnd).isFalse(); + } +}