Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Game of life
#8
Ok.   This is SECOND ITERATION !    Not sure I got the rules or algorithm right.

OK FIXED NOW !!!!

(Might be the Random number generator !!! ??? )

Code: (Select All)

Option _Explicit

Type GRIDTYPE
    WID As Long
    HEI As Long
    CellWidth As Long
    CellHeight As Long
    NumCells As Long
End Type

Type CellType
    IsAlive As Integer
    Row As Long
    Col As Long
    Parent As GRIDTYPE
End Type

Dim Shared MyScreen As Long

Dim Shared MYGRID As GRIDTYPE
Dim Shared NumberOfCells As Long
ReDim Shared TheCells1(0 To 1) As CellType
ReDim Shared TheCells2(0 To 1) As CellType

MYGRID.WID = 94
MYGRID.HEI = 68
MYGRID.CellWidth = 10
MYGRID.CellHeight = 10

_Title "Conways GAME OF LIFE "
InitGrid

Do
    UpdateGeneration TheCells1(), TheCells2()
    DRAWGRID TheCells2()
    _Delay .2
    UpdateGeneration TheCells2(), TheCells1()
    DRAWGRID TheCells1()
    _Delay .2
Loop Until InKey$ = Chr$(27)


Sleep
End




Sub UpdateGeneration (InCells() As CellType, OutCells() As CellType)
    Dim i As Long
    Dim CellPointer As Long
    Dim RowAbove As Long
    Dim RowBelow As Long
    Dim ColLeft As Long
    Dim ColRight As Long
    Dim NRow As Long
    Dim NCol As Long
    Dim Neighbors(0 To 7) As CellType

    CellPointer = 0
    For i = 0 To UBound(InCells)
        If InCells(i).Row = 0 Then
            RowAbove = InCells(i).Parent.HEI - 1
        Else
            RowAbove = InCells(i).Row - 1
        End If

        If InCells(i).Row = InCells(i).Parent.HEI - 1 Then
            RowBelow = 0
        Else
            RowBelow = InCells(i).Row + 1
        End If

        If InCells(i).Col = 0 Then
            ColLeft = InCells(i).Parent.WID - 1
        Else
            ColLeft = InCells(i).Col - 1
        End If

        If InCells(i).Col = InCells(i).Parent.WID - 1 Then
            ColRight = 0
        Else
            ColRight = InCells(i).Col + 1
        End If

        Neighbors(0) = InCells((RowAbove * MYGRID.WID) + ColLeft)
        Neighbors(1) = InCells((RowAbove * MYGRID.WID) + InCells(i).Col)
        Neighbors(2) = InCells((RowAbove * MYGRID.WID) + ColRight)

        Neighbors(3) = InCells((InCells(i).Row * MYGRID.WID) + ColLeft)
        Neighbors(4) = InCells((InCells(i).Row * MYGRID.WID) + ColRight)

        Neighbors(5) = InCells((RowBelow * MYGRID.WID) + ColLeft)
        Neighbors(6) = InCells((RowBelow * MYGRID.WID) + InCells(i).Col)
        Neighbors(7) = InCells((RowBelow * MYGRID.WID) + ColRight)
        OutCells(i).IsAlive = IsAlive(InCells(i), Neighbors())
    Next
End Sub

Function IsAlive (CellIn As CellType, CellNeighbors() As CellType)
    Dim LifeCount As Integer
    Dim i As Long
    LifeCount = 0
    For i = 0 To 7
        If CellNeighbors(i).IsAlive Then
            LifeCount = LifeCount + 1
        End If
    Next
    If CellIn.IsAlive And (LifeCount > 1 And LifeCount < 4) Then
        IsAlive = _TRUE
        Exit Function
    End If
    If CellIn.IsAlive And (LifeCount > 3) Then
        IsAlive = _FALSE
        Exit Function
    End If
    If Not (CellIn.IsAlive) And (LifeCount = 3) Then
        IsAlive = _TRUE
        Exit Function
    End If
    IsAlive = _FALSE
End Function

