• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Rybo5001

More efficient alternative to compare booleans?

12 posts in this topic

Say I have many booleans, for simplicity sake I will called them A, B, C and D.

so my code if:
[source lang="cpp"]if(A && B && C && D)
{
doSomething();
}
else if(A && B && C)//but NOT D
{
doSomethingElse();
}
else if(A && B && D)//but NOT C
{
doSomethingElseElse();
}
else if(B && C)//but NOT D or A[/source]
and this will continue onward for all possiblites (what if only A is true, none are true etc etc etc).


I know a switch statement could be used but that'd be just as long.

Is there a better way of doing this?
0

Share this post


Link to post
Share on other sites
[quote name='Rybo5001' timestamp='1349760106' post='4988223']
Is there a better way of doing this?
[/quote]

Yes =>

[quote name='Rybo5001' timestamp='1349760106' post='4988223']
I know a switch statement could be used
[/quote]

or, depending on the language you are using, you can use a hashmap (int->function), where int is the binary representation of your conditions.
0

Share this post


Link to post
Share on other sites
If you have 16 possibilities you can't get away with less than 16 "ifs" or switch cases, if that's what you mean. Sometimes it's appropriate to define an enum that lists all possibilities, and use the switch statement:
[CODE]
switch (convert_to_enum(A, B, C, D))
{
case <case1>: ...;
case <case2>: ...;
...
}
[/CODE]

Or, as Ashaman73 says, adopt the functional approach and define a function (or hash map) that gives you the function to be called.
0

Share this post


Link to post
Share on other sites
Or you can use bit flags.
const unsigned int BitA = 1 << 0;
const unsigned int BitB = 1 << 1;
...
if(value == (BitA | BitB)) ...
if(value == (BitA | BitB | BitC)) ...
Also you can make some hash map to dispatch the function invoke based on the bit value.
0

Share this post


Link to post
Share on other sites
If you have 16 [i]distinct[/i] pieces of code similar to [i]doSomething[/i], which for the sake of this thread I assume is several lines of code and have hardly anything in common, then you cant exactly shorten it much. Most times though, I would expect that there is a lot of commonality between cases that can be taken advantage of.

Other than that, you can convert the booleans to bit-flags:
i.e.[code]static const int AIsSet = 1;
static const int BIsSet = 2;
static const int CIsSet = 4;
static const int DIsSet = 8;

int choice = 0;
if (A) choice += AIsSet;
if (B) choice += BIsSet;
if (C) choice += CIsSet;
if (D) choice += DIsSet;

switch (choice) {
case AIsSet + BIsSet + CIsSet + DIsSet:
doSomething();
break;
case AIsSet + BIsSet + CIsSet:
doSomethingElse();
break;
case AIsSet + BIsSet + DIsSet:
doSomethingElseElse();
break;
case BIsSet + CIsSet:
doAnotherThing();
break;
};[/code] Edited by iMalc
0

Share this post


Link to post
Share on other sites
Thank you for all these excellent suggestions, looks like I'm not getting away with shorter coding! [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

I think I like the sound of [quote name='iMalc' timestamp='1349762749' post='4988231']
you can convert the booleans to bit-flags:
[/quote] as this seems like the best way for what I'm doing.

[b]EDIT: [/b]another thought, is there an easy way to work out all the possibilities such as A+B+C+D, A+B+C? Because at the moment my program may end up having to compare many many variables like this. Edited by Rybo5001
0

Share this post


Link to post
Share on other sites
[quote name='Rybo5001' timestamp='1349774203' post='4988268']
another thought, is there an easy way to work out all the possibilities such as A+B+C+D, A+B+C? Because at the moment my program may end up having to compare many many variables like this.
[/quote]
If you have four boolean states, and each state corresponds to a bit in an integer, then all possible combinations are just the bit patterns of the numbers from 0 to 2[sup]4[/sup]-1=15.
[code]
for(int i=0; i<16; ++i) {
std::cout << "pattern " << i << " = " << ((i&8) ? "D" : "") << ((i&4) ? "C" : "") << ((i&2) ? "B" : "") << ((i&1) ? "A" : "") << std::endl;
}
[/code]
Extend to as many bits as you want to use.
0

Share this post


Link to post
Share on other sites
[quote]People have suggested using a hash map from int to functions, but since we have indices that are small consecutive integers starting at 0, an array is much more reasonable.[/quote]

Don't most [all?] compilers convert switches into jump tables anyway?
0

Share this post


Link to post
Share on other sites
[quote name='turch' timestamp='1349789410' post='4988334']
[quote]People have suggested using a hash map from int to functions, but since we have indices that are small consecutive integers starting at 0, an array is much more reasonable.[/quote]

Don't most [all?] compilers convert switches into jump tables anyway?
[/quote]

Yes, that's probably what a switch would become. But the OP was complaining about the code being long, and using a container of function pointers is shorter, because you save yourself the `case' labels and the `break's.

Anyway, my comment was objecting to using a hash map: Using a switch statement is perfectly acceptable in my opinion.
0

Share this post


Link to post
Share on other sites
[quote]Don't most [all?] compilers convert switches into jump tables anyway?[/quote]

Not all switches are converted to jump tables in C++. Only switch statements with contiguous integer cases starting from 0 AFAIK.
0

Share this post


Link to post
Share on other sites
[quote name='greenvertex' timestamp='1349798616' post='4988394']
[quote]Don't most [all?] compilers convert switches into jump tables anyway?[/quote]

Not all switches are converted to jump tables in C++. Only switch statements with contiguous integer cases starting from 0 AFAIK.
[/quote]
There's no reason why any range compact enough could be implemented as a jump table. Any offset from zero can be accounted for by subtraction before lookup, and missing cases can be accounted for with an entry jumping to the end of the case statement, or to wherever the no-case route goes. Edited by Brother Bob
1

Share this post


Link to post
Share on other sites
This type of structure with a massive "if else if" is typically not the best style. The only way I see reducing this large block is to take a step back and see if you can group functionality in a different way. The way to tell if this will work is to look through all the body's of the if statements and see if they are unique. If they are not you can try and regroup the logic based on the statement bodies.

If you can't do this and you still want to have clean code, use a hash map as stated above because this will be the most efficient and expandable in the long term. Also rather than using individual booleans for A, B, C, and D, consider having a bit mask because it will make it easier to use as an index. For instance,
[source lang="cpp"]int ABCD = 0; // Flags variable

ABCD |= 1; // Set A
ABCD |= 2; // Set B

static Function functions[FUNC_NUM] = {
doSomething,
doSomethingElse,
...
}

int index = hash(ABCD);

function[index]();[/source]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0