Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9551ce6
feat(enum): add inner enum Name in ChessPiece
rogeriofrsouza Sep 14, 2024
be7833c
feat(ChessPiece): add @Getter, change constructor visibility and add …
rogeriofrsouza Sep 14, 2024
16af3ef
refactor: rename local variable
rogeriofrsouza Sep 14, 2024
68bb056
feat(enum): add inner enum Color in ChessPiece and deprecate the olde…
rogeriofrsouza Sep 14, 2024
2796851
fix: update code to the new inner Color structure
rogeriofrsouza Sep 14, 2024
d39d032
test: update unit tests to the inner Color design
rogeriofrsouza Sep 14, 2024
3e4caa9
feat: remove Color enum and update pieces imports
rogeriofrsouza Sep 14, 2024
00897f5
refactor(Piece): remove comments, change methods signatures and renam…
rogeriofrsouza Oct 5, 2024
c687a9d
feat(ChessMatch): add possible promoted pieces constant list and a me…
rogeriofrsouza Oct 11, 2024
73f498d
feat: add UI method to read a promoted piece and use it on program wh…
rogeriofrsouza Oct 11, 2024
0a785f8
feat(UI): improve input reading
rogeriofrsouza Oct 11, 2024
42076a8
test: change styles, improve string building and add parameterized tests
rogeriofrsouza Oct 12, 2024
7163b22
feat(ChessPiece): change color and letter fields to final
rogeriofrsouza Oct 12, 2024
1cf6372
refactor(UI): improve formatting, readability and efficiency
rogeriofrsouza Oct 12, 2024
14c604f
feat(Piece): change access modifier on fields, add lombok @Getter and…
rogeriofrsouza Oct 12, 2024
9476668
feat(ChessPiece): add final access modifier to name field
rogeriofrsouza Oct 12, 2024
0ce8cb7
refactor: change superclass constructor used on Bishop and move insta…
rogeriofrsouza Oct 19, 2024
f60c9f9
refactor: rename local variables
rogeriofrsouza Oct 19, 2024
9997b2a
refactor: extract northwest possible moves computing
rogeriofrsouza Oct 19, 2024
73ff677
feat(enum): add enum for possible directions of a chess move
rogeriofrsouza Oct 20, 2024
308e022
refactor(Bishop): extract duplicated code and position changes based …
rogeriofrsouza Oct 20, 2024
a8ea3c3
refactor(Bishop): simplify checkMoves method removing duplicated code
rogeriofrsouza Oct 20, 2024
6501f9b
refactor(Bishop): pull members up to ChessPiece
rogeriofrsouza Oct 20, 2024
3124190
feat(Bishop): create field for possible chess move directions and use…
rogeriofrsouza Oct 20, 2024
aa1a210
feat(ChessPosition): change method access modifier
rogeriofrsouza Oct 23, 2024
5f87f88
test: add unit test for computing possible moves of a bishop
rogeriofrsouza Oct 23, 2024
cda0819
feat(Rook): add chess move directions constant, update call to super,…
rogeriofrsouza Oct 23, 2024
01a9fad
test: add computePossibleMoves method unit test
rogeriofrsouza Oct 23, 2024
6ea3d4d
feat(Queen): add chess move directions and update super, toString and…
rogeriofrsouza Oct 23, 2024
ae34ef9
feat: change signature of ChessPiece constructor, add chessMoveDirect…
rogeriofrsouza Oct 23, 2024
99b54fe
feat: use getter for chessMoveDirections field and Optional chain
rogeriofrsouza Oct 23, 2024
6078193
build: remove unused pitest plugin and add .idea to gitignore
rogeriofrsouza May 28, 2025
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,5 @@ buildNumber.properties
# JDT-specific (Eclipse Java Development Tools)
.classpath

.idea
# End of https://www.toptal.com/developers/gitignore/api/java,maven,eclipse
13 changes: 0 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,6 @@
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>

<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>1.15.2</version>
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/rogeriofrsouza/app/Program.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.rogeriofrsouza.app;

import com.rogeriofrsouza.app.chess.ChessException;
import com.rogeriofrsouza.app.chess.ChessMatch;
import com.rogeriofrsouza.app.chess.ChessPiece;
import com.rogeriofrsouza.app.chess.ChessPosition;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Scanner;

import com.rogeriofrsouza.app.chess.ChessException;
import com.rogeriofrsouza.app.chess.ChessMatch;
import com.rogeriofrsouza.app.chess.ChessPiece;
import com.rogeriofrsouza.app.chess.ChessPosition;

