Worked on Patterns for the ai

This commit is contained in:
Malte Tammena 2017-10-19 14:26:12 +02:00
parent 6232484d63
commit 33c16320a9
4 changed files with 103 additions and 36 deletions

View file

@ -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)

View file

@ -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<Integer> winningOptions = getWinningOptions(options, board);
Set<Integer> winningOptions = getRowCompletionOptions(options, board, id);
for (Integer i: winningOptions) {
System.out.println("WINNING");
return i.intValue();
}
Set<Integer> 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<Integer> getWinningOptions(Set<Integer> options, int[][] board) {
// Four in a row with one hole
Set<Pattern> 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<Pattern> 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<Integer> getRowCompletionOptions(Set<Integer> options, int[][] board, int id) {
Set<Pattern> pats = PatternGenerator.winningPatterns(id);
Set<Pattern> matches = Pattern.matchingPatterns(pats, board);
Set<Integer> ret = new HashSet<>();
for (Pattern p: matches) {
Set<Position> positions = p.matches(board);
@ -67,7 +84,6 @@ public class MalteAI implements Player{
}
}
}
return ret;
}

View file

@ -102,19 +102,6 @@ public class Pattern {
return ret;
}
public static Set<Pattern> emptySpaceGenerator(Item... model) {
Set<Pattern> 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<Pattern> changeID(Set<Pattern> pats, int oldID, int newID) {
Set<Pattern> ret = new HashSet<>();
for (Pattern p: pats) {

View file

@ -0,0 +1,63 @@
package player.malte;
import java.util.Set;
import java.util.HashSet;
public class PatternGenerator {
private PatternGenerator() {}
public static Set<Pattern> winningPatterns(int id) {
// Four in a row with one hole
Set<Pattern> 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<Pattern> emptySpaceGenerator(Item... model) {
Set<Pattern> 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<Pattern> empty2SpaceGenerator(Item... model) {
Set<Pattern> 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<Pattern> twoOfFourRowPatterns(int id) {
// TODO: THIS
return null;
}
}