' -------------------------------------------------------------------- ' SWIMMING ON THE SURFACE OF A DONUT ' -------------------------------------------------------------------- Private Function northOf(ByVal p As Point) As Point Dim y As Integer = p.y - 1 If (y < 1) Then y = myOcean.Height End If Return New Point(p.x, y) End Function Private Function southOf(ByVal p As Point) As Point Dim y As Integer = p.y + 1 If (y > myOcean.Height) Then y = 1 End If Return New Point(p.x, y) End Function Private Function westOf(ByVal p As Point) As Point Dim x As Integer = p.x - 1 If (x < 1) Then x = myOcean.Width End If Return New Point(x, p.y) End Function Private Function eastOf(ByVal p As Point) As Point Dim x As Integer = p.x + 1 If (x > myOcean.Width) Then x = 1 End If Return New Point(x, p.y) End Function ' -------------------------------------------------------------------- ' FISH AND SHARK MOVEMENT ' -------------------------------------------------------------------- Private Sub MoveFish(ByVal newOcean As Ocean, ByVal f As Fish) ' Pick a random direction for the fish to move to Dim n As Integer = Rnd.Next(1, 5) Dim p As Point Select Case n Case 1 p = northOf(f.location) Case 2 p = eastOf(f.location) Case 3 p = southOf(f.location) Case 4 p = westOf(f.location) End Select ' Check to see if the new location is open water If (newOcean.myCell(p.x, p.y).GetType.Equals(GetType(Water))) Then ' Breed a new fish at this location and swim to the new one newOcean.myCell(f.location.x, f.location.y) = New Fish(f.location) f.location = p newOcean.myCell(p.x, p.y) = f Else ' Leave the fish where it is newOcean.myCell(f.location.x, f.location.y) = f End If End Sub Private Function HuntShark(ByVal newOcean As Ocean, ByVal s As Shark, ByVal p As Point) Dim ate As Boolean = False ' Check to see if there is a fish to eat at location p If (newOcean.myCell(p.x, p.y).GetType.Equals(GetType(Fish))) Then ' Breed a new shark at its current location and eat the fish newOcean.myCell(s.location.x, s.location.y) = New Shark(s.location) s.location = p newOcean.myCell(p.x, p.y) = s ate = True End If Return ate End Function Private Sub MoveShark(ByVal newOcean As Ocean, ByVal s As Shark) ' Hunt in a clockwise direction for fish to eat Dim ate As Boolean = HuntShark(newOcean, s, northOf(s.location)) If Not ate Then ate = HuntShark(newOcean, s, eastOf(s.location)) End If If Not ate Then ate = HuntShark(newOcean, s, southOf(s.location)) End If If Not ate Then ate = HuntShark(newOcean, s, westOf(s.location)) End If End Sub