A truco agent that learns to bluff
A Truco Gaúcho engine in C#, a second engine in Python, and an agent trained by self-play that has to bluff in order to play well, tried out in a Unity prototype.
The problem
Truco is interesting for reinforcement learning for one specific reason: much of the information is hidden, and lying about it is part of the rules. An agent that only computes hand strength plays badly. It has to learn that raising with a bad hand works sometimes, and that it works because the opponent cannot see the cards.
In a board game you can optimise while seeing everything. In truco the agent learns a policy knowing that the state it sees could have come from completely different hands.
Technical decisions
Illegal actions are masked, not punished
The simplest option would have been to let the agent pick any action and hand back a negative reward when it was invalid. Instead, training uses PPO with action masking: at every state the engine reports which moves are legal, and the agent only chooses among those.
For that to work, it was not enough for the rules engine to execute a move; for any state, it had to be able to answer “which actions are valid right now” before anyone played. I wrote the truco, envido and flor state machines with that question in mind from the start, which took more work than validating at the moment of the play.
Two implementations of the same rules, on purpose
The game has a pure C# engine, tested with xUnit, and training has a second engine in Python, tested with pytest. I could have kept a single engine and had training talk to the C# side across the Unity boundary, but then each episode would be far too expensive, and there are millions of episodes.
The risk with two implementations is that they drift apart without anyone noticing. So each side has its own test suite over the same card-hierarchy and hand-resolution rules. It doubles the maintenance for good, and I accepted that because without it the training would not have happened.
In the prototype, the model was served over HTTP, not embedded in the game
Instead of exporting the model and running it inside Unity, I put the trained agent behind a FastAPI service, and the Unity client asked it for the move.
That meant the game no longer worked on its own: if the service went down, there was no opponent. In return, I could retrain and swap the model without touching the game build, and play against different versions of the agent to compare them.
That bridge is not in the public repository. truco-rl keeps only the Python engine, the training and the evaluation; the C# engine and the Unity client are private.
What was left out
Trained models are not versioned in the repository. And the agent today plays 1v1; the 2v2 and 3v3 rules exist in the C# engine, but the training environment does not yet cover partner play, where the implicit communication between teammates changes the whole game.
[Stack]
- Python
- Reinforcement Learning
- Gymnasium
- FastAPI
- C#
- Unity
[Written about this project]
[Links]