@@ -81,7 +81,6 @@ protected void createTiles() {
8181 public boolean onTouch (View v , MotionEvent event ) {
8282 try {
8383 GameTile touchedTile = (GameTile )v ;
84- //Ln.d("%s\n\tTile touched: %s", touchedTile, event);
8584 if (touchedTile .isEmpty () || !touchedTile .isInRowOrColumnOf (emptyTile )) {
8685 return false ;
8786 } else {
@@ -139,8 +138,17 @@ protected void moveDraggedTilesByMotionEventDelta(MotionEvent event) {
139138 dyTile = tile .getY () + dyEvent ;
140139
141140 RectF candidateRect = new RectF (dxTile , dyTile , dxTile + tile .getWidth (), dyTile + tile .getHeight ());
141+ HashSet <GameTile > tilesToCheck = null ;
142+ if (tile .coordinate .row == emptyTile .coordinate .row ) {
143+ tilesToCheck = allTilesInRow (tile .coordinate .row );
144+ } else if (tile .coordinate .column == emptyTile .coordinate .column ) {
145+ tilesToCheck = allTilesInColumn (tile .coordinate .column );
146+ }
147+
142148 boolean candidateRectInGameboard = (gameboardRect .contains (candidateRect ));
143- impossibleMove = impossibleMove && (!candidateRectInGameboard || candidateRectForTileCollidesWithAnyOtherTile (tile , candidateRect ));
149+ boolean collides = candidateRectForTileCollidesWithAnyTileInSet (candidateRect , tile , tilesToCheck );
150+
151+ impossibleMove = impossibleMove && (!candidateRectInGameboard || collides );
144152 }
145153 if (!impossibleMove ) {
146154 for (GameTileMotionDescriptor gameTileMotionDescriptor : currentMotionDescriptors ) {
@@ -158,12 +166,11 @@ protected void moveDraggedTilesByMotionEventDelta(MotionEvent event) {
158166 }
159167 }
160168
161- protected boolean candidateRectForTileCollidesWithAnyOtherTile ( GameTile tile , RectF candidateRect ) {
169+ protected boolean candidateRectForTileCollidesWithAnyTileInSet ( RectF candidateRect , GameTile tile , HashSet < GameTile > set ) {
162170 RectF otherTileRect ;
163- for (GameTile otherTile : tiles ) {
171+ for (GameTile otherTile : set ) {
164172 if (!otherTile .isEmpty () && otherTile != tile ) {
165173 otherTileRect = new RectF (otherTile .getX (), otherTile .getY (), otherTile .getX () + otherTile .getWidth (), otherTile .getY () + otherTile .getHeight ());
166- //Ln.d("Checking rect for %s, %s against %s", otherTile, otherTileRect, candidateRect);
167174 if (RectF .intersects (otherTileRect , candidateRect )) {
168175 return true ;
169176 }
@@ -322,6 +329,26 @@ protected GameTile getTileAtCoordinate(Coordinate coordinate) {
322329 }
323330 return null ;
324331 }
332+
333+ protected HashSet <GameTile > allTilesInRow (int row ) {
334+ HashSet <GameTile > tilesInRow = new HashSet <GameTile >();
335+ for (GameTile tile : tiles ) {
336+ if (tile .coordinate .row == row ) {
337+ tilesInRow .add (tile );
338+ }
339+ }
340+ return tilesInRow ;
341+ }
342+
343+ protected HashSet <GameTile > allTilesInColumn (int column ) {
344+ HashSet <GameTile > tilesInColumn = new HashSet <GameTile >();
345+ for (GameTile tile : tiles ) {
346+ if (tile .coordinate .column == column ) {
347+ tilesInColumn .add (tile );
348+ }
349+ }
350+ return tilesInColumn ;
351+ }
325352
326353 protected GameTile createTileAtCoordinate (Coordinate coordinate ) {
327354 GameTile tile = new GameTile (getContext (), coordinate );
0 commit comments