Jump to content
  • Advertisement
Sign in to follow this  
MikeWulf

Simple answer on C++ conditions

This topic is 4881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
I see what you guys mean for all of your examples. I guess I just didn't like how big it looked. Thanks.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!