diff --git a/Lib/src/com/yarcat/chemistrylines/field/Cell.java b/Lib/src/com/yarcat/chemistrylines/field/Cell.java index 5958695..a0d148c 100644 --- a/Lib/src/com/yarcat/chemistrylines/field/Cell.java +++ b/Lib/src/com/yarcat/chemistrylines/field/Cell.java @@ -1,9 +1,20 @@ package com.yarcat.chemistrylines.field; +import java.util.EnumSet; + /** Represents a cell in a game field. */ public final class Cell { + // @formatter:off + public enum Mark { + SelectedAsSource, + SelectedAsDestination, + ReachableFromSource, + } + // @formatter:on + private Element mElement = null; + private EnumSet x; public boolean isEmpty() { return getElement() == null; @@ -16,4 +27,18 @@ public void setElement(Element element) { public Element getElement() { return mElement; } -} \ No newline at end of file + + public void setMark(Mark m) { + x.add(m); + } + + public void clearMark(Mark m) { + if (x.contains(m)) { + x.remove(m); + } + } + + public boolean hasMark(Mark m) { + return x.contains(m); + } +} diff --git a/Lib/src/com/yarcat/chemistrylines/field/Field.java b/Lib/src/com/yarcat/chemistrylines/field/Field.java index 659187c..895df50 100644 --- a/Lib/src/com/yarcat/chemistrylines/field/Field.java +++ b/Lib/src/com/yarcat/chemistrylines/field/Field.java @@ -1,5 +1,8 @@ package com.yarcat.chemistrylines.field; +import com.yarcat.chemistrylines.algorithms.Path; +import com.yarcat.chemistrylines.field.Cell.Mark; +import com.yarcat.chemistrylines.view.SelectionInView.SelectionListener; /** Game field (model) interface. */ public interface Field { @@ -41,7 +44,7 @@ public interface SequenceVisitor { public void removeCompound(int[] cells); /* Implementation of functionality common for all fields */ - public static abstract class BaseField implements Field { + public static abstract class BaseField implements Field, SelectionListener { @Override public void removeCompound(int[] cells) { @@ -49,5 +52,47 @@ public void removeCompound(int[] cells) { at(n).setElement(null); } } + + @Override + public void onNewSource(int n) { + if (n > -1) { + at(n).setMark(Mark.SelectedAsSource); + markCellsReachableFrom(n); + } + } + + @Override + public void onNewDestination(int n) { + if (n > -1) { + at(n).setMark(Mark.SelectedAsDestination); + } + } + + @Override + public void onSelectionCleared(int s, int d) { + if (s > -1) { + at(s).clearMark(Mark.SelectedAsSource); + clearCellsReachableFrom(s); + } + if (d > -1) { + at(d).clearMark(Mark.SelectedAsSource); + } + } + + private void markCellsReachableFrom(int n) { + Path p = new Path(this, n); + p.evaluate(); + for (int i = 0; i < getLength(); ++i) { + if (p.isReachable(i)) { + at(i).setMark(Mark.ReachableFromSource); + } + } + } + + private void clearCellsReachableFrom(int n) { + for (int i = 0; i < getLength(); ++i) { + at(i).clearMark(Mark.ReachableFromSource); + } + } } } diff --git a/Lib/src/com/yarcat/chemistrylines/view/SelectionInView.java b/Lib/src/com/yarcat/chemistrylines/view/SelectionInView.java index 00a9a11..a316983 100644 --- a/Lib/src/com/yarcat/chemistrylines/view/SelectionInView.java +++ b/Lib/src/com/yarcat/chemistrylines/view/SelectionInView.java @@ -1,18 +1,37 @@ package com.yarcat.chemistrylines.view; public class SelectionInView { + public interface SelectionListener { + + public void onNewSource(int n); + + public void onNewDestination(int n); + + public void onSelectionCleared(int src, int dst); + } + + private SelectionListener mListener; private int[] mSelection = new int[] { -1, -1 }; public void select(int n) { if (mSelection[0] == -1) { mSelection[0] = n; + if (mListener != null) { + mListener.onNewSource(n); + } } else { mSelection[1] = n; + if (mListener != null) { + mListener.onNewDestination(n); + } } } public void clear() { mSelection[0] = mSelection[1] = -1; + if (mListener != null) { + mListener.onSelectionCleared(mSelection[0], mSelection[1]); + } } public boolean hasSource() { @@ -30,4 +49,8 @@ public boolean hasDestination() { public int getDestination() { return mSelection[1]; } + + public void setListener(SelectionListener l) { + mListener = l; + } }