From 0ada4609a0ebc24915dfa904e62bfe6b322fa120 Mon Sep 17 00:00:00 2001 From: Bennet Gerlach Date: Sat, 18 Jun 2011 16:41:55 +0200 Subject: Fixed bug in AI (threw Internal AI Error, stones in hand weren't solved) git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@464 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/ai/TurnLogic.java | 68 +++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 43 deletions(-) (limited to 'src/jrummikub/ai') diff --git a/src/jrummikub/ai/TurnLogic.java b/src/jrummikub/ai/TurnLogic.java index 81391a7..8440425 100644 --- a/src/jrummikub/ai/TurnLogic.java +++ b/src/jrummikub/ai/TurnLogic.java @@ -101,10 +101,8 @@ public class TurnLogic { for (int i = 0; i < jokerIDs.size(); i++) { StoneState left = stones.get(jokerIDs.get(i)); - HashSet leftLeftGroup = new HashSet( - left.leftGroup); - HashSet leftLeftRun = new HashSet( - left.leftRun); + HashSet leftLeftGroup = new HashSet(left.leftGroup); + HashSet leftLeftRun = new HashSet(left.leftRun); leftLeftGroup.remove(null); leftLeftRun.remove(null); int runID, groupID; @@ -216,8 +214,8 @@ public class TurnLogic { public boolean isInterested(HashSet changes) { return !(Collections.disjoint(changes, leftRun) && Collections.disjoint(changes, rightRun) - && Collections.disjoint(changes, leftGroup) && Collections - .disjoint(changes, rightGroup)); + && Collections.disjoint(changes, leftGroup) && Collections.disjoint( + changes, rightGroup)); } public > boolean lessThan(T a, T b) { @@ -285,8 +283,7 @@ public class TurnLogic { if (!other.runNeighbor(this) || !other.rightRun.contains(id)) { leftRun.remove(i); changed = true; - } else if (other.rightRun.size() == 1 - && other.onTable == Boolean.TRUE) { + } else if (other.rightRun.size() == 1 && other.onTable == Boolean.TRUE) { changed |= leftRun.retainAll(Arrays.asList(i)); changed |= onTable != Boolean.TRUE; onTable = true; @@ -306,8 +303,7 @@ public class TurnLogic { if (!this.runNeighbor(other) || !other.leftRun.contains(id)) { rightRun.remove(i); changed = true; - } else if (other.leftRun.size() == 1 - && other.onTable == Boolean.TRUE) { + } else if (other.leftRun.size() == 1 && other.onTable == Boolean.TRUE) { changed |= rightRun.retainAll(Arrays.asList(i)); changed |= onTable != Boolean.TRUE; onTable = true; @@ -324,8 +320,7 @@ public class TurnLogic { relevantChanges.retainAll(changes); for (int i : relevantChanges) { StoneState other = top.stones.get(i); - if (!other.groupNeighbor(this) - || !other.rightGroup.contains(id)) { + if (!other.groupNeighbor(this) || !other.rightGroup.contains(id)) { leftGroup.remove(i); changed = true; } else if (other.rightGroup.size() == 1 @@ -350,8 +345,7 @@ public class TurnLogic { if (!this.groupNeighbor(other) || !other.leftGroup.contains(id)) { rightGroup.remove(i); changed = true; - } else if (other.leftGroup.size() == 1 - && other.onTable == Boolean.TRUE) { + } else if (other.leftGroup.size() == 1 && other.onTable == Boolean.TRUE) { changed |= rightGroup.retainAll(Arrays.asList(i)); changed |= onTable != Boolean.TRUE; onTable = true; @@ -407,9 +401,8 @@ public class TurnLogic { private boolean checkLonelyStone() { boolean changed = false; @SuppressWarnings("unchecked") - List> sets = Arrays.> asList( - leftGroup, rightGroup, leftRun, rightRun, leftGroup, - rightGroup, leftRun); + List> sets = Arrays.> asList(leftGroup, + rightGroup, leftRun, rightRun, leftGroup, rightGroup, leftRun); for (int i = 0; i < 4; i++) { if (isNullSet(sets.get(i)) && isNullSet(sets.get(i + 1)) && isNullSet(sets.get(i + 2))) { @@ -443,8 +436,8 @@ public class TurnLogic { private boolean checkStoneCanBeOnTable() throws Contradiction { boolean changed = false; - if (leftGroup.isEmpty() || rightGroup.isEmpty() - || leftRun.isEmpty() || rightRun.isEmpty()) { + if (leftGroup.isEmpty() || rightGroup.isEmpty() || leftRun.isEmpty() + || rightRun.isEmpty()) { if (onTable == Boolean.TRUE) { throw new Contradiction(); } @@ -472,23 +465,14 @@ public class TurnLogic { } public boolean isSolved() { - boolean solved = false; if (onTable == Boolean.FALSE) { - return solved; + return true; } if (onTable == null || color == null || value == null) { - return solved; - } - if (leftRun.size() > 1 || rightRun.size() > 1 - || leftGroup.size() > 1 || rightGroup.size() > 1) { - return solved; + return false; } - solved = bothGoupAndRun(); - return solved; - } - - private boolean bothGoupAndRun() { - if (!((isNullSet(leftRun) && isNullSet(rightRun)) || (isNullSet(leftGroup) && isNullSet(rightGroup)))) { + if (leftRun.size() > 1 || rightRun.size() > 1 || leftGroup.size() > 1 + || rightGroup.size() > 1) { return false; } return true; @@ -524,11 +508,11 @@ public class TurnLogic { * Creates new turn logic * * @param settings - * the game settings + * the game settings * @param tableStones - * all stones on the table + * all stones on the table * @param handStones - * all stones on the hand + * all stones on the hand */ public TurnLogic(GameSettings settings, Collection tableStones, Collection handStones) { @@ -557,18 +541,17 @@ public class TurnLogic { @Override public int compare(Pair o1, Pair o2) { int cmp; - cmp = ((Boolean) o1.getFirst().isJoker()).compareTo(o2 - .getFirst().isJoker()); + cmp = ((Boolean) o1.getFirst().isJoker()).compareTo(o2.getFirst() + .isJoker()); if (cmp != 0) { return -cmp; } - cmp = (o1.getFirst().getColor()).compareTo(o2.getFirst() - .getColor()); + cmp = (o1.getFirst().getColor()).compareTo(o2.getFirst().getColor()); if (cmp != 0) { return cmp; } - cmp = ((Integer) o1.getFirst().getValue()).compareTo(o2 - .getFirst().getValue()); + cmp = ((Integer) o1.getFirst().getValue()).compareTo(o2.getFirst() + .getValue()); return cmp; } }); @@ -635,8 +618,7 @@ public class TurnLogic { ArrayList setStones = new ArrayList(); while (true) { setStones.add(inputStones.get(stone.id)); - if (isNullSet(stone.rightRun) - && isNullSet(stone.rightGroup)) { + if (isNullSet(stone.rightRun) && isNullSet(stone.rightGroup)) { break; } Integer rightRunID = stone.rightRun.iterator().next(); -- cgit v1.2.3