Simple answer on C++ conditions

Started by
8 comments, last by GameDev.net 18 years, 10 months ago
Could someone tell me a different way of doing this. This was the only way I could get it to work. if((input == 11) || (input == 12) || (input == 13) || (input == 21) || (input == 22) || (input == 23) || (input == 31) || (input == 32) || (input == 33)) It works of course, but I know there has to be better ways. EDIT: Removed source tags
Advertisement
To be honest, I'm not sure what you would want to improve. With short circuiting, it's really not too bad execution wise. It's pretty clear what the code is doing to look at it. There are a handful of things you could do to knock out a few operations, but it would make the code less intuitive.

For instance,
if ((input % 10 >= 1) && (input % 10 <= 3) &&    (input / 10 >= 1) && (input / 10 <= 3))


I would probably just stick with the way you had it originally.
a switch

switch(input)
{
case 21:
case 22:
case 23:
case 28:
break;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are interested e-mail me at cminortunes@gmail.com    
                                                          
Please only message me for hobby projects, I am not looking to create music for anything serious.
If the amount of possible numbers is small enough, you can use a table.
A table with for instance 100 elements from 0-99.
Set array positions 11, 12, 13 (and so on) to 1. Then check...

if(array[input]==1)

--------------------------- OR ---------------------------

if( ((input-10)/10<3) && ((input-1)%10<3))

Works only if you use an unsigned for input.

[Edited by - xor on June 8, 2005 3:27:16 AM]
If you made input unsigned (I assume this for is tic-tac-toe) you could use:

if( ( (input / 10) - 1 < 4 ) && ( (input % 10) - 1 < 4 )
{
...
}

Worst case operation list is: 1/, 1%, 2-, 2compares
Worst case your way: 9compares

Your way is mush more readable. I'd probably stick with that.
.
I wasn't aware that this was for a tic tac toe game, that little piece of info would have helped, good thing ascorbic thought of it.
Btw ascorbic don't you mean <3?
I see what you guys mean for all of your examples. I guess I just didn't like how big it looked. Thanks.
< 3 it is... same difference. :^)
.
you'd have to rework alot of coe but you could do it like this:

get input as 2 variables and use them as an array index.
const int rowsize=3;
const int colsize=3;

int arr[rowsize * colsize];
int x, y;

if(arr[x + rowsize * y] == 1)

this is all good aslong as x and y are 0 - 2 (representing rows 1- 3 and cols 1-3)

if ( (input >=11 && input =21 && input =31 && input <

This topic is closed to new replies.

Advertisement