Skip to content

Commit

Permalink
Adding utility method to AIEngine for generating all _legal_ moves.
Browse files Browse the repository at this point in the history
  • Loading branch information
skytreader committed Jan 19, 2015
1 parent 243dc47 commit 4afb085
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Expand Up @@ -267,6 +267,10 @@ public Point[] getLastMove(){
return moveList.getLast();
}

public Board getBoard(){
return board;
}

public boolean isWhiteKingChecked(){
return whiteKingChecked;
}
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/net/skytreader/kode/chesstemplar/core/AIEngine.java
Expand Up @@ -2,8 +2,16 @@

import java.awt.Point;

import java.util.HashSet;
import java.util.Set;

import net.skytreader.kode.chesstemplar.Board;
import net.skytreader.kode.chesstemplar.GameArbiter;

import net.skytreader.kode.chesstemplar.exceptions.NotMeException;

import net.skytreader.kode.chesstemplar.pieces.ChessPiece;

public abstract class AIEngine{

private GameArbiter arbiter;
Expand All @@ -12,5 +20,42 @@ public AIEngine(GameArbiter arbiter){
this.arbiter = arbiter;
}

protected Set<Point[]> getAllLegalMoves(){
Set<Point[]> legalMoves = new HashSet<Point[]>();
Board b = arbiter.getBoard();
/*
TODO I'm doing this pattern a lot:
- get all the piece positions in the board
- for each piece position in the board, get their moves; this involves
repeated invocations of their position in the board.
Maybe I should refactor.
Moreover, this always needs me to catch NotMeExceptions even when I'm so
sure that it isn't going to happen this time.
*/
Set<Point> pieces = b.getPiecePositions();

try{
for(Point piecePos : pieces){
ChessPiece cp = b.getPieceAt(piecePos.x, piecePos.y);
Set<Point> pieceMoves = cp.getMoves(piecePos.x, piecePos.y, b);

for(Point terminalSquare : pieceMoves){
if(arbiter.requestMove(piecePos.x, piecePos.y, terminalSquare.x,
terminalSquare.y)){
Point[] pa = {piecePos, terminalSquare};
legalMoves.add(pa);
}
}
}
} catch(NotMeException nme){
nme.printStackTrace();
}

return legalMoves;
}

public abstract Point[] getMove();
}

0 comments on commit 4afb085

Please sign in to comment.