Implemented timer in base AI class
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@326 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
ae9ce59d36
commit
ad2f226369
1 changed files with 54 additions and 9 deletions
|
@ -1,23 +1,60 @@
|
|||
package jrummikub.control.turn;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import jrummikub.control.ITurnTimer;
|
||||
import jrummikub.control.TurnTimer;
|
||||
import jrummikub.util.Connection;
|
||||
import jrummikub.util.IListener;
|
||||
|
||||
/**
|
||||
* Base class for AI players
|
||||
*
|
||||
*/
|
||||
public class BaseAIControl extends AbstractTurnControl {
|
||||
long startTime;
|
||||
private ITurnTimer turnTimer;
|
||||
private List<Connection> connections = new ArrayList<Connection>();
|
||||
private Thread computeThread;
|
||||
|
||||
private volatile boolean stopRunning = false;
|
||||
|
||||
@Override
|
||||
public void startTurn() {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
turnTimer = new TurnTimer(view);
|
||||
connections.add(turnTimer.getTimeRunOutEvent().add(new IListener() {
|
||||
|
||||
@Override
|
||||
public void handle() {
|
||||
cleanUp();
|
||||
endOfTurnEvent.emit();
|
||||
}
|
||||
}));
|
||||
|
||||
computeThread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
compute();
|
||||
}
|
||||
});
|
||||
startTime = System.currentTimeMillis();
|
||||
thread.start();
|
||||
|
||||
|
||||
turnTimer.startTimer();
|
||||
computeThread.start();
|
||||
}
|
||||
|
||||
|
||||
private void cleanUp() {
|
||||
turnTimer.stopTimer();
|
||||
|
||||
for (Connection c : connections) {
|
||||
c.remove();
|
||||
}
|
||||
stopRunning = true;
|
||||
}
|
||||
|
||||
private void compute() {
|
||||
if (mayRedeal) {
|
||||
emitRedeal();
|
||||
|
@ -29,15 +66,16 @@ public class BaseAIControl extends AbstractTurnControl {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void layOut() {
|
||||
emitEndOfTurn();
|
||||
}
|
||||
|
||||
|
||||
private void emitRedeal() {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
cleanUp();
|
||||
redealEvent.emit();
|
||||
}
|
||||
});
|
||||
|
@ -45,9 +83,10 @@ public class BaseAIControl extends AbstractTurnControl {
|
|||
|
||||
private void emitEndOfTurn() {
|
||||
long timeElapsed = System.currentTimeMillis() - startTime;
|
||||
long timeNeeded = Math.min((long)(1000 + Math.random() * hand.getSize() * 100), 50000);
|
||||
long timeNeeded = Math.min((long) (1000 + Math.random() * hand.getSize()
|
||||
* 100), 50000);
|
||||
long waitTime = timeNeeded - timeElapsed;
|
||||
|
||||
|
||||
if (waitTime > 0) {
|
||||
try {
|
||||
Thread.sleep(waitTime);
|
||||
|
@ -55,15 +94,21 @@ public class BaseAIControl extends AbstractTurnControl {
|
|||
// This shouldn't happen
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
cleanUp();
|
||||
endOfTurnEvent.emit();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the factory for the base AI control
|
||||
*
|
||||
* @return the factory
|
||||
*/
|
||||
static public TurnControlFactory getFactory() {
|
||||
return new TurnControlFactory() {
|
||||
@Override
|
||||
|
|
Reference in a new issue