Skip to content

Commit

Permalink
Fixes some Sonar complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed May 14, 2024
1 parent 4a82474 commit bf36ed4
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/fathzer/jchess/ai/JChessEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void logSearchEnd(Board<Move> board, SearchHistory<Move> result) {
}

public String toString(Collection<EvaluatedMove<Move>> moves) {
return moves.stream().map(em -> toString(em)).collect(Collectors.joining(", ", "[", "]"));
return moves.stream().map(this::toString).collect(Collectors.joining(", ", "[", "]"));
}

public String toString(EvaluatedMove<Move> ev) {
Expand Down Expand Up @@ -118,7 +118,7 @@ public void logMoveChosen(Board<Move> board, EvaluatedMove<Move> evaluatedMove)
} else {
log.info("Move chosen :{}", evaluatedMove.getContent().toString(board.getCoordinatesSystem()));
final List<Move> pv = evaluatedMove.getPrincipalVariation();
log.info("pv: {}", pv.stream().map(m -> m.toString(board.getCoordinatesSystem())).collect(Collectors.toList()));
log.info("pv: {}", pv.stream().map(m -> m.toString(board.getCoordinatesSystem())).toList());
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/fathzer/jchess/ai/debug/NegaMaxSpy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static com.fathzer.games.ai.experimental.KeyBasedNegaMaxSpyFilter.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.fathzer.games.ai.AlphaBetaState;
Expand Down Expand Up @@ -90,7 +89,6 @@ private List<String> getMoves(TreeSearchStateStack<Move, Board<Move>> state) {
.mapToObj(i->{
final Move mv = state.get(i).lastMove;
return mv==null ? "?"+i: mv.toString(cs);
})
.collect(Collectors.toList());
}).toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.fathzer.jchess.chessutils;

import java.util.stream.IntStream;

import com.fathzer.chess.utils.adapters.BoardExplorer;
import com.fathzer.chess.utils.adapters.BoardExplorerBuilder;
import com.fathzer.jchess.Board;
Expand All @@ -13,10 +11,4 @@ public interface JChessBoardExplorerBuilder extends BoardExplorerBuilder<Board<M
default BoardExplorer getExplorer(Board<Move> board) {
return new JChessBoardExplorer(board);
}

// @Override
// default IntStream getPieces(Board<Move> board) {
// // TODO Auto-generated method stub
// return BoardExplorerBuilder.super.getPieces(board);
// }
}
3 changes: 1 addition & 2 deletions src/main/java/com/fathzer/jchess/fen/FENParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.OptionalInt;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.fathzer.games.Color;
Expand Down Expand Up @@ -152,7 +151,7 @@ private Collection<Castling> getCastlings(String code) {
if ("-".equals(code)) {
return Collections.emptyList();
} else {
return code.chars().mapToObj(c -> toCastling((char)c)).collect(Collectors.toList());
return code.chars().mapToObj(c -> toCastling((char)c)).toList();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import com.fathzer.games.MoveGenerator.MoveConfidence;
Expand Down Expand Up @@ -41,7 +40,7 @@ public String get(Board<Move> board, Move move) {
// First, keep only moves with the right destination
// This list will allow us to check if the move is valid and if it needs disambiguation
final int to = move.getTo();
final List<Move> candidates = StreamSupport.stream(state.spliterator(),false).filter(m -> m.getTo()==to).collect(Collectors.toList());
final List<Move> candidates = StreamSupport.stream(state.spliterator(),false).filter(m -> m.getTo()==to).toList();
if (!checkValidMove(move, candidates)) {
throw new IllegalArgumentException("Move "+moveToString(move, board)+" is not valid");
}
Expand Down Expand Up @@ -114,7 +113,7 @@ private CharSequence encodeMove(Board<Move> board, Move move, List<Move> candida
private String getAmbiguitiesRemoval(Board<Move> board, Move move, List<Move> candidates) {
final int from = move.getFrom();
final PieceKind kind = board.getPiece(from).getKind();
final List<Move> ambiguities = candidates.stream().filter(m -> m.getFrom()!=from && kind==board.getPiece(m.getFrom()).getKind()).collect(Collectors.toList());
final List<Move> ambiguities = candidates.stream().filter(m -> m.getFrom()!=from && kind==board.getPiece(m.getFrom()).getKind()).toList();
if (ambiguities.isEmpty()) {
return "";
}
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/fathzer/jchess/ai/MinimaxEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -247,7 +246,7 @@ void bug20230911_chase() {
spy.searchedKey = -3019684505475777408L; //d7d8
spy.searchedKey = 1283331931822092560L; //h6h8
e = ai.getBestMoves(Collections.singletonList(h1h6),new SearchParameters(8)).getList().get(0);
System.out.println("pv="+tt.collectPV(board, h1h6, 8).stream().map(m->m.toString(cs)).collect(Collectors.toList()));
System.out.println("pv="+tt.collectPV(board, h1h6, 8).stream().map(m->m.toString(cs)).toList());
assertEquals(Type.WIN, e.getEvaluation().getType());
assertEquals(4, e.getEvaluation().getCountToEnd());
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/fathzer/jchess/generic/ChessBoardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ private List<Move> getMoves(Board<Move> board, String from) {

private List<Move> getMoves(CoordinatesSystem cs, List<Move> moves, String from) {
final int fromIndex = cs.getIndex(from);
return moves.stream().filter(m-> m.getFrom()==fromIndex).collect(Collectors.toList());
return moves.stream().filter(m-> m.getFrom()==fromIndex).toList();
}

private Set<String> getTo(CoordinatesSystem cs, List<Move> moves) {
Expand All @@ -323,7 +323,7 @@ void kingsMoveGenerationTest() {

// Can castle
List<Move> moves = FENUtils.from("r3kb1r/ppp2ppp/2nqb2n/P2p4/2P1p3/6R1/1PQPPPPP/1NB1KBNR b Kkq - 1 8").getMoves();
moves = moves.stream().filter(m->"e8".equals(cs.getAlgebraicNotation(m.getFrom()))).collect(Collectors.toList());
moves = moves.stream().filter(m->"e8".equals(cs.getAlgebraicNotation(m.getFrom()))).toList();
assertEquals(Set.of("c8","d8", "d7", "e7"), getTo(cs, moves));
assertEquals(4, moves.size(), asString(moves, board));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

Expand All @@ -16,7 +15,7 @@
class MovesBuilderTest {
private static List<Move> filterFrom(List<Move> moves, String coord, CoordinatesSystem cs) {
final int index = cs.getIndex(coord);
return moves.stream().filter(m -> m.getFrom()==index).collect(Collectors.toList());
return moves.stream().filter(m -> m.getFrom()==index).toList();
}

@Test
Expand Down

0 comments on commit bf36ed4

Please sign in to comment.