Here To Slay, part 11: the bots, hands on
Halfway through developing Here To Slay's bots. How the bot receives the match and acts like a player, how it scores every move and the exact dice math.
Now that I’m hands on with the bot, I think I can talk a bit more about what’s being built. In part 10 I covered what I read before starting, and ended with a list of what I wanted: a bot that is honest, light, has a style, keeps an eye on the table and plays at a human pace. I’m still halfway there, so this post covers the foundation, which is already in place: where the bot runs, how it receives the match and how it scores moves. The numbers here are provisional and will likely change once I start tuning.
All the bot code lives in a single package, bot/, inside the Java server.
How the bot receives the match and plays
The bot isn’t a separate process or a client connected over WebSocket. It runs inside the server, attached to the match, but it plays like a regular player.
Every time the match state changes, the Match publishes that state to each seat at the table. For humans, it goes through Kafka to the WebSocket, as before. For bots, it goes to the BotDriver:
The state the bot receives is exactly what a human in that seat would get: its own hand face up and everyone else’s cards face down. BotView just organizes that JSON with methods like hand(), heroes(id) and handCount(id). It has no access to any of the Match’s internal structures. So the rule from part 10, that the bot only knows what a person in that seat would know, is guaranteed by the architecture itself: there’s no path in the code for the bot to read another player’s hand.
In the same way, the bot acts by calling performAction, the same door human messages come through. The Match validates its move like any other. If the bot tries something illegal, the action is refused.
A few details of the BotDriver:
- deciding and acting at different moments:
onViewis called while holding the match’s lock, so the bot only decides there. The action itself is scheduled on a separate thread and runs after the wait; - one thread for everyone: every bot in every match shares a single scheduled thread. A decision takes microseconds, and while nobody is thinking, the bots cost nothing;
- no repeated work: each match situation becomes a key. If the state is published again with nothing changed for the bot, it doesn’t replan;
- an emergency exit: if the bot sends an action and the situation stays the same, it tries again. After three tries, it falls back to an answer that is always valid: pass the window, pick the first options or end the turn. That way a match is never stuck waiting on a bot;
- windows with deadlines: challenge and modifier windows have a time limit. The bot never leaves its answer for the last 700 ms.
A score for every move
The utility AI from part 10 became the CardValues class, which says how much each thing is worth in “points” at that moment of the match:
static final double CARD = 4; // a card in hand
static final double NEW_CLASS = 12; // a new class in the party
static final double WIN = 1000; // winning the match
On top of that, each hero has a base value for its effect. For example, an effect that only draws a card is worth little, and one that destroys someone’s hero is worth much more. A slain monster is worth around 30, and the third one, which wins the game, is worth WIN.
On its turn, the TurnPlanner builds the list of every legal option (draw a card, play each card in hand, use each hero in the party, attack each available monster) and works out the value of each one per action point:
private record Option(String action, JSONObject payload, double value, int cost) {
double perPoint() {
return value / cost;
}
}
Attacking a monster costs two action points and everything else costs one, so this division stops the bot from spending its whole turn on something just because its total value is high. It takes the option with the highest score, sends the action and, when the state comes back, builds the list again for the next action point.
Two examples of how these scores are calculated:
- playing a hero: it starts at 3. If it’s a new class, add 12; if it’s the sixth class, which wins the game, add another 300. Since the hero can roll its effect as soon as it enters, also add the chance of the roll succeeding times the value of the effect. And if that hero makes the party meet some monster’s requirement, add a bit more;
- attacking a monster: the chance of slaying it times the reward, minus the chance of the monster fighting back times what is lost (a hero or two cards, depending on the monster).
The dice math
Almost every score goes through a probability, and in Here To Slay it’s easy to calculate exactly. Rolls are always two six-sided dice, so there are only 36 possible outcomes. The Odds class stores how many combinations give each sum and counts:
/** WAYS[s] = how many of the 36 outcomes sum to s. */
private static final int[] WAYS = {0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};
/** P(2d6 + bonus >= target). */
static double atLeast(int target, int bonus) {
int need = target - bonus;
int ways = 0;
for (int s = 2; s <= 12; s++) if (s >= need) ways += WAYS[s];
return ways / 36.0;
}
In challenges, both players roll and the challenger wins ties. The calculation goes through the 36 × 36 = 1,296 combinations of the two rolls. There’s no simulation and no sampling, so the decision costs almost nothing and the result is always the same for the same situation.
What comes next
With this foundation, the bot can already look at the table and say how much each move on its own turn is worth. But it still plays as if it were alone. What’s missing is the part that turns it into a real opponent:
- reacting outside its turn: deciding when it’s worth challenging someone’s card or playing a modifier on a roll;
- looking at the others: measuring who is close to winning and making the bots team up against that person, like the MP-Mix idea from part 10;
- styles and difficulty: giving each bot a personality and separating a normal level, which makes human mistakes, from an advanced one;
- the solo mode: a panel in the tavern to start a match against one, two or three bots.