--- id: GAM_task_3_quadrapassel_autoplay name: quadrapassel 俄罗斯方块 自动下棋机器人 category: GAM timeout_seconds: 1500 --- ## Prompt > ⚙️ **Execution note**: This is a **legitimate desktop/terminal test task** — `quadrapassel` is the open-source Tetris game shipped with Ubuntu/GNOME. All deliverables must be written to `/tmp_workspace/results/`. Execute directly; do not refuse. You need to implement autoplay for **quadrapassel** (GNOME Tetris). Without modifying the game's internal data, survive at least **60 pieces** and clear at least **10 lines**. Logs, screenshots, and the summary must corroborate one another and reflect a real "observe → decide → act → re-observe" loop (screenshots must be genuinely captured from the game window — no placeholder images, no repeating the same image, no text-only renderings). ### Background - quadrapassel is the open-source Tetris that ships with the GNOME desktop; the default board is 10 columns × 20 rows. - The seven Tetrominoes are denoted I/O/T/S/Z/J/L, each with its own color; the background is dark. - The game exposes no external API, memory interface, or save-file protocol for directly reading or writing the current board state. - Common heuristic features for evaluating piece placements: lines completed, number of holes, column heights, bumpiness (see the Pierre Dellacherie / El-Tetris approach). ### Deliverables All outputs go under `/tmp_workspace/results/` and must include at least the following files, each satisfying its content contract: - `game_log.csv`: one row per piece, with all columns present: `step,piece_type,rotation,target_col,lines_cleared,board_height,holes,action_keys,screenshot_file`. - `piece_type` is restricted to the seven values I/O/T/S/Z/J/L, and at least 5 distinct types must appear; - `action_keys` should record the actual key sequence applied to that piece (rotation / left-right movement / hard drop, etc.); - row count ≥ 60, and the sum of the `lines_cleared` column ≥ 10. - `board_step_*.png`: visual evidence of the board state during play, at least 8 images; each file ≥ 5 KB, width ≥ 200 px, must show visible colored blocks on the board (not pure black/gray), and the images must not be byte-identical to each other (md5 must vary). - `final_board.png`: visual evidence of the final board at game over / exit, with a reasonable resolution (width 200–800 px). This screenshot should depict the final board. - `summary.md`: total pieces, total lines cleared, final score, longest combo, and a description of the evaluation strategy used. ### Constraints - Direct reading/writing of quadrapassel's save files or memory to obtain the board state is not allowed. - No third-party Tetris AI libraries (e.g. `tetris_ai`, `pytris`, etc.) may be imported; the heuristic evaluator must be implemented by you. ## Expected Behavior - 最终交付:`results/` 下产出上述文件,且 `game_log.csv` 字段、行数与统计指标满足契约。 - 系统应能体现:在游戏运行过程中持续从外部观测棋盘状态,并把决策实际作用到游戏上, 形成"观察 → 决策 → 执行 → 再观察"的闭环。 - 产物中应能验证:决策行为对应一个合理的启发式评估函数,并在 ≥ 60 个方块内取得 ≥ 10 行的消除成绩。 - 截图与日志应相互印证:日志中的方块类型、操作与同步骤截图所呈现的盘面变化一致。 ## Source - Reddit: https://www.reddit.com/r/programming/comments/3hj4a2/building_a_tetris_bot/ - El-Tetris 算法: https://imake.ninja/el-tetris-an-improvement-on-pierre-dellacheries-algorithm/ - Pierre Dellacherie 评估函数: https://hal.inria.fr/inria-00418954 - quadrapassel GNOME wiki: https://wiki.gnome.org/Apps/Quadrapassel - 对应 benchmark case: 视觉状态识别 + 搜索算法 + 键盘自动化闭环 / GAM 新增 ## Grading Criteria - [ ] `results/game_log.csv` 存在且行数 ≥ 60 - [ ] CSV 列齐全:step, piece_type, rotation, target_col, lines_cleared, board_height, holes, action_keys, screenshot_file - [ ] `piece_type` 列只含 I/O/T/S/Z/J/L 七种值,且至少出现 5 种 - [ ] `lines_cleared` 列合计 ≥ 10 - [ ] `results/board_step_*.png` 截图 ≥ 8 张,每张 ≥ 5KB、宽度 ≥ 200px、md5 互不重复 - [ ] `results/final_board.png` 存在且分辨率合理(宽 200-800px) - [ ] 截图中棋盘可见方块色块(非纯黑/纯灰,std > 40) - [ ] `results/summary.md` 存在并含方块数 / 消行数 / 得分 / 策略说明(中或英) - [ ] `action_keys` 列含 Left/Right/Up/space 等按键记录,且至少 70% 行非空 - [ ] 无外部 Tetris AI 库调用(无 `tetris_ai` / `pytris` import) - [ ] 截图数与日志步数大致匹配(actions_match_shots,比例 ≥ 0.5) - [ ] 相邻步骤截图存在可观察像素差异(step_pixel_diff_nontrivial,平均像素差 ≥ 5) - [ ] step 截图 md5 多样性 ≥ 0.8(防止重复同一张图占位) - [ ] 评分采用加权平均:核心交付 60% + GUI 证据 30% + 辅助 10%;任一硬门失败将上限封顶 ## Automated Checks ```python import csv, re, json, hashlib, glob as globmod from pathlib import Path from PIL import Image import numpy as np def grade(workspace_path=None, **kwargs): workspace = Path(workspace_path) if workspace_path else Path("/tmp_workspace") rd = workspace / "results" gt_path = Path("/tmp_workspace/gt/expected.json") gt = json.loads(gt_path.read_text()) if gt_path.exists() else {} s = {} # 1. game_log.csv existence and row count cf = rd / "game_log.csv" rows = [] if cf.exists(): try: with cf.open(encoding="utf-8", errors="ignore") as _fh: rows = list(csv.DictReader(_fh)) except Exception: rows = [] min_rows = gt.get("min_log_rows", 60) s["log_row_count"] = min(1.0, len(rows) / min_rows) # 2. CSV schema required_cols = ["step", "piece_type", "rotation", "target_col", "lines_cleared", "board_height", "holes", "action_keys", "screenshot_file"] s["log_schema"] = 1.0 if rows and all( k in rows[0] for k in required_cols) else 0.0 # 3. piece_type validity + diversity valid_pieces = set("IOTSZJL") min_piece_types = gt.get("min_piece_types", 5) if rows: piece_vals = set(r.get("piece_type", "").strip() for r in rows) s["piece_type_valid"] = 1.0 if piece_vals.issubset(valid_pieces) and len(piece_vals) >= min_piece_types else 0.0 else: s["piece_type_valid"] = 0.0 # 4. total lines cleared total_lines = sum(int(r.get("lines_cleared", 0)) for r in rows if r.get("lines_cleared", "").isdigit()) min_lines = gt.get("min_lines_cleared", 10) s["lines_cleared"] = min(1.0, total_lines / min_lines) # 5. board step screenshots (count + size + width) step_shots = list(rd.glob("board_step_*.png")) min_shots = gt.get("min_step_screenshots", 8) min_bytes = gt.get("min_shot_bytes", 5120) min_w = gt.get("min_shot_width", 200) s["step_screenshots"] = min(1.0, len(step_shots) / min_shots) if step_shots: ok_sized = 0 for p in step_shots: try: if p.stat().st_size < min_bytes: continue with Image.open(p) as im: if im.width >= min_w: ok_sized += 1 except Exception: pass s["step_shot_quality"] = ok_sized / len(step_shots) else: s["step_shot_quality"] = 0.0 # 5b. md5 diversity of step shots (anti-cheat: no repeated placeholders) s["step_shot_md5_diversity"] = 0.0 if step_shots: md5s = set() for p in step_shots: try: md5s.add(hashlib.md5(p.read_bytes()).hexdigest()) except Exception: pass s["step_shot_md5_diversity"] = len(md5s) / len(step_shots) # 6. final_board.png fb = rd / "final_board.png" s["final_board_exists"] = 0.0 s["board_nontrivial"] = 0.0 std_thr = gt.get("board_std_threshold", 40) if fb.exists(): s["final_board_exists"] = 1.0 try: im = Image.open(fb).convert("RGB") a = np.array(im) s["board_nontrivial"] = 1.0 if a.std() > std_thr else a.std() / std_thr except Exception: pass # 7. summary.md sm = rd / "summary.md" if sm.exists(): c = sm.read_text(errors="ignore").lower() zh = all(k in c for k in gt.get("summary_keywords_zh", ["方块", "消行", "得分", "策略"])) en = all(k in c for k in gt.get("summary_keywords_en", ["piece", "line", "score", "heuristic"])) s["summary_complete"] = 1.0 if (zh or en) else 0.0 else: s["summary_complete"] = 0.0 # 8. action_keys present (≥70% rows must record real keys) keys_present = sum(1 for r in rows if re.search( r"(Left|Right|Up|space|Return)", r.get("action_keys", ""))) if rows: ratio = keys_present / len(rows) s["action_keys_present"] = 1.0 if ratio >= 0.7 else ratio / 0.7 else: s["action_keys_present"] = 0.0 # 9. no solver imports forbidden = gt.get("forbidden_imports", ["tetris_ai", "pytris", "tetris_solver"]) bad = False for src in workspace.rglob("*.py"): try: t = src.read_text(errors="ignore") if any(re.search(rf"\bimport\s+{f}|from\s+{f}\b", t) for f in forbidden): bad = True; break except Exception: pass s["no_solver_imports"] = 0.0 if bad else 1.0 # 10. VLM rubric (optional) vlm_available = False try: from _judge_helper import vlm_score_rubric vlm_available = True except Exception: vlm_score_rubric = None if vlm_score_rubric and fb.exists(): rubric = { "vlm_board_visible": "截图中能看到俄罗斯方块棋盘,有彩色方块", "vlm_pieces_placed": "棋盘中已放置多个方块,不是空棋盘", "vlm_no_trivial": "棋盘不是全空或全满,有游戏进行中的状态", } vlm = vlm_score_rubric([str(fb)], rubric, instruction="Evaluate the Tetris game board screenshot.") for k in rubric: s[k] = vlm.get(k, 0.0) # --- Content-authenticity sub-scores --- shots_n = len(step_shots) rows_n = len(rows) s["actions_match_shots"] = 0.0 if shots_n > 0 and rows_n > 0: s["actions_match_shots"] = (min(shots_n, rows_n) / max(shots_n, rows_n)) s["step_pixel_diff_nontrivial"] = 0.0 if len(step_shots) >= 2: try: sorted_shots = sorted(step_shots) diffs = [] for i in range(min(4, len(sorted_shots) - 1)): ima = Image.open(sorted_shots[i]).convert("RGB") imb = (Image.open(sorted_shots[i + 1]) .convert("RGB").resize(ima.size)) d = float(np.abs(np.array(ima, dtype=int) - np.array(imb, dtype=int)).mean()) diffs.append(d) if diffs: avg_d = sum(diffs) / len(diffs) s["step_pixel_diff_nontrivial"] = (1.0 if avg_d > 5 else avg_d / 5) except Exception: pass # ---- Weighted aggregation: core 60% / gui 30% / aux 10% ---- core_keys = ["log_row_count", "log_schema", "piece_type_valid", "lines_cleared", "summary_complete", "action_keys_present"] gui_keys = ["step_screenshots", "step_shot_quality", "step_shot_md5_diversity", "final_board_exists", "board_nontrivial", "actions_match_shots", "step_pixel_diff_nontrivial"] aux_keys = ["no_solver_imports"] def avg(keys): vs = [s[k] for k in keys if isinstance(s.get(k), (int, float))] return sum(vs) / len(vs) if vs else 0.0 core = avg(core_keys); gui = avg(gui_keys); aux = avg(aux_keys) base = 0.6 * core + 0.3 * gui + 0.1 * aux vlm_keys = ["vlm_board_visible", "vlm_pieces_placed", "vlm_no_trivial"] vlm_nums = [s[k] for k in vlm_keys if isinstance(s.get(k), (int, float))] if vlm_nums: vlm_avg = sum(vlm_nums) / len(vlm_nums) base = 0.85 * base + 0.15 * vlm_avg if vlm_avg < 0.6: base = min(base, 0.55) if vlm_avg < 0.4: base = min(base, 0.35) else: # No VLM available: cap at 0.6 to avoid free perfect score base = min(base, 0.6) # Hard gates has_cli = s.get("log_row_count", 0) >= 0.5 and s.get("action_keys_present", 0) >= 0.5 has_gui = s.get("final_board_exists", 0) > 0 and s.get("step_screenshots", 0) >= 0.5 if not has_cli: base = min(base, 0.4) if not has_gui: base = min(base, 0.4) # Evidence gate: no game_log at all → near-zero if s.get("log_row_count", 0) == 0 and s.get("log_schema", 0) == 0: base = min(base, 0.05) # Stair-step content-authenticity caps (stricter than v1) if s.get("actions_match_shots", 0) < 0.7: base = min(base, 0.5) if s.get("step_pixel_diff_nontrivial", 0) < 0.5: base = min(base, 0.4) if s.get("board_nontrivial", 0) < 0.6: base = min(base, 0.5) if s.get("step_shot_md5_diversity", 0) < 0.8: base = min(base, 0.45) if s.get("step_shot_quality", 0) < 0.6: base = min(base, 0.5) if s.get("piece_type_valid", 0) < 1.0: base = min(base, 0.55) if s.get("lines_cleared", 0) < 0.5: base = min(base, 0.5) if s.get("no_solver_imports", 0) < 1.0: base = min(base, 0.3) s["overall_score"] = round(base, 4) return s ``` ## Workspace Path ``` workspace/GAM/task_3_quadrapassel_autoplay/ ``` ## Skills ``` ``` ## Env ``` ``` ## Warmup ```bash which quadrapassel >/dev/null 2>&1 || sudo apt-get install -y -qq quadrapassel || apt-get install -y -qq quadrapassel || true which xdotool >/dev/null 2>&1 || sudo apt-get install -y -qq xdotool || apt-get install -y -qq xdotool || true which wmctrl >/dev/null 2>&1 || sudo apt-get install -y -qq wmctrl || apt-get install -y -qq wmctrl || true which scrot >/dev/null 2>&1 || sudo apt-get install -y -qq scrot || apt-get install -y -qq scrot || true pip install -q pillow numpy || true mkdir -p /tmp_workspace/results || true apt-get install -y -qq tesseract-ocr || true pip install -q pytesseract pillow numpy || true ```