Question: / * * * Moves the tile at position ( x , y ) as far up as possible. * * Rules for Tilt: *

/**
* Moves the tile at position (x, y) as far up as possible.
*
* Rules for Tilt:
*1. If two Tiles are adjacent in the direction of motion and have
* the same value, they are merged into one Tile of twice the original
* value and that new value is added to the score instance variable
*2. A tile that is the result of a merge will not merge again on that
* tilt. So each move, every tile will only ever be part of at most one
* merge (perhaps zero).
*3. When three adjacent tiles in the direction of motion have the same
* value, then the leading two tiles in the direction of motion merge,
* and the trailing tile does not.
*/
public void moveTileUpAsFarAsPossible(int x, int y){
Tile currTile = board.tile(x, y);
int myValue = currTile.value();
int targetY = y;
// TODO: Tasks 5,6, and 10. Fill in this function.
while (targetY < board.size()-1 && board.tile(x, targetY +1)== null){
targetY++;
}
if (targetY != y){
Tile tileAbove = board.tile(x, targetY -1);
if (tileAbove != null && !tileAbove.wasMerged() && tileAbove.value()== currTile.value()){
// Merge tiles
int mergedValue = currTile.value()*2;
Tile mergedTile = Tile.create(mergedValue, x, targetY -1);
board.move(x, targetY -1, mergedTile);
} else {
// Move the tile up
board.move(x, targetY, currTile);
}
}
my code isn't merging the tiles but is moving them

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!