Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ * * * 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory And Application Bio Science And Bio Technology International Conferences DTA And BSBT 2011 Held As Part Of The Future Generation In Computer And Information Science 258

Authors: Tai-hoon Kim ,Hojjat Adeli ,Alfredo Cuzzocrea ,Tughrul Arslan ,Yanchun Zhang ,Jianhua Ma ,Kyo-il Chung ,Siti Mariyam ,Xiaofeng Song

2011th Edition

ISBN: 3642271561, 978-3642271564

More Books

Students also viewed these Databases questions

Question

What are the four types of initiatives suggested in this chapter?

Answered: 1 week ago