package blackboard.client; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Vector; import java.util.Hashtable; /* * RandomAgent * A chess agent that generates and makes a random move. * * http://www.chesslab.com/PositionSearch.html * http://java.sun.com/j2se/1.4/docs/api/index.html * * * @author Gösta Jonasson gosta@kth.se * @version 0.4 05/05/2002 */ public class RandomAgent extends Thread { /* Should we perform a real move (true) or just suggest one to the Boss (false) */ private final boolean MAKE_REAL_MOVE = false; /* The name of THIS agent (The actual name is AGENT_NAME+" - "+the_name_supplied_when _Starting)*/ private final String AGENT_NAME = "RandomAgent"; /* The name of the agent this RandomAgent will tell moves to */ private final String BOSS_AGENT_NAME = "ChefsAgent"; private final int PAWN = 1; // Bonde private final int ROOK = 2; // Torn private final int KNIGHT = 3; // Springare private final int BISHOP = 4; // Löpare private final int QUEEN = 5; // Dam private final int KING = 6; // Kung private final String[] X_VALUES = {"","A","B","C","D","E","F","G","H"}; /* The current chessboard, an 8x8 int matrix with values 0-22 0=empty, 1=black pawn, 2=black rook.... 17=white pawn, 18=white rook... chessboard[x][y] gives you the piece on x,y */ private int[][] chessboard = { {ROOK+16,PAWN+16,0,0,0,0,PAWN,ROOK}, {KNIGHT+16,PAWN+16,0,0,0,0,PAWN,KNIGHT}, {BISHOP+16,PAWN+16,0,0,0,0,PAWN,BISHOP}, {KING+16,PAWN+16,0,0,0,0,PAWN,KING}, {QUEEN+16,PAWN+16,0,0,0,0,PAWN,QUEEN}, {BISHOP+16,PAWN+16,0,0,0,0,PAWN,BISHOP}, {KNIGHT+16,PAWN+16,0,0,0,0,PAWN,KNIGHT}, {ROOK+16,PAWN+16,0,0,0,0,PAWN,ROOK} }; public Client cl; private RandomMoveGenerator randommg; /* Are we playing as white? */ private boolean iswhite; private String myName; /* Contains the numbers for the different pieces for quick access */ private Hashtable piecenumbers; /** * Constructor that initiates the agent. * * @param name The name of this agent * @param server The server the agent should connect to * @param port The port of the server the agent should connect to */ public RandomAgent(String name, String server, int port){ myName=AGENT_NAME+" - "+name; cl=new Client(myName, server,port); randommg = new RandomMoveGenerator(); piecenumbers = new Hashtable(); piecenumbers.put("pawn", new Integer(PAWN)); piecenumbers.put("rook", new Integer(ROOK)); piecenumbers.put("knight", new Integer(KNIGHT)); piecenumbers.put("bishop", new Integer(BISHOP)); piecenumbers.put("queen", new Integer(QUEEN)); piecenumbers.put("king", new Integer(KING)); System.out.println(myName+" is started"); } /** * The running infinite loop of the agent * */ public void run(){ String message = "", id = "", sender = "", destination = ""; boolean isplayer = false; cl.connect(); while(true) { while (cl.hasMessages()) { // Go through all the arrived messages (if there are any) message = cl.getMessage(); // Store the message //System.out.println("Message: "+message); // Extract the ID of the message if (message.indexOf("") != -1) { id = message.substring(message.indexOf("")+"".length(), message.indexOf("")); } else { id = "UNKNOWN!"; } // Extract the sender of the message if (message.indexOf("") != -1) { sender = message.substring(message.indexOf("")+"".length(), message.indexOf("")); } else { sender = "UNKNOWN!"; } // Extract the destination of the message if (message.indexOf("") != -1) { destination = message.substring(message.indexOf("")+"".length(), message.indexOf("")); } else { destination = "UNKNOWN!"; } // Extract the body of the message if (message.indexOf("") != -1) { // The String message will only have the contents within the tag message = message.substring(message.indexOf("")+"".length(), message.indexOf("")); } if (isGotTurnMessage(message)) { // The message tells us it's our turn to make a move if (!isplayer) { System.out.println("*** FEL!!! Jag vet inte vilken färg jag är!!! ***"); } else { System.out.println("Vår tur, dags att göra ett drag... "); long start = System.currentTimeMillis(); /** DEBUG for (int i=7; i>-1 ;i--) { for (int j=7; j>-1 ;j--) { System.out.print(chessboard[j][i]+"\t"); } System.out.print("\n"); } END DEBUG */ String move = randommg.makeMove(chessboard); if (move != null) { System.out.println("Föreslår: "+move); makeMove(move); } else { // We could not generate a move! System.out.println("Det gick inte att generera fram något drag!!!"); } System.out.println("Det tog "+(System.currentTimeMillis()-start)+" ms att generera draget"); } } else if (isLostTurnMessage(message)) { // The message tells us that our turn is lost (we have either made a move or not) System.out.println("Turen går över till motståndaren"); } else if (ismovePerformedMessage(message)) { // The message tells us that our move was performed // System.out.println("Vårt drag genomfördes"); } else if (isIllegalMoveMessage(message)) { // The message tells us that we tried to make an illegal move System.out.println("Vi gjorde ett otillåtet drag! \n"+message); for (int i=7; i>-1 ;i--) { for (int j=7; j>-1 ;j--) { System.out.print(chessboard[j][i]+"\t"); } } //System.exit(0); // Give up! } else if (isBoardMessage(message)) { // The message contains the current chessboard System.out.print("Uppdaterar spelplanen... "); long start = System.currentTimeMillis(); updateBoard(message); System.out.println("tog "+(System.currentTimeMillis()-start)+" ms"); } else if (!isplayer){ // The game hasn't been started yet, we don't know which color we are if (message.indexOf("Connected as white") != -1) { System.out.println("Spelet startas, jag är vit"); iswhite = true; isplayer = true; randommg.setColor(iswhite); } else if (message.indexOf("Connected as black") != -1) { System.out.println("Spelet startas, jag är svart"); iswhite = false; isplayer = true; randommg.setColor(iswhite); } } else if (message.indexOf(" is winner because there is no ") != -1) { // The game is over // white is winner because there is no black king if (message.indexOf("white is winner") != -1) { if (iswhite) { System.out.println("*** Vi vann!!! ***"); } else { System.out.println("*** Vi förlorade... ***"); } } else if (message.indexOf("black is winner") != -1) { if (!iswhite) { System.out.println("*** Vi vann!!! ***"); } else { System.out.println("*** Vi förlorade... ***"); } } } else { // An unrecognized message System.out.println("Jag förstår inte:\n"+message+"\n"); } } try { Thread.sleep(500); // Check every half second for new messages } catch(Exception e) { e.printStackTrace(); } } } /** * Make the move given, either post it in a message or perform * the move (This depends on the variable MAKE_REAL_MOVE) * * @param move The move on the form "A2E4" */ public void makeMove(String move) { if (MAKE_REAL_MOVE) { cl.move(move.substring(0,2),move.substring(2)); } else { cl.message(""+(System.currentTimeMillis() % 10000), BOSS_AGENT_NAME, move); } } /** * Update the internal representation of the chessboard * * @param message The message containing the current chessboard */ public void updateBoard(String message) { // A message will look like: white rook @ A1 String placement; int type_of_piece = 0, x = 0, y = 0, piece_is_white = 0; chessboard = new int[8][8]; while (message.indexOf("") != -1) { // Go through all the pieces placement = message.substring(message.indexOf("")+"".length(), message.indexOf("")); //System.out.println(placement); message = message.substring(message.indexOf("")+"".length()); // Get the color of the piece if(placement.startsWith("white")) { piece_is_white = 1; placement = placement.substring(placement.indexOf("white")+"white".length()+1); } else { piece_is_white = 0; placement = placement.substring(placement.indexOf("black")+"black".length()+1); } // Get the type of the piece type_of_piece = ((Integer)piecenumbers.get(placement.substring(0, placement.indexOf(" ")))).intValue(); // Get the coordinates for the piece x = Character.getNumericValue(placement.charAt(placement.indexOf("@ ")+2))-10; y = Character.getNumericValue(placement.charAt(placement.indexOf("@ ")+3))-1; // Put the numerical value of the piece on the chessboard chessboard[x][y] = type_of_piece + piece_is_white*16; } } /** * Checks if the message tells us if we got the turn * * @param message The message which might tell us it is our turn * @return True if the message tells us it's our turn */ public boolean isGotTurnMessage(String message) { if (message.indexOf("got turn") != -1) { return true; } else { return false; } } /** * Checks if the message tells us that we made an illegal move * * @param message The message which might tell us that we made an illegal move * @return True if the message tells us that we made an illegal move */ public boolean isIllegalMoveMessage(String message) { if (message.indexOf("Illegal move") != -1) { return true; } else { return false; } } /** * Checks if the message tells us that we lost the turn and it's our opponents turn * * @param message The message which might tell us that we lost the turn * @return True if the message tells us that we lost the turn */ public boolean isLostTurnMessage(String message) { if (message.indexOf("lost turn") != -1) { return true; } else { return false; } } /** * Checks if the message tells us that our move was performed * * @param message The message which might tell us that our move was performed * @return True if the message tells us that our move was performed */ public boolean ismovePerformedMessage(String message) { if (message.indexOf("Move performed") != -1) { return true; } else { return false; } } /** * Checks if the message contains the current chessboard * * @param message The message which might contains the current chessboard * @return True if the message contains the current chessboard */ public boolean isBoardMessage(String message) { if (message.indexOf("") != -1) { return true; } else { return false; } } /** * Main method for starting an instance of this class * * @param message The message which might contains the current chessboard * @return True if the message contains the current chessboard */ public static void main(String[] args) throws Exception { String name="Dofus"; String server="localhost"; int port=6200; switch(args.length){ case 3: port=Integer.parseInt(args[2]); case 2: server=args[1]; case 1: name=args[0]; break; default: } RandomAgent cld=new RandomAgent(name, server,port); cld.start(); } }