From e77cd680e320ab709fd86a3771cb079d899adf22 Mon Sep 17 00:00:00 2001 From: Malte Tammena Date: Fri, 20 Oct 2017 22:05:35 +0200 Subject: [PATCH] Implemented twoOfFourPatterns, ai development --- src/player/malte/MalteAI.java | 27 +++++++++++ src/player/malte/PatternGenerator.java | 64 ++++++++++++++++++++------ 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/src/player/malte/MalteAI.java b/src/player/malte/MalteAI.java index 53ac38e..6b7b540 100644 --- a/src/player/malte/MalteAI.java +++ b/src/player/malte/MalteAI.java @@ -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 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 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 getTwoOfFourOptions(Set options, int[][] board, int id) { + Set twoOfFourPattern = PatternGenerator.winInTwoPatterns(id); + // Get patterns, that match anywhere on the board. + Set matches = Pattern.matchingPatterns(twoOfFourPattern, board); + // Create set to be returned. + Set ret = new HashSet<>(); + // Iterate over all matches. + for (Pattern p: matches) { + // Get all positions, this pattern matches. + Set 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. * diff --git a/src/player/malte/PatternGenerator.java b/src/player/malte/PatternGenerator.java index 33364e1..5b34f2b 100644 --- a/src/player/malte/PatternGenerator.java +++ b/src/player/malte/PatternGenerator.java @@ -48,6 +48,29 @@ public class PatternGenerator { return pats; } + public static Set winInTwoPatterns(int id) { + // Four in a row with two holes + Set 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 empty2SpaceGenerator(Item... model) { Set 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 twoOfFourRowPatterns(int id) { - return null; - } }