diff --git a/src/main/java/com/rogeriofrsouza/app/boardgame/Position.java b/src/main/java/com/rogeriofrsouza/app/boardgame/Position.java index 428a7c2..bd3c314 100644 --- a/src/main/java/com/rogeriofrsouza/app/boardgame/Position.java +++ b/src/main/java/com/rogeriofrsouza/app/boardgame/Position.java @@ -1,5 +1,6 @@ package com.rogeriofrsouza.app.boardgame; +import com.rogeriofrsouza.app.chess.ChessPosition; import lombok.AllArgsConstructor; import lombok.Data; @@ -14,4 +15,8 @@ public void setValues(int row, int column) { this.row = row; this.column = column; } + + public ChessPosition toChessPosition() { + return new ChessPosition((char) (column + 'a'), 8 - row); + } } diff --git a/src/main/java/com/rogeriofrsouza/app/chess/ChessMatch.java b/src/main/java/com/rogeriofrsouza/app/chess/ChessMatch.java index ded5a01..31f3835 100644 --- a/src/main/java/com/rogeriofrsouza/app/chess/ChessMatch.java +++ b/src/main/java/com/rogeriofrsouza/app/chess/ChessMatch.java @@ -95,6 +95,7 @@ public void performChessMove(ChessPosition sourcePosition, ChessPosition targetP } Piece capturedPiece = makeMove(source, target); + board.resetPossibleMoves(); if (testCheck(currentPlayer)) { undoMove(source, target, capturedPiece); diff --git a/src/main/java/com/rogeriofrsouza/app/chess/ChessPiece.java b/src/main/java/com/rogeriofrsouza/app/chess/ChessPiece.java index 9214c6a..aeadc21 100644 --- a/src/main/java/com/rogeriofrsouza/app/chess/ChessPiece.java +++ b/src/main/java/com/rogeriofrsouza/app/chess/ChessPiece.java @@ -37,7 +37,7 @@ protected ChessPiece( } public ChessPosition getChessPosition() { - return ChessPosition.fromPosition(position); + return position.toChessPosition(); } protected boolean isThereOpponentPiece(Position position) { diff --git a/src/main/java/com/rogeriofrsouza/app/chess/ChessPosition.java b/src/main/java/com/rogeriofrsouza/app/chess/ChessPosition.java index cbbf1ae..47c8bfc 100644 --- a/src/main/java/com/rogeriofrsouza/app/chess/ChessPosition.java +++ b/src/main/java/com/rogeriofrsouza/app/chess/ChessPosition.java @@ -2,40 +2,15 @@ import com.rogeriofrsouza.app.boardgame.Position; -public class ChessPosition { +public record ChessPosition(char column, int row) { - private final char column; - private final int row; - - public ChessPosition(char column, int row) { + public ChessPosition { if (column < 'a' || column > 'h' || row < 1 || row > 8) { throw new IllegalArgumentException("Invalid chess position. Should be a1 - h8"); } - - this.column = column; - this.row = row; - } - - public char getColumn() { - return column; - } - - public int getRow() { - return row; } - // Não permitir que a coluna e a linha sejam livremente alteradas - public Position toPosition() { return new Position(8 - row, column - 'a'); } - - protected static ChessPosition fromPosition(Position position) { - return new ChessPosition((char) (position.getColumn() + 'a'), 8 - position.getRow()); - } - - @Override - public String toString() { - return "" + column + row; // Macete para forçar a concatenação - } } diff --git a/src/test/java/com/rogeriofrsouza/app/ui/DisplayTest.java b/src/test/java/com/rogeriofrsouza/app/ui/DisplayTest.java index 2e20c24..6395d7f 100644 --- a/src/test/java/com/rogeriofrsouza/app/ui/DisplayTest.java +++ b/src/test/java/com/rogeriofrsouza/app/ui/DisplayTest.java @@ -66,27 +66,30 @@ void clearScreen() { @DisplayName("should print the match information and status") void printMatch_notCheckNotCheckmate_logMatch() { ChessMatch chessMatch = new ChessMatch(); - Board board = chessMatch.getBoard(); + List captured = List.of( new Rook(board, ChessPiece.Color.WHITE), new Rook(board, ChessPiece.Color.WHITE), new Rook(board, ChessPiece.Color.BLACK), new Rook(board, ChessPiece.Color.BLACK)); + chessMatch.setCapturedPieces(captured); - String outputExpected = "Captured pieces%nBlack: %s%s%s%nWhite: %s%s%s%n".formatted( - YELLOW, - captured.subList(2, 4), - RESET, - WHITE, - captured.subList(0, 2), - RESET) + - "Turn: " + chessMatch.getTurn() + "\n" + - "Waiting player: " + chessMatch.getCurrentPlayer() + "\n"; + String string = """ + Captured pieces + Black: %s%s%s + White: %s%s%s + Turn: %d + Waiting player: %s%s%s + """.formatted( + YELLOW, captured.subList(2, 4), RESET, + WHITE, captured.subList(0, 2), RESET, + chessMatch.getTurn(), + WHITE, chessMatch.getCurrentPlayer().getLabel(), RESET); doNothing().when(display).printBoard(board); display.printMatch(chessMatch); - assertEquals(outputExpected, outputStream.toString()); + assertEquals(string, outputStream.toString()); } @Test @@ -98,25 +101,26 @@ void printMatch_isCheck_logMatchAndCheck() { chessMatch.setCurrentPlayer(ChessPiece.Color.BLACK); Board board = chessMatch.getBoard(); - List captured = List.of( - new Rook(board, ChessPiece.Color.WHITE), new Rook(board, ChessPiece.Color.WHITE)); + List captured = List.of(new Rook(board, ChessPiece.Color.WHITE), new Rook(board, ChessPiece.Color.WHITE)); chessMatch.setCapturedPieces(captured); - String outputExpected = "Captured pieces%nBlack: %s%s%s%nWhite: %s%s%s%n".formatted( - YELLOW, - List.of(), - RESET, - WHITE, - captured, - RESET) + - "Turn: " + chessMatch.getTurn() + "\n" + - "Waiting player: " + chessMatch.getCurrentPlayer() + "\n" + - "CHECK!\n"; + String string = """ + Captured pieces + Black: %s%s%s + White: %s%s%s + Turn: %d + Waiting player: %s%s%s + CHECK! + """.formatted( + YELLOW, List.of(), RESET, + WHITE, captured, RESET, + chessMatch.getTurn(), + YELLOW, chessMatch.getCurrentPlayer().getLabel(), RESET); doNothing().when(display).printBoard(board); display.printMatch(chessMatch); - assertEquals(outputExpected, outputStream.toString()); + assertEquals(string, outputStream.toString()); } @Test @@ -128,23 +132,25 @@ void printMatch_isCheckMate_logMatchAndCheckMate() { chessMatch.setCurrentPlayer(ChessPiece.Color.BLACK); Board board = chessMatch.getBoard(); - List captured = List.of( - new Rook(board, ChessPiece.Color.BLACK), new Rook(board, ChessPiece.Color.BLACK)); + List captured = List.of(new Rook(board, ChessPiece.Color.BLACK), new Rook(board, ChessPiece.Color.BLACK)); chessMatch.setCapturedPieces(captured); - String stringBuilder = "Captured pieces%nBlack: %s%s%s%nWhite: %s%s%s%n".formatted( - YELLOW, - captured, - RESET, - WHITE, - List.of(), - RESET) + - "Turn: " + chessMatch.getTurn() + "\n" + - "CHECKMATE!\nWinner: " + chessMatch.getCurrentPlayer() + "\n"; + String string = """ + Captured pieces + Black: %s%s%s + White: %s%s%s + Turn: %d + CHECKMATE! + Winner: %s%s%s + """.formatted( + YELLOW, captured, RESET, + WHITE, List.of(), RESET, + chessMatch.getTurn(), + YELLOW, chessMatch.getCurrentPlayer().getLabel(), RESET); doNothing().when(display).printBoard(board); display.printMatch(chessMatch); - assertEquals(stringBuilder, outputStream.toString()); + assertEquals(string, outputStream.toString()); } }