Basti An

unread,
May 20, 2017, 9:30:58 AM5/20/17
to MIT App Inventor Forum
Hello folks,

I'm currently programming a little minesweeper clone for class and have a problem. 
I've come to point where a couple of random buttons are determined as mines and, if you click them, they explode.
If you, however, click a non-mine, it gets excavated. My problem now is, that I don't know how let their "neighbours" be automatically 
excavated as well, as long as there is no mine. A friend gave me the word "recursion", I don't really know how it should
work in my occasion, though.

Thanks in advance, Basti
minesweeperVA.aia

Abraham Getzler

unread,
May 20, 2017, 6:28:16 PM5/20/17
to MIT App Inventor Forum
Start by visiting 
to see the algorithm and data structures you will need.

Also think about how you will do right click and double button click in AI2.
The buttons have only Click and LongClick events.

See the Books section of this FAQ for how to pass procedure parameters,
and for how to write your own value procedures.

Learn to use the Any Blocks to pass button components to common procedures,
as you already have duplicated code. 

Also see the Lists section for how to work with lists.

TinyDB can be used to manage cell data if you design your tags right, including cell ID and attribute name in the tag.

Use the search box on this web page to look for discussions on recursion or recursive.
It's not guaranteed in AI2, but people have gotten away with it.

See the index of my projects for samples ...

ABG
some_blocks.png
Designer.png

Abraham Getzler

unread,
May 21, 2017, 4:30:58 PM5/21/17
to MIT App Inventor Forum
Regarding the recursion, here's the application of it:

Define a simple representation of a cell position, like "row,col"
(text JOIN(row,",",col)) 

Create a function Neighbors(cellID) that returns a list of the
direct neighbor cellIDs around input cellID. (0-8)

Create a function IsABomb(cellID) that returns true if there
is a bomb at cellID.

Create a function NeighboringBombs(cellID) that returns the count
(0-8) of bombs in Neighbors(cellID).

Create function Union(list1, list2) that returns the set union of
list1 and list2 (avoiding duplication.)

Create a function Frontier(cellList) that returns a list of cellIDs
that are neighbors of cells in cellList and have 0 neighboringBombs 
and are not in the list cellList.  Make Frontier contents cells visible. 
If Frontier is empty, return cellList,
else return Frontier(Union(cellList, Frontier(cellList))).

That's your recursion.

ABG


Reply all
Reply to author
Forward
0 new messages