Skip to main content
gamedev.net
🔒 Locked

Problem with 'switch' statement

Started by Koen VB 5 replies 200+ views
Original Post
Koen VB
Koen VB
Hi, I''m writing a breakout clone and I have some problems with a switch statement. This is the function that causes the problems: void TBreakout::UpdateGrid(int R, int C) { switch (Grid[R][C]) { case 1: Grid[R][C] = 0; break; case 2: Grid[R][C] = 0; break; case 3: Grid[R][C] = 0; break; case 4: Grid[R][C] = 5; break; case 5: Grid[R][C] = 6; break; case 6: Grid[R][C] = 7; break; case 7: Grid[R][C] = 8; break; case 8: Grid[R][C] = 0; break; case 9: Grid[R][C] = 0; break; } } The grid cells contain the type of block. 0 is empty, 1 to 3 are colored blocks, 4 to 8 are blocks with cracks in them that become larger and 9 is an exploding block. The effect should be that if you hit a block with cracks, they become bigger until the block is destroyed (wich should take 5 hits). The problem is that the blocks are always destroyed immediately. So if I hit a block with index 4, the index should become 5 and not 0. It seems like the rest of the switch statement is executed regardless of the break statement. Any suggestions on how to solve this? --------- http://users.pandora.be/koen.van.baelen ---------
ISOPimp
ISOPimp
Im pretty sure that is because after say Case 4: the grid is now equal to 5, then it goes to the next statement which is Case 5: which is also true, so it changes the grid to equal 6, and so on.

what about

  
void TBreakout::UpdateGrid(int R, int C)
{
if((Grid[R][C] > 0 && Grid[R][C] < 4) || Grid[R][C] == 9)
{
Grid[R][C] = 0;
}
else if(Grid[R][C] >=4 && Grid[R][C] < 9)
{
Grid[R][C]++;
}
else
{
//Handle unexpected call

}
}
davepermen
davepermen
assuming the grid value type is int..

  
void TBreakout::UpdateGrid(int R, int C)
{
const int updateValue[] = { -1,0,0,5,6,7,8,0,0 };
Grid[R][C] = updateValue[Grid[R][C]];
// or, as you can shift the index, you don''t need the -1 at the zero position..

const int updateValue[] = { 0,0,5,6,7,8,0,0 };
Grid[R][C] = updateValue[Grid[R][C]-1];
}

i love small lookuptables for stuff like this..

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Koen VB
Koen VB
Hi,
I''ve solved the problem. The UpdateGrid function was executed more than once if a collision occurred.

---------
http://users.pandora.be/koen.van.baelen
---------
Stan100
Stan100
Regarding the switch statement...
Can I use or|| in a switch statement?
case ''r'' || ''R'':
{
//
}
Or can you only use one statement?

Beatles are the best!
-----If you thought I was helpful, rate me down.If you thought I wasn't helpful, rate me down as well.This idiot didn't read my signature and tried to insult me.
Plasmadog
Plasmadog
quote:
Original post by Stan100
Regarding the switch statement...
Can I use or|| in a switch statement?
case ''r'' || ''R'':
{
//
}
Or can you only use one statement?

I don''t think so. But if you are looking for a way to shorten your switch block you could do this:
void TBreakout::UpdateGrid(int R, int C)
{
switch (Grid[R][C])
{
case 1:
case 2:
case 3:
case 8:
case 9:
Grid[R][C] = 0;
break;
case 4:
case 5:
case 6:
case 7:
Grid[R][C]++;
break;
}
}

You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter
Plasmadog
Plasmadog
Or alternatively:

void TBreakout::UpdateGrid(int R, int C)
{
switch (Grid[R][C])
{
case 4:
case 5:
case 6:
case 7:
Grid[R][C]++;
break;
default:
Grid[R][C] = 0;
break;
}
}

Or even better:
void TBreakout::UpdateGrid(int R, int C)
{
if ( (Grid[R][C] >= 4) && (Grid[R][C] <= 7) )
Grid[R][C]++;
else
Grid[R][C] = 0;
}


[edited by - Plasmadog on December 18, 2002 5:13:01 PM]
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.