From 238216b8263cf17501f226d1dbb2559686bf291c Mon Sep 17 00:00:00 2001 From: Christopher Luc Date: Wed, 24 Dec 2014 02:46:08 -0500 Subject: [PATCH] Refactored code in WordSearchGridView for easier readability. Modified constants for difficulties word search manager --- .../funsearch/WordSearchManager.java | 14 +- .../ui/gameplay/WordSearchFragment.java | 3 +- .../ui/gameplay/WordSearchGridView.java | 123 +++++++++--------- .../main/res/layout/wordsearch_fragment.xml | 8 +- 4 files changed, 76 insertions(+), 72 deletions(-) diff --git a/app/src/main/java/chrisjluc/funsearch/WordSearchManager.java b/app/src/main/java/chrisjluc/funsearch/WordSearchManager.java index 002bcdd..6587b6f 100644 --- a/app/src/main/java/chrisjluc/funsearch/WordSearchManager.java +++ b/app/src/main/java/chrisjluc/funsearch/WordSearchManager.java @@ -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; diff --git a/app/src/main/java/chrisjluc/funsearch/ui/gameplay/WordSearchFragment.java b/app/src/main/java/chrisjluc/funsearch/ui/gameplay/WordSearchFragment.java index 88109f6..7a38e89 100644 --- a/app/src/main/java/chrisjluc/funsearch/ui/gameplay/WordSearchFragment.java +++ b/app/src/main/java/chrisjluc/funsearch/ui/gameplay/WordSearchFragment.java @@ -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; } } diff --git a/app/src/main/java/chrisjluc/funsearch/ui/gameplay/WordSearchGridView.java b/app/src/main/java/chrisjluc/funsearch/ui/gameplay/WordSearchGridView.java index a704179..b056660 100644 --- a/app/src/main/java/chrisjluc/funsearch/ui/gameplay/WordSearchGridView.java +++ b/app/src/main/java/chrisjluc/funsearch/ui/gameplay/WordSearchGridView.java @@ -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; @@ -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 nodes; - private List 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 mWordSearchNodes; + private List 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 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(); + WordSearchGenerator wordSearch = manager.getWordSearch(WordSearchActivity.currentItem); + mXLength = wordSearch.nCol; + mYLength = wordSearch.nRow; + mWord = wordSearch.word; + List 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(); + 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; @@ -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) @@ -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: @@ -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; @@ -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; + } } diff --git a/app/src/main/res/layout/wordsearch_fragment.xml b/app/src/main/res/layout/wordsearch_fragment.xml index 08b58df..5b690b7 100644 --- a/app/src/main/res/layout/wordsearch_fragment.xml +++ b/app/src/main/res/layout/wordsearch_fragment.xml @@ -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">