MineSweeper Recursion
MineSweeper Recursion
MineSweeper Recursion
MineSweeper Recursion
MineSweeper Recursion
MineSweeper Recursion MineSweeper Recursion MineSweeper Recursion MineSweeper Recursion MineSweeper Recursion MineSweeper Recursion MineSweeper Recursion MineSweeper Recursion
MineSweeper Recursion MineSweeper Recursion
MineSweeper Recursion
Go Back  Xtreme Visual Basic Talk > > > MineSweeper Recursion


Reply
 
Thread Tools Display Modes
  #1  
Old 12-18-2011, 05:02 PM
Minesweeperhelp Minesweeperhelp is offline
Newcomer
 
Join Date: Dec 2011
Posts: 3
Default MineSweeper Recursion


Okay so im building a minesweeper game in Vb6. The main coding is pretty
straightforward however im having trouble making the code behind the zeros buttons (the minespots that have no mines adjacent). I am using two main sub procedures that are recursing off of each other with functions just checking where the minespot is located. I am using a control array and a boolean array. It is a 10*20 grid
code of trouble spot is as follows

Code:
Private Sub Determine(ByVal index As Integer)
'declare local variables
Dim intCount As Integer

'checks if mine swept is on a corner, side or in interior
If index = 0 Or index = 19 Or index = 180 Or index = 199 Then
    intCount = CornerMine(index)
ElseIf isSideMine(index) = "right side" Then 'checks if its on side but not corner
    intCount = RightMine(index)
ElseIf isSideMine(index) = "left side" Then
    intCount = LeftMine(index)
ElseIf isTopBottom(index) = "top" Then 'sends to appropriate function if on top of mine section
    intCount = TopMine(index)
ElseIf isTopBottom(index) = "bottom" Then ' sends to appropriate function if on top of mine section
    intCount = BottomMine(index)
Else
    intCount = Interior(index) 'then button clicked is indeed in the interior
End If
    
   'if zero
If intCount = 0 Then
    'complex looping structure with the determine sub procedure that allows indexs adjacent to the zero to be id'd.
'disable zero
cmdSpot(index).Enabled = False
cmdSpot(index).Caption = "0"
'disable or display captions of minespots adjacent to zero
If index = 0 Then
    Call Determine(index + 1)
    Call Determine(index + 21)
    Call Determine(index + 20)
ElseIf index = 19 Then
    Call Determine(index + 20)
    Call Determine(index + 19)
    Call Determine(index - 1)
ElseIf index = 180 Then
    Call Determine(index + 1)
    Call Determine(index - 20)
    Call Determine(index - 19)
ElseIf index = 199 Then
    Call Determine(index - 1)
    Call Determine(index - 20)
    Call Determine(index - 21)
ElseIf isTopBottom(index) = "top" Then
    Call Determine(index + 19)
    Call Determine(index + 20)
    Call Determine(index + 21)
    Call Determine(index + 1)
    Call Determine(index - 1)
ElseIf isTopBottom(index) = "bottom" Then
    Call Determine(index - 19)
    Call Determine(index - 20)
    Call Determine(index - 21)
    Call Determine(index - 1)
    Call Determine(index + 1)
ElseIf isSideMine(index) = "right side" Then 'check if on far left or far right row
    Call Determine(index - 20)
    Call Determine(inex - 21)
    Call Determine(index - 1)
    Call Determine(index + 19)
    Call Determine(index + 20)
ElseIf isSideMine(index) = "left side" Then
    Call Determine(index - 20)
    Call Determine(index - 19)
    Call Determine(index + 1)
    Call Determine(index + 20)
    Call Determine(index + 21)
Else 'means it is interior spot
    Call Determine(index + 19)
    Call Determine(index + 20)
    Call Determine(index + 21)
    Call Determine(index + 1)
    Call Determine(index - 1)
    Call Determine(index - 19)
    Call Determine(index - 20)
    Call Determine(index - 21)
End If
Else
    'determine what caption to display
    cmdSpot(index).Caption = intCount
End If

   

End Sub
the functions not displayed are pretty self explanatory i think. this is the problem ive been getting. I might be doing this all wrong as i taught myself recursion, so any thoughts or debugging tips are appreciated.
Attached Images
File Type: png minesweeper.png (75.6 KB, 16 views)
Reply With Quote
  #2  
Old 12-18-2011, 05:35 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default Iterative recursion

Quote:
The main coding is pretty
Looking at the code you provided that is somewhat hard to believe..

Quote:
..any thoughts or debugging tips are appreciated.
You code is full of "magic numbers" - you should dim them as individual variables or as an array (with comments on what each number is referring to in each case, so if later, down the road, the code ever has to be modified support a different grid arrangement).

In fact, ideally place a set of "master" numbers, assigned to variables at the top of the code (like grid number of horizontal and grid number of vertical squares)..then calculate all the sub numbers at run time (while still assigning them to an array for testing in an immediate windows and still adding commenting on what the values are referring to, of course).

The other thing I try never to do is "re-invent the wheel".

Since minesweeper games already exist, obvious someone else has already figured out how to code this.

