15 KiB
Chess
Chess is an old two-player board game, perhaps most famous and popular among all board games in history. It is a complete information game that simulates a battle of two armies on a 64x64 board with different battle pieces. Chess has a world-wide competitive community and is considered an intellectual sport but is also a topic of active research (chess is unlikely to be ever solved due to its non-trivial rules combined with enormous state space, Shannon estimated the number of possible games at 10^120) and programming (many chess engines, AIs and frontends are being actively developed).
{ There is a nice black and white indie movie called Computer Chess about chess programmers of the 1980s, it's pretty good, very oldschool, starring real programmers and chess players, check it out. ~drummyfish }
Drummyfish has created a suckless/LRS chess library smallchesslib which includes a simple engine called smolchess.
At LRS we consider chess to be one of the best games, if not the very best one, for the following reasons:
- It is just a greatly interesting and deep game in which luck plays minimal role.
- It is suckless, the rules are very simple, it can be implemented on simple 8bit computers. The game doesn't even require a computer to play and chess masters don't even need a physical board to play (they can completely visualize it in memory). In the end one can in theory just play against himself in his head, achieving ultimate freedom: the only dependency of the game is one's brain, i.e. it becomes a brain software. Chess is extremely inexpensive, doesn't discriminate against poor people and will survive even the most extreme technological collapse.
- No one owns chess, the game is hundreds of years old and many books about it are also already in the public domain. It is extremely free.
- It is a basis for other derived games, for example many different chess variants or chess puzzles which can be considered a "singleplayer chess game".
- It is a source of many interesting mathematical and programming challenges.
Chess in General
Chess evolved from ancient board games in India in about 6th century. Nowadays the game is internationally governed by FIDE which has taken the on role of an authority that defines the official rules: FIDE rules are considered to be the standard chess rules. FIDE also organizes tournaments, promotes the game and keeps a list of registered players whose performance it rates with so called Elo system – based on the performance it also grants titles such as Grandmaster (GM, strongest), Internation Master (IM, second strongest) or Candidate Master (CM).
Elo rating is a mathematical system of numerically rating the performance of players (it is used in many sports, not just chess). Given two players with Elo rating it is possible to compute the probability of the game's outcome (e.g. white has 70% chance of winning etc.). The FIDE set the parameters so that the rating is roughly this: < 1000: beginner, 1000-2000: intermediate, 2000-3000: master. More advanced systems have also been created, namely the Glicko system.
The rules of chess are quite simple (easy to learn, hard to master) and can be found anywhere on the Internet. In short, the game is played on a 64x64 board by two players: one with white pieces, one with black. Each piece has a way of moving and capturing (eliminating) enemy pieces, for example bishops move diagonally while pawns move one square forward and take diagonally. The goal is to checkmate the opponent's king, i.e. make the king attacked by a piece while giving him no way to escape this attack. There are also lesser known rules that noobs often miss and ignore, e.g. so called en-passant or the 50 move rule that declares a draw if there has been no significant move for 50 moves.
At the competitive level clock (so called time control) is used to give each player a limited time for making moves: with unlimited move time games would be painfully long and more a test of patience than skill. Clock can also nicely help balance unequal opponent by giving the stronger player less time to move. Based on the amount of time to move there exist several formats, most notably correspondence (slowest, days for a move), classical (slow, hours per game), rapid (faster, tens of minutes per game), blitz (fast, a few seconds per move) and bullet (fastest, units of seconds per move).
Currently the best player in the world is pretty clearly Magnus Carlsen from Norway with Elo rating 2800+.
During covid chess has experienced a small boom among normies and YouTube chess channels have gained considerable popularity. This gave rise to memes such as the bong cloud opening popularized by a top player and streamer Hikaru Nakamura; the bong cloud is an intentionally shitty opening that's supposed to taunt the opponent (it's been even played in serious tournaments lol).
Chess and Computers
{This is an absolutely amazing video about weird chess algorithms :) ~drummyfish}
Chess have a very strong relationship with computers, computers help people play chess, train their skills but also analyze positions, perform research about chess, create and search game databases and find new play styles. The game also provides a nice framework for machine learning.
There is a great online Wiki focused on programming chess engines: https://www.chessprogramming.org.
Chess software is usually separated to libraries, chess engines and frontends (or boards). Chess engine is typically a CLI program capable of playing chess but also doing other things such as evaluating arbitrary position, hinting best moves, saving and loading games etc. Frontends on the other hand are GUI programs that help people interact with the underlying engine.
For communication between different engines and frontends there exist standards such as XBoard (engine protocol), UCI (another engine protocol), FEN (way of encoding a position as a string), PGN (way of encoding games as strings) etc.
Computers have already surpassed the best humans in their playing strength (we can't exactly compute an engine's Elo as it depends on hardware used, but generally the strongest would rate high above 3000 FIDE). As of 2021 the strongest chess engine is considered to be the FOSS engine Stockfish, with other strong engines being e.g. Leela Chess Zero (also FOSS) or AlphaZero (proprietary, by Google). GNU Chess is a reasonably strong free software engine by GNU. There are world championships for chess engines such as the Top Chess Engine Championship or World Computer Chess Championship. CCRL is a list of chess engines along with their Elo ratings. Despite the immense strength of modern engines, there are still very specific situation in which humans beat the computer (shown e.g. in this video).
The first chess computer that beat the world champion (at the time Gary Kasparov) was famously Deep Blue in 1997. Alan Turing himself has written a chess playing algorithm but at his time there were no computers to run it, so he executed it by hand -- nowadays the algorithm has been implemented on computers (there are bots playing this algorithm e.g. on lichess).
For online chess there exist many servers such as https://chess.com or https://chess24.com, but for us the most important is https://lichess.org which is gratis and uses FOSS (it also allows users to run bots under special accounts which is an amazing way of testing engines against people and other engines). These servers rate players with Elo/Glicko, allow them to play with each other or against computer, solve puzzles, analyze games, play chess variants, explore opening databases etc.
Playing strength is not the only possible measure of chess engine quality, of course -- for example there are people who try to make the smallest chess programs (see countercomplex and golfing). As of 2022 the leading programmer of smallest chess programs seems to be Óscar Toledo G. (https://nanochess.org/chess.html). Unfortunately his programs are proprietary, even though their source code is public. The programs include Toledo Atomchess (392 x86 instructions), Toledo Nanochess (world's smallest C chess program, 1257 non-blank C characters) and Toledo Javascript chess (world's smallest Javascript chess program). He won the IOCCC. Another small chess program is micro-Max by H. G. Muller (https://home.hccnet.nl/h.g.muller/max-src2.html, 1433 C characters, Toledo claims it is weaker than his program).
{ Nanochess is actually pretty strong, in my testing it easily beat smallchesslib :') ~drummyfish }
Variants
Besides similar games such as shogi there are many variants of chess, i.e. slight modifications of rules, foremost worth mentioning is for example chess 960. The following is a list of some variants:
- antichess: The goal is to lose all pieces or get stalemated, rules are a bit changed, e.g. castling and checks are removed and taking is forced.
- chess 960 aka Fisher's random: Starting position is randomly modified by shuffling the non-pawn rows (with these rules: king must be between rooks, bishops on opposite colors and black/white's positions are mirrored). The rules are the same with a slight modification to castling. This was invented by Bobby Fisher to emphasize pure chess skill as opposed to memorizing the best opening moves, he saw the opening theory as harmful to chess. Chess 960 is nowadays even advocated by some to become the "main" version of chess.
- chess boxing: Chess combined with box, players switch between the two games, one wins either by checkmate or knockout.
- different pieces: Some variants use different pieces, e.g. empress (moves like rook and knight) or amazon (queen/knight).
- fog of war: Makes chess an incomplete-information game by allowing players to only see squares they can immediately move to (this is similarly to some strategy video games).
- horde chess: Asymmetric starting position: large number of black pawns vs a white army of traditional pieces. Rules are slightly modified, e.g. black can only be defeated by having all pawns captured (there is no black king).
- infinite chess: Infinite chessboard.
- minichess: Smaller chessboard, e.g. 4x4, 4x8 etc. Some are already solved (e.g. 3x3).
- more players: E.g. 3 man chess or 4 player chess allow more than two players to play, some use different boards.
- old chess: The rules of chess itself have been changing over time (e.g. adding the 50 move rule etc.). The older rule sets can be seen as variants as well.
- puzzle: For single player, chess positions are presented and the player has to find the best move or sequence of moves.
- racing kings: The starting position has both players on the same side, the goal is to get one's king to the other side first.
- 3D chess: 3D generalization of chess.
Programming Chess
Programming chess is a fun and enriching experience and is therefore recommended as a good exercise. There is nothing more satisfying than writing a custom chess engine and then watching it play on its own.
The core of chess programming is writing the AI, everything else, i.e. implementing the rules, communication protocols etc., is pretty straightforward (but still a good programming exercise). Nevertheless one has to pay a great attention to eliminating as many bugs as possible; really, the importance of writing automatic tests can't be stressed enough as debugging the AI will be hard enough and can become unmanageable with small bugs creeping in.
The AI itself works in almost all cases on the same principle: firstly we implement so called static evaluation function -- a function that takes a chess position and outputs its evaluation number which say how good the position is for white vs black (positive number favoring white, negative black). This function considers a number of factors such as total material of both players, pawn structure, king safety, piece mobility and so on (in new engines this function is often a learned neural network, but it may very well be written by hand). Secondly we implement a search algorithm that recursively searches the game tree and looks for a move that will lead to the best result, i.e. to position for which the evaluation function gives the best value. This basic principle, especially the search part, gets very complex as there are many possible weaknesses and optimizations.
Exhaustively searching the tree to great depths is not possible due to astronomical numbers of possible move combinations, so the engine has to limit the depth quite greatly. Normally it will search all moves to a small depth (e.g. 2 or 3 half moves or plys) and then extend the search for interesting moves such as exchanges or checks. Maybe the greatest danger of searching algorithms is so called horizon effect which has to be addressed somehow (e.g. by detecting quiet positions, so called quiescence). If not addressed, the horizon effect will make an engine misevaluate certain moves by stopping the evaluation at certain depth even if the played out situation would continue and lead to a vastly different result (imagine e.g. a queen taking a pawn which is guarded by another pawn; if the engine stops evaluating after the pawn take, it will think it's a won pawn, when in fact it's a lost queen). There are also many techniques for reducing the number of searched tree nodes and speeding up the search, for example pruning methods such as alpha-beta (which subsequently works best with correctly ordering moves to search), or transposition tables (remembering already evaluated position so that they don't have to be evaluated again when encountered by a different path in the tree).
Many other aspects come into the AI design such as opening books (databases of best opening moves), endgame tablebases (databases of winning moves in simple endgames), heuristics in search, clock management, pondering (thinking on opponent's move), learning from played games etc. For details see the above linked chess programming wiki.
Rules
TODO