I am tracking the position of characters on a 2D grid (this is a computer game). At every step each character can attempt to move (stationary, up, down, left, or right).
There are some rules that govern what moves are allowed. Example:
- Characters cannot move outside the confines of the grid.
- Characters cannot pass through each-other while moving.
- Characters cannot move into squares that contain another character.
- If two characters try to move into the same square:
- Characters moving directly down get priority.
- Characters moving diagonally down get 2nd priority.
- Characters moving horizontally get 3rd priority..
- If two characters try to move into the same square and neither has priority neither character is allowed to move.
So the nature of these rules is that the character's movements cannot be applied to the state sequentially, because the movements of other characters may affect if they are able to move or not.
I'm finding this code surprisingly complex to write. And that concerns me because I will
- Want to extend this system in the future to have more rules.
- Need to create similar systems where the inputs must be resolved together but depend on each-other.
My question is. Is there a pattern or framework that I could apply to simplify or at least standardize the approach for this kind of problem?
If I had to try and state the problem abstractly it would be: Making a rules based change to a state given multiple inputs which would, if processed in isolation, lead to conflicting final states.