edit: Looks like I struggle to win as tigers too
edit: Looks like I struggle to win as tigers too
Edit: https://github.com/sumn2u/baghchal/blob/master/src/library/u...
This is the code for legal movement and now I understand why it feels buggy: it's trying to express the logic by converting between a mishmash of numeric node indexes, directions and distances. This exposes several classes of bugs that aren't about the logic, but how the state is encoded.
Two approaches that would work better are:
1. Design a node data structure with 8 directional exits. Connect the graph through that structure; express movement as a traversal through nodes along the same movement direction once or twice, rejected if it reaches a null connection or node is occupied. Express board state by serializing the node graph down to an array of "goat, tiger, empty" and comparing the array to find repeated positions.
2. Generate the graph offline and use it to emit lookup tables for all board indexes and their possible movements. This would work as an optimization if you needed it to run very fast(e.g. real time game with thousands of goats and tigers), and it would dramatically reduce the amount of re-encoding of state going on in the inner loop.
I think this is mostly just possible because the AI plays imperfectly. Again, if you forget which tiger was last positioned where, you'll probably trigger a loss for yourself.
edit: I'm not sure the game is coded correctly. It seems like once a tiger moves from A to B, it can never move from B to A. Including cases where the tiger jumps A to C, it cannot jump back from C to A if the goat places another one in B... That feels very unintuitive and untrackable in a real world game.
As it stands now, a tiger player cannot move a tiger to it's previous spot from several turns prior, including capturing over the same space. Which means that even if the goat player loses a goat, they can put a goat back in the same space and it'll now be safe (vs. the tiger that just captured it at least). I'm not convinced it is possible to win against a competent goat player.