Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/com/rogeriofrsouza/app/boardgame/Position.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rogeriofrsouza.app.boardgame;

import com.rogeriofrsouza.app.chess.ChessPosition;
import lombok.AllArgsConstructor;
import lombok.Data;

Expand All @@ -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);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/rogeriofrsouza/app/chess/ChessMatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/rogeriofrsouza/app/chess/ChessPiece.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected ChessPiece(
}

public ChessPosition getChessPosition() {
return ChessPosition.fromPosition(position);
return position.toChessPosition();
}

protected boolean isThereOpponentPiece(Position position) {
Expand Down
29 changes: 2 additions & 27 deletions src/main/java/com/rogeriofrsouza/app/chess/ChessPosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
78 changes: 42 additions & 36 deletions src/test/java/com/rogeriofrsouza/app/ui/DisplayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Piece> 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
Expand All @@ -98,25 +101,26 @@ void printMatch_isCheck_logMatchAndCheck() {
chessMatch.setCurrentPlayer(ChessPiece.Color.BLACK);

Board board = chessMatch.getBoard();
List<Piece> captured = List.of(
new Rook(board, ChessPiece.Color.WHITE), new Rook(board, ChessPiece.Color.WHITE));
List<Piece> 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
Expand All @@ -128,23 +132,25 @@ void printMatch_isCheckMate_logMatchAndCheckMate() {
chessMatch.setCurrentPlayer(ChessPiece.Color.BLACK);

Board board = chessMatch.getBoard();
List<Piece> captured = List.of(
new Rook(board, ChessPiece.Color.BLACK), new Rook(board, ChessPiece.Color.BLACK));
List<Piece> 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());
}
}