Way to reduce 700+ Conditional Branches in Common Event?

EDIT EDIT: I changed my approach and everything seems to be working better. My Event is now creating 55% of a Sudoku puzzle (randomly) without any conflicts. Of course, the last 45% will probably be where things go off the rails. But there is still valuable information in this thread. I would just disregard my initial concept.

EDIT: NEVERMIND FOR NOW. @Mac15001900 helped me release that my approach isn't going to work. It is not making my brain hurt. Urgh Argh!


Hello, everyone. I posted a status update about realizing that the way I designed a concept in a Sudoku puzzle was going to require up to 32 Common Events that each have 700+ Conditional Branches to make work. At least the way I know it will work based on what I know how to do.

I have made a working Sudoku puzzle before, but it had pre-generated puzzles. So all the squares of the puzzle had a predetermined, fixed value.

I want to generate a random Sudoku puzzle. So the solution I came up with is this.

I have Location Arrays and Value Arrays for the 9 Columns, 9 Rows, and 9 Groups of the puzzle (81 total squares). Each Location Array has 9 values depicting the 9 possible squares in that Column, Row, or Group. Each Value Array has 9 values depicting the possible values in the Sudoku puzzle (example numbers 1-9).

I am also allowing the player to choose the number of displayed hints (14 is the lowest and 32 is the highest).

To randomly place the values in the puzzle, the Events go through all the Columns then the Rows then the Groups (until the Hint limit is reached).

So, the first Common Event randomly removes a Location value from the Column 1 Location Array to determine which square in Column 1 the hint will be displayed. Then it randomly removes a value from the Column 1 Value Array to determine which number is displayed (example 1-9).

The Location and Value are also removed from the appropriate Row and Group Arrays. So if the 1st square in Column 1 is picked, the 1st square location from the Row 1 and Group 1 Location arrays would also be removed so that square can't be picked again. And the value picked is also removed from the Row 1 and Group 1 value arrays.

All of that really isn't that bad.

But then I decided to have 9 different puzzle types just because I wanted to (Numbers, Letters, Colors, Hero Portraits, Villain Portraits, NPC Portraits, Food Icons, Elements Icons, and RPG Gear Icons).

Therefore, depending on what puzzle type is picked, what is displayed in the randomly selected square for the randomly selected value could be one of 9 things.

So, the 1st square in Column 1 is randomly selected and has a randomly selected value of 1. The player choose the traditional number puzzle so the number 1 will be displayed in that square. If they had chosen letter, then A would appear.

After the random square and value from column 1 is selected and displayed, it moves on to Column 2, then 3, up to Column 9. Then it starts on the Rows. Then the Groups. Then back to Columns until all hints are displayed.

This works and makes sure that no square is picked twice and no value is repeated in any given Column, Row, and Group.

The only way I know how to do this is nested Conditional Branches. Something like:

If Column 1 Location = 1
If Puzzle Type = 1
If Column 1 Value = 1
Show Picture in the correct spot
If Column 1 Value = 2
Show Picture in the correct spot
.....
If Puzzle Type = 2
If Column 1 Value = 1
Show Picture in the correct spot
....
If Column 1 Location = 2
...
ETC...ETC...ETC...LOTS OF CONDITIONAL BRANCHES.

So all of this is just to create a randomly generated puzzle.

I hope I explained this in a way that makes sense.

Can anyone see an easier way to approach this?

Spoiler: Common Event Example Screenshots
Random Sudoku 1.pngRandom Sudoku 2.pngRandom Sudoku 3.png
You make a multidimensional array of your picture filenames per value per type.

So if you do, say,
Code:
var puzzleImages = [
["colors1", "colors2", "colors3"],
["gears1", "gears2", "gears3"]
];

Then you say one variable is storing which puzzle theme it is (0 is colors, 1 is gears) and the other variable has your value. And you'd put puzzleImages[theme][value] into your show picture script call for the filename that you need.

Another approach is to name all of your files consistently, the way I did above. Then you can store the string description of the theme in one variable (so the variable = "colors"). So in your script call for the image filename you'd put theme+value and that makes "colors"+2 = "colors2"

