Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2048 game in java Implement private void moveLeft() in the Board class This method modifies the board array to perform a move to the left.

2048 game in java

Implement private void moveLeft() in the Board class This method modifies the board array to perform a move to the left. This involves the following:

  • In each row, shift all values as far left as possible, moving any zeros all the way to the right, but keeping the non-zero values in the same order.
  • In each row, combine pairs to same-value elements that are adjacent to each other after the shift.
    • After the combination of each pair, the remaining values should continue to shift left and a zero should be added to the row (on the right) to fill the empty cell resulting from combining a pair.

For example, if we had a row of the board which looked like [0 0 2 0] and we perform moveLeft(), the 2 would be shifted all the way to the left side of the board like

[2 0 0 0]. There are cases when tiles are also able to merge together like [0 2 0 2]. Then, after we perform moveLeft() this row would become [4 0 0 0].

Here are some things to watch out for (tricky edge cases):

  • Once a tile has merged, it is not eligible to be merged again during a given movement.
    • So after a moveLeft() [2 2 4 0] would become [4 4 0 0], NOT [8 0 0 0].
  • If there are three tiles that are eligible to merge like [0 4 4 4], then the leftmost tiles should be merged. Performing moveLeft() on this row would result in [8 4 0 0].
    • On the other hand, if there are 4 tiles, then they will merge in pairs. For example [4, 4, 4, 4] will become [8, 8, 0, 0]. My code is like:

      private void moveLeft() { for(int row=0;row

      { if(this.grid[row][col]!=0) { this.grid[row][0+count]=this.grid[row][col]; //Empty the original tile position once tile is moved if(0+count!=col) { this.grid[row][col]=0; } count++; } } } } How to fix it, thanks

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

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions

Question

2. What factors need to be considered when delegating tasks?

Answered: 1 week ago