Problem with building a Puzzle

Okay, fair point, but I can't think of many scenarios where you'd have so many events on a map that this would result in any appreciable lag anyway. 😛 It's never a bad idea to optimise your code, though.
@Trihan I don't know about that, in my experience, having more than 10 events in a single map already makes things lag more than I want on low performing machines. And in this situation there are already 16 events. XD
@Heirukichi @Trihan

In this puzzle, there are 5 red, 5 purple and 6 orange blocks.

If the developer designs the game, which, of course, they do...then they can choose which events are the red, which events are the purple, and which events are the orange (and, of course, which events are blocks at all). Since we can get the color of the moved block immediately using:

$game_map.events[@event_id]

and

$game_variables[same_id_you_used_before]["red"].include?(event_id_goes_here)

we can narrow the checks down to the ID numbers of the event blocks for the same color, as designed by the developer. If the event moved comes back red or purple, that means we only have to check events with red IDs or purple IDs (5 checks...or 4 if you can discount the ID we just used to narrow by color from the moved event). If orange, 6 checks (or 5). Make sense? 🤔 This way you're not checking all 16 blocks (or all map events) when 10 or 11 blocks play no part in whether the moved color block clears with others of its color or not.
The "include?" method for arrays already performs a linear check. If you perform the include check on all the three colors, you are already scanning all those events.
Code:
Pseudocode:
key = ""
For each color in hash do |color|
  if hash[color].include?(event_id)
    key = color
  end
end // 16 iterations
current_id = event_id
direction = 0
For each element in hash[key] do |block|
  continue if (block == current event)
  continue unless (block adjacent to event)
  direction = block_relative_position
  For each element in hash[key] do |block2|
    continue if (block2 == current event) || (block2 == block)
    continue unless (block2 aligned with block) || (block2 aligned with current event)
    mark current event
    mark block
    mark block 2
  end // maximum 6 iterations
end // maximum 6 iterations, for a total of 6 * 6 = 36 iterations
// total number of iterations: 36 + 16 = 52
remove marked blocks // depending on the implementation this can be slower or faster

After initializing a multi dimensional array, the algorithm is the following:
Code:
Pseudocode:
color = current square block color
// no include? used, colors are stored in the multi dimensional array
For each direction in [2, 4, 6, 8] do |dir|
  x2 = rounded x with dir
  y2 = rounded y with dir
  new color = square x2, y2 block color
  if (new color == color)
    x3 = rounded x with dir
    y3 = rounded y with dir
    new color = square x3, y3 block color
    if (new color == color)
      mark squares at (x, y) (x2, y2) (x3, y3)
    end
  end // 1 iteration
end // 8 iterations for a total of 16 iterations
remove marked blocks // same complexity as before

It is obvious that the multi-dimensional array provides a solution that is more than three times faster. However, checking only the blocks in the hash is the main reason why I told the OP to create that hash. Checking all the events in the map is much worse than checking only those in the hash. It was not my intention from the very beginning to use those built-in method, as they are very inefficient here. I only pointed out that using them was inefficient and that there is no direct access to events in VX Ace, but only linear access.

In the end, something that is three times slower is not that bad, on the contrary, as long as the difference is only that, it is a viable option. And it is also one of the reasons why I did not move on to the code, but waited for the OP answer instead. Both methods have their good points in this situation. One makes the code re-usable and efficient for larger puzzles, the other requires less memory and speeds up loading the map.
I would like to say again - I can not scrip 🙁
Originally, I repeatedly queried the positions through the parallel process using variables, and created conditions and so on.

Unfortunately that led to Lags, because there were too many events with too many queries ... Unfortunately, I'm still not clear how to handle this problem now.

@mathmaster74 @Heirukichi

I'm sorry if I'm acting stupid ...... I hope you still want to help me.
@Feraleth Will you be using this puzzle just 1 time, or will you be using this type of puzzle more than once? If more than once, will you be making bigger puzzles that use this theme? That's what @Heirukichi needs to know to finish the code for you. We're still here. 🙂
I would like to say again - I can not scrip 🙁
I know that, I have already read that in your previous post. However, it looks like you are missed my question.
That's what @Heirukichi needs to know to finish the code for you. We're still here. 🙂
Precisely, I cannot provide you a script since the information you provided is lacking. Once I have enough information I can proceed with that. As we all mentioned above, we need to know how you want to approach this problem, if it is a one-time problem it does not make much difference, but if you are going to use this more than once, it is important to write it in the most efficient way. Try answering the following questions.
  • How big is the area where your puzzle takes place?
  • Could you provide a screenshot of the map where that happens?
  • Can the size be reduced or are there reasons preventing you from doing so?
  • Are you going to use this puzzle only in this situation, or are you going to use it in different situations, with a different number of blocks?
If you answer these questions, I think we can move on to the code.
@mathmaster74 @Heirukichi
Oh, I am very sorry. That actually escaped me.

The range is 9x15 (Screenshot)
Since I reconstruct this puzzle, the area should remain as large as possible.
I would like to use this kind of puzzle more often if possible (different colors and number of blocks).

Edit: Dont worry, its are 5 Red, 5 Purple and 6 Yellow Blocks (The additional Reds was for testing something).

Thanks again, for your help 🙂
@Feraleth thank you for providing a screenshot and the informations I need. By the way, eliminating the red blocks looks quite easy to me, is it not? I mean, you can just push the middle one by 1 step and then push it back to its original spot. Are you sure you want them to be in that starting position?

Anyway, I am going to write the code as soon as I can. I will post a link to the code here when I complete it.
@Feraleth thank you for providing a screenshot and the informations I need. By the way, eliminating the red blocks looks quite easy to me, is it not? I mean, you can just push the middle one by 1 step and then push it back to its original spot. Are you sure you want them to be in that starting position?

Anyway, I am going to write the code as soon as I can. I will post a link to the code here when I complete it.

@Heirukichi Thank you very much for this 🙂

The blocks are just arbitrary. I hope the position was not important. Otherwise, I'll gladly make you a new screenshot. In the attachment, I send you the original for safety's sake 🙂
I hope the position was not important.
It is not, not for writing the code at least. Even so, when the player plays your game and tries to solve your puzzle, blocks position plays an important role; this is why I pointed that out.

Original thread: https://forums.rpgmakerweb.com/threads/problem-with-building-a-puzzle.110133/