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

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.

主站蜘蛛池模板: 昌江| 师宗县| 凤山县| 南平市| 香河县| 龙里县| 雷州市| 丘北县| 盐城市| 梁平县| 望奎县| 高州市| 长春市| 原阳县| 湖南省| 常山县| 镇江市| 平武县| 扎囊县| 托里县| 鹤岗市| 闽清县| 满洲里市| 东城区| 六枝特区| 乳山市| 古田县| 平阴县| 柞水县| 台州市| 新沂市| 攀枝花市| 和顺县| 龙山县| 仙游县| 奉节县| 大宁县| 沅江市| 武宣县| 大港区| 福安市|