Working on Patterns, doing great

This commit is contained in:
Malte Tammena 2017-10-18 15:16:05 +02:00
parent 2d106f36bc
commit bbd22f830e
4 changed files with 110 additions and 18 deletions

View file

@ -13,12 +13,12 @@ public class Game {
/**
* Number of columns on the board.
*/
private final static int GAME_COLUMNS = 7;
public final static int GAME_COLUMNS = 7;
/**
* Number of rows on the board.
*/
private final static int GAME_ROWS = 6;
public final static int GAME_ROWS = 6;
/**
* Player One

View file

@ -11,7 +11,7 @@ public class MalteAI implements Player{
private String name;
private Random ran;
private int id = 2;
private int id = 1;
public MalteAI(String name){
this.name = name;
@ -25,26 +25,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),
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));
Pattern leftInRow = new Pattern(new Item(0, 0, 0),
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));
Pattern rightInRow = new Pattern(new Item(0, 0, 0),
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));
return null;
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) {

View file

@ -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;
}
}

View 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);
}
}