blob: 4e627c3d0a10c8438e8a4931f126c3df31a21d00 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package jrummikub.control.turn;
import javax.swing.SwingUtilities;
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) {
emitRedeal();
} else {
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 timeNeeded = Math.min((long)(1000 + Math.random() * hand.getSize() * 100), 50000);
long waitTime = timeNeeded - 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
public ITurnControl create() {
return new BaseAIControl();
}
};
}
}
|