What? You don't know Minesweeper? Well ... if you're going to do this tute, you're going to have to take time out to play!
Minesweeper has been offered with Windows since Day One. So ... off you go and master it cos we're going to build our own version of this famous game.
I started out by realising that yet again I'm going to need a grid. Here's what I started with:
Graphics 640,480 SetBuffer BackBuffer() SeedRnd MilliSecs() Global gn_rows=5 Global gn_cols=6 Dim grid(gn_rows+1,gn_cols+1) init() display() Flip WaitKey End Function init() End Function Function display() End Function
Note the g in front of my global variables. This is an essential survival skill if we're not going to get lost when programs become large. My games for example run to over 10,000 lines of code!
Another trick of the trade is to surround the playing grid with a border of unused squares - that's what the +1 is for. And of course you didn't know that BB arrays start with 0 not 1.
And this is how I'll use the grid:
Ex. 6:
I plan to make about 1 in 5 squares, mines. See if you can write the init() function yourself.
My solution.
I like to see how things are developing and that I haven't made any mistakes,
so I made this crude display() function and checked the result:
Function display()
y=50
For r=0 To gn_rows+1
x=50
For c=0 To gn_cols+1
Text x,y,grid(r,c)
x=x+20
Next
y=y+20
Next
End Function
|
|
Ex. 7:
The key to this program is to be able to tell how many mines surround a particular square.
Recall that a square may have as many as 8 neighbours. See if you can write
mines(r0,c0) to return how many mines surround (r0,c0).
My solution takes only 9 lines!
A new display() function to see how we're going:
Function display()
y=50
For r=1 To gn_rows
x=50
For c=1 To gn_cols
If grid(r,c)=1 Then
Color 255,0,0
Text x,y,"M"
Else
n=mines(r,c)
Color 255,255,255
Text x,y,n
End If
x=x+20
Next
y=y+20
Next
End Function
|
|
We're going to need another array to keep track of the state of each square. Here's my proposal:
Ex. 8:
So we'll need to add this new array and make sure we initialise it.
Also we'll need to set up a left_click() and a right_click()
function. The code before the functions will need altering too.
For the moment, just use a small x for the unclicked squares
so we know where to click!
See how far you can get before checking
my solution.
Did you notice that left_click() and right_click()
only differ by one character?
I'm replacing them by click(n) where n is 1 or 2.
This would also be a good time to globalise our layout numbers: 50,50 and 20.
I've used gx0, gy0 and gw.
Check your program thoroughly before looking at
mine.
You'll note that I've tried to add borders with the result as shown.
|
|
Note how the symbols are in the top left instead of the centre.
Easily fixed:
Global gw2=gw/2 ... Text x+gw2,y+gw2,"x",True,True etc |
|
So the game is fully functional BUT it doesn't behave like the original does it? Try and figure out the magic that occurs in the original that we need to reproduce. We'll look into it in the next tute and weave some magic of our own as well!