Grid Backpack
A grid backpack is a rectangle of cells where each item covers a rectangle of its own, so a long sword takes more room than a ring. An item can sit anywhere it is wholly inside and covers no cell another item covers. Placing, moving and turning are checked first and change nothing when they do not fit.

Paste this to your AI
Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.
Add "Grid Backpack" to my game.
What it should do: Shape turns carrying into a puzzle. The player decides what to take, and also how to pack it, so a full bag is a question of fitting things together, not only of counting them.
Use these starting values:
- width: 8 cells
- height: 5 cells
- allowTurn: 1 0 or 1
Watch out for these common mistakes:
- An item blocked by itself: Tell your AI: when moving or turning an item, leave that item out of the overlap check.
- Half off the edge: Tell your AI: check the whole rectangle, the far edge included, not only the top-left corner.
- Room in total but not in one piece: Tell your AI: look for an actual spot where the whole shape fits. Say plainly that there is no room, however many cells are free.
- A failed move loses the item: Tell your AI: check the new spot first and only then change the item. Never lift it out before knowing it fits.
- Turning around the wrong corner: Tell your AI: keep the top-left corner where it is, swap the sides, and check the new rectangle in full.
- The same item put in twice: Tell your AI: refuse to place an item whose id is already in the bag.
Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP
What it does
Shape turns carrying into a puzzle. The player decides what to take, and also how to pack it, so a full bag is a question of fitting things together, not only of counting them.
Good starting values
| Value | Start with | What it changes |
|---|---|---|
| width | 8 cells | How many cells wide the bag is. Eight is wide enough to fit a long item beside a few small ones and still fit on a screen. |
| height | 5 cells | How many cells tall the bag is. Five rows make forty cells, enough to hold a handful of mixed items while tight enough that packing matters. |
| allowTurn | 1 0 or 1 | With 1, an item may be turned a quarter, swapping its width and height. This makes packing kinder. With 0 every item keeps its shape, which suits items that must stay upright. |
What usually goes wrong
An item blocked by itself
The player nudges a long item one cell to the right. Most of the cells it wants are the ones it already covers, so a check against every item, itself included, says the way is blocked.
Fix: Tell your AI: when moving or turning an item, leave that item out of the overlap check.
Half off the edge
The player drops a wide item with its corner on the last column. Only the corner cell is checked, it is inside, and the item hangs off the side of the bag.
Fix: Tell your AI: check the whole rectangle, the far edge included, not only the top-left corner.
Room in total but not in one piece
Four cells are free, but they are scattered. A game that only compares the free count with the size of the item says there is room, then fails to place it.
Fix: Tell your AI: look for an actual spot where the whole shape fits. Say plainly that there is no room, however many cells are free.
A failed move loses the item
The game lifts the item out of the grid to try the new spot. The new spot is taken, the code gives up, and the item was never put back.
Fix: Tell your AI: check the new spot first and only then change the item. Never lift it out before knowing it fits.
Turning around the wrong corner
A tall item is turned and the game keeps its centre. The item now sticks out past the edge by half a cell, or lands on cells that were never checked.
Fix: Tell your AI: keep the top-left corner where it is, swap the sides, and check the new rectangle in full.
The same item put in twice
A pickup arrives twice, or a drag is repeated. Two entries with one id exist, and removing one removes both, or neither.
Fix: Tell your AI: refuse to place an item whose id is already in the bag.
How to check it works
- An empty bag of 8 by 5 cells. you place a sword 1 wide and 3 tall at column 2, row 1, and a shield 2 by 2 at column 3, row 0: both sit where you put them, 7 cells are used and 33 are free.
- A 2 by 2 item at the top left. you place another 2 by 2 item one cell right and one down, and then one that only touches its edge: the first is refused and the bag stays as it was; the one that only touches is placed.
- An empty bag of 8 by 5 cells. you place an item so that any part is outside, such as 2 wide at column 7, 2 tall at row 4, a corner at column 8 or row 5, a negative position, or 9 wide: every one is refused. The bottom-right corner exactly, a 2 by 2 item at column 6, row 3, is placed.
- A bag with a 3 by 1 item in the top left corner. you add a 2 by 2 item and then a 5 by 1 item automatically: the 2 by 2 goes right of the first item at column 3, row 0, and the 5 by 1 goes to column 0, row 2, the first row where five cells in a row are free.
- A bag 3 cells wide and 5 tall. you add a 5 by 1 item automatically: it is turned to stand upright at the top left. An item that already fits is never turned.
- A bag 3 cells wide and 5 tall with turning switched off. you add a 5 by 1 item automatically: there is no room and the bag stays empty.
- A 3 by 1 item at the top left. you move it one cell to the right: it moves, even though two of the three cells it needs are cells it already covers.
- A 2 by 2 item at the top left and another at column 4. you move the first to column 3, where it would overlap, or to a spot off the bag: both moves are refused and the item stays where it was.
- A 3 by 1 item at the top left. you turn it with free space below, then with a small item right under its corner, and turn a 1 by 3 item in the last column, which would be 3 wide there: the first turn gives a 1 by 3 item; the blocked turn and the one that would run off the right side are refused and the item keeps its shape.
- A bag that holds an item called a. you place or add another item called a: it is ignored and the bag is as it was.
- A bag with one item. you place an item with a width of zero, a negative height, a fractional size or position, or no name: each is ignored and the bag is as it was.
Works well with
- Item Stacking: A stack that grows must not grow into a second cell without a check, or it overlaps its neighbour. A stack takes one rectangle. Decide the stack rule first, then place the stack in the grid.
- Weight Limit: A bag can have free cells and no weight left, or the other way round. Report which one stopped the pickup. Check weight for the whole pickup, then look for a spot in the grid.
- Loot Table: What does not fit stays in the world. Never delete a drop because the shape did not fit. Roll the drop first, then try to place each piece in the grid.
Variations: Inventory System.
Common questions
what do i tell my ai to add a grid inventory
Paste this: Add a bag that is a grid of 8 by 5 cells. Each item covers a rectangle. An item may only be placed wholly inside the grid on cells nobody else covers. Moving and turning must ignore the item's own cells, and a failed action must leave everything exactly as it was.
why can't i move an item one cell in my grid inventory
The check probably counts the item's own cells as blocked. When you move an item, leave it out of the check, since the cells it covers now will be free once it moves.
how big should a grid backpack be
Eight by five is a good start. Make the biggest item about a third of the bag's long side, so it fits with room for others. If every item fits easily, make the bag smaller.
should items be allowed to turn in a grid inventory
Usually yes. Turning makes packing kinder, and the player does not feel stuck by an item that almost fits. Switch it off only when items must stay upright, such as long tools hung on a rack.
Where it comes from
We could not find a reliable origin for this one yet.
What was checked
- 16 tests pass on the reference code.
Your AI gets the code
Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it