And, of course, all of this can be put into game variables, but I know you know some bits of stuff - I don't want to get into over-explaining unless you need it.
To randomly place the values in the puzzle, the Events go through all the Columns then the Rows then the Groups (until the Hint limit is reached).

So, the first Common Event randomly removes a Location value from the Column 1 Location Array to determine which square in Column 1 the hint will be displayed. Then it randomly removes a value from the Column 1 Value Array to determine which number is displayed (example 1-9).

The Location and Value are also removed from the appropriate Row and Group Arrays. So if the 1st square in Column 1 is picked, the 1st square location from the Row 1 and Group 1 Location arrays would also be removed so that square can't be picked again. And the value picked is also removed from the Row 1 and Group 1 value arrays.

All of that really isn't that bad.

But then I decided to have 9 different puzzle types just because I wanted to (Numbers, Letters, Colors, Hero Portraits, Villain Portraits, NPC Portraits, Food Icons, Elements Icons, and RPG Gear Icons).

Therefore, depending on what puzzle type is picked, what is displayed in the randomly selected square for the randomly selected value could be one of 9 things.

So, the 1st square in Column 1 is randomly selected and has a randomly selected value of 1. The player choose the traditional number puzzle so the number 1 will be displayed in that square. If they had chosen letter, then A would appear.

After the random square and value from column 1 is selected and displayed, it moves on to Column 2, then 3, up to Column 9. Then it starts on the Rows. Then the Groups. Then back to Columns until all hints are displayed.

This works and makes sure that no square is picked twice and no value is repeated in any given Column, Row, and Group.
The problem with this approach is that it likely won't generate a solvable puzzle, regardless of whether it's evented or coded. For example, consider the following partially generated puzzle:

Code:
. . . | . . . | 3 4 .
1 2 . | . . . | . . .
. . . | 1 X . | . . .
------+-------+ ------
. . . | . . . | . . .
. . . | . . . | . . .
. . . | . . . | . . .
------+-------+ ------
. . . | . . . | . . .
. . . | . . . | . . .
. . . | . . . | . . .


When choosing a digit to place in the spot marked with X, your approach will still consider 2 to be a valid option, even though it won't be possible to place a 2 in box 3 if it's chosen. As you add more and more digits, that chances of something like this eventually happening grow exponentially.

And of course there's the problem of such puzzles often not having any logical path to solving them even if they do have valid solutions, though I seem to recall mentioning this in your earlier thread and it not being an issue for you.

If you'd like your puzzles to be solvable, I'd recommend just using an existing JavaScript-based sudoku generator (example) and basing the digits off that.

EDIT: Fixed the ones in the example
@ATT_Turan Thanks for that! I haven't used multidimensional arrays before, but I think I see what you are talking about. I will have to experiment a bit to see if I fully understand how to incorporate it.

@Mac15001900 I don't understand why 2 wouldn't be a valid value for your X spot in your example. Your example also isn't valid because it has the number 1 twice in the first group of 9 squares. So that would be a problem itself.

I believe the method I described using Column, Row, and Group arrays ensures that no column, no row, and no group of 9 squares contains a duplicate number. Although I see a problem now with checking the Group Location Arrays after checking all the Column and Row Arrays. I have to think about that more.

I acknowledge that a randomly generated puzzle could have more than 1 solution, especially if there are less than 17 hints to start (which means it isn't a true Sudoku puzzle). But in reality, as long as the puzzle can be solved, the player would never know that.

In fact, as long as the randomly generated hints work, the player would never know that it wasn't based on a pre-generated puzzle with only one unique solution unless they were told.

I guess I don't understand why a puzzle with 14 hints (which isn't a true Sudoku puzzle because it could have more than one solution) wouldn't have a logical solution following the same process that you follow to solve any puzzle. If you follow the same process and solve the puzzle, why isn't that a logical solution?

Perhaps you mean it doesn't have one unique solution?

EDIT: Actually, I see a bigger problem that I have to examine. Hmmm...interesting.

EDIT EDIT: So I think I see a solution to make sure that the random hints wouldn't conflict, but it actually creates MORE Conditional Branches.

For instance:

