Skip to content

Commit

Permalink
Refactored code in WordSearchGridView for easier readability. Modifie…
Browse files Browse the repository at this point in the history
…d constants for difficulties word search manager
  • Loading branch information
chrisjluc committed Dec 24, 2014
1 parent 60ede7d commit 238216b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 72 deletions.
14 changes: 7 additions & 7 deletions app/src/main/java/chrisjluc/funsearch/WordSearchManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ public class WordSearchManager {

private final static int MEDIUM_MIN_WORDLENGTH = 3;
private final static int MEDIUM_MAX_WORDLENGTH = 4;
private final static int MEDIUM_MIN_DIMENSION_OFFSET = 0;
private final static int MEDIUM_MIN_DIMENSION_OFFSET = 1;
private final static int MEDIUM_MAX_DIMENSION_OFFSET = 1;

private final static int HARD_MIN_WORDLENGTH = 4;
private final static int HARD_MIN_WORDLENGTH = 5;
private final static int HARD_MAX_WORDLENGTH = 6;
private final static int HARD_MIN_DIMENSION_OFFSET = 1;
private final static int HARD_MAX_DIMENSION_OFFSET = 2;
private final static int HARD_MIN_DIMENSION_OFFSET = 2;
private final static int HARD_MAX_DIMENSION_OFFSET = 3;

private final static int ADVANCED_MIN_WORDLENGTH = 5;
private final static int ADVANCED_MIN_WORDLENGTH = 7;
private final static int ADVANCED_MAX_WORDLENGTH = 9;
private final static int ADVANCED_MIN_DIMENSION_OFFSET = 2;
private final static int ADVANCED_MAX_DIMENSION_OFFSET = 4;
private final static int ADVANCED_MIN_DIMENSION_OFFSET = 3;
private final static int ADVANCED_MAX_DIMENSION_OFFSET = 5;

private final static String[] WORDS = {"alfred", "hello", "hey", "heat", "time", "steam", "elephant", "scissor", "point", "star", "tree", "bob", "airplane", "tail", "mouth", "chin", "phone", "jar", "ear", "drum", "room"};
private final static int SIZE = 6;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.wordsearch_fragment, container, false);
TextView tv = (TextView) rootView.findViewById(R.id.section_label);
WordSearchGenerator generator = WordSearchManager.getInstance().getGenerator(WordSearchActivity.currentItem);
tv.setText(generator.word);
WordSearchGridView grid = (WordSearchGridView) rootView.findViewById(R.id.gridView);
grid.setWordFoundListener((WordSearchGridView.WordFoundListener)getActivity());
tv.setText(grid.getWord());
return rootView;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.List;

import chrisjluc.funsearch.R;
import chrisjluc.funsearch.WordSearchManager;
import chrisjluc.funsearch.adapters.WordSearchGridAdapter;
import chrisjluc.funsearch.wordSearchGenerator.generators.WordSearchGenerator;
Expand All @@ -17,84 +18,84 @@

public class WordSearchGridView extends GridView {

private int xLength;
private int yLength;
private String word;
private Point p1, p2;
private int dimension;
private int marginLeft = 64;
private int padding = 16;
private List<Node> nodes;
private List<Node> highlightedNodes;
private Point startWordPoint;
private Point endWordPoint;
public boolean isFound = false;
private WordFoundListener listener;

WordSearchGridAdapter adapter;
private int mXLength, mYLength;
private int mColumnWidth;
private int mHorizontalMargin, mVerticalMargin;
private Point mStartDrag, mEndDrag;
private List<Node> mWordSearchNodes;
private List<Node> mWordSearchHighlightedNodes;
private String mWord;
private Point mWordStart, mWordEnd;
public boolean mIsWordFound = false;
private WordFoundListener mListener;
private WordSearchGridAdapter mAdapter;

int x1, y1;
int x2, y2;

public WordSearchGridView(Context context, AttributeSet attrs) {
super(context, attrs);

WordSearchManager manager = WordSearchManager.getInstance();
WordSearchGenerator generator = manager.getGenerator(WordSearchActivity.currentItem);
xLength = generator.nCol;
yLength = generator.nRow;
word = generator.word;
List<Point> points = generator.getStartAndEndPointOfWord();
startWordPoint = new Point(points.get(0).y, points.get(0).x);
endWordPoint = new Point(points.get(1).y, points.get(1).x);
nodes = generator.generateNodeList();
highlightedNodes = new ArrayList<Node>();
WordSearchGenerator wordSearch = manager.getWordSearch(WordSearchActivity.currentItem);
mXLength = wordSearch.nCol;
mYLength = wordSearch.nRow;
mWord = wordSearch.word;
List<Point> points = wordSearch.getStartAndEndPointOfWord();

// Convert cartesian from matrix coordinates
mWordStart = new Point(points.get(0).y, points.get(0).x);
mWordEnd = new Point(points.get(1).y, points.get(1).x);
mWordSearchNodes = wordSearch.generateNodeList();
mWordSearchHighlightedNodes = new ArrayList<Node>();
mHorizontalMargin = (int) getResources().getDimension(R.dimen.activity_horizontal_margin);
mVerticalMargin = (int) getResources().getDimension(R.dimen.activity_vertical_margin);

// Calculate column dimensions
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
dimension = (width - 2 * marginLeft) / (xLength);
setColumnWidth(dimension);
adapter = new WordSearchGridAdapter(context, nodes, dimension);
setAdapter(adapter);
mColumnWidth = (width - 2 * mHorizontalMargin / 3) / (mXLength);
setColumnWidth(mColumnWidth);
mAdapter = new WordSearchGridAdapter(context, mWordSearchNodes, mColumnWidth);
setAdapter(mAdapter);
}

public void setWordFoundListener(WordFoundListener listener) {
this.listener = listener;
this.mListener = listener;
}

int x1, y1;
int x2, y2;

private void isWordFound() {
if ((startWordPoint.equals(p1) && endWordPoint.equals(p2))
|| (startWordPoint.equals(p2) && endWordPoint.equals(p1))) {
isFound = true;
listener.notifyWordFound();
if ((mWordStart.equals(mStartDrag) && mWordEnd.equals(mEndDrag))
|| (mWordStart.equals(mEndDrag) && mWordEnd.equals(mStartDrag))) {
mIsWordFound = true;
mListener.notifyWordFound();
}
}

private void updateCurrentHighlightedNodes(Point p) {
if (p.y < 0 || p.y >= yLength || p.x < 0 || p.x >= xLength) return;
if (p.y < 0 || p.y >= mYLength || p.x < 0 || p.x >= mXLength) return;

if (p.equals(p2)) return;
if (p.equals(mEndDrag)) return;

p2 = p;
mEndDrag = p;

boolean isValid = false;
// diagonal
if (Math.abs(p2.x - p1.x) == Math.abs(p2.y - p1.y)) {
if (Math.abs(mEndDrag.x - mStartDrag.x) == Math.abs(mEndDrag.y - mStartDrag.y)) {
isValid = true;
// horizontal
} else if (p2.x - p1.x == 0) {
} else if (mEndDrag.x - mStartDrag.x == 0) {
isValid = true;
// vertical
} else if (p2.y - p1.y == 0) {
} else if (mEndDrag.y - mStartDrag.y == 0) {
isValid = true;
}

if (!isValid) return;

clearHighlightedNodes();
int dX = p2.x - p1.x;
int dY = p2.y - p1.y;
int dX = mEndDrag.x - mStartDrag.x;
int dY = mEndDrag.y - mStartDrag.y;

int length = 0;

Expand All @@ -105,7 +106,7 @@ private void updateCurrentHighlightedNodes(Point p) {
length = Math.abs(dY);

for (int i = 0; i < length + 1; i++) {
Point point = new Point(p1.x, p1.y);
Point point = new Point(mStartDrag.x, mStartDrag.y);
if (dX != 0)
point.x += dX > 0 ? i : -i;
if (dY != 0)
Expand All @@ -116,37 +117,37 @@ private void updateCurrentHighlightedNodes(Point p) {
System.out.println(e.getMessage());
}
}
adapter.notifyDataSetChanged();
mAdapter.notifyDataSetChanged();
}

private void highlightNodeAt(Point p) throws Exception {
int index = p.y * xLength + p.x;
if (index < 0 || index >= nodes.size()) {
int index = p.y * mXLength + p.x;
if (index < 0 || index >= mWordSearchNodes.size()) {
throw new Exception("Invalid Row: " + p.y + " and col: " + p.x);
}
Node n = nodes.get(index);
if (!highlightedNodes.contains(n)) {
highlightedNodes.add(n);
Node n = mWordSearchNodes.get(index);
if (!mWordSearchHighlightedNodes.contains(n)) {
mWordSearchHighlightedNodes.add(n);
n.setHighlighted(true);
}
}

private void clearHighlightedNodes() {
for (Node n : highlightedNodes)
for (Node n : mWordSearchHighlightedNodes)
n.setHighlighted(false);
highlightedNodes.clear();
adapter.notifyDataSetChanged();
mWordSearchHighlightedNodes.clear();
mAdapter.notifyDataSetChanged();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
boolean result = false;
if (isFound) return false;
if (mIsWordFound) return false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = x2 = (int) event.getX();
y1 = y2 = (int) event.getY();
p1 = new Point(calcRelativeX(x1), calcRelativeY(y1));
mStartDrag = new Point(calcRelativeX(x1), calcRelativeY(y1));
result = true;
break;
case MotionEvent.ACTION_MOVE:
Expand All @@ -159,7 +160,7 @@ public boolean onTouchEvent(MotionEvent event) {
x2 = (int) event.getX();
y2 = (int) event.getY();
isWordFound();
if (!isFound)
if (!mIsWordFound)
clearHighlightedNodes();
result = true;
break;
Expand All @@ -170,14 +171,18 @@ public boolean onTouchEvent(MotionEvent event) {
}

private int calcRelativeX(int d) {
return (d - padding) / dimension;
return (d - mHorizontalMargin) / mColumnWidth;
}

private int calcRelativeY(int d) {
return (d - padding) / dimension;
return (d - mVerticalMargin) / mColumnWidth;
}

public interface WordFoundListener {
public void notifyWordFound();
}

public String getWord() {
return mWord;
}
}
8 changes: 4 additions & 4 deletions app/src/main/res/layout/wordsearch_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
tools:context=".WordSearchActivity$WordSearchFragment">

<TextView
Expand Down

0 comments on commit 238216b

Please sign in to comment.