TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How to build a chess engine

230 点作者 fredrickd将近 3 年前

14 条评论

mtlmtlmtlmtl将近 3 年前
I recommend anyone who&#x27;s interested in this topic, or in data structures and optimisation, to pick apart Stockfish&#x27; transposition table implementation[0,1]. It&#x27;s essentially a highly customised hash table. It has to handle multiple threads reading and replacing entries, with no locking. It can&#x27;t grow dynamically, and so it has to &quot;age out&quot; entries.<p>Think of it like hash table implementation with the difficulty set to brutal. You can&#x27;t grow dynamically(too many entries). You can&#x27;t store the key, since it&#x27;ll almost double the size of each entry. You can&#x27;t use locks. Lookup times need to be practically constant time.<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;official-stockfish&#x2F;Stockfish&#x2F;blob&#x2F;master&#x2F;src&#x2F;tt.h" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;official-stockfish&#x2F;Stockfish&#x2F;blob&#x2F;master&#x2F;...</a><p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;official-stockfish&#x2F;Stockfish&#x2F;blob&#x2F;master&#x2F;src&#x2F;tt.cpp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;official-stockfish&#x2F;Stockfish&#x2F;blob&#x2F;master&#x2F;...</a>
评论 #31965531 未加载
评论 #31965696 未加载
评论 #31966169 未加载
评论 #31993426 未加载
mixedmath将近 3 年前
Several years ago I built a chess engine in C, based on various ideas gleaned from the chessprogramming wiki [1]. I haven&#x27;t looked at in a while, but that wiki was an extraordinarily useful resource.<p>I didn&#x27;t write a better stockfish or anything, but I was surprised that it didn&#x27;t take too long to write a chess engine that could routinely beat me at chess.<p>I&#x27;ll also note that chess protocals are very standardized, and it was straightforward to attach the engine to one of the many chess GUIs out there (I used Chess Arena and scid). This was a great project and I highly recommend it.<p>[1]: <a href="https:&#x2F;&#x2F;www.chessprogramming.org&#x2F;Main_Page" rel="nofollow">https:&#x2F;&#x2F;www.chessprogramming.org&#x2F;Main_Page</a>
评论 #31965260 未加载
thom将近 3 年前
I think chess engines are an amazing thing to study. So much fundamental computer science is covered, from extremely low level CPU instructions to high level data structures and algorithms. There&#x27;s no surprise to me that many of the greats have turned their eye to representing and solving chess, from Turing to Knuth. I strongly suspect you could teach an entire CS degree never stepping outside of chess.<p>Even now there are so many rich directions you could go in. For example, many people are working on explainability in machine learning, but I think in many applications you want to go even further and actually optimise a model to _teach humans_ not just explain its predictions. This is especially relevant to engines when making strategic moves rather than calculating short term tactics.<p>I also think engines as tools for match preparation could be much better. What if you could more easily set an engine to find forced draws in certain lines, or tailor an engine to find tricky lines that a particular opponent is less likely to follow correctly, or find and optimise for certain endgame structures an opponent is weak at? Doing this in ChessBase (a truly awful but necessary piece of software) is quite painful.
dmeybohm将近 3 年前
This is a wonderful article and I really enjoyed the graphs and the clear explanations.<p>I wrote a simple chess engine in C and then ported it to C++ [1]. It doesn&#x27;t use transposition tables or quiescence search. Instead of that, I simply search only to depth=1 and then even depths (2, 4, 6...) after that, which tends to limit the horizon effect. It&#x27;s not that sophisticated, but I usually lose against it. Even so, it&#x27;s fun when I pull off a win.<p>I even tried porting it to web assembly [2]. It mostly works, but the display code still has a bug where the interface disappears sometimes, and I&#x27;m still trying to figure out why. Also it doesn&#x27;t work on mobile browsers I&#x27;ve tried.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;dmeybohm&#x2F;wisdom-chess" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dmeybohm&#x2F;wisdom-chess</a><p>[2] <a href="https:&#x2F;&#x2F;wisdom-chess.netlify.app&#x2F;" rel="nofollow">https:&#x2F;&#x2F;wisdom-chess.netlify.app&#x2F;</a>
tikej将近 3 年前
Chess programming is fascinating but it seems for me that it has somewhat stalled in creativity in terms of classical (non DNN) engines. They all seem to use improved versions of min-max&#x2F;alpha-beta. Which results in computer players that are extremely powerful but also quite dull. This is because they assume that the opponent will play perfect game and will do it at the full strength of the engine.<p>Basically there is no point playing against the engines at full strength as a human, since it will beat almost any human beyond grandmaster level. And that’s for weaker engines – the medium tier ones will regularly beat grandmasters and the top ones around the stockfish tier will probably not loose 1 game in 100 plays.<p>That considered I’ve wondered what’s possible dropping the assumptions about perfect play of the opponent. Some probabilistic models of opponent or different way of rating whole tree of moves in the context, instead of position only. For example considering the alternative moves and how difficult it would be to play perfect game for the opponent, it might be better to play combination with a lot of traps with possibility of loosing some position if the opponent plays perfectly. Current engines wouldn’t play such kind of move because they’d assume (via min-max&#x2F;alpha-beta) that opponent will have perfect answer. Of course, if the opponent is an engine it will, but against humans it would make things much more interesting.
评论 #31967094 未加载
评论 #31967022 未加载
nphard85将近 3 年前
chessprogramming.org is a treasure trove of knowledge on building chess engines. With its help, I wrote one in C++ a few years ago that got quite good (2100+ rating on FICS but that&#x27;s nowhere close to the likes of Stockfish). In fact, writing a reasonably strong chess engine is straightforward (and incredibly fun) but at the top end of strength, there&#x27;s immense depth, and after a point making improvements gets increasingly resource intensive (tuning params, running experiments to verify strength gain all takes a lot of compute).<p>Chess programming is also extremely addictive. On forums like talkchess.com, you see folks hanging out who have been doing it for decades (most of them are also super helpful to newbies).
评论 #31966290 未加载
Jaruzel将近 3 年前
One day I&#x27;d like to write a rubbish Chess Engine purely for personal use, so that I can play chess against the computer and actually have a good probability of winning, instead of getting thrashed even on the &#x27;easy&#x27; setting.<p>The way my brain works, the act of writing the engine would probably also level up my chess playing skills as a side benefit.
评论 #31968526 未加载
评论 #31971134 未加载
tony_codes将近 3 年前
Great article. Buidling a chess engine is an awesome way to level up as a software developer! <a href="https:&#x2F;&#x2F;blog.devgenius.io&#x2F;level-up-as-a-software-engineer-by-writing-a-chess-engine-896b7f8eb443" rel="nofollow">https:&#x2F;&#x2F;blog.devgenius.io&#x2F;level-up-as-a-software-engineer-by...</a>
paradite将近 3 年前
I&#x27;m also building an AI engine for games, where you can try different algorithms (heuristics, Monte Carlo tree search, expectimax, deep ML).<p>For starter I made it for 2048. You can check it out here: <a href="https:&#x2F;&#x2F;ai-simulator.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;ai-simulator.com&#x2F;</a>
jeremysalwen将近 3 年前
Can highly recommend this article. I wrote a small chess engine for fun, mostly during a long plane flight back in 2018 when I had no internet access.<p>My process for writing the engine followed almost exactly the series of steps laid out in this article. The key improvement that I made was to add quiescence search with null move pruning. After I added that, it became quite tricky to beat (for my ~1450 lichess rated self). It definitely results in an engine that is extremely greedy, and almost anti-positional, but very tricky to prove wrong.<p>You can check it out at <a href="https:&#x2F;&#x2F;github.com&#x2F;jeremysalwen&#x2F;grubchess" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jeremysalwen&#x2F;grubchess</a>.
russianGuy83829将近 3 年前
horrible website, scrolling doesn’t work
评论 #31966492 未加载
ape4将近 3 年前
This seems wrong:<p><pre><code> &#x2F;&#x2F; call this function again in 5 secs window.setTimeout(makeRandomMove, 500)</code></pre>
infogulch将近 3 年前
What is the most compact representation for a complete chess game state? Can it be as low as 64 bits?
评论 #31965837 未加载
评论 #31966079 未加载
评论 #31966182 未加载
评论 #31971554 未加载
评论 #31966424 未加载
评论 #31965818 未加载
franciscop将近 3 年前
By &quot;chess engine&quot; the author refers to the AI&#x2F;Machine Player, not the base of how to position the pieces, valid moves, turn taking, etc. which is what I thought &quot;engine&quot; would mean here (my thought was similar to a game engine vs AI&#x2F;characters). For the &quot;game engine&quot; they&#x27;re importing Chess.js. Totally fine, just a heads up since I did expect to see a truly from-0 since it says &quot;how to build a Chess engine from scratch&quot; and was confused when they imported a fully working &quot;engine&quot;.<p>Maybe &quot;How to build a Chess Automaton&#x2F;AI Player&quot; would be a better title? Unless in the Chess industry the &quot;chess engine&quot; refers to the machine player, which I do not know.<p>PS, this website also hijacks the &quot;down key&quot; and the &quot;space key&quot;, making keyboard navigation harsh...
评论 #31966051 未加载
评论 #31965127 未加载
评论 #31965373 未加载
评论 #31966207 未加载
评论 #31965095 未加载
评论 #31966145 未加载