09-25-2021 01:26 AM
as a challenge, I tried to implemente the logic of recursion in Power Apps. The best example I found was the game Mine Sweeper (old school cool !).
I'm a great designer, I know
The rule is pretty simple : a grid of cells contains a certain number of mines. The goal is to reveal every cell that do not contain a mine.
We can summarize the steps in the game like this:
So ! The challenge it the recursion. If a cell has no surrounding mines, then reveal the next cells. And repeat this again. And again.
As @mr-dang proposed on Twitter, we could prepare a list of every cell to reveal for each cell in the collection. I did not try that, and I'm not sure how to do it (seems like recusion anyway).
So I came up with the workaround of having toggles in each cell. The default value of the toggle is "ThisItem.Status=2", knowing that the field [Status] = 2 means the cell is revealed.
So what's going on is :
Performance is not very good however, I struggle to find ideas to improve it. Any tips or crazy algorithm in mind ?
Have fun !
Hi Paul, thanks for sharing this! I've used this game as a learning tool for new languages over the years and was excited to see I didn't have to start from scratch. I took a stab at it, but performance is still not great. I did, however, make quite a few code changes to make it more like the traditional game. Still not perfect, but a fun exercise. Let me know what you think and if you are interested in what I modified.
Very interesting neurons-stimulating use case 🙂
I'm wondering: wouldn't it be possible to implement it only using formulas inside toggles without any Patch() to a collection? I'm thinking of (in each cell):
The collection would be used only to initialize the grid and store each cell mines count.
I haven't given it too much thought so it might be not possible. And now that I write theses lines, I imagine there would probably be a circular reference issue... 😅
Other suggestion: no toggles, just a collection with the status of each cell. Create a canvas component with an output parameters that serves as a function that would call itself based on an exit condition (that would be the real recursive part). I haven't tested this of course. It's just a thought... 🙂 I'll give it a try these days to see if it's possible.
And another suggestion: using the OnReset event of a canvas component that would be triggered through a toggle OnChange event and condition that OnChange event -> that would create an endless loop with the exit condition stopping it.