Worked on Patterns for the ai
This commit is contained in:
parent
b0ed66a786
commit
9769fc0c6d
1
Makefile
1
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
|
||||
|
||||
|
|
|
@ -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<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);
|
||||
|
@ -72,7 +88,6 @@ public class MalteAI implements Player{
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
63
src/player/malte/PatternGenerator.java
Normal file
63
src/player/malte/PatternGenerator.java
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue