From 6a868eba27fd6b647eb547cc7c88ffb38972c237 Mon Sep 17 00:00:00 2001 From: Dawid Nogacz Date: Fri, 17 May 2019 20:30:05 +0200 Subject: [PATCH] Improve computer hard skill --- .../pl/nogacz/chess/application/Computer.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/pl/nogacz/chess/application/Computer.java b/src/main/java/pl/nogacz/chess/application/Computer.java index 649615f..4660389 100644 --- a/src/main/java/pl/nogacz/chess/application/Computer.java +++ b/src/main/java/pl/nogacz/chess/application/Computer.java @@ -22,6 +22,7 @@ public class Computer { private Set possibleKick = new HashSet<>(); private Set possibleMoves = new HashSet<>(); + private Set possibleKickAndNotIsEnemyKickMe = new HashSet<>(); private BoardPoint boardPoint = new BoardPoint(); @@ -81,6 +82,7 @@ public void getGameData() { possibleMoves.clear(); possibleKick.clear(); + possibleKickAndNotIsEnemyKickMe.clear(); for (Map.Entry entry : cacheBoard.entrySet()) { if (entry.getValue().getColor().isBlack()) { @@ -216,9 +218,15 @@ private Coordinates chooseMoveHard(Coordinates coordinates) { possibleMove.addAll(moves.getPossibleMoves()); possibleMove.addAll(moves.getPossibleKick()); - Set test = getListWithOnlyMinNumber(possibleMove, pawn); + Set 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 list, PawnClass actualPawn) { @@ -266,6 +274,29 @@ private Set getListWithOnlyMinNumber(Set list, PawnCla return returnList; } + private void checkEnemyKickField(Coordinates coordinates, PawnClass actualPawn) { + PawnClass oldPawn = Board.addPawnWithoutDesign(coordinates, actualPawn); + + Set possibleEnemyKick = new HashSet<>(); + + for (Map.Entry 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 list) { Object[] object = list.toArray(); return (Coordinates) object[random.nextInt(object.length)];