package blackboard.client; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Vector; import java.util.Hashtable; import java.util.Random; import java.util.ArrayList; /* * OpeningAgent * A chess agent that makes a opening strategeis. * * http://www.chesslab.com/PositionSearch.html * http://java.sun.com/j2se/1.4/docs/api/index.html * * * @author Mattias Gustafsson * @version 0.3 25/04/2002 */ public class OpeningAgent 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 = "OpeningAgent"; /* The name of the agent this RandomAgent will tell moves to */ private final String BOSS_AGENT_NAME = "ChefsAgent"; public Client cl; /* Are we playing as white? */ private boolean iswhite; private String myName; /*RandomNr*/ private int rndOpening; String move = null; int rnd; int position=0; /* Number of opening strategies */ private int OPENING_STRATEGIES = 10; /* Opening strategies */ public String white[][] = {{"D2D4","G1F3","B1C3"}, {"D2D4","E2E3"}, {"D2D4","G1F3","C2C3"}, {"D2D3","E2E4"}, {"G1F3","B1C3"}, {"B2B3","C1B2","C2C4"}, {"F2F3","G2G4","E1G3"}, {"G2G3","F1G2","D2D3"}, {"G1F3","B2B3","C1B2"}, {"G2G3","D2D3","C1F4"}}; public String black[][] = {{"D7D5","G8F6","B8C6"}, {"D7D5","E7E6"}, {"D7D5","G8F6","C7C6"}, {"D7D6","E7E5"}, {"G8F6","B7B6"}, {"B7B6","C8B7","C7C5"}, {"F7F6","G7G5","E8G6"}, {"G7G6","F8G7","D7D6"}, {"G8F6","B7B6","C8B7"}, {"G7G5","D7D6","F7F6"}}; /*Opening Vector*/ Vector openBlack = new Vector(); Vector openWhite = new Vector(); /** * 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 OpeningAgent(String name, String server, int port){ myName=AGENT_NAME+" - "+name; cl=new Client(myName, server,port); System.out.println(myName+" is started"); myName=name; /*Random a nr for which Opening strategies */ rnd = (int)(Math.random()*OPENING_STRATEGIES); System.out.println("Väljer öpnning nr "+rnd); /*init. the vectors with a opening strategie */ for (int i=0; i < black[rnd].length ; i++) { openBlack.addElement(black[rnd][i]); } for (int i=0; i < white[rnd].length ; i++) { openWhite.addElement (white[rnd][i]); } } /** * The running infinite loop of the agent * */ public void run(){ String message = "", id = "", sender = "", destination = ""; boolean isplayer = false; String move=null; 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(); /*get a elemet from the vector and init. the move*/ if (iswhite){ move= (String)openWhite.elementAt(position); System.out.println("Föreslår: "+move); } else{ move= (String)openBlack.elementAt(position); System.out.println("Föreslår: "+move); } /*Do the move*/ if (move != null && move.length() == 4) { makeMove(move); position++; } 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); } else if (isBoardMessage(message)) { // The message contains the current chessboard } 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; } else if (message.indexOf("Connected as black") != -1) { System.out.println("Spelet startas, jag är svart"); iswhite = false; isplayer = true; } } 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"); } /*Stop the agent when all moves are finished*/ if(position == (white[rnd].length) || position == (black[rnd].length)){ //this.stop(); System.exit(0); } } 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); } } /** * 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: } OpeningAgent cld=new OpeningAgent(name, server,port); cld.start(); } }