"Cascade" effect in minesweeper

Blitz3D Forums/Blitz3D Programming/"Cascade" effect in minesweeper

Cancerian(Posted 2004) [#1]
Hi! I'm working on a minesweeper game in order to learn Blitz Basic and so far everything is working perfectly except creating the cascading effect that occurs when the player clicks on an empty square and any surrounding empty squares are also revealed. I hope I'm describing this okay.

I can't seem to wrap my head around how to go about revealing all the nearby blank squares to the one that has just been clicked. How do I reach out further than just the 6 touching squares around it.

All of the tutorials I've seen on-line are in C++ or Java, which I don't understand but they talk about using "recursion" to do this.

Any help? Thanks!


Rook Zimbabwe(Posted 2004) [#2]
POsting souurce code would help here... Are you using an array??? How are you checking the square??? I don't think I can answer this question but there are some very smart people here that probably can... You can post code buy typeing "
" and close that post with "
" (don't use the " things!) : )

Good luck
-RZ


Rook Zimbabwe(Posted 2004) [#3]
OK that example didn't work. You know the button that has { and [??? use [WORD] [/WORD] where WORD = code


Cancerian(Posted 2004) [#4]
I can't reveal my source code - I have to protect my ultra secret game design! Just kidding ;)

I'm using 2 arrays - "game_board(y,x)" for the values in the puzzle (0 = blank, 9 = mine,1-6 = number of corners touching mines) and "visible(y,x)" for checking visibility (1 = revealed, 0 = obscured). y and x are obviously the current puzzle board square locations.

For example, this is the code I use to draw the board. I'm about to change all the if-thens to case-selects but you get the idea:

;Draw the puzzle out
For y = 1 To 28
	For x = 1 To 27
		new_x = x_start + (20 * (x - 1))
		new_y = y_start + (20 * (y - 1))
		If visible(y,x) = 0
			DrawImage blank,new_x,new_y
		Else
			If game_board(y,x) = 0
				DrawImage empty,new_x,new_y
			ElseIf game_board(y,x) = 9
				DrawImage mine,new_x,new_y
			ElseIf game_board(y,x) = 1
				DrawImage one,new_x,new_y
			ElseIf game_board(y,x) = 2
				DrawImage two,new_x,new_y
			ElseIf game_board(y,x) = 3
				DrawImage three,new_x,new_y
			ElseIf game_board(y,x) = 4
				DrawImage four,new_x,new_y
			ElseIf game_board(y,x) = 5
				DrawImage five,new_x,new_y
			ElseIf game_board(y,x) = 6
				DrawImage six,new_x,new_y
			EndIf				
		EndIf
	Next
Next


and here is the code I used to determine numerical values for each piece (based on if corners are touching mines. I suspect this is close the method I will need to cascade but I'm not certain:

;evaluate the puzzle and look for numerical values
For y = 1 To 28
	For x = 1 To 27
		;look at all 5 corners around the location
		corners = 0
		If Not game_board(y,x) = 9
			;check upper left
				If game_board(y-1,x-1) = 9 Then corners = corners + 1
			;check upper
				If game_board(y-1,x) = 9 Then corners = corners + 1
			;check upper right
				If game_board(y-1,x+1) = 9 Then corners = corners + 1
			;check left
				If game_board(y,x-1) = 9 Then corners = corners + 1
			;check right
				If game_board(y,x+1) = 9 Then corners = corners + 1
			;check bottom left
				If game_board(y+1,x-1) = 9 Then corners = corners + 1
			;check bottom
				If game_board(y+1,x) = 9 Then corners = corners + 1
			;check bottom right
				If game_board(y+1,x+1) = 9 Then corners = corners + 1
			;write the board value
			game_board(y,x) = corners
		EndIf
	Next
Next


I hope this answers any questions. I might compile all this into a beginners tutorial once I'm done so any help is much appreciated :)


TomToad(Posted 2004) [#5]
I'd do it with recursion. In case you don't know, recursion is when a function calls itself

Something like:

Function checksquare(x,y)

if minefield(x,y) = false ; empty square
 reveal(x,y) ; uncover square
 nx = getnextx(x,y)
 ny = getnexty(x,y) ;get the next x,y coordinate to check
 if nx <> -1
  checksquare(x,y) ;if not all the squares been checked
 end if
 return false ;square does not contain a mine
else 
 return true ;square contains a mine
end if
end function


reveal(x,y) in the code above would be a function used to flag a revealed square.

getnextx(x) and getnexty(y) would be used to get the next x,y coordinates for the next revealed squares. Maybe you could start with the square above and check in a clockwise direction, return a -1 when all the squares have been checked.

This was hacked together in this window so I don't know how well it'd work. Mainly just trying to give you an idea of the program flow rather than giving you an actual usuable function.
I'm sure the getnextx(x) idea probably isn't the best way to do it. Maybe create a list of surrounding squares at the beginning of the function and looping through the list would be better.


Cancerian(Posted 2004) [#6]
Functions can call themselves, from within themselves? Cool!

Thanks, I will try this out later :)


TomToad(Posted 2004) [#7]
I noticed one little flaw in your program. You have it so that it only displays up to 6, but there are 8 possible locations for a mine. if you have something like

***
*E*
***

where E is an empty square and * is a mine, then your program will draw nothing.


Rook Zimbabwe(Posted 2004) [#8]
Way to go TomToad!!! Very cool!!! recursion... Could I have ever thought of that? Don't answer that! :)