Created
June 2, 2014 04:39
-
-
Save alexjlockwood/a8245f3336939fb524fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Shifts all tiles to the left, merging consecutive tiles as necessary. | |
*/ | |
public void moveLeft() { | |
if (mIsGameOver) { | |
return; | |
} | |
final int oldScore = mCurrentScore; | |
boolean moved = false; | |
final List<Tile> currentStreak = new ArrayList<>(); | |
final List<Tile> shiftTiles = new ArrayList<>(); | |
final List<Tile> mergeTiles = new ArrayList<>(); | |
for (int row = 0; row < mNumRows; row++) { | |
currentStreak.clear(); | |
shiftTiles.clear(); | |
mergeTiles.clear(); | |
for (int col = 0; col < mNumCols; col++) { | |
final Tile currentTile = mGrid[row][col]; | |
if (currentTile != null) { | |
if (isConsecutiveTile(currentStreak, currentTile)) { | |
// Then the current tile matches the last tile in | |
// the current streak. | |
currentStreak.add(currentTile); | |
if (currentStreak.size() == mMaxMergedTiles) { | |
// If the maximum streak size has been reached, then | |
// merge all of the tiles in the current streak. | |
mergeTiles.addAll(currentStreak); | |
currentStreak.clear(); | |
} | |
} else { | |
// Then the current tile does not match the last tile | |
// in the current streak. | |
if (mMinMergedTiles <= currentStreak.size()) { | |
// The current streak satisfies the minimum number | |
// of tiles required, so merge the tiles. | |
mergeTiles.addAll(currentStreak); | |
currentStreak.clear(); | |
currentStreak.add(currentTile); | |
} else { | |
shiftTiles.addAll(currentStreak); | |
currentStreak.clear(); | |
currentStreak.add(currentTile); | |
} | |
} | |
} | |
if (col + 1 == mNumCols) { | |
if (mMinMergedTiles <= currentStreak.size()) { | |
// If at the edge and the streak satisfies the minimum | |
// allowed size, then merge the tiles. | |
mergeTiles.addAll(currentStreak); | |
} else { | |
// Otherwise, shift the leftover tiles. | |
shiftTiles.addAll(currentStreak); | |
} | |
} | |
if (!mergeTiles.isEmpty()) { | |
// TODO: merge the tiles. | |
} | |
if (!shiftTiles.isEmpty()) { | |
// TODO: shift the tiles. | |
} | |
} | |
} | |
if (moved) { | |
for (int i = 0; i < mRules.getNumDropTiles(); i++) { | |
addRandomTile(); | |
} | |
} | |
if (oldScore != mCurrentScore) { | |
mListener.onScoreChanged(oldScore, mCurrentScore); | |
} | |
resetMoveState(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment