diff --git a/Makefile b/Makefile index e134f54..48429a8 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ src/player/Player.java \ src/player/malte/MalteAI.java \ src/player/malte/Pattern.java \ src/player/malte/Item.java \ +src/player/malte/PatternGenerator.java \ src/player/maurizio/MaurizioAI.java OBJECTS=$($(subst $(CLASSPATH),$(BUILDS),$(CLASSES)):.java=.class) diff --git a/src/player/malte/MalteAI.java b/src/player/malte/MalteAI.java index 95207f8..b719ae1 100644 --- a/src/player/malte/MalteAI.java +++ b/src/player/malte/MalteAI.java @@ -12,6 +12,7 @@ public class MalteAI implements Player{ private String name; private Random ran; private int id; + private int enemyID; public MalteAI(String name){ this.name = name; @@ -20,6 +21,7 @@ public class MalteAI implements Player{ public void setPlayerID(int id) { this.id = id; + this.enemyID = id == 1 ? 2: 1; } public int move(int[][] board){ @@ -29,35 +31,50 @@ public class MalteAI implements Player{ options.remove(i); } } - Set winningOptions = getWinningOptions(options, board); + Set winningOptions = getRowCompletionOptions(options, board, id); for (Integer i: winningOptions) { + System.out.println("WINNING"); return i.intValue(); } + Set preventionsOptions = getRowCompletionOptions(options, board, enemyID); + for (Integer i: preventionsOptions) { + int[][] fakeBoard = makeMove(copyBoard(board), i, id); + if (getRowCompletionOptions(options, fakeBoard, enemyID).size() == 0) { + System.out.println("PREVENTING"); + return i.intValue(); + } + } + + System.out.println("RANDOM"); return takeRandom(options).intValue(); } - private Set getWinningOptions(Set options, int[][] board) { - // Four in a row with one hole - Set patterns = Pattern.emptySpaceGenerator(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 one hole - patterns.addAll(Pattern.emptySpaceGenerator(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 one hole - patterns.addAll(Pattern.emptySpaceGenerator(new Item(0, 0, id), - new Item(1, 1, id), - new Item(2, 2, id), - new Item(3, 3, id))); - patterns.add(new Pattern(new Item(0, 0, 0), - new Item(0, 1, id), - new Item(0, 2, id), - new Item(0, 3, id))); - Set matches = Pattern.matchingPatterns(patterns, board); + private int[][] copyBoard(int[][] board) { + int[][] copy = new int[board.length][board[0].length]; + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[i].length; j++) { + copy[i][j] = board[i][j]; + } + } + return copy; + } + + private int[][] makeMove(int[][] board, int choice, int id) { + int column = 0; + if (board[choice][column] != 0) { + return board; + } + while (board[choice][column + 1] == 0) { + column++; + } + board[choice][column] = id; + return board; + } + + private Set getRowCompletionOptions(Set options, int[][] board, int id) { + Set pats = PatternGenerator.winningPatterns(id); + Set matches = Pattern.matchingPatterns(pats, board); Set ret = new HashSet<>(); for (Pattern p: matches) { Set positions = p.matches(board); @@ -67,7 +84,6 @@ public class MalteAI implements Player{ } } } - return ret; } diff --git a/src/player/malte/Pattern.java b/src/player/malte/Pattern.java index ffab4b2..a44108c 100644 --- a/src/player/malte/Pattern.java +++ b/src/player/malte/Pattern.java @@ -102,19 +102,6 @@ public class Pattern { return ret; } - public static Set emptySpaceGenerator(Item... model) { - Set ret = new HashSet<>(); - 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); - } - return ret; - } - public static Set changeID(Set pats, int oldID, int newID) { Set ret = new HashSet<>(); for (Pattern p: pats) { diff --git a/src/player/malte/PatternGenerator.java b/src/player/malte/PatternGenerator.java new file mode 100644 index 0000000..6f7b5b8 --- /dev/null +++ b/src/player/malte/PatternGenerator.java @@ -0,0 +1,63 @@ +package player.malte; + +import java.util.Set; +import java.util.HashSet; + +public class PatternGenerator { + + private PatternGenerator() {} + + public static Set winningPatterns(int id) { + // Four in a row with one hole + Set pats = PatternGenerator.emptySpaceGenerator(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 one hole + pats.addAll(PatternGenerator.emptySpaceGenerator(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 one hole + pats.addAll(PatternGenerator.emptySpaceGenerator(new Item(0, 0, id), + new Item(1, 1, id), + new Item(2, 2, id), + new Item(3, 3, id))); + pats.add(new Pattern(new Item(0, 0, 0), + new Item(0, 1, id), + new Item(0, 2, id), + new Item(0, 3, id))); + return pats; + } + + public static Set emptySpaceGenerator(Item... model) { + Set ret = new HashSet<>(); + 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); + } + return ret; + } + + public static Set empty2SpaceGenerator(Item... model) { + Set ret = new HashSet<>(); + 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); + } + return ret; + } + + public static Set twoOfFourRowPatterns(int id) { + // TODO: THIS + return null; + } +}