blob: 324c8d73f2aecafc6f744ab5d82722d870c4b0ba (
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
package jrummikub.control.turn;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import jrummikub.ai.TurnLogic;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneSet;
import jrummikub.util.Pair;
/**
* Base class for AI players
*
*/
public class AIControl extends AbstractTurnControl {
private TurnLogic logic;
private boolean turnDone = false;
boolean useBackgroundThread = true;
long startTime;
@Override
public void startTurn() {
timer.startTimer();
startTime = System.currentTimeMillis();
compute();
}
protected void timeOut() {
executeTurn();
}
@Override
protected void cleanUp() {
if (logic != null)
logic.abort();
turnDone = true;
super.cleanUp();
}
private void emitEndOfTurn() {
turnDone = true;
long turnLength = System.currentTimeMillis() - startTime;
if (useBackgroundThread) {
Timer timer = new Timer(Math.max(0,
(int) (1000 + Math.random() * 2000 - turnLength)),
new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
cleanUp();
endOfTurnEvent.emit();
}
});
timer.setRepeats(false);
timer.start();
} else {
cleanUp();
endOfTurnEvent.emit();
}
}
private void compute() {
switch (turnInfo.getTurnMode()) {
case MAY_REDEAL:
emitEndOfTurn();
break;
case INSPECT_ONLY:
emitEndOfTurn();
break;
case NORMAL_TURN:
turn();
break;
}
}
private void turn() {
List<Stone> tableStones = new ArrayList<Stone>();
List<Stone> handStones = new ArrayList<Stone>();
for (Pair<Stone, Position> entry : turnInfo.getHand()) {
handStones.add(entry.getFirst());
}
if (turnInfo.getLaidOut()) {
for (Pair<StoneSet, Position> entry : turnInfo.getTable()) {
for (Stone stone : entry.getFirst()) {
tableStones.add(stone);
}
}
}
logic = new TurnLogic(settings, tableStones, handStones);
if (!turnInfo.getLaidOut()) {
logic.needIntialMeldThreshold();
}
if (useBackgroundThread) {
Timer timer = new Timer(10000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
logic.autoAbort();
}
});
timer.setRepeats(false);
timer.start();
Thread computeThread = new Thread(new Runnable() {
@Override
public void run() {
logic.optimize();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
executeTurn();
}
});
}
});
computeThread.start();
} else {
logic.optimize();
executeTurn();
}
}
private void executeTurn() {
if (turnDone) {
return;
}
List<StoneSet> result = logic.getResult();
if (result != null) {
if (turnInfo.getLaidOut()) {
outerLoop: for (Iterator<Pair<StoneSet, Position>> it = turnInfo
.getTable().iterator(); it.hasNext();) {
Pair<StoneSet, Position> pair = it.next();
setSearch: for (Iterator<StoneSet> it2 = result.iterator(); it2
.hasNext();) {
StoneSet set = it2.next();
if (set.getSize() != pair.getFirst().getSize()) {
continue;
}
for (int i = 0; i < set.getSize(); i++) {
if (set.get(i) != pair.getFirst().get(i)) {
continue setSearch;
}
}
it2.remove();
continue outerLoop;
}
it.remove();
}
}
for (StoneSet set : result) {
turnInfo.getTable().drop(
set,
new Position(10 * (Math.random() * 2 - 1), 5 * (Math
.random() * 2 - 1)));
for (Stone stone : set) {
turnInfo.getHand().pickUp(stone);
}
}
}
emitEndOfTurn();
}
/**
* Get the factory for the base AI control
*
* @return the factory
*/
static public TurnControlFactory getFactory() {
return new TurnControlFactory() {
@Override
public ITurnControl create() {
return new AIControl();
}
};
}
}
|