I am making Tetris for a learning experience in XNA, and I'm having trouble deleted completed rows.
I am having an issue with deleting rows, for some reason when I go to delete a row, I end up creating more blocks or not deleting all of the blocks in the row. The method I am using to detect and remove blocks is as follows:
// 'rowCount' is an int[20] holding the number of blocks in each row
// (my playing field is 20 rows tall and 10 blocks wide).
for (int i = 0; i < rowCount.Length; i++)
{
if (rowCount[i] >= 10)
{
// The row is full, remove the blocks.
// 'blocks' is a list of all blocks that have settled.
for (int j = 0; j < blocks.Count; j++)
{
if (blocks[j].Row == i)
{
// If the settled block is in this row, remove it.
blocks.RemoveAt(j);
rowCount[i]--;
}
}
// Move any blocks above the now-empty row down one row.
foreach (Block b in blocks)
{
if (b.Row < i)
{
b.Row++;
rowCount[b.Row]++;
}
}
}
}
This is done in my main Update
method after I've determined if the current block has settled or not.
As implemented, this algorithm will usually end up recording more than ten blocks in some rows and will not correctly clear completed rows and move the rows above down one unit. Why is this, and what can I do to fix the algorithm?