Hacker News new | ask | show | jobs
by syntheweave 1167 days ago
The game seems to be glitchy about its ruleset. I lost as tigers when only six goats were placed.

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.

2 comments

I think the movement works correctly if you accept that tigers can't go back to their previous spot, which is rough. However, the bug seems to be that sometimes, when a tiger TRIES to do the above, it will trigger a loss for the tigers even if there are legal moves. I have managed to get a win against Hard Goat AI. Generally you want to be positioning tigers with 2 spaces between them, and when you need to break the pattern, do so with a tiger that can get back to its original space in a right triangle pattern with the original spot being the right angle vertex.

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.

But the AI tigers are able to go back to their own spots.
Thank you for the suggestions.