Dentosal / python-sc2

A StarCraft II bot api client library for Python 3
MIT License
586 stars 182 forks source link

XOR function for Units #174

Closed janbolle closed 5 years ago

janbolle commented 5 years ago

Hey, first: thank you for that nice framework! It is very good to use and I love it!

Second: could you please add to units.py an XOR function? def xor(self, other: "Units") -> "Units": tags = {unit.tag for unit in other} units = [unit for unit in self if unit.tag not in tags] return Units(units, self.game_data)

Regards Jan

BurnySc2 commented 5 years ago

Wouldn't your example output something different than the expected XOR outcome?

group1 = Units([A, B])
group2 = Units([B, C])
group3 = group1.xor(group2) 
# In your example it would now contain Units([A])
# since it doesn't loop through group2 to check if tags are not in group1

I suppose you would want the result to be Units([A, C]) though? This could be solved with the following code at the moment:

xor_tags = group1.tags ^ group2.tags
group3 = (group1 | group2).tags_in(xor_tags) # Now contains Units([A, C])

Since the .tags operation returns a set, you can use set operations: https://docs.python.org/2/library/sets.html#set-objects

janbolle commented 5 years ago

Thank you for your fast reply. You're right, my code is not XOR :-/ What I want is Units([A]), which is simple group1-group2.

Thank you very much for your time! Please close this issue...

BurnySc2 commented 5 years ago

Yes exactly, group3 = group1 - group2 should give you the result you want!