public class Program {

public static void main(String[] args) {
Expand Down Expand Up @@ -42,14 +42,14 @@ public static void main(String[] args) {

if (chessMatch.getPromoted() != null) {
System.out.print("Enter piece for promotion (B/N/R/Q): ");
String type = scanner.nextLine().substring(0, 1).toUpperCase();
ChessPiece.Name pieceName = ui.readPromotedPiece(scanner);

while (!List.of("B", "N", "R", "Q").contains(type)) {
while (pieceName == null) {
System.err.print("Invalid value! Enter piece for promotion (B/N/R/Q): ");
type = scanner.nextLine().substring(0, 1).toUpperCase();
pieceName = ui.readPromotedPiece(scanner);
}

chessMatch.replacePromotedPiece(type);
chessMatch.replacePromotedPiece(pieceName);
}
} catch (ChessException | InputMismatchException exception) {
System.err.println(exception.getMessage());
Expand Down
56 changes: 36 additions & 20 deletions src/main/java/com/rogeriofrsouza/app/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import com.rogeriofrsouza.app.chess.ChessMatch;
import com.rogeriofrsouza.app.chess.ChessPiece;
import com.rogeriofrsouza.app.chess.ChessPosition;
import com.rogeriofrsouza.app.chess.Color;

import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class UI {

Expand All @@ -30,21 +28,18 @@ public void clearScreen() {
public void printMatch(ChessMatch chessMatch, List<ChessPiece> captured) {
printBoard(chessMatch.getPieces(), null);

List<ChessPiece> white =
captured.stream()
.filter(piece -> piece.getColor() == Color.WHITE)
.collect(Collectors.toList());
List<ChessPiece> white = captured.stream()
.filter(piece -> piece.getColor() == ChessPiece.Color.WHITE)
.toList();

List<ChessPiece> black =
captured.stream()
.filter(piece -> piece.getColor() == Color.BLACK)
.collect(Collectors.toList());
List<ChessPiece> black = captured.stream()
.filter(piece -> piece.getColor() == ChessPiece.Color.BLACK)
.toList();

System.out.println("\nCaptured pieces");

System.out.printf(
"White: %s%s%n%sBlack: %s%s%n%s",
ANSI_WHITE, white, ANSI_RESET, ANSI_YELLOW, black, ANSI_RESET);
System.out.printf("White: %s%s%n%sBlack: %s%s%n%s",
ANSI_WHITE, white, ANSI_RESET, ANSI_YELLOW, black, ANSI_RESET);

System.out.println("\nTurn: " + chessMatch.getTurn());

Expand All @@ -64,7 +59,7 @@ public void printBoard(ChessPiece[][] pieces, boolean[][] possibleMoves) {
StringBuilder stringBuilder = new StringBuilder();

for (int i = 0; i < pieces.length; i++) {
stringBuilder.append((8 - i) + " ");
stringBuilder.append((8 - i)).append(" ");

for (int j = 0; j < pieces[i].length; j++) {
if (possibleMoves != null && possibleMoves[i][j]) {
Expand All @@ -74,9 +69,10 @@ public void printBoard(ChessPiece[][] pieces, boolean[][] possibleMoves) {
if (pieces[i][j] == null) {
stringBuilder.append("-");
} else {
String color =
pieces[i][j].getColor() == Color.WHITE ? ANSI_WHITE : ANSI_YELLOW;
stringBuilder.append(color + pieces[i][j]);
String color = pieces[i][j].getColor() == ChessPiece.Color.WHITE
? ANSI_WHITE
: ANSI_YELLOW;
stringBuilder.append(color).append(pieces[i][j]);
}

stringBuilder.append(ANSI_RESET + " ");
Expand All @@ -86,20 +82,40 @@ public void printBoard(ChessPiece[][] pieces, boolean[][] possibleMoves) {
}

stringBuilder.append(" a b c d e f g h");
System.out.println(stringBuilder.toString());
System.out.println(stringBuilder);
}

public ChessPosition readChessPosition(Scanner scanner) {
private String readInput(Scanner scanner) {
String input = scanner.nextLine();

if (input == null || input.trim().isBlank()) {
throw new InputMismatchException("Error reading input: cannot be null or blank.");
}

return input.trim();
}

public ChessPosition readChessPosition(Scanner scanner) {
String input = readInput(scanner);

if (!input.matches("[a-h][1-8]")) {
throw new InputMismatchException(
"Error reading ChessPosition. Valid values are from a1 to h8.");
"Error reading ChessPosition. Valid values are from a1 to h8.");
}

char column = input.charAt(0);
int row = Character.getNumericValue(input.charAt(1));

return new ChessPosition(column, row);
}

public ChessPiece.Name readPromotedPiece(Scanner scanner) {
String input = readInput(scanner).substring(0, 1);

return ChessMatch.possiblePromotedPieces
.stream()
.filter(pieceName -> pieceName.getLetter().equalsIgnoreCase(input))
.findFirst()
.orElse(null);
}
}
36 changes: 11 additions & 25 deletions src/main/java/com/rogeriofrsouza/app/boardgame/Piece.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,31 @@
package com.rogeriofrsouza.app.boardgame;

import lombok.EqualsAndHashCode;
import lombok.Getter;

@Getter
@EqualsAndHashCode
public abstract class Piece {

// Não é ainda a posição do xadrez, é uma posição simples de matriz. Não deve ser visível na
// camada de xadrez
protected Position position;
private final Board board;

private Board board;

// A posição de uma peça recém criada será nula, pois ainda não foi colocada no tabuleiro
public Piece(Board board) {
protected Piece(Board board) {
this.board = board;
// position = null;
}

// Somente classes do mesmo pacote e subclasses podem acessar o Board
protected Board getBoard() {
return board;
}

// Não permitir que o Board seja alterado. Remover o setBoard()

public abstract boolean[][] possibleMoves();
public abstract boolean[][] computePossibleMoves();

/*
* Hook method: método que faz um gancho com a subclasse.
* Chama uma possível implementação de alguma subclasse concreta da classe Piece
*/
public boolean possibleMove(Position position) {
return possibleMoves()[position.getRow()][position.getColumn()];
public boolean isTargetPossibleMove(Position target) {
return computePossibleMoves()[target.getRow()][target.getColumn()];
}

public boolean isThereAnyPossibleMove() {
boolean[][] mat = possibleMoves();
boolean[][] possibleMoves = computePossibleMoves();

for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {
if (mat[i][j]) {
for (boolean[] row : possibleMoves) {
for (boolean move : row) {
if (move) {
return true;
}
}
Expand Down
Loading