I've rewritten the function that rearranges my Bejeweled field twice already, and it still doesn't work! The first version used to crash my game immediately, the second version actually does rearrange, but it causes some very random bugs (it screws with the selector position and state, and it makes the game hang up when certain matches of 3 jewels occur)
My question:
Why doesn't this function work properly?
The function:
// Moves jewels in tetris style and returns amount of successions
int iRearrange()
{
if( DEBUG ) {
debugsz = "Rearranging jewels";
}
int result = 0;
for( int i = FIELD_SIZE; i > 0; i-- ) {
for( int j = 0; j < FIELD_SIZE; j++ ) {
cell& current = field[j];
cell& above = field[i-1][j];
if( !current.used ) {
if( above.used ) {
current.used = true;
current.jewel = above.jewel;
above.used = false;
result++;
}
}
}
}
for( int i = 0; i < FIELD_SIZE; i++ ) {
cell& c = field[0];
if( !c.used ) {
c.used = true;
c.jewel = rand() %7;
result++;
}
}
return result;
}
It's usage:
while( iCheckMatches() > 0 ) { // Check for matches
bejstate = REMOVING;
iscore+= iRemoveMatches(); // Remove matches
updateScreen( bejstate );
Sleep( 500 );
while( iRearrange() > 0 ) { // Rearrange until everything is arranged :)
bejstate = ARRANGING;
updateScreen( bejstate );
Sleep( 500 );
}
}