diff --git a/src/player/malte/MalteAI.java b/src/player/malte/MalteAI.java index 32ce9a9..b0c944f 100644 --- a/src/player/malte/MalteAI.java +++ b/src/player/malte/MalteAI.java @@ -34,26 +34,41 @@ public class MalteAI implements Player{ options.remove(i); } } - Set instantWins = getWinningOptions(options, board); - // Set preventions = getPreventionOptions(options, board); + Set winningOptions = getWinningOptions(options, board); + for (Integer i: winningOptions) { + return i.intValue(); + } return takeRandom(options).intValue(); } private Set getWinningOptions(Set options, int[][] board) { - Pattern topOfColumn = new Pattern(new Item(0, 0, 0), - new Item(0, 1, id), - new Item(0, 2, id), - new Item(0, 3, id)); - Pattern leftInRow = new Pattern(new Item(0, 0, 0), - new Item(1, 0, id), - new Item(2, 0, id), - new Item(3, 0, id)); - Pattern rightInRow = new Pattern(new Item(0, 0, 0), - new Item(-1, 0, id), - new Item(-2, 0, id), - new Item(-3, 0, id)); - return null; + Pattern[] pats = new Pattern[]{ + // Three in a column, empty above + new Pattern(new Item(0, 0, 0), + new Item(0, 1, id), + new Item(0, 2, id), + new Item(0, 3, id)), + // Three in a row, empty left + new Pattern(new Item(0, 0, 0), + new Item(1, 0, id), + new Item(2, 0, id), + new Item(3, 0, id)), + // Three in a row, empty right + new Pattern(new Item(0, 0, 0), + new Item(-1, 0, id), + new Item(-2, 0, id), + new Item(-3, 0, id))} + Set matches = pats.matches(board); + Set ret = new HashSet<>(); + for (Position x: matches) { + System.out.println(x); + if (options.contains(x.getPosX())) { + ret.add(new Integer(x.getPosX())); + } + } + + return ret; } private Set copySet(Set s) { diff --git a/src/player/malte/Pattern.java b/src/player/malte/Pattern.java index 156f03f..979b3ca 100644 --- a/src/player/malte/Pattern.java +++ b/src/player/malte/Pattern.java @@ -1,5 +1,10 @@ package player.malte; +import java.util.Set; +import java.util.HashSet; + +import game.Game; + public class Pattern { private Item[] parts; @@ -7,4 +12,49 @@ public class Pattern { public Pattern(Item... parts) { this.parts = parts; } + + public Set matches(int[][] board) { + // Preparing iteration + int maxLeft = 0, + maxRight = 0, + maxUp = 0, + maxDown = 0; + for (Item i: parts) { + if (maxLeft < -i.getPosX()) { + maxLeft = -i.getPosX(); + } + if (maxRight > -i.getPosX()) { + maxRight = -i.getPosX(); + } + if (maxUp < -i.getPosY()) { + maxUp = -i.getPosY(); + } + if (maxDown > -i.getPosY()) { + maxDown = -i.getPosY(); + } + } + // Iteration + Position accumulation + Set set = new HashSet<>(); + for (int i = maxLeft; i < Game.GAME_COLUMNS + maxRight; i++) { + inner:for (int j = maxUp; j < Game.GAME_ROWS + maxDown; j++) { + for (Item k: parts) { + int posX = i + k.getPosX(); + int posY = j + k.getPosY(); + if (!k.hasID(board[posX][posY])) { + continue inner; + } + } + set.add(new Position(i, j)); + } + } + return set; + } + + public static Set matches(Pattern[] pats, int[][] board) { + Set ret = new HashSet<>(); + for (Pattern p: pats) { + ret.addAll(p.matches(board)); + } + return ret; + } } diff --git a/src/player/malte/Position.java b/src/player/malte/Position.java new file mode 100644 index 0000000..3f28b12 --- /dev/null +++ b/src/player/malte/Position.java @@ -0,0 +1,27 @@ +package player.malte; + +import java.lang.String; + +public class Position { + + private final int posX; + private final int posY; + + public Position(int posX, int posY) { + this.posX = posX; + this.posY = posY; + } + + public int getPosX() { + return this.posX; + } + + public int getPosY() { + return this.posY; + } + + @Override + public String toString() { + return String.format("(%d, %d)", posX, posY); + } +}