Question

Sub left()  

 Dim m As Integer
 Dim n As Integer
 For m = 0 To 3
  For n = 0 To 3
    If j < 3 Then
      GoTo end
    End If
    If board(m + i - 1, n + j) = 1 Then
      If board(i + m - 1, j + n - 1) = 2 Then
        GoTo end
      End If
    End If
  Next n
 Next m
 j = j - 1
 end:
 End Sub

so im trying to make tetris game on excel and my "moveleft" function doesnt work correctly, when i press left 2 or 3 times very quickly it skips the checking if there is a cell with value of 2(full cell) and goes through full cells. any suggestions?

Was it helpful?

Solution

First, I'd recommend probing the code to see where your jumping out of the command. For example, debug.print "j<3" inside your first if command will tell you if that's where the code is exiting from. Put a different line in multiple spots and watch your immediate window to see what is being processed. You might want to print out your global variables as well to make sure they are what is intended.

Second, don't use end as the goto point since end is a keyword in VBA. I've changed it to endleft

If the speed is an issue then the following two points might help:

Third, since j doesn't change inside the for loops, pull it outside. This is one less eval your doing inside every loop.

Fourth, put your other two if's together into one and use the logical AND. Now your doing one less eval on every loop when the first condition is false.


Sub left()
  Dim m As Integer
  Dim n As Integer
  If Not (j < 3) Then
    For m = 0 To 3
      For n = 0 To 3
        If ((board(m + i - 1, n + j) = 1) And (board(i + m - 1, j + n - 1) = 2)) Then
          GoTo endleft
        End If
      Next n
    Next m
  j = j - 1
  End If
endleft:
End Sub

Fifth, cool project. Will you share it when done?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top