How do I randomly place actors on a grid without them colliding?

I'm trying to make a puzzle game. Specifically, a clone of the Barrels minigame from Mario & Luigi: Superstar Saga (see here: https://youtu.be/vYZpsaA66Ug?si=d6aHAMAOtY_oLfSG&t=73). The first thing I wanna nail down is populating the board with different-color blocks, 12 red and 12 blue, and placing them on random spots in the grid without having them overlap. The other mechanics, such as detecting if every block in a column or row is the same then erasing them, incrementing score, etc., are not fully implemented.

Each block belongs to an Actor Group called "Block" that is set to not collide with itself so as to avoid misalignment with the grid. With that said, my approach was to make a 2D List that contains six lists corresponding to each row in the board. Each list contains six values, going from 0 to 5, corresponding to the position of each column within the row. I then created a custom block with a loop that randomly picks a list, then a value within the list, and places the colored block of choice in that position. To (ideally) prevent the same spot from being picked again, the value is removed from the list, then the list is removed from the 2D list once its empty.

Unfortunately, there's a flaw in the code that I can't identify, and blocks are being placed within positions that already have a block in them. I figure if I share it here (in the attachment below), someone else can tell me what I did wrong. I've also thought of using Photon's 2D List Extension, but I wanted to see if I can at least identify the problem before moving on, or perhaps think of a more optimal method.

Luyren

  • *
  • Posts: 2805
One list with 6 items. Each item is itself a list with 6 more items (call these one cells). Each cell can have a value of 0 (empty), 1 (blue block) or 2(red block). You'll be manipulating numbers when you move the rows and colors, not actors.

The actors are displayed on top of that, either by animtions on based on the cell value, or by sliding the created actors as you move the rows.

This would be the base for my implementation.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.

That should help me fix my issue, thank you! I'll implement it once I get another chance to work on the game.

How would you suggest checking if a row or column has 6 matching blocks? My only idea would be to manually check each row and column every time a move is made, but that doesn't seem optimal.

Luyren

  • *
  • Posts: 2805
Code: [Select]
Set row count to 0
For each item in columns (this is the primary list, items becomes your rows list).
-set blue count to 0
-set red count to 0
--for each item in item (the first item block now contains individual cells).
---if item = 1, increment blue count
---if item = 2, increment blue count
-if blue count >= 6, call an event to clear the column, setting the cell values to 0.
-increment row count
Rough pseudocode, checking for full rows. If you can implement that, you'll be able to expand it to check for columns as well.
My Stencyl resources are available here: https://luyren.itch.io/
Cutscenes, RPG Elements, Particles, Map System and many more.

Worked like a dream, thank you! In order to make it check columns, I just duplicated the code for checking rows, altering it as necessary. I'm sure there's a way to check for both at once, but I'll take the memory hit for now.

The game is now in a playable state! I have the 2D list visualized via a separate actor as per your suggestion, but I want to try and create different actors on top of it so as to have them animate properly. I'll make a different post if I run into problems there. Thanks again!