{
  "_comment": "PGS (PlayCraft Gameplay Schema) 参考骨架 - 网格合成升级玩法。Agent 以此为起点编写或 fork 玩法规则。",
  "version": "1.0",
  "gameplayType": "merge_core",
  "entities": {
    "board": {
      "type": "grid",
      "cols": 5,
      "rows": 5,
      "description": "游戏棋盘，默认 5×5 格子"
    },
    "item": {
      "type": "cell-item",
      "tierCount": 9,
      "description": "棋盘格子上的可合成物品，等级从 1 到 maxTier（默认 9）"
    },
    "score": {
      "type": "counter",
      "initial": 0,
      "description": "当前得分，每次合成增加 2^tier 分"
    },
    "moves": {
      "type": "counter",
      "initial": 0,
      "description": "已使用操作次数（记录用）"
    }
  },
  "rules": {
    "merge_same_tier": {
      "description": "拖动物品 A 到相邻格子 B，若 A.tier === B.tier 则合并：移除 A 和 B，在 B 位置放置 tier+1 物品",
      "adjacency": "orthogonal",
      "condition": "same_tier",
      "result": "place_tier_plus_one_at_target",
      "maxTierCheck": true
    },
    "spawn_new_item": {
      "description": "每次合并操作后，在棋盘随机空格自动生成 1 个 tier-1 物品（通常 tier=1）",
      "trigger": "after_merge",
      "spawnTier": 1,
      "spawnCount": 1,
      "randomEmptyCell": true
    },
    "level_up_check": {
      "description": "合并后检查新物品等级，若达到 maxTier 则触发胜利",
      "winTier": "maxTier"
    }
  },
  "algorithm": {
    "description": "主循环：选择 → 拖动 → 合并 → 生成 → 胜利/失败检测",
    "steps": [
      "1. 玩家拖动某格子物品 A 到相邻格子 B",
      "2. 检测合并条件：A 和 B 存在且 tier 相同，且 B 在 A 的相邻位置",
      "3. 若不合法则取消拖动",
      "4. 合法则：移除 A，将 B 升级为 tier+1，moves + 1，score += 2^(tier+1)",
      "5. 检测胜利：新物品 tier >= maxTier",
      "6. 若 autoSpawn，在随机空格生成 spawnTier 物品",
      "7. 检测失败：棋盘已满且无任何可合并对"
    ]
  },
  "winCondition": {
    "type": "reach_max_tier",
    "description": "任意物品达到 maxTier 等级，或累计得分达到 goalScore"
  },
  "loseCondition": {
    "type": "board_full_no_merge",
    "description": "棋盘完全填满且不存在任何相邻同级物品对"
  },
  "testCases": [
    {
      "id": "tc_basic_merge",
      "description": "基础合并：两个 tier-1 物品相邻，合并为 tier-2",
      "initialState": {
        "board": [
          [1, 1, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null]
        ],
        "score": 0,
        "moves": 0
      },
      "action": { "type": "merge", "from": { "col": 0, "row": 0 }, "to": { "col": 1, "row": 0 } },
      "expectedState": {
        "board_cell_1_0": 2,
        "board_cell_0_0": null,
        "score": 4,
        "moves": 1,
        "win": false
      }
    },
    {
      "id": "tc_invalid_merge_different_tier",
      "description": "非法合并：两个物品等级不同，拒绝",
      "initialState": {
        "board": [
          [1, 2, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null]
        ],
        "score": 10,
        "moves": 3
      },
      "action": { "type": "merge", "from": { "col": 0, "row": 0 }, "to": { "col": 1, "row": 0 } },
      "expectedState": {
        "mergeRejected": true,
        "score": 10,
        "moves": 3,
        "boardUnchanged": true
      }
    },
    {
      "id": "tc_invalid_merge_non_adjacent",
      "description": "非法合并：两个物品不相邻",
      "initialState": {
        "board": [
          [1, null, 1, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null]
        ],
        "score": 0,
        "moves": 0
      },
      "action": { "type": "merge", "from": { "col": 0, "row": 0 }, "to": { "col": 2, "row": 0 } },
      "expectedState": {
        "mergeRejected": true,
        "boardUnchanged": true
      }
    },
    {
      "id": "tc_auto_spawn",
      "description": "合并后自动生成新的 tier-1 物品",
      "initialState": {
        "board": [
          [2, 2, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null]
        ],
        "score": 0,
        "moves": 0,
        "autoSpawn": true
      },
      "action": { "type": "merge", "from": { "col": 0, "row": 0 }, "to": { "col": 1, "row": 0 } },
      "expectedState": {
        "board_cell_1_0": 3,
        "board_cell_0_0": null,
        "totalNonNullCells": 2,
        "spawned": { "tier": 1 },
        "score": 8
      }
    },
    {
      "id": "tc_win_reach_max_tier",
      "description": "胜利：合并后物品达到 maxTier（9）",
      "initialState": {
        "board": [
          [8, 8, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null],
          [null, null, null, null, null]
        ],
        "score": 500,
        "moves": 20,
        "maxTier": 9
      },
      "action": { "type": "merge", "from": { "col": 0, "row": 0 }, "to": { "col": 1, "row": 0 } },
      "expectedState": {
        "board_cell_1_0": 9,
        "win": true,
        "score": { "min": 1012 }
      }
    },
    {
      "id": "tc_lose_board_full_no_merge",
      "description": "失败：棋盘满且无相邻同级物品",
      "initialState": {
        "board": [
          [1, 2, 1, 2, 1],
          [2, 1, 2, 1, 2],
          [1, 2, 1, 2, 1],
          [2, 1, 2, 1, 2],
          [1, 2, 1, 2, 1]
        ],
        "score": 200,
        "moves": 30
      },
      "action": { "type": "check" },
      "expectedState": {
        "gameOver": true,
        "win": false,
        "mergablePairs": []
      }
    }
  ]
}
