- Home /
C#: algorithm to check for multiple winning conditions
I try to to make a game where winning conditions of each stage vary. It can be
Player A grabs the winning item
Player B grabs the winning item
Both Players grab the winning item
None of the Players grab the winning item
all within a given time. There can be multiple conditions active at the same time with different winning items.
The current setup is that I instantiate a quest object storing the winning condition and set it up using observer pattern via delegate event. This event is triggered every time one of the players picks up an item. I then make a check if it is the right item and then check the winning conditions.
I have it working in a prototype, but the code is a mess of if's, switches and temporary storage variables. Likely a nightmare to maintain.
And pretty cumbersome to extend. Currently I think about adding new quest types requiring one of the players to pick the correct item up twice or having different items for both players).
I therefore hope someone can teach me a more elegant approach. Pointers in the right direction are appreciated, don't bother to write the code.
Thanks
i don't understand it completely but couldn't you give each player their own "winning conditions"( or quest object ), and then that object would handle all those conditions ( like checking if the player got some item ) and when they are all met, it will inform the main object(somewhere floating in your scene) that the player fullfilled his quest
Please post the code you have, that will help us direct you in the right path.
A design question like this one is better asked on Unity Forums. Better yet, since there is nothing Unity in this question, a program$$anonymous$$g forum. I'm sure a program$$anonymous$$g forum will have lots of ideas.
The right solution will be highly dependent on your application. The first thing that comes to $$anonymous$$d is the use of bit sets. You set one bit for each condition you want. You can also have a 'don't' care mask that indicates any conditions for this specific objective that you don't care about. When checking you simply mask out any 'don't care' conditions and then compare the one bit set to another to deter$$anonymous$$e if the winning condition has been met.
The bitset looks promising. Didn't know about it, but it has lots of powerful operators. I guess, this might help. Otherwise I will take up the suggestion to post the question in a program$$anonymous$$g forum, since indeed it has nothing to do with Unity.
Thanks everybody for the quick reply. $$anonymous$$uch appreciated.