So, in the game I'm working on, I want to have a Minesweeper minigame at one part of the game. How would I go about developing this? I had an idea of using arrays, self variables, and other nonsense like that, but I was wondering if there was a smarter/easier way to do this. Basically, what I need is for a tile to be able to randomize it's state to be either a bomb or a normal tile, and normal tiles can sense bombs touching them on all four edges and also on the diagonals. Then, I need to be able to count the number of bombs to give you a decent number of flags to flag the bombs with, and I need to be able to sense when all of the bombs have been defused. Is there an easier way to do this?
Minesweeper
RPG Maker MV SupportThe bomb randomization and flag management is relatively easy, although it may not scale well depending on how many bomb tiles you'll need.
You can treat each tile as an event. The first page of the event is a parallel process that generates a random number and a conditional branch based on that number.
One branch is the "I'm a bomb" option. It gives the player a flag, adds 1 to a "total bombs" variable, pushes the event's coordinates to an array or variable, and activates a self-switch that moves onto the "bomb" page of the event.
The other branch activates a separate self-switch that moves onto the "sweeper" page of the event.
The bomb page is an action button event that gives the player a choice to "flag" the tile or sweep it.
Flagging the tile removes a flag and checks to see if the flagged tile matches a recorded bomb coordinate. If it does, it adds 1 to a "flagged bombs" variable and checks to see if "flagged bombs" equals "total bombs". If "flagged bombs" and "total bombs" are equal, the player wins. If not, the event flips another self-switch that moves onto the "flagged" event page.
Sweeping the bomb makes it go BOOM. The player is teleported back to the start of the sweeper puzzle, and the whole room reloads.
The sweeper page is an action button event that gives the player a choice to "flag" the tile or sweep it.
Flagging performs the same function as flagging a bomb tile.
Sweeping adds the sweeper's event ID to a "swept tiles" array and checks the event's coordinates against each stored bomb coordinate.
First, if the tile is already in the swept tile array, that means the player already hit this one, and we don't need to recalculate for a bomb value.
Next, your event code is going to get lengthy. For each bomb coordinate, you'll want to check if the sweeper event's X and Y coordinates are equal to +1 or -1 of a bomb's X or Y coordinate. Whenever the sweeper detects a bomb in its range, it adds 1 to a variable. After all bomb coordinates are checked, run a conditional branch based on the number of bombs it detected, change the tile graphic to something representing the number of bomb's detected, and change the passability of the event to "through".
You can treat each tile as an event. The first page of the event is a parallel process that generates a random number and a conditional branch based on that number.
One branch is the "I'm a bomb" option. It gives the player a flag, adds 1 to a "total bombs" variable, pushes the event's coordinates to an array or variable, and activates a self-switch that moves onto the "bomb" page of the event.
The other branch activates a separate self-switch that moves onto the "sweeper" page of the event.
The bomb page is an action button event that gives the player a choice to "flag" the tile or sweep it.
Flagging the tile removes a flag and checks to see if the flagged tile matches a recorded bomb coordinate. If it does, it adds 1 to a "flagged bombs" variable and checks to see if "flagged bombs" equals "total bombs". If "flagged bombs" and "total bombs" are equal, the player wins. If not, the event flips another self-switch that moves onto the "flagged" event page.
Sweeping the bomb makes it go BOOM. The player is teleported back to the start of the sweeper puzzle, and the whole room reloads.
The sweeper page is an action button event that gives the player a choice to "flag" the tile or sweep it.
Flagging performs the same function as flagging a bomb tile.
Sweeping adds the sweeper's event ID to a "swept tiles" array and checks the event's coordinates against each stored bomb coordinate.
First, if the tile is already in the swept tile array, that means the player already hit this one, and we don't need to recalculate for a bomb value.
Next, your event code is going to get lengthy. For each bomb coordinate, you'll want to check if the sweeper event's X and Y coordinates are equal to +1 or -1 of a bomb's X or Y coordinate. Whenever the sweeper detects a bomb in its range, it adds 1 to a variable. After all bomb coordinates are checked, run a conditional branch based on the number of bombs it detected, change the tile graphic to something representing the number of bomb's detected, and change the passability of the event to "through".
Thank you, @duty! I had already thought of most of those steps, but some of them are really smart! The only problem I have is, I want the Minesweeper game to be tileable. What I'm hoping for is that all of the code can just be duplicated across events, and I don't want it to take up, like 9 bazillion variables. Do you know of any way to work around this?
I would approach this by using regex pattern matches on the event names and using "bomb" or "safe" names for the events so that the code can all be centralised.
You can store arrays in variables and use the script calls feature on the event to parse the values.
Check out option 4 on the latest blog tutorial. It's pretty relevant to this solution. You might even be able to do a proper for loop this way.
www.rpgmakerweb.com
How big do you plan to make the minesweeper grid?
Check out option 4 on the latest blog tutorial. It's pretty relevant to this solution. You might even be able to do a proper for loop this way.
Randomness in NPC Dialogue | The Official RPG Maker Blog
“Welcome to our village.” “Welcome to our village.” “Welcome to our village.” “Welcome to our village.” Sometimes hearing an NPC say the same thing over and over can be boring. For players who are looking for some world-building or little lore bits, these one-phrase NPCs can be disappointing and...
How big do you plan to make the minesweeper grid?
I think the biggest thing for this would be the part where clicking on an empty space that isn't mine-adjacent uncovers all tiles up to the first mine-adjacent number tile in all directions.
Regular expressions use patterns to match text, it's used a lot in script/plugin notetags: like all those <stuff> notetags you've seen in plugins generally use regex to match the brackets with some text inside them.
In this case I would basically just check that the event name is "bomb" or "safe" so probably wouldn't even need regex, come to think of it. As I said, the most complex part would be the recursive revealing of squares when you click on one that isn't adjacent to a mine.
In this case I would basically just check that the event name is "bomb" or "safe" so probably wouldn't even need regex, come to think of it. As I said, the most complex part would be the recursive revealing of squares when you click on one that isn't adjacent to a mine.
this is a clear example of *systems modelling*: it requires a focus on *operations* rather than data structures or conditions.
in SM, you focus on each individual operation and their own individual, isolated results.
so, in this case you have to worry about three things:
-exploding of a mine.
-exploding of all mines.
-exploding of no mines.
exploding of all mines is easy: for each explosion you decrease the score, and once it reaches 0 it's game over.
exploding of no mines and exploding of one mine are linked together: they all require interaction with one tile.... and that's the core function you should look at.
work it out to "click -> mine?/yes/no -> process" and then go from there.
"process" in this case would deal with handling the score and game over if it's a mine, and triggering adjacent tiles if it's no mine.... WHICH, feeds back into the loop for the next analysis, and repeat.
once you solve the triggering of *a* mine, then *all mines* would obey to whatever structure you use to lay them out, and that can be handled separately.
in SM, you focus on each individual operation and their own individual, isolated results.
so, in this case you have to worry about three things:
-exploding of a mine.
-exploding of all mines.
-exploding of no mines.
exploding of all mines is easy: for each explosion you decrease the score, and once it reaches 0 it's game over.
exploding of no mines and exploding of one mine are linked together: they all require interaction with one tile.... and that's the core function you should look at.
work it out to "click -> mine?/yes/no -> process" and then go from there.
"process" in this case would deal with handling the score and game over if it's a mine, and triggering adjacent tiles if it's no mine.... WHICH, feeds back into the loop for the next analysis, and repeat.
once you solve the triggering of *a* mine, then *all mines* would obey to whatever structure you use to lay them out, and that can be handled separately.
It would work for determining whether the event you're over is a mine or not a mine, which is the only thing I was using the name for.
*the data structure* will tell you that.
a tile will be *whatever the result of the generation process says it will be*
work with both things separate: one, you focus on handling the input, and the other, you focus on generating the array.
once you set the tile to refer itself to the board, it'll all work itself out, because then you can set another process to replicate the tile, which is what you haven't been considering.
events can be replicated dynamically, you don't have to program and lay out each one individually.
Since the first time we had scripting ability in the engine, really. An event is just an instance of a class, so if you have a reference to one you can create duplicates of it by just creating another new instance with the same properties.
@Trihan, that's interesting. How would one go about doing that?
The exact method depends on which RM you're doing it in.
@Trihan, OK, I'm using MV because I haven't had the time to go pick up the new and shiny MZ yet...
yep.
as long as the original targets an entity that will be there at run time, so that when you activate it, it doesn't throw an error.
that's why I say to focus on the operation: if the operation states that an object should be read, then you must consider that object outside of the event.
when the event is set to simply "do *this*" you no longer have to worry about the event, only about the *this*
if you set the event to just "ping Minesweeper main", then the core object (Minesweeper) will decide whether or not that ping comes from a mine or an empty tile, based on it's own logic.
that logic works outside the event.
the event only sends "it's me!" to the manager.... if it's an empty tile, then the manager pings the adjacent tiles, and the process repeats.
if it's a mine, then the manager blows it up, updates the score, and goes back to waiting.
HOW does it know which is which? it reads it's own table, where all the mines are marked.
HOW do you distribute those mines? easiest way is to randomize the spawn over the map, nice and even.
HOW do you handle an empty tile? that's another topic, because you have to decide whether or not you'll show how many adjacent mines there are to that tile (as in the original minesweeper) which is what stops the search.
I've moved this thread to MV Support. Thank you.
The other function that may be difficult to code is clicking on a number indicator that's already had all of its adjacent mines marked to uncover all adjacent non-revealed tiles that aren't mines.
Original thread: https://forums.rpgmakerweb.com/threads/minesweeper.126233/