/**
*
*/
package de.axxeed.animosy.ai;
import org.apache.log4j.Logger;
import de.axxeed.animosy.gui.PanelRepository;
import de.axxeed.animosy.model.BoardModel;
import de.axxeed.animosy.model.Constants;
import de.axxeed.animosy.model.Game;
import de.axxeed.animosy.model.Link;
import de.axxeed.animosy.model.Manager;
import de.axxeed.animosy.model.Node;
/**
* MrX.java
* Created 21.01.2008 14:54:29
* @author Markus J. Luzius
*
*/
public class MrX extends Thread implements Constants {
private static Logger log = Logger.getLogger(MrX.class);
private MrXInterface myMrX;
public MrX(Class mrXClass) {
log.debug("Starting MrX thread...");
try {
myMrX = (MrXInterface) mrXClass.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassCastException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
start();
}
/* (non-Javadoc)
* @see de.axxeed.animosy.ai.MrXInterface#run()
*/
public void run() {
int moveNo = -1;
Game game = Manager.getGame();
while (true) {
if(game.getState()==Game.MOVE_MRX) {
if(game.getCurrentMove() != moveNo) {
this.setPriority(6);
log.info("Moving MrX in separate thread (move #"+game.getCurrentMove()+")...");
moveNo = game.getCurrentMove();
Node mrXMove = moveMrX();
int type = game.getBoard().getMrX().changePosition(mrXMove);
Manager.getGame().getTracker().nextMove(type);
log.debug("MrX move done...");
this.setPriority(Thread.NORM_PRIORITY);
Manager.getGame().nextDetective();
}
}
if(game.getState()==Game.WIN_MRX || game.getState()==Game.WIN_DET || game.getState()==Game.NO_GAME) {
break;
}
else {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
log.debug("MrX was interrupted...");
}
}
}
log.debug("Terminating MrX thread...");
}
private Node moveMrX() {
long ts = System.currentTimeMillis();
Node n = myMrX.getMove();
/*
Node n = null;
if(Manager.getOptions().getMrXAlgorithm()==MRX_INTELLIGENT) {
log.debug("MrX intelligent move");
VirtualBoard vb = new VirtualBoard(Manager.getGame().getBoard());
n = BoardModel.getNode(vb.moveMrX().getNode());
}
if(Manager.getOptions().getMrXAlgorithm()==MRX_MAXDIST) {
log.debug("MrX maxdist move");
n = maxDistMove();
}
else {
log.debug("MrX random move");
n = randomMove();
}
*/
log.info("Moving MrX");
log.debug("to "+n.getPosition()+" in "+(System.currentTimeMillis()-ts)+"ms");
return n;
}
}