Skip to main content
Microsoft logo
Power Apps
    • AI Builder
    • Automate processes
    • Azure + Power Apps
    • Build apps
    • Connect data
    • Pages
    • Take a guided tour
  • Pricing
    • Overview
    • Become a Partner
    • Find a Partner
    • Find partner offers
    • Partner GTM Resources
    • Blog
    • Customer stories
    • Developer Plan
    • Documentation
    • For IT Leaders
    • Roadmap
    • Self-paced learning
    • Webinars
    • App development topics
    • Overview
    • Issues
    • Give feedback
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • User groups
    • Register
    • ·
    • Sign in
    • ·
    • Help
    Go To
    • Power Apps Community
    • Welcome to the Community!
    • News & Announcements
    • Get Help with Power Apps
    • Building Power Apps
    • Microsoft Dataverse
    • AI Builder
    • Power Apps Governance and Administering
    • Power Apps Pro Dev & ISV
    • Power Apps Portals
    • Connector Development
    • Power Query
    • GCC, GCCH, DoD - Federal App Makers (FAM)
    • Power Platform Integration - Better Together!
    • Power Platform Integrations
    • Power Platform and Dynamics 365 Integrations
    • Community Blog
    • Power Apps Community Blog
    • Galleries
    • Community Connections & How-To Videos
    • Community App Samples
    • Webinars and Video Gallery
    • Canvas Apps Components Samples
    • Kid Zone
    • Emergency Response Gallery
    • Events
    • 2021 MSBizAppsSummit Gallery
    • 2020 MSBizAppsSummit Gallery
    • 2019 MSBizAppsSummit Gallery
    • Community Engagement
    • Community Calls Conversations
    • Experimental
    • Error Handling
    • Power Apps Experimental Features
    • Community Support
    • Community Accounts & Registration
    • Using the Community
    • Community Feedback
    cancel
    Turn on suggestions
    Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
    Showing results for 
    Search instead for 
    Did you mean: 
    • Power Apps Community
    • Galleries
    • Community App Samples
    • MineSweeper - Recursion on Power Apps

    MineSweeper - Recursion on Power Apps

    09-25-2021 01:26 AM

    PaulKypeo
    Regular Visitor
    1145 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    PaulKypeo
    PaulKypeo
    Regular Visitor
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    MineSweeper - Recursion on Power Apps

    ‎09-25-2021 01:26 AM

    Hi everyone,

     

    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 !).

     

    PaulKypeo_0-1632558311203.png

    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:

     

    1. The grid gets generated depending on the level. Harder means more rows and columns, and more mines.
    2. The user clic on a first cell. This first cell and the surrounding ones can't contain a mine. So we have do either generate the mines right during the clic, or get rid of all the mines in this area (I choosed the later).
    3. If the cell that is revealed has no mine around, then we need to reveal all the surrounding cells. Repeat this for every new cell revealed. This is where the recursion occurs.
    4. If the cell has one or more mine around, simply display the number of mines around.
    5. Then the user just clic on every cell he thinks has no mine in it. The first clic allows you to "flag" the cell (if you think theres a mine in it), the 2nd clic reveals the cell.

     

    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 :

    • the user clicks on a cell. We use Patch to change the Status value of ThisItem (one click=1, 2 clicks=2)
    • the toggle of the cell is triggered thanks to the default value
    • if the number of mines arround is >0, nothing happens
    • if the number of mines arround is =0, then we UpdateIt the collection to change the Status value of every surrounding cell
    • it triggers the toggles for each of this cells, propagating the logic.

     

    Performance is not very good however, I struggle to find ideas to improve it. Any tips or crazy algorithm in mind ?

     

    Have fun !

     

    Paul.

    MineSweeper_Kypeo.msapp
    Labels:
    • Labels:
    • Formulas and Controls
    • Galleries and Forms
    • Mobile App Design and User Experience
    • Offline App Design
    Message 1 of 3
    1,145 Views
    1 Kudo
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic
    AnthonyS1
    AnthonyS1
    New Member
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎04-07-2023 10:20 AM

    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.

    MineVersion1_20230330175832.zip
    Message 2 of 3
    429 Views
    0 Kudos
    Reply
    R3dKap
    R3dKap Community Champion
    Community Champion
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎04-11-2023 01:55 AM

    Hi @PaulKypeo,

    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):

    • one toggle to know if there's a mine
    • one toggle to know if the cell was clicked
    • one toggle to know if the cell is revealed
    • one label to show the number of mines

    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.

    Message 3 of 3
    398 Views
    0 Kudos
    Reply

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Pages
    • Power Automate
    • Power Virtual Agents

    Browse

    • Sample apps
    • Services

    Downloads

    • Windows
    • iOS
    • Android

    Learn

    • Documentation
    • Support
    • Community
    • Give feedback
    • Blog
    • Partners

    • © 2023 Microsoft
    • Follow Power Apps
    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Trademarks
    Consumer Privacy Act (CCPA) Opt-Out Icon Your Privacy Choices