diff --git a/src/controller/Game.java b/src/controller/Game.java index 6340558..cdb354f 100755 --- a/src/controller/Game.java +++ b/src/controller/Game.java @@ -42,14 +42,6 @@ public void requestMove(Move move) { gamePanel.displayMessage("It's a tie! Be more aggressive next time."); // Disable UI here } - /* - try { - Thread.sleep(500); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - */ /* If the piece that jumped has no more jumps */ if (move.isJump() && getAvailableMoves(move.destination).isEmpty()) @@ -70,8 +62,8 @@ public void requestMove(Move move) { gamePanel.movePiece(thunkMove); while (inJumpSequence) { - thunkMove = getThunkMove(); - + thunkMove = getMinimaxMove(GameConstants.MAX_SEARCH_DEPTH); + System.out.println("REPEATING MOVE"); if (thunkMove != null) { if (movePromotesPiece(thunkMove)) { inJumpSequence = false; @@ -113,57 +105,68 @@ public Move getThunkMove() { } } - public Move getMinimaxMove(int depth) { - ArrayList boardFrontier = board.generateFrontier(GameConstants.THUNK_COLOR); - ArrayList moveFrontier = board.generateAllMoves(GameConstants.THUNK_COLOR); - ArrayList moveScores = new ArrayList(); - - Color otherColor = GameConstants.THUNK_COLOR == Color.BLACK ? - Color.WHITE : Color.BLACK; - // Recurse for each one here - for (Board board : boardFrontier) { - moveScores.add(this.getMinimaxScore(otherColor, board, depth)); - } - - int maxScore = Integer.MIN_VALUE; + public Move getMinimaxMove(int depth, boolean currentlyIsInJumpSequence) { + // need to store the jump sequence at each call stack Move bestMove = null; - - for (int i = 0; i < moveScores.size(); ++i) { - if (moveScores.get(i) > maxScore) { - bestMove = moveFrontier.get(i); - maxScore = moveScores.get(i); + if (inJumpSequence) { + ArrayList boardFrontier = board.generateJumpFrontierForPiece(board.getLastPieceMoved()); + ArrayList moveFrontier = board.generateJumpMovesForPiece(board.getLastPieceMoved()); + ArrayList moveScores = new ArrayList(); + + Color otherColor = + } + else { + ArrayList boardFrontier = board.generateFrontier(GameConstants.THUNK_COLOR); + ArrayList moveFrontier = board.generateAllMoves(GameConstants.THUNK_COLOR); + ArrayList moveScores = new ArrayList(); + + Color otherColor = GameConstants.THUNK_COLOR == Color.BLACK ? + Color.WHITE : Color.BLACK; + // Recurse for each one here + for (Board b : boardFrontier) { + moveScores.add(this.getMinimaxScore(otherColor, b, depth)); + } + + int maxScore = Integer.MIN_VALUE; + + + for (int i = 0; i < moveScores.size(); ++i) { + if (moveScores.get(i) > maxScore) { + bestMove = moveFrontier.get(i); + maxScore = moveScores.get(i); + } } - System.out.println("Score[" + i + "] = " + moveScores.get(i)); } - System.out.println("---"); - return bestMove; } public int getMinimaxScore(Color color, Board b, int depth) { - if (depth == 0 || b.getBlackPieces() == 0 || b.getWhitePieces() == 0) { - Color otherColor = color == Color.BLACK ? Color.WHITE : Color.BLACK; - return b.getHeuristic(otherColor); - } - ArrayList boardFrontier = board.generateFrontier(color); - - ArrayList moveScores = new ArrayList(); - - for (Board board : boardFrontier) { - Color nextColor = color == Color.BLACK ? Color.WHITE : Color.BLACK; - moveScores.add(getMinimaxScore(nextColor, board, depth - 1)); - } - for (int score : moveScores) { - System.out.println(score); - } - System.out.println("---"); - if (color == GameConstants.THUNK_COLOR) { - // Maximize - return Collections.max(moveScores); + if (inJumpSequence) { + } else { - // Minimize - return Collections.min(moveScores); + ArrayList boardFrontier = b.generateFrontier(color); + 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); + } + + ArrayList moveScores = new ArrayList(); + + for (Board board : boardFrontier) { + Color nextColor = color == Color.BLACK ? Color.WHITE : Color.BLACK; + moveScores.add(getMinimaxScore(nextColor, board, depth - 1)); + } + + if (color == GameConstants.THUNK_COLOR) { + // Maximize + return Collections.max(moveScores); + } + else { + // Minimize + return Collections.min(moveScores); + } } } diff --git a/src/model/Board.java b/src/model/Board.java index 852886a..96f74c5 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -53,8 +53,8 @@ public Board(Board other) { movesSinceCapture = other.getMovesSinceCapture(); lastPieceMoved = other.getLastPieceMoved(); lastMove = other.getLastMove(); - blackPieces = other.blackPieces; - whitePieces = other.whitePieces; + blackPieces = other.getBlackPieces(); + whitePieces = other.getWhitePieces(); } /** @@ -98,7 +98,12 @@ public void movePiece(Move move) { Color color_removed = representation[monkeyRow][monkeyCol].getColor(); - if (color_removed == Color.BLACK) --blackPieces; else --whitePieces; + if (color_removed == Color.BLACK) { + --blackPieces; + } + else { + --whitePieces; + } /* Remove the piece being jumped ("monkey in the middle") */ representation[monkeyRow][monkeyCol] = null; @@ -135,7 +140,7 @@ public void movePiece(Move move) { */ public ArrayList generateFrontier(Color color) { ArrayList from_jumps = generateFrontierFromJumps(color); - if(from_jumps.isEmpty()) { + if (from_jumps.isEmpty()) { return generateFrontierFromRegularMoves(color); } return from_jumps; @@ -231,6 +236,17 @@ public ArrayList generateRegularMovesForPiece(Piece p) { return avail_moves; } + public ArrayList generateJumpFrontierForPiece(Piece p) { + ArrayList frontier = new ArrayList(); + ArrayList moves = generateJumpMovesForPiece(p); + for (Move move : moves) { + Board board = new Board(this); + board.movePiece(move); + frontier.add(board); + } + return frontier; + } + /** * Returns the possible jumps a piece can take. * @param color @@ -377,7 +393,9 @@ public boolean isPromotionLocation(Location location) { } public int getHeuristic(Color color) { - return color == Color.BLACK ? this.blackPieces : this.whitePieces; + return color == Color.BLACK ? + this.blackPieces - this.whitePieces : + this.whitePieces - this.blackPieces; } public Piece getPiece(Location location) {