Working on Patterns, doing great
This commit is contained in:
parent
f0c3999797
commit
dee946cb5a
|
@ -34,26 +34,41 @@ public class MalteAI implements Player{
|
|||
options.remove(i);
|
||||
}
|
||||
}
|
||||
Set<Integer> instantWins = getWinningOptions(options, board);
|
||||
// Set<Integer> preventions = getPreventionOptions(options, board);
|
||||
Set<Integer> winningOptions = getWinningOptions(options, board);
|
||||
for (Integer i: winningOptions) {
|
||||
return i.intValue();
|
||||
}
|
||||
|
||||
return takeRandom(options).intValue();
|
||||
}
|
||||
|
||||
private Set<Integer> getWinningOptions(Set<Integer> 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<Position> matches = pats.matches(board);
|
||||
Set<Integer> 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<Integer> copySet(Set<Integer> s) {
|
||||
|
|
|
@ -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<Position> 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<Position> 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<Position> matches(Pattern[] pats, int[][] board) {
|
||||
Set<Position> ret = new HashSet<>();
|
||||
for (Pattern p: pats) {
|
||||
ret.addAll(p.matches(board));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
|
27
src/player/malte/Position.java
Normal file
27
src/player/malte/Position.java
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue