FANDOM


El código de la clase Bacteria Editar

Option Explicit On

Public Class Bacteria

  Private bViva As Boolean
  Private bCreada As Boolean
  Private iColumna As Byte
  Private iFila As Byte

  Event Nacimiento( _
    ByVal Columna As Byte, _
    ByVal Fila As Byte _
  )

  Event Muerte( _
    ByVal Columna As Byte, _
    ByVal Fila As Byte _
  )

  Sub Crear( _
    ByVal Columna As Byte, _
    ByVal Fila As Byte _
  )
    iColumna = Columna
    iFila = Fila
    bCreada = True
  End Sub

  Function Nacer() As Boolean
    Nacer = False
    If Not bCreada Then Exit Function
    bViva = True
    Nacer = True
    RaiseEvent Nacimiento(iColumna, iFila)
  End Function

  Function Morir() As Boolean
    Morir = False
    If Not bCreada Then Exit Function
    bViva = False
    Morir = True
    RaiseEvent Muerte(iColumna, iFila)
  End Function

  ReadOnly Property Viva() As Boolean
    Get
      Viva = bViva
    End Get
  End Property

  ReadOnly Property Creada() As Boolean
    Get
      Creada = bCreada
    End Get
  End Property

  Property Columna() As Byte
    Get
      Columna = iColumna
    End Get
    Set(ByVal iQueColumna As Byte)
      iColumna = iQueColumna
    End Set
  End Property

  Property Fila() As Byte
    Get
      Fila = iFila
    End Get
    Set(ByVal iQueFila As Byte)
      iFila = iQueFila
    End Set
  End Property

  Public Sub New( _
    ByVal Columna As Byte, _
    ByVal Fila As Byte _
  )
    Crear(Columna, Fila)
  End Sub

End Class

El código de la clase Bacterias Editar

Option Explicit On

Public Class Bacterias

  Inherits System.Collections.CollectionBase

  Private aBacterias(,) As Bacteria
  Private iColumnas As Byte
  Private iFilas As Byte
  Private bCreadas As Boolean

  Event Nacimiento( _
    ByVal Columna As Long, _
    ByVal Fila As Long _
  )

  Event Muerte( _
    ByVal Columna As Long, _
    ByVal Fila As Long _
  )

  Public Sub New()

  End Sub

  Public Sub New( _
    ByVal Columna As Byte, _
    ByVal Fila As Byte _
  )
    Crear(Columna, Fila)
  End Sub

  Public Sub Crear( _
    ByVal Columnas As Long, _
    ByVal Filas As Long _
  )
    If bCreadas Then Destruir()

    iColumnas = Columnas
    iFilas = Filas
    ReDim aBacterias( _
      iColumnas + 2, _
      iFilas + 2 _
    )

    Dim i As Long
    Dim j As Long

    For j = 0 To iFilas + 1
      For i = 0 To iColumnas + 1
        aBacterias(i, j) = New Bacteria(i, j)
        If _
          i <> 0 And _
          j <> 0 And _
          i <> iColumnas + 1 And _
          j <> iFilas + 1 _
        Then
          List.Add(aBacterias(i, j))
        End If
      Next i
    Next j

    bCreadas = True

  End Sub

  Private Sub Destruir()

    Dim i As Long
    Dim j As Long

    For j = 0 To iFilas + 1
      For i = 0 To iColumnas + 1
        aBacterias(i, j) = Nothing
      Next i
    Next j

  End Sub

  ReadOnly Property Creadas() As Boolean
    Get
      Creadas = bCreadas
    End Get
  End Property

  ReadOnly Property Columnas() As Byte
    Get
      Columnas = iColumnas
    End Get
  End Property

  ReadOnly Property Filas() As Byte
    Get
      Filas = iFilas
    End Get
  End Property

  Default ReadOnly Property Bacteria( _
    ByVal Columna As Byte, _
    ByVal Fila As Byte _
  ) As Bacteria
    Get
      If _
        Columna < 1 Or _
        Columna > iColumnas Or _
        Fila < 1 Or _
        Fila > iFilas _
      Then
        Bacteria = Nothing
      Else
        Bacteria = aBacterias(Columna, Fila)
      End If
    End Get
  End Property

  Function Procrear() As Boolean

    Procrear = False
    If Not bCreadas Then Exit Function

    Dim abBacteriasNG(,) As Boolean
    ReDim abBacteriasNG( _
      iColumnas, _
      iFilas _
    )

    Dim i As Byte
    Dim j As Byte
    Dim iVecinas As Byte

    For j = 1 To iFilas
      For i = 1 To iColumnas

        iVecinas = 0

        If aBacterias(i - 1, j - 1).Viva Then
          iVecinas = iVecinas + 1
        End If
        If aBacterias(i - 0, j - 1).Viva Then
          iVecinas = iVecinas + 1
        End If
        If aBacterias(i + 1, j - 1).Viva Then
          iVecinas = iVecinas + 1
        End If

        If aBacterias(i - 1, j - 0).Viva Then
          iVecinas = iVecinas + 1
        End If
        If aBacterias(i + 1, j - 0).Viva Then
          iVecinas = iVecinas + 1
        End If

        If aBacterias(i - 1, j + 1).Viva Then
          iVecinas = iVecinas + 1
        End If
        If aBacterias(i - 0, j + 1).Viva Then
          iVecinas = iVecinas + 1
        End If
        If aBacterias(i + 1, j + 1).Viva Then
          iVecinas = iVecinas + 1
        End If

        Select Case iVecinas
          Case 2
            abBacteriasNG(i, j) = _
              aBacterias(i, j).Viva
          Case 3
            abBacteriasNG(i, j) = True
          Case Else
            abBacteriasNG(i, j) = False
        End Select
      Next i
    Next j

    For j = 1 To iFilas
      For i = 1 To iColumnas
        If abBacteriasNG(i, j) = True And _
          aBacterias(i, j).Viva = False _
        Then
          aBacterias(i, j).Nacer()
          RaiseEvent Nacimiento(i, j)
        ElseIf abBacteriasNG(i, j) = False _
          And aBacterias(i, j).Viva = True _
        Then
          aBacterias(i, j).Morir()
          RaiseEvent Muerte(i, j)
        End If
      Next i
    Next j

    Procrear = True
    Exit Function

  End Function

  Public ReadOnly Property Item( _
    ByVal index As Integer _
  ) As Bacteria
    Get
      Return CType(List.Item(index), Bacteria)
    End Get
  End Property

  Protected Overrides Sub Finalize()
    Destruir()
    MyBase.Finalize()
  End Sub

End Class

¡Interferencia de bloqueo de anuncios detectada!


Wikia es un sitio libre de uso que hace dinero de la publicidad. Contamos con una experiencia modificada para los visitantes que utilizan el bloqueo de anuncios

Wikia no es accesible si se han hecho aún más modificaciones. Si se quita el bloqueador de anuncios personalizado, la página cargará como se esperaba.