chessai

college code for ai playing chess in java

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

commit 12b0c512dc1567a433da0ce742bb728a82f8360b
parent 0832d5fad12dbf7bb80cf3d7dba5285e5df03ced
Author: Jul <jul@9o.is>
Date:   Thu,  6 Dec 2012 14:20:03 -0500

Game flow added. Can now play with server.

Diffstat:
Msrc/main/java/chess/ChessGame.java | 85++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 56 insertions(+), 29 deletions(-)

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 = 7; + final int gameId = 20; final int teamNo = 1; final String secret = "32c68cae"; @@ -26,21 +26,17 @@ public class ChessGame { StatusResponse status = server.poll(); - while (true) { + boolean playing = true; + while (playing) { // our turn while(status.isReady()) { + ChessRules.INSTANCE.setLog(false); - List<ChessCoordinate> playerPieces = new ArrayList<ChessCoordinate>(); - List<ChessMove> moves = new ArrayList<ChessMove>(); + List<ChessCoordinate> playerPieces = + board.getPlayerPieces(player); - final byte[][] state = board.getState().getState(); - for(int i=0; i<state.length; i++) - for(int j=0; j<state[i].length; j++) { - ChessPiece piece = ChessPiece.find(state[i][j]); - if(piece.getColor() == player.getColor()) - playerPieces.add(new ChessCoordinate(j,i)); - } + List<ChessMove> moves = new ArrayList<ChessMove>(); for(ChessCoordinate src : playerPieces) { List<ChessMove> possibleMoves = @@ -56,8 +52,6 @@ public class ChessGame { ChessClock clock = new ChessClock(status.getSecondsLeft()); - // TODO iterate board & get player's pieces - // TODO for each piece, get all possible moves // TODO evaluate capture/protection points // TODO store tree in memory (or other storage option) // TODO eval clock and pick best move @@ -66,7 +60,7 @@ public class ChessGame { if(response.isResult() && board.move(move)) { player.incrementMoves(); System.out.println("Player Moved: "+ - status.getLastMove()+" with "+ + move.toString()+" with "+ status.getSecondsLeft()+" seconds left."); } @@ -76,32 +70,65 @@ public class ChessGame { // TODO minmax for every opponents possible move // TODO concurrently hopefully? - // print board in console every 5 moves - if(player.getMoves() % 5 == 0) { - System.out.println("Moves: "+ - status.getLastMoveNumber()+"\n"+ - board.printState()); - } - - // opponent's turn + // OPPONENT's turn while(!status.isReady()) { // poll every 5 seconds try { Thread.sleep((long)(5 * 1000)); status = server.poll(); } catch (InterruptedException e) {} - } - // move opponent's last move - if(board.move(status.srcLastMove(), status.destLastMove())) { - opponent.incrementMoves(); - System.out.println("Opponent Moved: "+ - status.getLastMove()+" with "+ - status.getSecondsLeft()+" seconds left."); + if(status.isReady()) { + // move opponent's last move + if(board.move(status.srcLastMove(), status.destLastMove())) { + opponent.incrementMoves(); + System.out.println("Opponent Moved: "+ + status.getLastMove()+" with "+ + status.getSecondsLeft()+" seconds left."); + } + } } // TODO if generated minmax table during opponent's move, // TODO choose move the opponent chose and continue from there + + printBoard(status, board); + status = server.poll(); + + if(isGameover(status,board,player)) + playing = false; + } + } + + public static void printBoard(StatusResponse status, ChessBoard board) { + // print board in console every 5 moves + if(status.getLastMoveNumber() % 10 == 0) { + System.out.println("Moves: "+ + status.getLastMoveNumber()+"\n"+ + board.printState()); + } + } + + public static boolean isGameover( + StatusResponse status, + ChessBoard board, + ChessPlayer player) { + + if(status.isGameover()) { + ChessPlayer w = new ChessPlayer(status.getWinner()); + boolean winner = player.getColor() == w.getColor(); + + System.out.println("Moves: "+ + status.getLastMoveNumber()+"\n"+ + board.printState()); + + if(winner) + System.out.println("Congratulations! You Won!"); + else + System.out.println("GAMEOVER! You Lost!"); + + return true; } + return false; } }