Column 1 has a value of 1 in the 1st square. So the value of 1 is removed from both the Row 1 and Group 1 array too. Column 2 wants to put a value of 1 in its 1st square. But a check of the values in the Row 1 and Group 1 Arrays say that value isn't available. So Column 2 generates a different random value and it turns out to be 3. So the value of 3 is removed from the Row 1 and Group 1 Arrays. Column 3 wants to put a value of 1 in the 3rd square of that Column. But a check of the Row 3 and Group 1 Arrays shows that the value of 3 isn't available in the Group 1 Array so Column 3 has to generate a different random value.

I think that would work. But I need to try to walk through a puzzle generation on paper to see if it really would work without conflicts.

And then I have to decide if doing all that work is worth it.

I will also check out the Sudoku plugin to better understand what that does.
@ATT_Turan Thanks for that! I haven't used multidimensional arrays before, but I think I see what you are talking about. I will have to experiment a bit to see if I fully understand how to incorporate it.

@Mac15001900 I don't understand why 2 wouldn't be a valid value for your X spot in your example. Your example also isn't valid because it has the number 1 twice in the first group of 9 squares. So that would be a problem itself.

I believe the method I described using Column, Row, and Group arrays ensures that no column, no row, and no group of 9 squares contains a duplicate number. Although I see a problem now with checking the Group Location Arrays after checking all the Column and Row Arrays. I have to think about that more.

I acknowledge that a randomly generated puzzle could have more than 1 solution, especially if there are less than 17 hints to start (which means it isn't a true Sudoku puzzle). But in reality, as long as the puzzle can be solved, the player would never know that.

In fact, as long as the randomly generated hints work, the player would never know that it wasn't based on a pre-generated puzzle with only one unique solution unless they were told.

I guess I don't understand why a puzzle with 14 hints (which isn't a true Sudoku puzzle because it could have more than one solution) wouldn't have a logical solution following the same process that you follow to solve any puzzle. If you follow the same process and solve the puzzle, why isn't that a logical solution?

Perhaps you mean it doesn't have one unique solution?

EDIT: Actually, I see a bigger problem that I have to examine. Hmmm...interesting.
Sorry, I meant to spread the ones across two boxes. Though from that edit I'm assuming you've noticed the problem. For people that haven't: if you place a 2 in the spot marked with X, where can you put the digits 1 and 2 in the top-right box?

In general, determining if you can place a digit somewhere without breaking the puzzle requires you to create a full sudoku solver, which is not an exercise I'd recommend for a beginner programmer.
@Mac15001900 Well crap! Yes, I see that problem now too.

Oh, dear. This would be so much harder than I initially thought. The stubborn part of me wants to keep pondering it. The logical part of me says...don't be stupid.
Well, I updated the initial message to say that I have an Event that can create 55% of a random Sudoku puzzle without any conflicts. But the last 45% will probably be the most difficult. I guess time will tell.

I have 36 squares of the puzzle left. We will see.

I changed my approach from trying to make just the random hints to trying to make an entire random puzzle. That seems to be going much better, but we will see if the logic I am trying to use will pan out.

It might not, but at least I am still learning things as I go.
Are you sure that 55% part is now created in a way that will make it possible to create the rest? Because if not, the rest might prove rather difficult 😉

If you're looking for a simpler way, it might be simpler to manually prepare a set of full grids, and when generating a puzzle pick one of them, then scramble it a bit (swap columns within the same 3-wide band, similarly swap rows, swap 3s with 7s etc), and then pick your clues from that (still valid) grid. It will only generate a smaller set of puzzles, but that might be good enough.
@Mac15001900 No, I am definitely not sure. I am going one Group at a time and testing the entire generation process as I go, working out the kinks that I didn't foresee. I have Group 5, 6, 8, and 9 left. Most likely the most problematic. I have finished Groups 1, 2, 3, 4, and 7. I am starting on Group 5 now.

I think the real problems will show themselves when I try to do Group 8 and 9 at the end. Right now, I have been able to just redo the latest Group whenever it got to a point where it couldn't proceed.

But I know I will hit a point where just redoing that Group won't be enough. Then I will resort to having it restart the puzzle from the beginning.

