LamKat Posted December 22, 2011 Posted December 22, 2011 hi all. I've been trying to make a basic version of mine sweeper. when i ran the program it did not give me all the expected outputs. i have tracked back the error to this bit of code that i have eddited down to just the bits that dont run and the context in which the code makes sence... can anyone see the problem Dim Xlength As Integer = 5 - 1, _ YLength As Integer = 5 - 1, _ chosen(0 To Xlength, 0 To YLength) As Boolean, _ value(0 To Xlength, 0 To YLength) As Integer, _ NoOfMines As Integer Private Sub set_board() Handles Me.Load NoOfMines = (((Xlength + 1) * (YLength + 1)) ^ 0.5) - 1 For FirstD As Integer = 0 To Xlength For SecondD As Integer = 0 To YLength chosen(FirstD, SecondD) = False value(FirstD, SecondD) = 0 Next Next Dim RandomD1, RandomD2 As Integer Randomize() For counter As Integer = 1 To NoOfMines Do RandomD1 = Rnd() * Xlength RandomD2 = Rnd() * YLength Loop Until chosen(RandomD1, RandomD2) = False chosen(RandomD1, RandomD2) = True Next Dim number As Integer For FirstD As Integer = 0 To Xlength For SecondD As Integer = 0 To YLength If chosen(FirstD, SecondD) = False Then number = 0 For X As Integer = -1 To 1 For Y As Integer = -1 To 1 If chosen(FirstD + X, SecondD + Y) = True Then number += 1 Next Y Next X value(FirstD, SecondD) = number Else value(FirstD, SecondD) = -1 End If Next SecondD Next FirstD End Sub i have tryed message boxing parts out and it seems to just stop funning the code at If chosen(FirstD + X, SecondD + Y) = True Then number += 1. MS visual basic 2010 is not giving me any errors, can anyone help me out Quote
Leaders snarfblam Posted December 22, 2011 Leaders Posted December 22, 2011 I started reading the code and trying to figure out what was wrong when I realized, you don't explain what behavior you are expecting to see and what behavior you are actually seeing that is incorrect. Without comments, I had a tough time understanding how you're actually going about setting up the playfield. But 'll give it a whirl. Let's see how I do. The most obvious issue is that it looks like you aren't doing bounds checks. For FirstD As Integer = 0 To Xlength For SecondD As Integer = 0 To YLength ' ... For X As Integer = -1 To 1 For Y As Integer = -1 To 1 [color="Red"]If chosen(FirstD + X, SecondD + Y) = True Then number += 1[/color] Next Y Next X ' ... Next SecondD Next FirstD What happens on the highlighted line when, say, FirstD = 0. X loops from -1 to 1, so you access the array from (FirstD - 1) to (FirstD + 1). And what's the value of (FirstD - 1) when FirstD = 0? You need to make sure that your indecies are valid when accessing an array. The reason you probably aren't seeing an exception is because .NET has a known problem of swallowing exceptions (you never get an error) in the Form.Load event. I think this problem only affects the 64-bit version of the run-time, but I'm not sure. It would explain why the code stops running on the line you mentioned. I generally avoid running any code in the Load event because of this problem, unless I'm doing something Q&D. The constructor (Sub New) would probably be a good place to run this code, but VB tries to hide the constructor from you. (The constructor is probably in the form's .designer.vb file, hidden by default.) Did I do good? Quote [sIGPIC]e[/sIGPIC]
LamKat Posted December 23, 2011 Author Posted December 23, 2011 yh. i kinda understand what you are saying but can you think of any way to code it so that this problem is avoided without losing the changeability of the current non functioning code? Quote
Leaders snarfblam Posted December 23, 2011 Leaders Posted December 23, 2011 You need to make sure that your indecies are valid when accessing an array. You gotta at least try, man. Suppose FirstD = 0 and X = -1. (FirstD + X) evaluates to -1, which means you are accessing chosen(-1, SecondD + Y). chosen(-1, anything) does not exist. -1 is not a valid subscript for the first dimension. What would it mean to access chosen(-1, anything)? What do you think should happen in this case? Take a moment to think about it. The solution should naturally come from the answers to these questions, so really take a moment. Now that you've thought it over for a moment, you've probably realized that what it means to access chosen(-1, anything) is that you are trying to access outside the game field. So, what should happen in this case? Well, you know there are no mines outside the game field, so you should make a point of not checking locations outside the game field. How do we accomplish this? Start with a simple change in the code. Not strictly necessary, but it makes things easier. If chosen(FirstD + X, SecondD + Y) = True Then number += 1 ' Becomes [color="Red"]Dim gameFieldX As Integer = FirstD + X Dim gameFieldY As Integer = SecondD + Y[/color] If chosen([color="Red"]gameFieldX, gameFieldY[/color]) = True Then Number += 1 Now, we know that we don't want to check for mines in locations outside the game field. That means that first we need to figure out whether the location is within the game field or not. How? Well, if gameFieldX is less than zero, then we are looking beyond the left edge of the game field. If gameFieldX is greater that XLength, we are looking beyond the right edge of the game field. We can use the same logic to determine whether we are looking beyond the top or bottom of the game field. Here is some pseudo-code, with italics to indicate fill-in-the-blanks. Dim IsOutsideGameField = ([i]isBeyondLeftEdge[/i]) Or _ ([i]isBeyondRightEdge[/i]) Or _ ([i]isBeyondTopEdge[/i]) Or _ ([i]isBeyondBottomEdge[/i]) Once we know whether the location is outside the game field, we know whether or not to check that spot for mines. Put it all together: Dim gameFieldX As Integer = FirstD + X Dim gameFieldY As Integer = SecondD + Y Dim IsOutsideGameField = ([i]isBeyondLeftEdge[/i]) Or _ ([i]isBeyondRightEdge[/i]) Or _ ([i]isBeyondTopEdge[/i]) Or _ ([i]isBeyondBottomEdge[/i]) ' Are we inside the gamefield? If Not IsOutsideGameField Then ' If so, check for a mine! If chosen(gameFieldX, gameFieldY) = True Then Number += 1 End If Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.