GitHub 热门项目: tetris-sql

2026-06-11 1 阅读 GitHub Trending
GitHub 项目:tetris-sql 仓库地址:https://github.com/nuno-faria/tetris-sql 星级:1014 | 作者:努诺法利亚 项目描述:利用SQL的图灵完备性构建俄罗斯方块 =================================================== 自述文件内容: # 俄罗斯方块-SQL SQL 查询中俄罗斯方块的完整实现。 ![](演示.gif) (初始延迟是由 Postgres 使用 JIT 编译优化计划引起的。可以使用“SET jit = off”禁用此功能。) ## 用法 ### 安装 - 要求: - PostgreSQL 服务器(使用版本 10 到 18 进行测试)。 - 安装了 `psycopg2` 扩展的 Python3。 - 一个终端。测试用: - Windows Terminal >= v1.22(以前的版本会导致渲染闪烁) - Windows CMD - GNOME 终端(使用 Ubuntu 24.04 测试) - iTerm2(MacOS) - 终端(MacOS) - 使用 Docker 安装所有内容: - Postgres 容器: ````外壳 docker run --name pg -p 5432:5432 -d -e POSTGRES_PASSWORD=postgres postgres:18 ```` - 直接在“pg”容器中安装“psycopg2”: ````外壳 docker exec pg apt 更新 docker exec pg apt install -y python3-psycopg2 ```` - 将代码复制到容器中: ````外壳 git 克隆 https://github.com/nuno-faria/tetris-sql docker cp tetris-sql pg:tetris-sql docker exec pg chmod +x ./tetris-sql/input.py ```` ### 跑步 1. 在一个终端中,运行“input.py”脚本: - 如果使用 Docker: ````外壳 docker exec -it pg ./tetris-sql/input.py ```` - 如果本地运行 Python: ````外壳 python3 input.py [-h] [-H 主机] [-P 端口] [-d DB] [-u 用户] [-p 密码] ```` 2. 在另一个终端中,运行俄罗斯方块查询: ````外壳 docker exec pg psql -U postgres -f tetris-sql/game.sql ```` 3.切换到输入终端进行游戏。 ## 实施细节 ### PGConf.EU 谈话 该项目的实施细节已在 PGConf.EU 2025 上展示。演示幻灯片可在 https://nuno-faria.github.io/papers/tetris-sql.pdf 上查看。 ### 结构 该代码分为两个部分: - [game.sql](game.sql) - 包含俄罗斯方块的 SQL 实现,以及一些先决条件。 - [input.py](input.py) - 将用户输入转换为 SQL 命令。稍后将提供有关为何需要单独脚本的详细信息。 ### 游戏循环 虽然 SQL 是一种用于查询和修改数据的强大声明性语言,但它并不是为一般编程任务而设计的。然而,自从 [SQL:1999](https://en.wikipedia.org/wiki/SQL:1999#Common_table_expressions_and_recursive_queries) 中引入递归通用表表达式 (CTE) 以来,SQL 成为一种[图灵完备语言](https://wiki.postgresql.org/wiki/Cyclic_Tag_System)。通俗地说,这意味着理论上我们可以在其中实现“任何”算法。递归 CTE 的复杂示例包括 [Mandelbrot 集](https://wiki.postgresql.org/wiki/Mandelbrot_set)、[3D 渲染引擎](https://observablehq.com/@pallada-92/sql-3d-engine)、[光线追踪器](https://github.com/chunky/sqlraytracer),甚至[GPT](https://github.com/quassnoi/explain-extend-2024)。 递归 CTE 包含两项:*非递归项*,它生成