blob: fc1b97d716f956ef0f749b17d15bc2b0ca08bdfb (
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
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() {
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();
turnTimer.startTimer();
computeThread.start();
}
private void cleanUp() {
turnTimer.stopTimer();
for (Connection c : connections) {
c.remove();
}
stopRunning = true;
}
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() {
cleanUp();
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() {
cleanUp();
endOfTurnEvent.emit();
}
});
}
/**
* Get the factory for the base AI control
*
* @return the factory
*/
static public TurnControlFactory getFactory() {
return new TurnControlFactory() {
@Override
public ITurnControl create() {
return new BaseAIControl();
}
};
}
}
|