I searched around and looked for a working VB6 minesweeper sample and came across "vbwinmine-g.zip" as the very last download entry/link on the bottom of this page:
http://www.dutchthewiz.com/vb/games/

Look for "Private Sub OpenBlanks" code in the clsWinMine class module and you will have something that works and you can start tweaking from there..

Last edited by surfR2911; 12-18-2011 at 05:49 PM.
Reply With Quote
  #3  
Old 12-20-2011, 01:20 PM
passel's Avatar
passelMineSweeper Recursion passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

Some people like to figure out how to solve a problem just for the joy of figuring out how to solve the problem, otherwise why write a game that's been written.

The error indicates that your function is calling itself repeatedly without meeting an exit condition.
For instance, how do you handle the case where the first index is 0, and the second index is 0?
If the first index is 0, one of the things it says to check is if the index+1 is 0.
If the index+1 is 0, one of the things it says to check is if the index-1 is 0.
If you don't have a way to note that you've already tested the first index, you will just go back and forth between the two checks, continually pushing more and more data on the stack to keep track of the nested values of recursion until you run out of space.

Visual Basic has excellent debug tools, you can simply set a breakpoint and step through the code to verify what it is doing and if that is what you want it to do.

I suspect that this might be solved with a single If block around your current code that tests if the cmdSpot(index).Caption is "empty". If it has a 0, or some other number in it, it has already been processed so the Sub should exit without processing it again. This should allow your recursion to "unwind".
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 12-20-2011 at 01:32 PM.
Reply With Quote
  #4  
Old 12-20-2011, 07:07 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default UIX/Game design Fork

This thread (and passel's post) spawned a fork:
Design & UIX threads rarely seen..
Reply With Quote
  #5  
Old 12-21-2011, 05:07 PM
Minesweeperhelp Minesweeperhelp is offline
Newcomer
 
Join Date: Dec 2011
Posts: 3
Default

yeah haha passels advice on that one if blocked helped me get rid of that sort
of infinite loop, but i do have a invalid control array index to clean up, that
shouldnt be to bad. just a quick question though, does anyone no how i would be able to make a "right click" event procedure, if that is possible? i looked
at the list of even procedures in the coding section but it wasnt there.
thanks all
Reply With Quote
  #6  
Old 12-22-2011, 07:27 AM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default Testing for right click using mouse events

Quote:
..just a quick question though, does anyone no how i would be able to make a "right click" event procedure, if that is possible? i looked at the list of even procedures in the coding section but it wasn't there.
It's done using the any of the mouse events (MouseDown, MouseMove, or MouseUp) by testing for button 2.
I usually use the mouse down event:
Code:
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
 If Button = 2 Then
  MsgBox "Right Click!"
 End If
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
 If Button = 2 Then
  MsgBox "Right Click!"
 End If
End Sub
Reply With Quote
  #7  
Old 12-22-2011, 10:00 AM
passel's Avatar
passelMineSweeper Recursion passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

Would probably be more "proper" to check against vbRightButton, rather than a literal 2.
The others are vbLeftButton (1) , vbMiddleButton (4)

Don't know if you're aware that the Click event occurs at MouseUP, not MouseDown.
This allows "changing your mind". If you click on a button in windows, but realize you didn't want to click on the button, if it is implemented standardly, you can drag the mouse off of the button before releasing and the Click event won't occur (but both MouseDown and MouseUp events always occur).
The sequence in VB is MouseDown, [MouseMove], {button released} [MouseClick, if inside the control], MouseUp.
If you want the "RightClick" to emulate the (Left)Click behavior, you would want to check on the MouseUp, and look to see if you're within the bounds of the control.
We don't have games installed on our work machine, so I can't verify (now) if Minesweeper reacts to the right mouse button (switch symbols) on mouse down, or mouse up.
Code:
Option Explicit

Private Sub Command1_Click()
  Debug.Print "Click"
End Sub

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Debug.Print "Down"
End Sub

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Debug.Print "Up"
  If (X < 0 Or X > Command1.Width) Or _
     (Y < 0 Or Y > Command1.Height) Then
     Debug.Print "No click"
  End If
End Sub
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #8  
Old 12-22-2011, 12:02 PM
Minesweeperhelp Minesweeperhelp is offline
Newcomer
 
Join Date: Dec 2011
Posts: 3
Default

Well i got the right click to start working. Sometimes, the mines near the top
glitch out like so.
edit: yeah got that working...
Attached Images
File Type: jpg second minesweep problems.jpg (184.5 KB, 7 views)

Last edited by Minesweeperhelp; 12-22-2011 at 12:26 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
MineSweeper Recursion
MineSweeper Recursion
MineSweeper Recursion MineSweeper Recursion
MineSweeper Recursion
MineSweeper Recursion
MineSweeper Recursion MineSweeper Recursion MineSweeper Recursion MineSweeper Recursion MineSweeper Recursion MineSweeper Recursion MineSweeper Recursion
MineSweeper Recursion
MineSweeper Recursion
 
MineSweeper Recursion
MineSweeper Recursion
 
-->