My thoughts are that if my approach could generate 1 valid puzzle out of however many tries, then it just becomes a question of how long it takes to generate a valid puzzle. So my current efforts are just to see if I can manage to generate a valid puzzle with this approach.

But we will see.

I will have to think more about your other suggestion. I have a hard time conceptualizing things sometimes. I often have to try to Event something to figure out why it won't work.

I will definitely share my ultimate success or failure with this approach. But even though it is a lot of work, I am still learning things in the process so there is value in the journey at least.
Ah, so I guess you're going through every cell one by one and placing a random valid digit into each of those? If so, now I'm really curious as to what the probability of that succeeding is, the results of that are definitely going to be very interesting!
We're dealing with "700+" conditional branches because we're trying to appropriate a system designed for cutscenes to doing something it was never intended to do, and that's algorithmically generate a solveable Sudoku puzzle. If this was coded traditionally, you would not need 700+ conditionals, you need only just a couple of algorithms that are mostly for-loops. If you're not familiar with for-loops, I strongly recommend researching those

EDIT: Also there is no shame into googling algorithms for creating Sudoku puzzles. Programming is often not a game of creating original works. The best code is one that performs the job, without errors, with the fewest calculations and that's what is rewarded.
@Mac15001900 Yes, I am building the Event Group by Group and each Group checks each new square against the applicable row and column as it goes. I just finished testing Group 5 and it still works. It had to restart the Group about 3 times to get a valid 77% finished puzzle. It will definitely be interesting to see at what point just restarting the latest Group won't be enough. Only 3 Groups to go (27 squares).

@Gensun I definitely get your point about using things that are already built. But, for me, there is something very satisfying with figuring out how to make things work on my own, particularly through Eventing. That I why I still haven't delved very deeply into plugins. Because I am still learning what I can achieve with just Eventing and Scripts.

Even if I am able to make this work, there is no guarantee that I will choose to use the Event over something that works better and more efficiently.

But for now, I am having fun experimenting with this and learning quite a bit from the effort. So, for now, there is still added value for me to keep trying.

And I want to also experiment with some of the previous suggestions on how to reduce the number of Conditional Branches.

