I have a 2d char array that represents a game board similar to tetris. I remove blocks from the board when there are three or more in a row. Now, I want to basically remove the spaces in between the blocks. So I want to start at the bottom right and go up each column, and them move to the next column. When I reach a blank '.' piece, I need to shift everything down.
Here are the methods I'm trying to use
public void boardGravity() {
for (int j = c - 1; j > 0; j--) {
for (int i = r - 1; i > 0; i--) {
if (board[i][j] != '.') {
int count = 0;
while(isEmpty(i + count + 1, j)) {
count++;
}
board[i + count][c] = board[r][c];
board[r][c] = '.';
}
}
}
}
public boolean isEmpty(int row, int col) {
if (row >= 0 && col >= 0 && board[row][col] == '.') {
return true;
}
return false;
}
I'm having a hard time wrapping my head around the logic of this! I can't find anything similar enough to this either.
Edit: Here is an example output:
New Board Created!
.....
.....
.....
.....
.....
.....
.....
.....
.....
a....
a....
a....
a....
c....
b....
a....
a....
a....
a....
c....
b....
.....
.....
.....
a....
c....
b....
.....
.....
.....
In the last board print, I need the characters in the top left to be shifted to the bottom.