Skip to content

Commit

Permalink
Improve computer hard skill
Browse files Browse the repository at this point in the history
  • Loading branch information
sloenthran committed May 17, 2019
1 parent b9a096a commit 6a868eb
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/main/java/pl/nogacz/chess/application/Computer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Computer {

private Set<Coordinates> possibleKick = new HashSet<>();
private Set<Coordinates> possibleMoves = new HashSet<>();
private Set<Coordinates> possibleKickAndNotIsEnemyKickMe = new HashSet<>();

private BoardPoint boardPoint = new BoardPoint();

Expand Down Expand Up @@ -81,6 +82,7 @@ public void getGameData() {

possibleMoves.clear();
possibleKick.clear();
possibleKickAndNotIsEnemyKickMe.clear();

for (Map.Entry<Coordinates, PawnClass> entry : cacheBoard.entrySet()) {
if (entry.getValue().getColor().isBlack()) {
Expand Down Expand Up @@ -216,9 +218,15 @@ private Coordinates chooseMoveHard(Coordinates coordinates) {
possibleMove.addAll(moves.getPossibleMoves());
possibleMove.addAll(moves.getPossibleKick());

Set<Coordinates> test = getListWithOnlyMinNumber(possibleMove, pawn);
Set<Coordinates> listWithOnlyMinNumber = getListWithOnlyMinNumber(possibleMove, pawn);

return selectRandom(test);
listWithOnlyMinNumber.forEach(entry -> checkEnemyKickField(entry, pawn));

if(possibleKickAndNotIsEnemyKickMe.size() > 0) {
return selectRandom(possibleKickAndNotIsEnemyKickMe);
} else {
return selectRandom(listWithOnlyMinNumber);
}
}

private int getMinNumber(Set<Coordinates> list, PawnClass actualPawn) {
Expand Down Expand Up @@ -266,6 +274,29 @@ private Set<Coordinates> getListWithOnlyMinNumber(Set<Coordinates> list, PawnCla
return returnList;
}

private void checkEnemyKickField(Coordinates coordinates, PawnClass actualPawn) {
PawnClass oldPawn = Board.addPawnWithoutDesign(coordinates, actualPawn);

Set<Coordinates> possibleEnemyKick = new HashSet<>();

for (Map.Entry<Coordinates, PawnClass> entry : Board.getBoard().entrySet()) {
if (!Board.isThisSameColor(entry.getKey(), actualPawn.getColor()) && !entry.getValue().getPawn().isKing()) {
PawnMoves moves = new PawnMoves(entry.getValue(), entry.getKey());
possibleEnemyKick.addAll(moves.getPossibleKick());
}
}

Board.removePawnWithoutDesign(coordinates);

if(oldPawn != null) {
Board.addPawnWithoutDesign(coordinates, oldPawn);
}

if(!possibleEnemyKick.contains(coordinates)) {
possibleKickAndNotIsEnemyKickMe.add(coordinates);
}
}

public Coordinates selectRandom(Set<Coordinates> list) {
Object[] object = list.toArray();
return (Coordinates) object[random.nextInt(object.length)];
Expand Down

0 comments on commit 6a868eb

Please sign in to comment.