I have seen and read about for-loops before, but they aren't something I fully grasp just yet. But the more I go back and re-read the Java Script course, the more I understand so I will keep plugging away.
I have seen and read about for-loops before, but they aren't something I fully grasp just yet.
It's just a way to notate "Perform these instructions this many times." What don't you get?
(no snark, I'll answer your questions if you have them)

The problem (in my head, I haven't tried doing it out), is that even using a few nested for loops, I think you could still end up pigeonholed with a few spaces left at the end where you don't have valid choices.

And it's easy for a person to process that visually, say "Okay I'll swap these numbers so I have a valid space here," but that's complicated to make procedural instructions to account for.
@ATT_Turan I get the general concept of the for-loop when I read it, but when it comes to actually applying it in what I am working on, I sort of get a little lost. But you already gave me some ideas that I need to experiment with and I really need to go back to the Java Script course and try experimenting more with the concepts I have read about.

And I'm not using loops or nested conditional branches in my current experiment. I am taking each square one by one can checking the random number against the row and column.

I essentially have arrays for all the rows, columns, and groups. As a value is used in a square, it is removed from the row, column, and/or group arrays, as appropriate. So I am using the remaining values in the arrays to check to see if a randomly selected value can be placed in the square.

I agree that the kicker will be the last two groups. I only have 3 groups left and, so far, it has been sufficient to just restart the last group that is being generated.

But it will get harder and hard, most likely, toward the last few squares.
But for now, I am having fun experimenting with this and learning quite a bit from the effort. So, for now, there is still added value for me to keep trying.
By the way, this is the only reason I'm mostly sitting back and enjoying popcorn here, rather than just giving you a quick script that would do all of that for you. But if you would like such a script do feel free to ask 😉

I think you could still end up pigeonholed with a few spaces left at the end where you don't have valid choices.
My napkin math seems to get me about a 90%-99% chance of that happening. So you'd probably only need to generate ~100 puzzles until you get a working one, unless my estimates are way off.
@Mac15001900 I will definitely ask for a quick script after, if it is relatively easy to do and you don't mind sharing. The popcorn part is very valid though. :popcorn:

And here is the funny thing, if it really only takes about 100 attempts to get a valid puzzle, I don't think that is bad. Especially since the game can do all that checking pretty quickly and then only display the valid puzzle when it is complete.

I will be overwhelmingly ecstatic if I can manage to get this event to generate a valid puzzle. And if it can, I will then be very curious to see if I can track how many attempts it takes on average and the average time to generate a valid puzzle.
@Mac15001900 & @ATT_Turan I am declaring victory and a smashing success!

I still have some additional testing to do, but I just generated 4 valid puzzles. And the last 3 didn't require any puzzle restarts and each of the last 3 puzzles took less than 20 seconds to generate.

This definitely isn't the most efficient way to do it I'm sure, but it also definitely works.

Here are the screenshots of the 4 valid puzzles. The first valid puzzle actually required 10 puzzle restarts, but I discovered a mistake in my Variable tracking. But the other 3 puzzles were all generated sequentially with no puzzle restarts.

I am not a numbers person so I have no idea how to explain these results. Other than...here they are.

I am going to make a (hopefully) short video of the Event running.

I am incredibly happy with this.

Spoiler: Valid Puzzles
Valid Puzzle.pngValid Puzzle 2.pngValid Puzzle 3.pngValid Puzzle 4.png
@Mac15001900 & @ATT_Turan I am declaring victory and a smashing success!

I still have some additional testing to do, but I just generated 4 valid puzzles. And the last 3 didn't require any puzzle restarts and each of the last 3 puzzles took less than 20 seconds to generate.

This definitely isn't the most efficient way to do it I'm sure, but it also definitely works.

Here are the screenshots of the 4 valid puzzles. The first valid puzzle actually required 10 puzzle restarts, but I discovered a mistake in my Variable tracking. But the other 3 puzzles were all generated sequentially with no puzzle restarts.

I am not a numbers person so I have no idea how to explain these results. Other than...here they are.

I am going to make a (hopefully) short video of the Event running.

I am incredibly happy with this.

Congrats! This is a really complex thing to make with just eventing. And it does look like my napkin math was indeed quite a bit off, though luckily in a good way.

As to that script I mentioned, what would you like to see? The shuffling approach I mentioned earlier, your approach but re-implemented with pure JavaScript, or using an existing generator?
Because if that last one, I spent a while thinking about exactly how I'd do that, and ended up just... doing it. So expect a sudoku minigame plugin coming out soon 😛
Making a plug-in sounds awesome! That is way beyond my limited abilities right now.

I would be happy to try anything you wanted to share.

My ongoing problem is that I still struggle sometimes to figure out how to follow up from a script. Meaning the script does its thing, but then the transition back to Event Commands is not always clear to me. But I am working on it. Baby steps!

And I am very surprised by the results. I am still testing my Event and improving it, but so far, it still hasn’t taken more than 8 puzzle restarts to generate a valid puzzle and by eliminating the Show Picture commands until the puzzle is complete, the longest time has been cut in half…about 90 seconds.

But I don’t think that would be a problem since I would have the puzzle generation running first thing as a Parallel Event while another Event is gathering information about certain player choices.

And I can probably still speed it up a bit more.
I hope you don't mind me reanimating this thread, but discussing it on a status post doesn't work.

1707836391796.png


So, I don't fully know what you mean. But I'll say stuff, and if some of it doesn't apply to your setup, you'll clarify and we'll go from there.

To me, this is an array where each member is an array of four strings - e.g. one variable, not 384.

Let's say I'm tracking this in variable 69. First you need to initialize that variable to be an array:
Code:
$gameVariables._data[69] = [];

Then I could store the 4 notes for the third square thusly:
Code:
$gameVariables.value(69)[2] = ["Not a 4", "Not a 2", "Could be 5", "Could be 8"];

And retrieve the second note on the third square thusly:
Code:
$gameVariables.value(69)[2][1];

Original thread: https://forums.rpgmakerweb.com/threads/way-to-reduce-700-conditional-branches-in-common-event.164711/