• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [SOLVED] Trouble collapsing multiple columns

C

Cupid Stunt

Guest
I have a board of tiles:

as you play tiles are eliminated.
In order to select tiles to play the tiles must be touching top, bottom, left, or right. For this to happen empty columns must be collapsed. Currently my code successfully collapses any single empty column towards the center.
Myopically, I did not consider that 2 or even three columns may become empty at the same time. I am trying to figure out how to make that happen.

Here is my working code that collapses any single empty column toward the center of the screen:
Code:
width = 0;
height = 0;
for(x_index = 0; x_index < ds_list_size(global.board); x_index += 1) {
    column = global.board[| x_index];
    if(ds_list_empty(column)) { // Colapse a column
        if(x_index < (size_x / 2)) { // Left Side
            for(left_index = x_index - 1; left_index >= 0; left_index -= 1) {
                scr_list_alarm_0(global.board[| left_index], size);
            }
        }
        else { // Right Side
            for(right_index = x_index + 1; right_index < ds_list_size(global.board); right_index += 1) {
                shift = -1 * size;
                scr_list_alarm_0(global.board[| right_index], shift);
            }
        }
        ds_list_add(column, -1); // Invalidate column
    } else {
        if(column[| 0] != -1) {
            width += 1;
        }
        if(ds_list_size(column) > height) {
            height = ds_list_size(column);
        }
    }
}

Here is my attempt to collapse multiple columns towards the center. It doesn't do anything and causes errors elsewhere:
Code:
width = 0;
height = 0;
left_col = -1;
left_width = 0;
right_col = -1;
right_width = 0;
for(x_index = 0; x_index < (size_x / 2); x_index += 1) {
    column = global.board[| x_index];
    if(ds_list_empty(column)) { // Colapse a column
        if(left_col == -1){
            left_col = x_index;
        }
        left_width += 1;
        ds_list_add(column, -1); // Invalidate column
    }
    if(column[| 0] != -1) {
        width += 1;
    }
    if(ds_list_size(column) > height) {
        height = ds_list_size(column);
    }
}
for(left_index = left_col - 1; left_index >= 0; left_index -= 1) {
    scr_list_alarm_0(global.board[| left_index], size*left_width);
}
for(x_index = (size_x / 2); x_index < ds_list_size(global.board); x_index += 1) {
    column = global.board[| x_index];
    if(ds_list_empty(column)) { // Colapse a column
        right_col = x_index;
        right_width += 1;
        ds_list_add(column, -1); // Invalidate column
    }
    if(column[| 0] != -1) {
        width += 1;
    }
    if(ds_list_size(column) > height) {
        height = ds_list_size(column);
    }
}
for(left_index = left_col - 1; left_index >= 0; left_index -= 1) {
    scr_list_alarm_0(global.board[| left_index], size*right_width*-1);
}
Please ask me for any needed clarification, and please offer any advice you have or corrections that you see.
I think this is my last big hurdle for this program.
 
Last edited by a moderator:
C

Cupid Stunt

Guest
When facing a complex problem such as this in other languages, I will go to paper and map it out. Due to lack of experience in this particular language I still find that difficult. Instead I made a new project, complete with test data. I was able to work it out with that. I still have to integrate it but it preforms as expected with all potential cases.

This is a single object project.
The create event code:
Code:
board = ds_list_create();
for(index = 0; index < 11; index += 1) {
    ds_list_add(board, ds_list_create());
}
ds_list_add(board[| 0],  1, 1, 1, 1, 1, 1);
ds_list_add(board[| 1],  0, 1, 1, 1, 1, 1);
ds_list_add(board[| 2],  1, 1, 1, 1, 1, 0);
ds_list_add(board[| 3],  1, 0, 1, 1, 1, 1);
ds_list_add(board[| 4],  1, 1, 1, 0, 1, 1);
ds_list_add(board[| 5],  1, 0, 0, 1, 1, 1);
ds_list_add(board[| 6],  1, 1, 1, 0, 0, 1);
ds_list_add(board[| 7],  1, 1, 1, 0, 0, 0);
ds_list_add(board[| 8],  0, 0, 0, 1, 1, 1);
ds_list_add(board[| 9],  1, 0, 0, 0, 1, 1);
ds_list_add(board[| 10], 1, 1, 0, 0, 0, 1);
test_case = 0;
The step event code:
Code:
data_str = "";
data_in = board[| test_case];
width = 0;
left_index = -1;
left_width = 0;
right_index = -1;
right_width = 0;
// Display the entire data set
for(index = 0; index < ds_list_size(data_in); index += 1) {
    data_str += string(data_in[| index]);
}
show_debug_message(data_str);
// Display left half of set
for(index = 0; index < (ds_list_size(data_in) / 2); index += 1) {
    if(data_in[| index] == 0) {
        if(index == 0) {
            break;
        }
        if(left_index == -1) {
            left_index = index - 1;
        }
        left_width += 1;
    }
}
for(index = 0; index < (ds_list_size(data_in) / 2); index += 1) {
    if(data_in[| index] == 0) {
        data_in[| index] = -1;
    } else {
        width += 1;
    }
}
for(index = 0; index <= left_index; index += 1) {
    show_debug_message(index);
}
show_debug_message("Left Index: " + string(left_index) + " move " + string(left_width) + " columns");
// Display right half of set
data_str = "";
for(index = (ds_list_size(data_in) / 2); index < ds_list_size(data_in); index += 1) {
    if(data_in[| index] == 0) {
        right_index = index + 1;
        right_width += 1;
        data_in[| index] = -1;
    } else {
        width += 1;
    }
}
if(right_index >= ds_list_size(data_in)) {
    right_index = -1;
}
if(right_index > (ds_list_size(data_in) / 2)) {
    for(index = right_index; index < ds_list_size(data_in); index += 1) {
        show_debug_message(index);
    }
}
show_debug_message("Right Index: " + string(right_index) + " move " + string(right_width) + " columns");
data_str = "";
// Display the entire data set
for(index = 0; index < ds_list_size(data_in); index += 1) {
    data_str += string(data_in[| index]);
}
show_debug_message("Width: " + string(width));
show_debug_message(data_str);
show_debug_message("");
test_case += 1;
Code:
111111
Left Index: -1 move 0 columns
Right Index: -1 move 0 columns
Width: 6
111111
011111
Left Index: -1 move 0 columns
Right Index: -1 move 0 columns
Width: 5
-111111
111110
Left Index: -1 move 0 columns
Right Index: -1 move 1 columns
Width: 5
11111-1
101111
0
Left Index: 0 move 1 columns
Right Index: -1 move 0 columns
Width: 5
1-11111
111011
Left Index: -1 move 0 columns
4
5
Right Index: 4 move 1 columns
Width: 5
111-111
100111
0
Left Index: 0 move 2 columns
Right Index: -1 move 0 columns
Width: 4
1-1-1111
111001
Left Index: -1 move 0 columns
5
Right Index: 5 move 2 columns
Width: 4
111-1-11
111000
Left Index: -1 move 0 columns
Right Index: -1 move 3 columns
Width: 3
111-1-1-1
000111
Left Index: -1 move 0 columns
Right Index: -1 move 0 columns
Width: 3
-1-1-1111
100011
0
Left Index: 0 move 2 columns
4
5
Right Index: 4 move 1 columns
Width: 3
1-1-1-111
110001
0
1
Left Index: 1 move 1 columns
5
Right Index: 5 move 2 columns
Width: 3
11-1-1-11
So with some more hard work, I was able to fix it.
 
Top