From 16377f7b7a792ced52157a79c7e4996699be790b Mon Sep 17 00:00:00 2001 From: jwb11006 Date: Sat, 30 Apr 2016 17:26:15 -0400 Subject: [PATCH] When jump sequence end, minimax now considers other player's moves --- src/controller/Game.java | 58 ++++++++++++++++--------------- src/controller/GameConstants.java | 2 +- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/controller/Game.java b/src/controller/Game.java index d3413f4..8f03922 100755 --- a/src/controller/Game.java +++ b/src/controller/Game.java @@ -20,7 +20,7 @@ public Game(Board board) { this.board = board; this.inJumpSequence = false; } - + /* Either the server or the user will request a move */ public void requestMove(Move move) { @@ -28,34 +28,34 @@ public void requestMove(Move move) { if (move.isJump() && !inJumpSequence) { inJumpSequence = true; } - + if (movePromotesPiece(move)) { inJumpSequence = false; } - + board.movePiece(move); gamePanel.movePiece(move); - + if (board.getMovesSinceCapture() > GameConstants.MAX_PASSIVE_MOVES) { gamePanel.displayMessage("It's a tie! Be more aggressive next time."); // Disable UI here } - + /* If the piece that jumped has no more jumps */ if (move.isJump() && getAvailableMoves(move.destination).isEmpty()) inJumpSequence = false; - + /* If the game is not in the middle of a jump sequence, move the thunk */ if (!inJumpSequence) { Move thunkMove = getMinimaxMove(GameConstants.MAX_SEARCH_DEPTH, inJumpSequence); if (thunkMove != null) { if (thunkMove.isJump()) inJumpSequence = true; - + if (movePromotesPiece(thunkMove)) { inJumpSequence = false; } - + board.movePiece(thunkMove); gamePanel.movePiece(thunkMove); @@ -77,13 +77,13 @@ public void requestMove(Move move) { } } } - + public boolean movePromotesPiece(Move move) { return board.getPiece(move.source).getType() != Type.KING && board.isPromotionLocation(move.destination); } - + public Move getThunkMove() { ArrayList availableMoves; @@ -93,7 +93,7 @@ public Move getThunkMove() { } else { availableMoves = board.generateAllMoves(GameConstants.THUNK_COLOR); } - + if (!availableMoves.isEmpty()) { /* Just take the first move we see */ Move moveChoice = availableMoves.get(0); @@ -112,7 +112,7 @@ public Move getMinimaxMove(int depth, boolean inJumpSequence) { /* Generate the frontier only for the piece that just moves */ boardFrontier = board.generateJumpFrontierForPiece(board.getLastPieceMoved()); moveFrontier = board.generateJumpMovesForPiece(board.getLastPieceMoved()); - + /* If we can't jump anymore, we can't make a move */ if (boardFrontier.isEmpty()) { return null; @@ -123,7 +123,7 @@ public Move getMinimaxMove(int depth, boolean inJumpSequence) { boardFrontier = board.generateFrontier(GameConstants.THUNK_COLOR); moveFrontier = board.generateAllMoves(GameConstants.THUNK_COLOR); } - + Color nextColor; /* Determine the next color to move */ if (inJumpSequence) { @@ -154,39 +154,41 @@ public Move getMinimaxMove(int depth, boolean inJumpSequence) { return bestMove; } - + public 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); + + if (depth == 0) { + return b.getHeuristic(otherColor); + } + if (inJumpSequence) { /* Generate the frontier only for the piece that just moved */ boardFrontier = b.generateJumpFrontierForPiece(b.getLastPieceMoved()); - + /* If we can't jump anymore, get out of the jump sequence */ if (boardFrontier.isEmpty()) { - inJumpSequence = false; + return getMinimaxScore(otherColor, b, depth-1, inJumpSequence); } } else { /* Generate the frontier for all pieces */ boardFrontier = b.generateFrontier(color); } - + /* If we have reached the maximum depth or an end state for the game */ if (depth == 0 || b.getBlackPieces() == 0 || b.getWhitePieces() == 0 || boardFrontier.size() == 0) { - Color otherColor = (color == Color.BLACK ? Color.WHITE : Color.BLACK); return b.getHeuristic(otherColor); } - + Color nextColor; /* Determine the next color to move */ if (inJumpSequence) { nextColor = color; - } else if (GameConstants.THUNK_COLOR == Color.BLACK) { - nextColor = Color.WHITE; } else { - nextColor = Color.BLACK; + nextColor = otherColor; } for (Board board : boardFrontier) { @@ -204,12 +206,12 @@ public int getMinimaxScore(Color color, Board b, int depth, boolean inJumpSequen } } - + public void notifyClientWin() { gamePanel.displayMessage("Game over. Client has won!"); } - + public ArrayList getAvailableMoves(Location source) { if (inJumpSequence) { return board.generateJumpMovesForPiece(board.getPiece(source)); @@ -217,15 +219,15 @@ public ArrayList getAvailableMoves(Location source) { return board.generateMovesForPiece(board.getPiece(source)); } } - + public ArrayList getAllAvailableJumpMoves(Color player) { return board.generateAllJumpMoves(player); } - + public ArrayList getAllAvailableMoves(Color player) { return board.generateAllMoves(player); } - + public void setGamePanel(GamePanel panel) { this.gamePanel = panel; } diff --git a/src/controller/GameConstants.java b/src/controller/GameConstants.java index d308d43..989e7fe 100644 --- a/src/controller/GameConstants.java +++ b/src/controller/GameConstants.java @@ -8,5 +8,5 @@ public class GameConstants { public static final Color THUNK_COLOR = Color.WHITE; public static final Color USER_COLOR = Color.BLACK; public static final int MAX_PASSIVE_MOVES = 50; - public static final int MAX_SEARCH_DEPTH = 12; + public static final int MAX_SEARCH_DEPTH = 5; }