Implemented twoOfFourPatterns, ai development
This commit is contained in:
parent
bc9b43fcc0
commit
e77cd680e3
|
@ -74,6 +74,12 @@ public class MalteAI implements Player{
|
|||
for (Integer i: preventionsOptions) {
|
||||
return i.intValue();
|
||||
}
|
||||
// Choose a move that will continue a sequence that already exists.
|
||||
Set<Integer> twoOfFour = getTwoOfFourOptions(options, board, id);
|
||||
for (Integer i: twoOfFour) {
|
||||
return i.intValue();
|
||||
}
|
||||
// Choose a move that will not lead to an instant win of the enemy
|
||||
Set<Integer> choosewisely = new HashSet<>(options);
|
||||
while (choosewisely.size() > 0){
|
||||
Integer i = takeRandom(choosewisely);
|
||||
|
@ -87,6 +93,27 @@ public class MalteAI implements Player{
|
|||
return takeRandom(options).intValue();
|
||||
}
|
||||
|
||||
private Set<Integer> getTwoOfFourOptions(Set<Integer> options, int[][] board, int id) {
|
||||
Set<Pattern> twoOfFourPattern = PatternGenerator.winInTwoPatterns(id);
|
||||
// Get patterns, that match anywhere on the board.
|
||||
Set<Pattern> matches = Pattern.matchingPatterns(twoOfFourPattern, board);
|
||||
// Create set to be returned.
|
||||
Set<Integer> ret = new HashSet<>();
|
||||
// Iterate over all matches.
|
||||
for (Pattern p: matches) {
|
||||
// Get all positions, this pattern matches.
|
||||
Set<Position> positions = p.matches(board);
|
||||
// Get empty spaces in the pattern.
|
||||
for (Item i: p.getZeros()) {
|
||||
// Add all options to the set.
|
||||
for(Position pos: positions) {
|
||||
ret.add(new Integer(i.getPosX() + pos.getPosX()));
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the given board.
|
||||
*
|
||||
|
|
|
@ -48,6 +48,29 @@ public class PatternGenerator {
|
|||
return pats;
|
||||
}
|
||||
|
||||
public static Set<Pattern> winInTwoPatterns(int id) {
|
||||
// Four in a row with two holes
|
||||
Set<Pattern> pats = PatternGenerator.empty2SpaceGenerator(new Item(0, 0, id),
|
||||
new Item(1, 0, id),
|
||||
new Item(2, 0, id),
|
||||
new Item(3, 0, id));
|
||||
// Four in a diagonal line from lower left to upper right with two holes
|
||||
pats.addAll(PatternGenerator.empty2SpaceGenerator(new Item(0, 0, id),
|
||||
new Item(-1, 1, id),
|
||||
new Item(-2, 2, id),
|
||||
new Item(-3, 3, id)));
|
||||
// Four in a diagonal line from upper left to lower right with two holes
|
||||
pats.addAll(PatternGenerator.empty2SpaceGenerator(new Item(0, 0, id),
|
||||
new Item(1, 1, id),
|
||||
new Item(2, 2, id),
|
||||
new Item(3, 3, id)));
|
||||
// Three on top of each other.
|
||||
pats.add(new Pattern(new Item(0, 0, 0),
|
||||
new Item(0, 1, id),
|
||||
new Item(0, 2, id)));
|
||||
return pats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates patterns with one empty space.
|
||||
* Returns a set of patterns, each one containing an empty space.
|
||||
|
@ -76,25 +99,40 @@ public class PatternGenerator {
|
|||
}
|
||||
|
||||
/**
|
||||
* TODO: THIS
|
||||
* Generates Patterns with two empty holes.
|
||||
* Generates all possible patterns given the parts, which contain
|
||||
* two empty spaces supported by two parts.
|
||||
*
|
||||
* @param model The pattern's basis.
|
||||
* @return All possible patterns with two holes.
|
||||
*/
|
||||
public static Set<Pattern> empty2SpaceGenerator(Item... model) {
|
||||
Set<Pattern> ret = new HashSet<>();
|
||||
// Iterate over all models.
|
||||
for (int i = 0; i < model.length; i++) {
|
||||
Pattern newP = new Pattern(model);
|
||||
Item x = model[i];
|
||||
Item newI = new Item(x.getPosX(), x.getPosY(), 0);
|
||||
newP.replaceItem(x, newI);
|
||||
newP.addItem(new Item(x.getPosX(), x.getPosY() + 1, new int[]{1, 2}));
|
||||
ret.add(newP);
|
||||
for (int j = 0; j < model.length; j++) {
|
||||
// If we're looking at the same model, skip.
|
||||
if (i == j) {
|
||||
continue;
|
||||
}
|
||||
// Create a new Pattern with the models.
|
||||
Pattern newP = new Pattern(model);
|
||||
Item x = model[i];
|
||||
Item y = model[j];
|
||||
// Create new Items to replace two items.
|
||||
Item newXI = new Item(x.getPosX(), x.getPosY(), 0);
|
||||
Item newYI = new Item(y.getPosX(), y.getPosY(), 0);
|
||||
// Actually replace them.
|
||||
newP.replaceItem(x, newXI);
|
||||
newP.replaceItem(y, newYI);
|
||||
// Add support items.
|
||||
newP.addItem(new Item(x.getPosX(), x.getPosY() + 1, new int[]{-1, 1, 2}));
|
||||
newP.addItem(new Item(y.getPosX(), y.getPosY() + 1, new int[]{-1, 1, 2}));
|
||||
// Add the pattern to the set.
|
||||
ret.add(newP);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: THIS
|
||||
*/
|
||||
public static Set<Pattern> twoOfFourRowPatterns(int id) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue