From 09aecf4b7b62c4255bf10bb73c0a1b131d541302 Mon Sep 17 00:00:00 2001 From: John Bojorquez Date: Sat, 30 Apr 2016 21:10:40 -0400 Subject: [PATCH] Fixed bug with jump sequence for user. A random move is now chosen when all moves return the same score --- src/controller/Game.java | 32 ++++++++++++++++++++++++++++---- src/view/GameEventListener.java | 2 +- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/controller/Game.java b/src/controller/Game.java index d3413b9..1c23ae5 100755 --- a/src/controller/Game.java +++ b/src/controller/Game.java @@ -2,6 +2,7 @@ package controller; import java.util.ArrayList; import java.util.Collections; +import java.util.concurrent.ThreadLocalRandom; import model.Board; import model.Color; @@ -78,13 +79,13 @@ public class Game { } } - public boolean movePromotesPiece(Move move) { + private boolean movePromotesPiece(Move move) { return board.getPiece(move.source).getType() != Type.KING && board.isPromotionLocation(move.destination); } - public Move getThunkMove() { + private Move getThunkMove() { ArrayList availableMoves; if (inJumpSequence) { @@ -103,7 +104,7 @@ public class Game { } } - public Move getMinimaxMove(int depth, boolean inJumpSequence) { + private Move getMinimaxMove(int depth, boolean inJumpSequence) { ArrayList boardFrontier = null; ArrayList moveFrontier = null; ArrayList moveScores = new ArrayList(); @@ -154,12 +155,16 @@ public class Game { System.out.println(moveFrontier.get(i) + " --> " + moveScores.get(i)); } + /* All moves have the same outcome */ + if (!moveScores.isEmpty() && maxScore == Collections.min(moveScores)) { + return getBestOfSimilarMoves(moveFrontier); + } System.out.println("Choosing: " + bestMove); return bestMove; } - public int getMinimaxScore(Color color, Board b, int depth, boolean inJumpSequence) { + private int getMinimaxScore(Color color, Board b, int depth, boolean inJumpSequence) { ArrayList boardFrontier; ArrayList moveScores = new ArrayList(); Color otherColor = (color == Color.BLACK ? Color.WHITE : Color.BLACK); @@ -210,6 +215,25 @@ public class Game { } } + + private Move getBestOfSimilarMoves(ArrayList moves) { + ArrayList regularMoves = new ArrayList(); + + for (Move move : moves) { + if (!move.isJump()) { + regularMoves.add(move); + } + } + + /* We prefer to advance regular checkers over kings + * if all moves have the same outcome + */ + if (!regularMoves.isEmpty()) { + return regularMoves.get(ThreadLocalRandom.current().nextInt(regularMoves.size())); + } + + return moves.get(ThreadLocalRandom.current().nextInt(moves.size())); + } public void notifyClientWin() { gamePanel.displayMessage("Game over. Client has won!"); diff --git a/src/view/GameEventListener.java b/src/view/GameEventListener.java index 47da3e8..3db9013 100644 --- a/src/view/GameEventListener.java +++ b/src/view/GameEventListener.java @@ -44,7 +44,7 @@ public class GameEventListener implements MouseListener, KeyListener, ActionList if (square.isSelected()) gamePanel.highlightValidDestinations(square.getCellLocation()); gamePanel.updateMoveMessage(); - } else if (square.isValid()) { + } else if (!square.hasPiece() && square.isValid()) { gamePanel.setMoveDestination(square); gamePanel.moveSelectedPiece(); gamePanel.updateMoveMessage();