I'm currently working on a small project. I'm writing a simple java program for the L game. I need to write a method that will move the content of a 4x4 array. This method will take in (row, column) parameters. and the contents will move accordingly.
{ 'x',' ',' ',' ' },
{ 'x',' ',' ',' ' },
{ 'x','x',' ',' ' },
{ ' ',' ',' ',' ' }
move (0, 2)
--->
{ ' ',' ','x',' ' },
{ ' ',' ','x',' ' },
{ ' ',' ','x','x' },
{ ' ',' ',' ',' ' }
I'm not sure where to start. I really appreciate any help given on this.
Thank you so much for your help.
解决方案
your method should look something like this
char[][] array = new char[4][4];
public static void move(row, column){
for (int i = 0, i < 4; i++) {
for (int j = 0; j < 4; j++){
if (array[i][j] != null) {
// add rows and column accordingly
array[i + row][j + column] = array[i][j];
array[i][j] = null;
}
}
}
}
This is considering there is only one x per line, which in your case, some lines have two. I'll let you figure that one out.