Sub InitGrid
    Dim I As Long
    Dim GRow As Long
    Dim GCol As Long
    Dim CellPointer As Long
    Randomize Timer
    CellPointer = 0
    NumberOfCells = MYGRID.WID * MYGRID.HEI
    ReDim TheCells1(0 To NumberOfCells - 1) As CellType
    ReDim TheCells2(0 To NumberOfCells - 1) As CellType


    For GRow = 0 To MYGRID.HEI - 1
        For GCol = 0 To MYGRID.WID - 1
            TheCells1(CellPointer).Row = GRow
            TheCells1(CellPointer).Col = GCol
            TheCells2(CellPointer).Row = GRow
            TheCells2(CellPointer).Col = GCol
            TheCells1(CellPointer).Parent = MYGRID
            TheCells2(CellPointer).Parent = MYGRID

            If (Int(Rnd * 100) + 1) < 25 Then
                TheCells1(CellPointer).IsAlive = _TRUE
            Else
                TheCells1(CellPointer).IsAlive = _FALSE
            End If
            CellPointer = CellPointer + 1
        Next
    Next
    MyScreen = _NewImage(MYGRID.WID * MYGRID.CellWidth, MYGRID.HEI * MYGRID.CellHeight, 32)
    Screen MyScreen
    For I = 0 To NumberOfCells - 1
        DrawCell TheCells1(I)
    Next
End Sub

Sub DRAWGRID (Cells() As CellType)
    Dim i As Long
    For i = 0 To UBound(Cells)
        DrawCell Cells(i)
    Next
    _Display
End Sub



Sub DrawCell (Cell As CellType)
    Dim CellColor As Long
    Dim X As Long
    Dim Y As Long
    Dim X2 As Long
    Dim Y2 As Long
    If Cell.IsAlive Then
        CellColor = _RGB32(22, 188, 28, 255)
    Else
        CellColor = _RGB32(50, 13, 23)
    End If
    X = (Cell.Col * Cell.Parent.CellWidth)
    Y = (Cell.Row * Cell.Parent.CellHeight)
    X2 = X + Cell.Parent.CellWidth
    Y2 = Y + Cell.Parent.CellHeight
    Line (X, Y)-(X2, Y2), _RGB32(165, 185, 185, 255), BF
    Line (X + 1, Y + 1)-(X2 - 1, Y2 - 1), CellColor, BF
End Sub



  I also need to set up USER selection of Grid  Not just random !

Reply


Messages In This Thread
Game of life - by Unseen Machine - 01-08-2026, 01:49 PM
RE: Game of life - by bplus - 01-08-2026, 04:43 PM
RE: Game of life - by Unseen Machine - 01-08-2026, 07:30 PM
RE: Game of life - by ahenry3068 - 01-08-2026, 05:45 PM
RE: Game of life - by bplus - 01-08-2026, 06:16 PM
RE: Game of life - by ahenry3068 - 01-08-2026, 06:20 PM
RE: Game of life - by bplus - 01-08-2026, 06:24 PM
RE: Game of life - by ahenry3068 - 01-08-2026, 06:36 PM
RE: Game of life - by ahenry3068 - 01-08-2026, 06:50 PM
RE: Game of life - by bplus - 01-08-2026, 07:32 PM
RE: Game of life - by ahenry3068 - 01-08-2026, 07:43 PM
RE: Game of life - by MasterGy - 01-08-2026, 07:51 PM
RE: Game of life - by bplus - 01-08-2026, 08:46 PM
RE: Game of life - by bplus - 01-08-2026, 09:12 PM
RE: Game of life - by ahenry3068 - 01-08-2026, 11:26 PM
RE: Game of life - by ahenry3068 - 01-09-2026, 03:45 PM
RE: Game of life - by bplus - 01-09-2026, 01:14 AM
RE: Game of life - by bplus - 01-09-2026, 01:27 AM
RE: Game of life - by ahenry3068 - 01-09-2026, 02:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Game of Life again but by way of Parallelism bplus 2 927 02-25-2024, 07:46 AM
Last Post: Pete
  3D rendering of Game of Life by ubi44 bplus 3 1,285 02-16-2024, 02:50 AM
Last Post: bplus
  Pixel life James D Jarvis 9 2,019 10-17-2023, 12:39 AM
Last Post: James D Jarvis
  Life Experiments bplus 3 1,138 08-18-2022, 09:10 PM
Last Post: James D Jarvis
  Bad Life James D Jarvis 2 930 08-16-2022, 05:58 PM
Last Post: James D Jarvis

Forum Jump:


Users browsing this thread: 1 Guest(s)