From 9769fc0c6d6dcf81a226431a767f2bbb865a5909 Mon Sep 17 00:00:00 2001 From: Malte Tammena Date: Thu, 19 Oct 2017 14:26:12 +0200 Subject: [PATCH] Worked on Patterns for the ai --- Makefile | 1 + src/player/malte/MalteAI.java | 61 +++++++++++++++---------- src/player/malte/Pattern.java | 13 ------ src/player/malte/PatternGenerator.java | 63 ++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 36 deletions(-) create mode 100644 src/player/malte/PatternGenerator.java diff --git a/Makefile b/Makefile index 3b45812..320d6ff 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ src\game\PlayerObject.java \ src\player\Player.java \ src\player\malte\MalteAI.java \ src\player\malte\Pattern.java \ +src\player\malte\PatternGenerator.java \ src\player\malte\Item.java \ src\player\maurizio\MaurizioAI.java diff --git a/src/player/malte/MalteAI.java b/src/player/malte/MalteAI.java index 404dd81..df17d17 100644 --- a/src/player/malte/MalteAI.java +++ b/src/player/malte/MalteAI.java @@ -21,6 +21,7 @@ public class MalteAI implements Player{ public void setPlayerID(int id) { this.id = id; + this.enemyID = id == 1 ? 2: 1; } public void setEnemyID(int id) { @@ -34,35 +35,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); @@ -72,7 +88,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; + } +}