2010-01-26 86 views
0

Grid http://www.imagechicken.com/uploads/1264550987064245200.png網格的益智遊戲板塊移除算法

我具有由整數的一維數組表示的「samegame」網格。 0到63代表一個8x8網格。

規則是兩個或多個相同的彩色塊可以通過點擊刪除。塊然後從上面滑下來。如果列是空列,其他列將從兩側移入。

當有人點擊底部的綠色塊時,這些列應該從側面滑入。黑線左側的列向右滑動,黑線右側的列向左滑動。所以在這種情況下,只有兩個最右邊的列在移除綠色塊後從右邊滑到黑線。

我遇到的問題是在設計一個合適的算法去除後的崩潰。

我目前的做法是從左到右測試每列,看它是否爲空。如果它是空的,那麼我將任何列向左(如果在黑線左邊)或右邊(如果在黑線右邊)滑過空白列,並在直接從我滑動的位置重複此操作。

問題是,當兩列爲空時,此方法失敗,因爲第二個空列滑過第一列,然後例行程序從下一列開始循環,而不是將所有內容混洗在一起。

我想知道是否有比我採取的方法更簡單的方法?

Public Sub CollapseEmptyColumns() 

     For x = 0 + 1 To 7 - 1 
      Dim EmptyColumn As Boolean = True 
      For y = 0 To 7 

       Dim BlockIndex As Integer = y * 8 + x 
       If Blocks(BlockIndex).BlockColor <> eBlockColor.None Then 
        EmptyColumn = False 
        Exit For 
       End If 

      Next 

      If EmptyColumn = True Then 

       If x < 4 Then ' less than 4 then slide from left 

        SlideColumns(x - 1, 0, 1) 

       Else ' greater or equal to 4 slide from right 

        SlideColumns(x + 1, 7, -1) 

       End If 


      End If 

     Next 

    End Sub 

    Private Sub SlideColumns(ByVal First As Integer, ByVal Last As Integer, ByVal Direction As Integer) 

     For x = First To Last Step -Direction 

      For y = 0 To 7 

       Blocks(y * 8 + (x + Direction)).BlockColor = Blocks(y * 8 + x).BlockColor 
       Blocks(y * 8 + x).BlockColor = eBlockColor.None 

      Next 

     Next 

    End Sub 

回答

1

檢查前三欄從左到右,再從右檢查的最後三個欄到左:

Private Function EmptyColumn(x As Integer) As Boolean 
    For y As Integer = 0 To 7 
    If Blocks(y * 8 + x).BlockColor <> eBlockColor.None Then 
     Return False 
    End If 
    Next 
    Return True 
End Function 

Public Sub CollapseEmptyColumns() 
    For x = 1 To 3 
    If EmptyColumn(x) Then 
     SlideColumns(x - 1, 0, 1) 
    End If 
    Next 
    For x = 6 to 4 Step -1 
    If EmptyColumn(x) Then 
     SlideColumns(x + 1, 7, -1) 
    End If 
    Next 
End Sub 
+0

我不知道有兩個空列時,將解決這一問題? – PeanutPower 2010-01-26 23:54:06

+0

@Peanut:會的。問題在於您將第二個空列移動到已經檢查過的列中。通過檢查另一個方向的列,你不會將任何東西移動到已經檢查過的列中。 – Guffa 2010-01-27 00:00:33

+0

謝謝我會放棄它今天我的大腦不能很好地工作。乾杯 – PeanutPower 2010-01-27 00:09:37