官术网_书友最值得收藏!

Time for action – making the connection

  1. Add the PropagateWater() method to the GameBoard class:
    Public Sub PropagateWater(x As Integer, y As Integer, fromDirection As String)
    
      If (y >= 0) And (y <= GameBoardHeight) And
        (x >= 0) And (x <= GameBoardWidth) Then
        If boardSquares(x, y).HasConnector(fromDirection) And
          Not (boardSquares(x, y).PieceSuffix.Contains("W")) Then
          FillPiece(x, y)
          waterTracker.Add(New Vector2(x, y))
          For Each pipeEnd As String In 
            boardSquares(x, y).GetOtherEnds(fromDirection)
            Select Case pipeEnd
              Case "Left"
                PropagateWater(x - 1, y, "Right")
              Case "Right"
                PropagateWater(x + 1, y, "Left")
              Case "Top"
                PropagateWater(x, y - 1, "Bottom")
              Case "Bottom"
                PropagateWater(x, y + 1, "Top")
            End Select
          Next
        End If
      End If
    End Sub
  2. Add the GetWaterChain() method to the GameBoard class:
    Public Function GetWaterChain(y As Integer) As List(Of Vector2)
      waterTracker.Clear()
      PropagateWater(0, y, "Left")
      Return waterTracker
    End Function

What just happened?

Together, GetWaterChain() and PropagateWater() are the keys to the entire Flood Control game, so understanding how they work is vital. When the game code wants to know if the player has completed a scoring row, it will call the GetWaterChain() method once for each row on the game board:

What just happened?

The WaterTracker list is cleared and GetWaterChain() calls PropagateWater() for the first square in the row, indicating that the water is coming from the Left direction.

The PropagateWater() method checks to make sure that the x and y coordinates passed to it exist within the board and, if they do, it checks to see if the piece at that location has a connector matching the fromDirection parameter and that the piece is not already filled with water. If all of these conditions are met, that piece gets filled with water and added to the WaterTracker list.

Finally, PropagateWater() gets a list of all other directions that the piece contains (in other words, all directions the piece contains that do not match fromDirection). For each of these directions PropagateWater() recursively calls itself, passing in the new x and y location as well as the direction the water is coming from.

主站蜘蛛池模板: 周口市| 河津市| 高碑店市| 岢岚县| 濮阳县| 班玛县| 临城县| 宁阳县| 绵阳市| 扶风县| 景德镇市| 黑山县| 抚宁县| 礼泉县| 新化县| 壶关县| 驻马店市| 星座| 贺州市| 深泽县| 莒南县| 东兰县| 天柱县| 峨边| 通道| 潜江市| 翼城县| 尼勒克县| 西林县| 诸城市| 四川省| 怀远县| 台安县| 当阳市| 胶州市| 柯坪县| 冕宁县| 赤水市| 巩义市| 开鲁县| 平凉市|