Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public static int [][] moveDown(int [][] grid) { for(int i = 0; i < grid.length; i--) { for(int j = 0; j < grid.length; j++)

public static int [][] moveDown(int [][] grid) {

for(int i = 0; i < grid.length; i--) {

for(int j = 0; j < grid.length; j++) {

int currentValue = grid[i][j];

if(currentValue != 0) {

if(i !=0) {

for(int k = i+1; k >= 0; k++) {

if(grid[j][k] != 0) {

if(currentValue == grid[j][k]) {

grid[j][k] = 2 * currentValue;

currentValue = grid[j][k];

grid[k+1][j] = 0;

}

} else {

grid[j][k] = currentValue;

grid[k+1][j] = 0;

}

}

}

}

}

}

This is my code and i get the following error..

can you please explain how can i fix it.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 4

at Game1024.moveDown(Game1024.java:110)

at Game1024.main(Game1024.java:193)

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

Students also viewed these Programming questions

Question

What is the role of a unified database in an ERP system?

Answered: 1 week ago