Regex Chess: A sequence of 84,688 regular expressions that can play a valid (not entirely terrible) move given a chess board as input. It's a simple program consisting mainly of a list of regular expressions that operate on a stack-based data structure representing the computer state.
- Computer Design: Organizes data as a single string with the program "stack" and variables. Basic instructions include
Push(adds a value to the top of the stack),Pop(removes the top element from the stack), variableLookup(loads a variable's value onto the stack), and variableAssignment(handles variable creation and update). - (Branch-Free) Conditionals: Uses a conditional instruction
condto handle control flow. The program's state is marked withTrueorFalse, and instructions are conditionally executed based on these marks. There's also areactivateinstruction to reactivate inactive parts of the program. - Loops (are impossible): Since the program consists of a sequence of regular expressions, loops are not directly supported. Bounded computations can be achieved by unrolling loops.
- Single-Instruction Multiple-Data: Regular expressions can perform substitution globally over the entire string, allowing multiple "threads" to run simultaneously. Fork instructions (
fork_inactiveandfork_bool) create parallel execution paths.
- Computer Design: Organizes data as a single string with the program "stack" and variables. Basic instructions include
- Compiling to our little language: Uses symbolic execution to compile Python-like code into a sequence of instructions. Handles branching control flow by creating separate trace paths for true and false cases and merging them later.
- Writing a Chess Engine: Applies the above concepts to write a chess engine. Forking is used to handle parallel pawn moves, and other operations like move generation and validation are performed simultaneously across multiple states.
- Playing a Turn: The overall game loop includes reading the human's move, validating it using parallel processing, generating the computer's reply with scoring, and keeping the best scoring board.
- MiniMax Search (for free): Implements a depth-2 minimax search by generating pseudo-legal moves and deleting those that leave the king in check. This is not a full depth-2 minimax search but gives a reasonable approximation.
- Some Performance Tricks: Deletes intermediate variables to reduce computation time and memory usage. Optimizes regex matching by adding a newline prefix to ensure the regex engine skips over most True/False strings. Writes specialized instructions for specific tasks to improve performance. Maximizes parallelism by generating multiple moves or evaluations simultaneously.
- Conclusion: Encourages others to do pointless projects like this as it's fun, teaches a lot about various areas of computer science, and there will be more such projects in the future. Mentioned other related articles like playing tic-tac-toe with C's
printfand a Doom clone in 13kB of JavaScript.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用。你还可以使用@来通知其他用户。