@Override
public Placement find(Player player, AIConfig config,
List<Letter> required, List<Letter> excluded) {
Placement bestPlacement = null;
SearchPlacementImpl hori = null, vert = null;
// Use multithreading if has enough core (at least 2)
if (Main_old.CPU_CORES_NUMBER > 1) {
hori = new SearchPlacementImpl(
this.game, player, config, required, excluded, false);
vert = new SearchPlacementImpl(
this.game, player, config, required, excluded, true);
hori.start();
vert.start();
try {
hori.join();
}
catch (InterruptedException ex) {
Display.fatal("Search Placement",
"An error occured during horizontal search");
}
try {
vert.join();
}
catch (InterruptedException ex) {
Display.fatal("Search Placement",
"An error occured during vertical search");
}
}
// Use singlethreading else (sequential)
else {
hori = new SearchPlacementImpl(
this.game, player, config, required, excluded, false);
hori.start();
try {
hori.join();
}
catch (InterruptedException ex) {
Display.fatal("Search Placement",
"An error occured during horizontal search");
}
vert = new SearchPlacementImpl(
this.game, player, config, required, excluded, true);
vert.start();
try {
vert.join();
}
catch (InterruptedException ex) {
Display.fatal("Search Placement",
"An error occured during vertical search");
}
}
List<Placement> best = new ArrayList<Placement>(16);
int id = 0;
Iterator<Placement> itrH = hori.getBestPlacements();
while (itrH.hasNext()) {
Placement current = itrH.next();
current.setID(id);
id++;
best.add(current);
}
Iterator<Placement> itrV = vert.getBestPlacements();
while (itrV.hasNext()) {
Placement current = itrV.next();
current.setID(id);
id++;
best.add(current);
}
Collections.sort(best);
List<Placement> allBest = new ArrayList<Placement>(13);
int len = best.size();
for (int i = 0; i < len; i++) {
Placement p = best.get(len - 1 - i);
if (i < 13) {
allBest.add(p);
}
else {
p.clearActions();
}
}
// Display list of best placements found
if (Debug.STATE) {
Iterator<Placement> itr = allBest.iterator();
while (itr.hasNext()) {
Placement p = itr.next();
System.out.println(p.getScore() + " " + p.getFirstWord());
}
System.out.println("-----");
}
// Keep best placement found
Placement bestHoriPlacement = hori.getBestPlacement();
Placement bestVertPlacement = vert.getBestPlacement();
if (bestVertPlacement.getScore() > bestHoriPlacement.getScore()) {
bestPlacement = bestVertPlacement;
bestHoriPlacement.clearActions();
}
else {
bestPlacement = bestHoriPlacement;
bestVertPlacement.clearActions();
}
bestHoriPlacement = null;
bestVertPlacement = null;
hori = null;