Skip to content

Commit

Permalink
Made modifications to minimax algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
john committed Apr 30, 2016
1 parent 0d1369f commit 914d373
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 57 deletions.
107 changes: 55 additions & 52 deletions src/controller/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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;
Expand Down Expand Up @@ -113,57 +105,68 @@ public Move getThunkMove() {
}
}

public Move getMinimaxMove(int depth) {
ArrayList<Board> boardFrontier = board.generateFrontier(GameConstants.THUNK_COLOR);
ArrayList<Move> moveFrontier = board.generateAllMoves(GameConstants.THUNK_COLOR);
ArrayList<Integer> moveScores = new ArrayList<Integer>();

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<Board> boardFrontier = board.generateJumpFrontierForPiece(board.getLastPieceMoved());
ArrayList<Move> moveFrontier = board.generateJumpMovesForPiece(board.getLastPieceMoved());
ArrayList<Integer> moveScores = new ArrayList<Integer>();

Color otherColor =
}
else {
ArrayList<Board> boardFrontier = board.generateFrontier(GameConstants.THUNK_COLOR);
ArrayList<Move> moveFrontier = board.generateAllMoves(GameConstants.THUNK_COLOR);
ArrayList<Integer> moveScores = new ArrayList<Integer>();

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<Board> boardFrontier = board.generateFrontier(color);

ArrayList<Integer> moveScores = new ArrayList<Integer>();

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<Board> 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<Integer> moveScores = new ArrayList<Integer>();

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);
}
}
}

Expand Down
28 changes: 23 additions & 5 deletions src/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -135,7 +140,7 @@ public void movePiece(Move move) {
*/
public ArrayList<Board> generateFrontier(Color color) {
ArrayList<Board> from_jumps = generateFrontierFromJumps(color);
if(from_jumps.isEmpty()) {
if (from_jumps.isEmpty()) {
return generateFrontierFromRegularMoves(color);
}
return from_jumps;
Expand Down Expand Up @@ -231,6 +236,17 @@ public ArrayList<Move> generateRegularMovesForPiece(Piece p) {
return avail_moves;
}

public ArrayList<Board> generateJumpFrontierForPiece(Piece p) {
ArrayList<Board> frontier = new ArrayList<Board>();
ArrayList<Move> 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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 914d373

Please sign in to comment.