Implemented dummy AI
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@318 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
f3f52956f7
commit
15558d7138
5 changed files with 74 additions and 13 deletions
|
@ -1,6 +1,7 @@
|
|||
package jrummikub.control.turn;
|
||||
|
||||
import jrummikub.model.IHand;
|
||||
import jrummikub.model.IPlayer;
|
||||
import jrummikub.model.ITable;
|
||||
import jrummikub.util.Event;
|
||||
import jrummikub.util.IEvent;
|
||||
|
@ -13,6 +14,7 @@ public abstract class AbstractTurnControl implements ITurnControl {
|
|||
|
||||
protected Event endOfTurnEvent = new Event();
|
||||
protected Event redealEvent = new Event();
|
||||
protected IPlayer player;
|
||||
protected IHand hand;
|
||||
protected ITable table;
|
||||
protected IView view;
|
||||
|
@ -31,9 +33,10 @@ public abstract class AbstractTurnControl implements ITurnControl {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setup(IHand hand, ITable table, IView view,
|
||||
public void setup(IPlayer player, ITable table, IView view,
|
||||
boolean inspectOnly, boolean mayRedeal) {
|
||||
this.hand = hand;
|
||||
this.player = player;
|
||||
this.hand = player.getHand();
|
||||
this.table = table;
|
||||
this.view = view;
|
||||
this.inspectOnly = inspectOnly;
|
||||
|
|
|
@ -1,21 +1,74 @@
|
|||
package jrummikub.control.turn;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import jrummikub.model.IHand;
|
||||
import jrummikub.model.ITable;
|
||||
import jrummikub.view.IView;
|
||||
|
||||
|
||||
public class BaseAIControl extends AbstractTurnControl {
|
||||
|
||||
long startTime;
|
||||
@Override
|
||||
public void startTurn() {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
compute();
|
||||
}
|
||||
});
|
||||
startTime = System.currentTimeMillis();
|
||||
thread.start();
|
||||
|
||||
}
|
||||
|
||||
private void compute() {
|
||||
if (mayRedeal) {
|
||||
redealEvent.emit();
|
||||
emitRedeal();
|
||||
} else {
|
||||
endOfTurnEvent.emit();
|
||||
if (player.getLaidOut()) {
|
||||
layOut();
|
||||
} else {
|
||||
emitEndOfTurn();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void layOut() {
|
||||
emitEndOfTurn();
|
||||
}
|
||||
|
||||
private void emitRedeal() {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
redealEvent.emit();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void emitEndOfTurn() {
|
||||
long timeElapsed = System.currentTimeMillis() - startTime;
|
||||
long waitTime = 2000 - timeElapsed;
|
||||
|
||||
if (waitTime > 0) {
|
||||
try {
|
||||
Thread.sleep(waitTime);
|
||||
} catch (InterruptedException e) {
|
||||
// This shouldn't happen
|
||||
}
|
||||
}
|
||||
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
endOfTurnEvent.emit();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static public TurnControlFactory getFactory() {
|
||||
return new TurnControlFactory() {
|
||||
@Override
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package jrummikub.control.turn;
|
||||
|
||||
import jrummikub.model.IHand;
|
||||
import jrummikub.model.IPlayer;
|
||||
import jrummikub.model.ITable;
|
||||
import jrummikub.util.Event;
|
||||
import jrummikub.util.IEvent;
|
||||
|
@ -21,8 +22,8 @@ public interface ITurnControl {
|
|||
* @param mayRedeal
|
||||
* true when the current player may decide to redeal
|
||||
*/
|
||||
public abstract void setup(IHand hand, ITable table, IView view,
|
||||
boolean inspectOnly, boolean mayRedeal);
|
||||
public void setup(IPlayer player, ITable table, IView view,
|
||||
boolean inspectOnly, boolean mayRedeal);
|
||||
|
||||
/**
|
||||
* Get the event that is emitted when the turn is over
|
||||
|
|
Reference in a new issue