Skip to content

Commit

Permalink
changed from using copy-constructor to using a method, to allow copyi…
Browse files Browse the repository at this point in the history
…ng any kind of rule
  • Loading branch information
Zomis committed Jan 3, 2015
1 parent cbccf38 commit 7a09a15
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public AnalyzeFactory() {
public AnalyzeResult<T> solve() {
List<FieldRule<T>> original = new ArrayList<FieldRule<T>>(this.rules.size());
for (FieldRule<T> rule : this.rules) {
original.add(new FieldRule<T>(rule));
original.add(rule.copy());
}

List<FieldRule<T>> inProgress = new ArrayList<FieldRule<T>>(this.rules.size());
for (FieldRule<T> rule : this.rules) {
inProgress.add(new FieldRule<T>(rule));
inProgress.add(rule.copy());
}

final List<Solution<T>> solutions = new ArrayList<Solution<T>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private AnalyzeFactory<T> solutionToNewAnalyze(Solution<T> solution, List<FieldR
List<FieldRule<T>> newRules = new ArrayList<FieldRule<T>>();
for (FieldRule<T> rule : extraRules) {
// Create new rules, because the older ones may have been simplified already.
newRules.add(new FieldRule<T>(rule));
newRules.add(rule.copy());
}
AnalyzeFactory<T> newRoot = new AnalyzeFactory<T>(solution, newRules);
return newRoot;
Expand All @@ -103,7 +103,7 @@ public AnalyzeResult<T> cloneAddSolve(List<FieldRule<T>> extraRules) {
newRules.addAll(extraRules);
AnalyzeFactory<T> copy = new AnalyzeFactory<T>();
for (FieldRule<T> rule : newRules) {
copy.addRule(new FieldRule<T>(rule));
copy.addRule(rule.copy());
}
return copy.solve();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ public class BoundedFieldRule<T> extends FieldRule<T> {
*
* @param copyFrom Rule to copy
*/
public BoundedFieldRule(BoundedFieldRule<T> copyFrom) {
super(copyFrom);
private BoundedFieldRule(BoundedFieldRule<T> copyFrom) {
super(copyFrom.getCause(), copyFrom.fields);
this.minResult = copyFrom.minResult;
this.maxResult = copyFrom.maxResult;
}

/**
Expand Down Expand Up @@ -122,4 +124,9 @@ public String toString() {
rule.append(maxResult);
return rule.toString();
}

@Override
public FieldRule<T> copy() {
return new BoundedFieldRule<T>(this);
}
}
17 changes: 14 additions & 3 deletions src/main/java/net/zomis/minesweeper/analyze/FieldRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @author Simon Forsberg
* @param <T> Field type
*/
public class FieldRule<T> {
public class FieldRule<T> implements RuleConstraint<T> {

private final T cause;
protected final List<FieldGroup<T>> fields;
Expand All @@ -22,12 +22,17 @@ public class FieldRule<T> {
*
* @param copyFrom Rule to copy
*/
public FieldRule(FieldRule<T> copyFrom) {
private FieldRule(FieldRule<T> copyFrom) {
this.fields = new ArrayList<FieldGroup<T>>(copyFrom.fields); // Deep copy? Probably not. FieldGroup don't change much.
this.result = copyFrom.result;
this.cause = copyFrom.cause;
}

protected FieldRule(T cause, List<FieldGroup<T>> fields) {
this.cause = cause;
this.fields = new ArrayList<FieldGroup<T>>(fields);
}

/**
* Create a rule from a list of fields and a result (create a new FieldGroup for it)
*
Expand Down Expand Up @@ -121,7 +126,7 @@ public FieldGroup<T> getSmallestFieldGroup() {
return result;
}

public boolean isEmpty () {
public boolean isEmpty() {
return fields.isEmpty() && result == 0;
}

Expand All @@ -131,6 +136,7 @@ public double nCr() {
return Combinatorics.nCr(this.getFieldsCountInGroups(), this.result);
}

@Override
public SimplifyResult simplify(GroupValues<T> knownValues) {
if (this.isEmpty()) {
return SimplifyResult.NO_EFFECT;
Expand Down Expand Up @@ -199,4 +205,9 @@ public String toString() {
rule.append(result);
return rule.toString();
}

@Override
public FieldRule<T> copy() {
return new FieldRule<T>(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private double solveRules(List<Solution<T>> solutions) {

List<FieldRule<T>> rulesCopy = new ArrayList<FieldRule<T>>(); // deep copy!
for (FieldRule<T> rule : this.rules) {
rulesCopy.add(new FieldRule<T>(rule));
rulesCopy.add(rule.copy());
}

total += new GameAnalyze<T>(mapCopy, rulesCopy, this.callback).solve(solutions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.zomis.minesweeper.analyze;

public interface RuleConstraint<T> {

SimplifyResult simplify(GroupValues<T> knownValues);

FieldRule<T> copy();
}

0 comments on commit 7a09a15

Please sign in to comment.