chessai

college code for ai playing chess in java

git clone https://9o.is/git/chessai.git

commit 1cfae43b75fe87d4453ba39f37b6795e270017e9
parent e4df84ff35d8e790dfc16a9543f33f76bcda6cbf
Author: Jul <jul@9o.is>
Date:   Sat,  1 Dec 2012 16:41:14 -0500

Implemented legalSkip test in ChessRules.

Diffstat:
Msrc/chess/ChessRules.java | 52++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 48 insertions(+), 4 deletions(-)

diff --git a/src/chess/ChessRules.java b/src/chess/ChessRules.java @@ -1,6 +1,7 @@ package chess; import chess.piece.ChessPiece; +import chess.piece.ChessType; /** * Check @@ -52,7 +53,7 @@ public class ChessRules { this.src = src; this.dest = dest; - return srcHasPiece() && uninhabitedSpace(); + return srcHasPiece() && uninhabitedSpace() && legalSkip(); } /* @@ -61,7 +62,7 @@ public class ChessRules { private boolean srcHasPiece() { final ChessPiece srcPiece = ChessPiece.find(state.get(src)); if(srcPiece == ChessPiece.NA) { - System.err.println(failurePrefix+"Empty test"); + System.err.println(failurePrefix+"srcHasPiece test"); return false; } else return true; @@ -76,11 +77,54 @@ public class ChessRules { // are the source and destination pieces same color? if(srcPiece.getColor() == destPiece.getColor()) { - System.err.println(failurePrefix+"Uninhabited Space"); + System.err.println(failurePrefix+"uninhabitedSpace test"); return false; } else return true; } - //private boolean + /* + * Checks if source piece skips over other pieces when moving. + */ + private boolean legalSkip() { + final ChessPiece srcPiece = ChessPiece.find(state.get(src)); + + // Knight is an exception. + if(srcPiece.getType() == ChessType.KNIGHT) + return true; + + final int hor = dest.getFile() - src.getFile(); + final int ver = dest.getRank() - src.getRank(); + + int rank = dest.getRank(); + int file = dest.getFile(); + while (src.getFile() != file || src.getRank() != rank) { + try { + final byte pieceValue = + state.get(new ChessCoordinate((char)file, rank)); + ChessPiece check = ChessPiece.find(pieceValue); + + if(check != ChessPiece.NA) { + System.err.println(failurePrefix+"legalSkip test"); + return false; + } + + } catch (Exception e) { + System.err.println(failurePrefix+ + "Legal Skip method generated illegal chess"+ + " coordinate. This should not happen. Weird"); + e.printStackTrace(); + } + + if(hor != 0) { + if(hor > 0) file--; + else file++; + } + if(ver != 0) { + if(ver > 0) rank--; + else rank++; + } + } + return true; + } }