Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed bug with jump sequence for user. A random move is now chosen wh…
…en all moves return the same score
  • Loading branch information
john committed May 1, 2016
1 parent d30ebd6 commit 09aecf4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
32 changes: 28 additions & 4 deletions src/controller/Game.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Move> availableMoves;

if (inJumpSequence) {
Expand All @@ -103,7 +104,7 @@ public class Game {
}
}

public Move getMinimaxMove(int depth, boolean inJumpSequence) {
private Move getMinimaxMove(int depth, boolean inJumpSequence) {
ArrayList<Board> boardFrontier = null;
ArrayList<Move> moveFrontier = null;
ArrayList<Integer> moveScores = new ArrayList<Integer>();
Expand Down Expand Up @@ -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<Board> boardFrontier;
ArrayList<Integer> moveScores = new ArrayList<Integer>();
Color otherColor = (color == Color.BLACK ? Color.WHITE : Color.BLACK);
Expand Down Expand Up @@ -210,6 +215,25 @@ public class Game {
}

}

private Move getBestOfSimilarMoves(ArrayList<Move> moves) {
ArrayList<Move> regularMoves = new ArrayList<Move>();

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!");
Expand Down
2 changes: 1 addition & 1 deletion src/view/GameEventListener.java
Expand Up @@ -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();
Expand Down

0 comments on commit 09aecf4

Please sign in to comment.