chessai

college code for ai playing chess in java

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

commit 93b801f6c308ecc5e6ca2cb5e2dac66ebb7c9bb1
parent 1bad212500c0a722f31f595db978913d8adb04a7
Author: Jul <jul@9o.is>
Date:   Fri,  7 Dec 2012 00:21:55 -0500

Created copy constructor for ChessBoard and extended ChessState to it.

Diffstat:
Msrc/main/java/chess/ChessBoard.java | 60+++++++++++++++++++++++++++++-------------------------------
Msrc/main/java/chess/ChessGame.java | 5+++--
Msrc/main/java/chess/ChessMove.java | 28----------------------------
Msrc/main/java/chess/ChessPlayer.java | 50+++++++++++++++++++++++++++++++++++++++-----------
Msrc/main/java/chess/ChessRules.java | 1+
Msrc/main/java/chess/ChessState.java | 16+++++++++++-----
6 files changed, 83 insertions(+), 77 deletions(-)

diff --git a/src/main/java/chess/ChessBoard.java b/src/main/java/chess/ChessBoard.java @@ -9,45 +9,48 @@ import java.util.List; /** * */ -public class ChessBoard { - private ChessState state; +public class ChessBoard extends ChessState implements Cloneable { public ChessBoard() { - state = new ChessState(); + super(); init(); } + public ChessBoard(ChessBoard chessBoard) { + super(chessBoard); + } + //CHECK public void init() { - state.set(new ChessCoordinate('a',8), ChessPiece.BLACK_ROOK); - state.set(new ChessCoordinate('b',8), ChessPiece.BLACK_KNIGHT); - state.set(new ChessCoordinate('c',8), ChessPiece.BLACK_BISHOP); - state.set(new ChessCoordinate('d',8), ChessPiece.BLACK_QUEEN); - state.set(new ChessCoordinate('e',8), ChessPiece.BLACK_KING); - state.set(new ChessCoordinate('f',8), ChessPiece.BLACK_BISHOP); - state.set(new ChessCoordinate('g',8), ChessPiece.BLACK_KNIGHT); - state.set(new ChessCoordinate('h',8), ChessPiece.BLACK_ROOK); + set(new ChessCoordinate('a', 8), ChessPiece.BLACK_ROOK); + set(new ChessCoordinate('b', 8), ChessPiece.BLACK_KNIGHT); + set(new ChessCoordinate('c', 8), ChessPiece.BLACK_BISHOP); + set(new ChessCoordinate('d', 8), ChessPiece.BLACK_QUEEN); + set(new ChessCoordinate('e', 8), ChessPiece.BLACK_KING); + set(new ChessCoordinate('f', 8), ChessPiece.BLACK_BISHOP); + set(new ChessCoordinate('g', 8), ChessPiece.BLACK_KNIGHT); + set(new ChessCoordinate('h', 8), ChessPiece.BLACK_ROOK); for(int i=1; i<= ChessCoordinate.MAX; i++) - state.set(new ChessCoordinate(i,7), ChessPiece.BLACK_PAWN); + set(new ChessCoordinate(i,7), ChessPiece.BLACK_PAWN); - state.set(new ChessCoordinate('a',1), ChessPiece.WHITE_ROOK); - state.set(new ChessCoordinate('b',1), ChessPiece.WHITE_KNIGHT); - state.set(new ChessCoordinate('c',1), ChessPiece.WHITE_BISHOP); - state.set(new ChessCoordinate('d',1), ChessPiece.WHITE_QUEEN); - state.set(new ChessCoordinate('e',1), ChessPiece.WHITE_KING); - state.set(new ChessCoordinate('f',1), ChessPiece.WHITE_BISHOP); - state.set(new ChessCoordinate('g',1), ChessPiece.WHITE_KNIGHT); - state.set(new ChessCoordinate('h',1), ChessPiece.WHITE_ROOK); + set(new ChessCoordinate('a',1), ChessPiece.WHITE_ROOK); + set(new ChessCoordinate('b', 1), ChessPiece.WHITE_KNIGHT); + set(new ChessCoordinate('c', 1), ChessPiece.WHITE_BISHOP); + set(new ChessCoordinate('d', 1), ChessPiece.WHITE_QUEEN); + set(new ChessCoordinate('e', 1), ChessPiece.WHITE_KING); + set(new ChessCoordinate('f', 1), ChessPiece.WHITE_BISHOP); + set(new ChessCoordinate('g', 1), ChessPiece.WHITE_KNIGHT); + set(new ChessCoordinate('h', 1), ChessPiece.WHITE_ROOK); for(int i=1; i<= ChessCoordinate.MAX; i++) - state.set(new ChessCoordinate(i,2), ChessPiece.WHITE_PAWN); + set(new ChessCoordinate(i,2), ChessPiece.WHITE_PAWN); } public boolean move(ChessMove move) { ChessCoordinate src = move.getCoordinate(); - return move(src, src.getCoordinate(state, move)); + return move(src, src.getCoordinate(this, move)); } //CHECK public boolean move( @@ -55,19 +58,19 @@ public class ChessBoard { ChessDirection direction, int steps) { ChessMove move = new ChessMove(src, direction, steps); - return move(src, src.getCoordinate(state, move)); + return move(src, src.getCoordinate(this, move)); } //CHECK public boolean move(ChessCoordinate src, ChessCoordinate dest) { ChessRules.INSTANCE.setLog(true); - ChessRules.INSTANCE.setState(state); + ChessRules.INSTANCE.setState(this); ChessRules.INSTANCE.setSrc(src); ChessRules.INSTANCE.setDest(dest); if(ChessRules.INSTANCE.validMove()) { ChessRules.INSTANCE.applyCaptureRule(); - state.move(src, dest); + super.move(src, dest); return true; } else return false; @@ -77,7 +80,7 @@ public class ChessBoard { List<ChessCoordinate> playerPieces = new ArrayList<ChessCoordinate>(); for(int i=1; i<=ChessCoordinate.MAX; i++) for(int j=1; j<=ChessCoordinate.MAX; j++) { - ChessPiece piece = ChessPiece.find(state.get(new ChessCoordinate(j,i))); + ChessPiece piece = ChessPiece.find(get(new ChessCoordinate(j, i))); if(piece.getColor() == color) playerPieces.add(new ChessCoordinate(j,i)); } @@ -86,7 +89,6 @@ public class ChessBoard { //CHECK public String printState() { - byte[][] state = this.state.getState(); String result = ""; for(int i=0; i< ChessCoordinate.MAX; i++) { @@ -96,8 +98,4 @@ public class ChessBoard { } return result; } - - public ChessState getState() { - return state; - } } diff --git a/src/main/java/chess/ChessGame.java b/src/main/java/chess/ChessGame.java @@ -12,7 +12,7 @@ import java.util.List; public class ChessGame { public static void main(String[] args) { - final int gameId = 20; + final int gameId = 25; final int teamNo = 1; final String secret = "32c68cae"; @@ -34,9 +34,10 @@ public class ChessGame { ChessClock clock = new ChessClock(status.getSecondsLeft()); + ChessRules.INSTANCE.setLog(false); ChessMove move = player.bestPossibleMove(board); - MoveResponse response = server.push(board.getState(), move); + MoveResponse response = server.push(board, move); if(response.isResult() && board.move(move)) { player.incrementMoves(); System.out.println("Player Moved: "+ diff --git a/src/main/java/chess/ChessMove.java b/src/main/java/chess/ChessMove.java @@ -36,34 +36,6 @@ public class ChessMove { return coordinate; } - public static List<ChessMove> possibleMoves( - ChessCoordinate src, ChessState state) { - - List<ChessMove> moves = new ArrayList<ChessMove>(); - ChessRules.INSTANCE.setState(state); - ChessRules.INSTANCE.setSrc(src); - - final ChessDirection[] directions = - ChessRules.INSTANCE.possibleDirections(); - - for(ChessDirection direction : directions) { - boolean valid = true; - int steps = 1; - while (valid) { - ChessMove move = new ChessMove(src, direction, steps); - ChessCoordinate dest = src; - dest = dest.getCoordinate(state, move); - ChessRules.INSTANCE.setDest(dest); - valid = ChessRules.INSTANCE.validMove(); - - if(valid) moves.add(move); - steps++; - } - } - - return moves; - } - public int getEvaluation() { return evaluation; } diff --git a/src/main/java/chess/ChessPlayer.java b/src/main/java/chess/ChessPlayer.java @@ -1,6 +1,7 @@ package chess; import chess.utils.ChessColor; +import chess.utils.ChessDirection; import chess.utils.ChessType; import java.util.ArrayList; @@ -42,10 +43,10 @@ public class ChessPlayer { public List<ChessMove> possibleMoves(List<ChessCoordinate> pieces, ChessState state) { + ChessRules.INSTANCE.setLog(false); List<ChessMove> moves = new ArrayList<ChessMove>(); for(ChessCoordinate src : pieces) { - List<ChessMove> possibleMoves = - ChessMove.possibleMoves(src,state); + List<ChessMove> possibleMoves = possibleMoves(src, state); for(ChessMove move : possibleMoves) moves.add(move); } @@ -53,20 +54,19 @@ public class ChessPlayer { } public ChessMove bestPossibleMove(ChessBoard board) { - ChessRules.INSTANCE.setLog(false); List<ChessCoordinate> pieces = board.getPieces(color); - List<ChessMove> moves = possibleMoves(pieces, board.getState()); + List<ChessMove> moves = possibleMoves(pieces, board); ChessMove bestMove = null; int eval = -999999999; while(!moves.isEmpty()) { ChessMove move = moves.get(0); - - ChessBoard tmpBoard = board; + ChessBoard tmpBoard = new ChessBoard(board); tmpBoard.move(move); - int ourscore = evaluate(move,tmpBoard); + int ourscore = evaluate(move,board); int oppEval = evaluateOpponentMove(tmpBoard, ourscore); + if(oppEval > eval){ bestMove=move; eval= oppEval; @@ -80,7 +80,7 @@ public class ChessPlayer { public int evaluateOpponentMove(ChessBoard board, int ourscore) { List<ChessCoordinate> pieces = board.getPieces(color.opposite()); - List<ChessMove> moves = possibleMoves(pieces, board.getState()); + List<ChessMove> moves = possibleMoves(pieces, board); int eval = 999999999; while(!moves.isEmpty()) { @@ -100,10 +100,10 @@ public class ChessPlayer { private int evaluate(ChessMove move, ChessBoard board) { ChessCoordinate src = move.getCoordinate(); - ChessCoordinate dest = src.getCoordinate(board.getState(), move); + ChessCoordinate dest = src.getCoordinate(board, move); - ChessPiece srcPiece = ChessPiece.find(board.getState().get(src)); - ChessPiece destPiece = ChessPiece.find(board.getState().get(dest)); + ChessPiece srcPiece = ChessPiece.find(board.get(src)); + ChessPiece destPiece = ChessPiece.find(board.get(dest)); ChessType srcType = srcPiece.getType(); ChessType destType = destPiece.getType(); @@ -116,6 +116,34 @@ public class ChessPlayer { } } + public List<ChessMove> possibleMoves( + ChessCoordinate src, ChessState state) { + + List<ChessMove> moves = new ArrayList<ChessMove>(); + ChessRules.INSTANCE.setState(state); + ChessRules.INSTANCE.setSrc(src); + + final ChessDirection[] directions = + ChessRules.INSTANCE.possibleDirections(); + + for(ChessDirection direction : directions) { + boolean valid = true; + int steps = 1; + while (valid) { + ChessMove move = new ChessMove(src, direction, steps); + ChessCoordinate dest = src; + dest = dest.getCoordinate(state, move); + ChessRules.INSTANCE.setDest(dest); + valid = ChessRules.INSTANCE.validMove(); + + if(valid) moves.add(move); + steps++; + } + } + + return moves; + } + public ChessColor getColor() { return color; } diff --git a/src/main/java/chess/ChessRules.java b/src/main/java/chess/ChessRules.java @@ -21,6 +21,7 @@ public enum ChessRules { private ChessPiece destPiece; private ChessType srcType; + // TODO evaluating? private boolean pawnCanDoubleMove = true; private boolean log = true; diff --git a/src/main/java/chess/ChessState.java b/src/main/java/chess/ChessState.java @@ -4,17 +4,22 @@ package chess; * Check */ public class ChessState { - private byte[][] state; + protected byte[][] state; public ChessState() { state = new byte[ChessCoordinate.MAX][ChessCoordinate.MAX]; } - public byte[][] getState() { - return state; + public ChessState(ChessState chessState) { + byte[][] state = chessState.state; + this.state = new byte[state.length][]; + + for(int i=0; i<state.length; i++) { + this.state[i] = state[i].clone(); + } } - public void set(ChessCoordinate coor, ChessPiece piece) { + protected void set(ChessCoordinate coor, ChessPiece piece) { final int rank = fixRankOrder(coor.getRank()) - 1; final int file = coor.getFileNumber() - 1; @@ -28,7 +33,7 @@ public class ChessState { return state[rank][file]; } - public void move(ChessCoordinate coor, ChessCoordinate coor1) { + protected boolean move(ChessCoordinate coor, ChessCoordinate coor1) { final int rank = fixRankOrder(coor.getRank()) - 1; final int rank1 = fixRankOrder(coor1.getRank()) - 1; @@ -37,6 +42,7 @@ public class ChessState { state[rank1][file1] = state[rank][file]; state[rank][file] = 0; + return true; } private int fixRankOrder(int rank) {