chessai
college code for ai playing chess in java
git clone https://9o.is/git/chessai.git
commit 044b7a0ebadc99641601159e1078d7f9087d1a5e parent da83b5272947e9d22b23072ae2765d5bbd07d60f Author: Jul <jul@9o.is> Date: Sat, 1 Dec 2012 18:19:19 -0500 Implemented insideBoard test in ChessRules and other stuff. Diffstat:
| M | src/chess/ChessBoard.java | | | 45 | +++++++++++++++++++++++++++++++++++---------- |
| M | src/chess/ChessGame.java | | | 12 | ++++-------- |
| M | src/chess/ChessRules.java | | | 17 | +++++++++++++++++ |
3 files changed, 56 insertions(+), 18 deletions(-)
diff --git a/src/chess/ChessBoard.java b/src/chess/ChessBoard.java @@ -23,7 +23,7 @@ public class ChessBoard { } //CHECK - public void init() throws Exception { + public void init() { board.set(new ChessCoordinate('a',8), ChessPiece.BLACK_ROOK); board.set(new ChessCoordinate('b',8), ChessPiece.BLACK_KNIGHT); board.set(new ChessCoordinate('c',8), ChessPiece.BLACK_BISHOP); @@ -53,7 +53,7 @@ public class ChessBoard { public void move( ChessCoordinate coor, ChessMove move, - int steps) throws Exception { + int steps) { move(coor, getCoordinate(coor, move, steps)); } @@ -63,7 +63,7 @@ public class ChessBoard { ChessMove firstMove, int steps1, ChessMove secondMove, - int steps2) throws Exception { + int steps2) { ChessCoordinate coor1 = getCoordinate(coor, firstMove, steps1); ChessCoordinate coor2 = getCoordinate(coor1, secondMove, steps2); move(coor, coor2); @@ -76,13 +76,13 @@ public class ChessBoard { board.move(coor, coor1); moves++; } else - System.err.println("Move: "+coor+coor1); + System.err.println("Invalid Move: "+coor+coor1); } //CHECK public String getState() { byte[][] state = board.getState(); - String result = "Move: "+moves+"\n"; + String result = "Moves: "+moves+"\n"; for(int i=0; i< ChessCoordinate.MAX; i++) { for(int j=0; j< ChessCoordinate.MAX; j++) @@ -96,7 +96,7 @@ public class ChessBoard { private ChessCoordinate getCoordinate( ChessCoordinate coor, ChessMove move, - int steps) throws Exception { + int steps) { int x = coor.getFileNumber(); int y = coor.getRank(); @@ -105,15 +105,40 @@ public class ChessBoard { else if(move == ChessMove.BACK) y -= steps; else if(move == ChessMove.LEFT) x -= steps; else if(move == ChessMove.RIGHT) x += steps; - } else if(player.getColor() == ChessColor.BLACK) { + + else if(move == ChessMove.FORWARD_LEFT) { + x -= steps; + y += steps; + } else if(move == ChessMove.FORWARD_RIGHT) { + x += steps; + y += steps; + } else if(move == ChessMove.BACK_LEFT) { + x -= steps; + y -= steps; + } else if(move == ChessMove.BACK_RIGHT) { + x += steps; + y -= steps; + } + } else { if(move == ChessMove.FORWARD) y -= steps; else if(move == ChessMove.BACK) y += steps; else if(move == ChessMove.LEFT) x += steps; else if(move == ChessMove.RIGHT) x -= steps; - } else - throw new Exception( - this.getClass()+": player is invalid color."); + else if(move == ChessMove.FORWARD_LEFT) { + x += steps; + y -= steps; + } else if(move == ChessMove.FORWARD_RIGHT) { + x -= steps; + y -= steps; + } else if(move == ChessMove.BACK_LEFT) { + x += steps; + y += steps; + } else if(move == ChessMove.BACK_RIGHT) { + x -= steps; + y += steps; + } + } return new ChessCoordinate(x,y); } } diff --git a/src/chess/ChessGame.java b/src/chess/ChessGame.java @@ -16,14 +16,10 @@ public class ChessGame { Player player = new Player(ChessColor.WHITE); ChessBoard game = new ChessBoard(player); - try { - game.init(); - game.move(new ChessCoordinate('d',2), ChessMove.FORWARD, 1); - game.move(new ChessCoordinate('b',1), ChessMove.FORWARD, 2, ChessMove.RIGHT, 1); - } catch (Exception e) { - e.printStackTrace(); - } - + game.init(); + game.move(new ChessCoordinate('c',2), ChessMove.FORWARD, 1); System.out.println(game.getState()); + + } } diff --git a/src/chess/ChessRules.java b/src/chess/ChessRules.java @@ -55,6 +55,7 @@ public class ChessRules { return pieceMoves() && srcHasPiece() && + insideBoard() && uninhabitedSpace() && legalSkip(); } @@ -83,6 +84,22 @@ public class ChessRules { } /* + * Checks if the destination coordinate is inside the chess + * board. + */ + private boolean insideBoard() { + final char file = dest.getFile(); + final int rank = dest.getRank(); + + if(file > 'h' || file < 'a' || + rank > ChessCoordinate.MAX || rank < 1) { + System.err.println(failurePrefix+"insideBoard test"); + return false; + } + return true; + } + + /* * Checks if player moved a chess piece onto one of his own pieces. */ private boolean uninhabitedSpace() {