Working on pattern implementation
This commit is contained in:
parent
bbd22f830e
commit
3085c9255b
|
@ -44,13 +44,31 @@ public class MalteAI implements Player{
|
||||||
new Pattern(new Item(0, 0, 0),
|
new Pattern(new Item(0, 0, 0),
|
||||||
new Item(1, 0, id),
|
new Item(1, 0, id),
|
||||||
new Item(2, 0, id),
|
new Item(2, 0, id),
|
||||||
new Item(3, 0, id)),
|
new Item(3, 0, id),
|
||||||
|
new Item(0, 1, new int[]{1, 2})),
|
||||||
// Three in a row, empty right
|
// Three in a row, empty right
|
||||||
new Pattern(new Item(0, 0, 0),
|
new Pattern(new Item(0, 0, 0),
|
||||||
new Item(-1, 0, id),
|
new Item(-1, 0, id),
|
||||||
new Item(-2, 0, id),
|
new Item(-2, 0, id),
|
||||||
new Item(-3, 0, id))}
|
new Item(-3, 0, id),
|
||||||
Set<Position> matches = pats.matches(board);
|
new Item(0, 1, new int[]{1, 2})),
|
||||||
|
// Three in a diagonal line from lower left to upper right, empty right above
|
||||||
|
new Pattern(new Item(0, 0, 0),
|
||||||
|
new Item(-1, 1, id),
|
||||||
|
new Item(-2, 2, id),
|
||||||
|
new Item(-3, 3, id),
|
||||||
|
// TODO: Improve IDs here
|
||||||
|
new Item(0, 1, new int[]{1, 2})),
|
||||||
|
// Three in a diagonal line from upper left to lower right, empty left above
|
||||||
|
new Pattern(new Item(0, 0, 0),
|
||||||
|
new Item(1, 1, id),
|
||||||
|
new Item(2, 2, id),
|
||||||
|
new Item(3, 3, id),
|
||||||
|
// TODO: Improve IDs here
|
||||||
|
new Item(0, 1, new int[]{1, 2})),
|
||||||
|
};
|
||||||
|
// Set<Pattern> patterns =
|
||||||
|
Set<Position> matches = Pattern.matches(pats, board);
|
||||||
Set<Integer> ret = new HashSet<>();
|
Set<Integer> ret = new HashSet<>();
|
||||||
for (Position x: matches) {
|
for (Position x: matches) {
|
||||||
System.out.println(x);
|
System.out.println(x);
|
||||||
|
|
|
@ -2,15 +2,16 @@ package player.malte;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import game.Game;
|
import game.Game;
|
||||||
|
|
||||||
public class Pattern {
|
public class Pattern {
|
||||||
|
|
||||||
private Item[] parts;
|
private Set<Item> parts;
|
||||||
|
|
||||||
public Pattern(Item... parts) {
|
public Pattern(Item... parts) {
|
||||||
this.parts = parts;
|
this.parts = new HashSet<Item>(Arrays.asList(parts));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Position> matches(int[][] board) {
|
public Set<Position> matches(int[][] board) {
|
||||||
|
@ -50,6 +51,17 @@ public class Pattern {
|
||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void replaceItem(Item oldItem, Item newItem) {
|
||||||
|
if (this.parts.contains(oldItem)) {
|
||||||
|
this.parts.remove(oldItem);
|
||||||
|
this.parts.add(newItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addItem(Item item) {
|
||||||
|
this.parts.add(item);
|
||||||
|
}
|
||||||
|
|
||||||
public static Set<Position> matches(Pattern[] pats, int[][] board) {
|
public static Set<Position> matches(Pattern[] pats, int[][] board) {
|
||||||
Set<Position> ret = new HashSet<>();
|
Set<Position> ret = new HashSet<>();
|
||||||
for (Pattern p: pats) {
|
for (Pattern p: pats) {
|
||||||
|
@ -57,4 +69,17 @@ public class Pattern {
|
||||||
}
|
}
|
||||||
return ret;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue