Having problems with the Pattern, losing

This commit is contained in:
Malte Tammena 2017-10-19 13:44:03 +02:00
parent e126b18cde
commit 5495b3e157
4 changed files with 84 additions and 5 deletions

View file

@ -86,6 +86,7 @@ public class Game {
for (int i = 0; i < runs; i++) {
int result = new Game(p1, p2).start(false, true);
// TODO: Improve IDs
System.out.println(result);
statistic[result]++;
}
double[] percents = new double[3];
@ -140,6 +141,7 @@ public class Game {
return state; // the result of the game
}
}
System.out.println("BUG!");
return checkState(false); // it is impossible to reach this, but it makes the compiler happy ;)
}
@ -166,7 +168,8 @@ public class Game {
// Check his choice against the current board.
if (choice < 0 || choice > GAME_COLUMNS || this.board[choice][0] != 0) {
// If a player makes a false move, the game punishes him.
this.gameOn = false;
// TODO: Fix this, causes -1 return of checkstate in Simulation mode, breaks the game.
// this.gameOn = false;
//TODO the game gets stopped, but checkState doesn't know what that means!?
if(output){
log(currentP.getP().getName() + " made an illegal move and lost!");

View file

@ -36,4 +36,31 @@ public class Item {
}
return false;
}
public void changeID(int oldID, int newID) {
for (int id: ids) {
if (id == newID) {
return;
}
}
for (int i = 0; i < ids.length; i++) {
if (ids[i] == oldID) {
ids[i] = newID;
return;
}
}
}
@Override
public String toString() {
String s = String.format("(%d, %d, [", posX, posY);
for (int i: ids) {
s += i + " ";
}
return s + "])";
}
public Item copy() {
return new Item(posX, posY, ids);
}
}

View file

@ -31,6 +31,7 @@ public class MalteAI implements Player{
}
Set<Integer> winningOptions = getWinningOptions(options, board);
for (Integer i: winningOptions) {
System.out.println(i);
return i.intValue();
}
@ -57,10 +58,15 @@ public class MalteAI implements Player{
new Item(0, 1, id),
new Item(0, 2, id),
new Item(0, 3, id)));
Set<Position> matches = Pattern.matches(patterns, board);
// for (Pattern p: patterns) {
// System.out.println(p);
// }
Set<Pattern> matches = Pattern.matchingPatterns(patterns, board);
Set<Integer> ret = new HashSet<>();
for (Position x: matches) {
for (Pattern p: matches) {
for (Item i: p.getZeros()) {
ret.add(new Integer(i.getPosX()));
}
}
return ret;

View file

@ -14,6 +14,10 @@ public class Pattern {
this.parts = new HashSet<Item>(Arrays.asList(parts));
}
public Pattern(Set<Item> parts) {
this.parts = parts;
}
public Set<Position> matches(int[][] board) {
// Preparing iteration
int maxLeft = 0,
@ -72,6 +76,14 @@ public class Pattern {
return ret;
}
public Pattern copy() {
Set<Item> itemCopy = new HashSet<>();
for (Item i: parts) {
itemCopy.add(i.copy());
}
return new Pattern(itemCopy);
}
public static Set<Position> matches(Set<Pattern> pats, int[][] board) {
Set<Position> ret = new HashSet<>();
for (Pattern p: pats) {
@ -80,6 +92,16 @@ public class Pattern {
return ret;
}
public static Set<Pattern> matchingPatterns(Set<Pattern> pats, int[][] board) {
Set<Pattern> ret = new HashSet<>();
for (Pattern p: pats) {
if (p.matches(board).size() > 0) {
ret.add(p);
}
}
return ret;
}
public static Set<Pattern> emptySpaceGenerator(Item... model) {
Set<Pattern> ret = new HashSet<>();
for (int i = 0; i < model.length; i++) {
@ -87,9 +109,30 @@ public class Pattern {
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}));
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) {
Pattern cpy = p.copy();
for (Item i: p.parts) {
i.changeID(oldID, newID);
}
ret.add(cpy);
}
return ret;
}
@Override
public String toString() {
String s = "";
for (Item i: parts) {
s += i + ", ";
}
return s + "\n";
}
}