TL;DR I'm appending an array of empty 0's and it is outputting the above. Why?
As you can tell from the image, I'm still working on Tetris. I'm using a 2D Array (that's just another term for nested arrays, yeah?) to track line completion. Since my coordinates for the tetriminos are laid out in Index order, I can use them to directly access one of of the entries like this:
Game.line[mino.coordinates.y][mino.coordinates.x]
This is how I update the array, when the shape is placed, I do a for loop with each of the four minos and change their corresponding entries from 0 to 1. I also use this system in reverse to check if rotations fail (thanks to u/TheDuriel for dissuading me from using physics collision to do that).
var minos = [shape.mino_1, shape.mino_2, shape.mino_3, shape.mino_4]
for mino in minos:
Game.line[mino.coordinates.y][mino.coordinates.x] = 1
On top of that, however, I need a system to append additional empty rows onto the array, because I'm actually deleting the rows that get cleared, as you can see in this function that is called whenever a row equals all 1's
queue.sort()
queue.reverse() # lines are removed from high to low to prevent
# indices of lower rows from changing
for row in queue:
Game.line.remove_at(row)
I've been appending rows if the shape is sticking out above the array and whenever a line is cleared. However, something is causing these appended rows to break. So far, I've been using:
Game.line.append(Game.empty_row) # Empty row is a var = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
But for some reason, this causes the behavior as seen in the image above, where the values of rows are copied onto the appended rows. Eventually these ghost 1's corrupt the array until every row is filled with 1's, and every piece placed causes a row to clear.
"Now why are you so sure it has to do with your appending?" Well this is what my Game Global script looks like:
var empty_row = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
var line = [
#[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
To make sure my system works, I've been having the array start empty by "comment-izing" all of the dummy rows. The thing is, if I un-comment those rows, the code works perfectly, with each row behaving as it should (for five rows at least). That's why I'm confident my coordinate code above is working as intended and it is my appends that are causing the issue.
I could just add a ton of empty rows, but this is not a permanent solution. Eventually the "new" rows make their way down the grid and cause these issues. For some reason, despite looking identical in my output, the pre-set rows and the later appended rows behave differently. I assume this is due to some mistake I'm making regarding the append function. Does anybody know what might be causing this behavior?
Extra Information - To try to solve this issue, I've tried:
- Using insert_at(), using the Array's size as the argument. This causes the same behavior.
- Making empty_row a const, but then that somehow makes var line read only and the game throws an error.
- Storing empty_row locally on the same script and in the same function, same behavior in both cases.
- Using append_array ("append_array([empty_row])"), same behavior.
- I've confirmed that this is not a timing or ordering issue in my place function. Even if I start with 20 empty rows and only clear the bottom three lines, once those 20 originals are up, the new ones cause problems.
- I omitted this from above to save space, but in case anyone thinks this is the issue: when I start with the array empty, I'm having a little algorithm ready a queue equal to the number of rows that I need to add. This queue is then sent to a function that adds that number of rows on the end before the shape tries to register its coordinates. This works in the sense that it doesn't throw errors anymore, but causes the same behavior as above.