01-08-2026, 06:50 PM
(This post was last modified: 01-08-2026, 07:16 PM by ahenry3068.)
Ok. This is SECOND ITERATION ! Not sure I got the rules or algorithm right.
OK FIXED NOW !!!!
(Might be the Random number generator !!! ??? )
I also need to set up USER selection of Grid Not just random !
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 !

