Solving the fox game

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Title: Solving the fox game

Christoffer Stjernlöf posted about the Fox Game, an old Norse asymmetric board game (seemingly also called “halatafl”) where twenty sheep try to outsmart two foxes and get to the other side. His AI uncovered some interesting insights, but left the question open as of whether the foxes or the sheep actually win with perfect play (the general sentiment in his family was that winning with the foxes was easy).

Christoffer Stjernlöf 曾发布过关于“狐狸棋”(Fox Game)的文章,这是一种古老的北欧非对称棋盘游戏(似乎也被称为“halatafl”),游戏中二十只羊试图智取两只狐狸并到达另一侧。他的 AI 揭示了一些有趣的见解,但对于在完美博弈下究竟是狐狸还是羊获胜的问题,仍未给出定论(他家人的普遍看法是,用狐狸获胜很容易)。

This was too tempting for me to leave on the table, so I set out to make a strong solve. Since I don’t know this game myself, I’ve stayed with the rules he describes even when they differ from e.g. Wikipedia (in particular, I do not use forced fox jumps, or multi-jumps); the only real change is that I don’t have a 40-move draw rule, any position that is a forced win for one of the sides would be a win no matter how long it would take.

这对我来说太有诱惑力了,无法置之不理,所以我着手进行了一次彻底的求解。由于我本人并不了解这个游戏,我坚持使用他所描述的规则,即使这些规则与维基百科等来源有所不同(特别是,我不使用强制狐狸跳跃或多重跳跃);唯一的真正改变是我没有 40 步平局规则,任何一方的强制胜局,无论需要多少步,都将被视为胜利。

(I consider this just an implementation detail in his AI, although morally, I consider a draw a loss for the sheep.) You might want to read his post to get the full rules, because I’m not going to repeat them here. The results first: The sheep win with perfect play, even with only 19 sheep (remove one on the flank), or 18 if they are allowed to go first.

(我认为这只是他 AI 中的一个实现细节,尽管在道德上,我认为平局对羊来说就是输。)你可能需要阅读他的文章以获取完整规则,因为我不会在这里重复它们。先说结果:在完美博弈下,即使只有 19 只羊(移除侧翼的一只),或者如果允许羊先走,即使只有 18 只羊,羊也能获胜。

Even with only 9 sheep (the minimum possible that isn’t an instant win for the foxes), the sheep can force a draw right from the start. With one fox, 11 sheep suffice for a forced win. (The draw margin is seemingly so large because one fox can just camp in the last spot in the pen, and it’s hard to force it away without getting harassed by the other fox, unless you have lots of sheep.)

即使只有 9 只羊(这是狐狸不会立即获胜的最小数量),羊也能从一开始就强制达成平局。如果只有一只狐狸,11 只羊就足以强制获胜。(平局的余地之所以看起来如此之大,是因为一只狐狸可以驻扎在围栏的最后一个位置,除非你有大量的羊,否则很难在不被另一只狐狸骚扰的情况下将其逼走。)

The rest is about the implementation. I’ll skip over a lot of wrong turns, and unsurprisingly, there were a lot of bugs to iron out, but hopefully, it got to a good place in the end. :-) I started out trying to make something very simple, and it worked for the first few cases, but then things got more complicated than I would have liked in the end. Still only at ~1000 lines of code, though, with no dependencies.

其余部分是关于实现的。我将跳过许多弯路,不出所料,有很多错误需要解决,但希望最终结果还不错。:-) 我最初试图做一个非常简单的东西,它在最初的几个案例中运行良好,但后来事情变得比我预想的要复杂。不过,代码仍然只有约 1000 行,且没有任何依赖项。

Board representation: The board has 33 squares. I tried a few different ways of represent the state of the game, and ended up on a simple bitboard representation: There’s a 64-bit bitmap representing where there are sheep, and another bitmap representing where there are foxes. Since there’s plenty of room in there, I use a spare bit to represent side-to-move.

棋盘表示:棋盘有 33 个方格。我尝试了几种不同的方式来表示游戏状态,最终采用了简单的位棋盘(bitboard)表示法:有一个 64 位的位图表示羊的位置,另一个位图表示狐狸的位置。由于空间充足,我使用一个备用位来表示轮到哪一方走棋。

This is compact (which mattered a lot earlier on, but not as much later), easy to iterate over, and has little redundancy, so it’s easy to keep valid. You need fast 64-bit population count and bit-scan instructions, but a) that’s common now everywhere, and b) I decided fairly early that I wanted to require BMI2 anyway.

这种表示法很紧凑(这在早期很重要,但后来没那么重要了),易于迭代,且冗余度低,因此很容易保持有效性。你需要快速的 64 位总体计数和位扫描指令,但 a) 这在现在随处可见,b) 我很早就决定无论如何都要要求 BMI2 指令集。

The bit order mostly doesn’t matter; I just started at top-left and went from there. All it really influences is square iteration order. There’s a small tweak in that I store the right half of the board mirrored (reverse x order) for symmetry purposes; more on that below.

位顺序大多无关紧要;我只是从左上角开始。它真正影响的只是方格的迭代顺序。有一个小的调整是,为了对称性,我将棋盘的右半部分进行了镜像存储(反转 x 顺序);下文会详细说明。