Simplify

5:50 we can simplify that condition:

IsEnemy ^ TurnSystem.Instance.IsPlayerTurn

1 Like

What is the ^ operator?

It’s the XOR operator. Doesn’t really simplify it if people don’t know what it does.

4 Likes

Thanks.

XOR is not as commonly used as && and ||. One thing to always remember when simplifying algorythms is that you want to make sure it passes the six month test. This test is simple: Will you remember what that piece of code does and how it works in six months? Sometimes, a comment will suffice to ensure that, and other times writing out the operation explicitly will do it. Another part of the six month test is when working in a team environment: Will my teammates be able to intuit what this piece of code does if I’m not there to explain it to them?

3 Likes

Doesn’t really simplify it if people don’t know what it does

Couldn’t agree more. The more readable the better if there’s no tangible performance degradation.

However, if the bools consistently referred to the enemy e.g isEnemy and IsEnemyTurn(), it’d even further improve readability, not having to negate back and forth.

So you could do something like

if (isEnemy != TurnSystem.Instance.IsEnemyTurn()) { return; }
ResetActionPoints();

The emphasis is not on the code simplification but the naming convention.

Perhaps there’s a reason for the inconsistency and will make sense in future lectures, but at this point it threw me off a bit why sometimes refer to the player, sometimes to the enemy.

Great course btw, learning a lot and have recommended it to others too!

3 Likes

To truly generalize it replace the isEnemy boolean for int (or enum) for faction ID and add previous faction and next faction to arguments for end turn event. This will allow more than two factions, you refresh the action points of current faction. Keep a special field for last faction and advance the turn counter when previous faction is last faction.

1 Like

Actually I can’t understand why doing this check in the first place: the turn ends - reset all action points. Why bother?

Privacy & Terms