home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / Game_Of_Li210822412008.psc / fGameOfLife.frm < prev    next >
Text File  |  2008-04-01  |  28KB  |  941 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form fGameOfLife 
  4.    BackColor       =   &H00E0E0E0&
  5.    BorderStyle     =   1  'Fest Einfach
  6.    ClientHeight    =   10065
  7.    ClientLeft      =   45
  8.    ClientTop       =   735
  9.    ClientWidth     =   14025
  10.    Icon            =   "fGameOfLife.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    ScaleHeight     =   10065
  14.    ScaleWidth      =   14025
  15.    StartUpPosition =   2  'Bildschirmmitte
  16.    Begin VB.CommandButton btRestart 
  17.       Caption         =   "Restart"
  18.       Height          =   315
  19.       Left            =   7530
  20.       TabIndex        =   13
  21.       Top             =   9525
  22.       Width           =   840
  23.    End
  24.    Begin VB.CommandButton btInfo 
  25.       Caption         =   "?"
  26.       BeginProperty Font 
  27.          Name            =   "MS Sans Serif"
  28.          Size            =   8.25
  29.          Charset         =   0
  30.          Weight          =   700
  31.          Underline       =   0   'False
  32.          Italic          =   0   'False
  33.          Strikethrough   =   0   'False
  34.       EndProperty
  35.       Height          =   315
  36.       Left            =   8640
  37.       TabIndex        =   12
  38.       Top             =   9525
  39.       Width           =   330
  40.    End
  41.    Begin VB.CommandButton btNext 
  42.       Caption         =   "Step"
  43.       Enabled         =   0   'False
  44.       Height          =   315
  45.       Left            =   6570
  46.       TabIndex        =   11
  47.       Top             =   9525
  48.       Width           =   840
  49.    End
  50.    Begin VB.CommandButton btPause 
  51.       Caption         =   "Pause"
  52.       Height          =   315
  53.       Left            =   5610
  54.       TabIndex        =   10
  55.       Top             =   9525
  56.       Width           =   840
  57.    End
  58.    Begin MSComDlg.CommonDialog CDl 
  59.       Left            =   9870
  60.       Top             =   9420
  61.       _ExtentX        =   847
  62.       _ExtentY        =   847
  63.       _Version        =   393216
  64.       CancelError     =   -1  'True
  65.    End
  66.    Begin VB.HScrollBar scrDelay 
  67.       Height          =   225
  68.       LargeChange     =   5
  69.       Left            =   1995
  70.       Max             =   0
  71.       Min             =   50
  72.       TabIndex        =   8
  73.       Top             =   9435
  74.       Value           =   25
  75.       Width           =   2595
  76.    End
  77.    Begin VB.PictureBox picBuffer 
  78.       AutoRedraw      =   -1  'True
  79.       BackColor       =   &H00000000&
  80.       FillStyle       =   0  'Ausgefⁿllt
  81.       Height          =   9060
  82.       Left            =   225
  83.       ScaleHeight     =   600
  84.       ScaleMode       =   3  'Pixel
  85.       ScaleWidth      =   900
  86.       TabIndex        =   1
  87.       Top             =   240
  88.       Visible         =   0   'False
  89.       Width           =   13560
  90.    End
  91.    Begin VB.PictureBox picView 
  92.       AutoRedraw      =   -1  'True
  93.       BackColor       =   &H00000000&
  94.       FillStyle       =   0  'Ausgefⁿllt
  95.       Height          =   9060
  96.       Left            =   225
  97.       OLEDropMode     =   1  'Manuell
  98.       ScaleHeight     =   600
  99.       ScaleMode       =   3  'Pixel
  100.       ScaleWidth      =   900
  101.       TabIndex        =   0
  102.       Top             =   240
  103.       Width           =   13560
  104.    End
  105.    Begin VB.Label lbCpS 
  106.       Alignment       =   1  'Rechts
  107.       BackStyle       =   0  'Transparent
  108.       Caption         =   "0"
  109.       Height          =   195
  110.       Left            =   3015
  111.       TabIndex        =   17
  112.       Top             =   9765
  113.       Width           =   1260
  114.    End
  115.    Begin VB.Label lb 
  116.       BackStyle       =   0  'Transparent
  117.       Caption         =   "CpS"
  118.       Height          =   195
  119.       Index           =   5
  120.       Left            =   4335
  121.       TabIndex        =   16
  122.       Top             =   9765
  123.       Width           =   285
  124.    End
  125.    Begin VB.Label lb 
  126.       BackStyle       =   0  'Transparent
  127.       Caption         =   "FpS"
  128.       Height          =   195
  129.       Index           =   4
  130.       Left            =   2340
  131.       TabIndex        =   15
  132.       Top             =   9765
  133.       Width           =   285
  134.    End
  135.    Begin VB.Label lbFPS 
  136.       Alignment       =   1  'Rechts
  137.       BackStyle       =   0  'Transparent
  138.       Caption         =   "0"
  139.       Height          =   195
  140.       Left            =   2010
  141.       TabIndex        =   14
  142.       Top             =   9765
  143.       Width           =   270
  144.    End
  145.    Begin VB.Image img 
  146.       Height          =   630
  147.       Left            =   195
  148.       Picture         =   "fGameOfLife.frx":08CA
  149.       Top             =   9345
  150.       Width           =   675
  151.    End
  152.    Begin VB.Label lb 
  153.       AutoSize        =   -1  'True
  154.       BackStyle       =   0  'Transparent
  155.       Caption         =   "Speed"
  156.       Height          =   195
  157.       Index           =   3
  158.       Left            =   1335
  159.       TabIndex        =   9
  160.       Top             =   9630
  161.       Width           =   465
  162.    End
  163.    Begin VB.Label lb 
  164.       BackStyle       =   0  'Transparent
  165.       Caption         =   "Generation: "
  166.       Height          =   195
  167.       Index           =   2
  168.       Left            =   11445
  169.       TabIndex        =   7
  170.       Top             =   9345
  171.       Width           =   870
  172.    End
  173.    Begin VB.Label lbGen 
  174.       Alignment       =   1  'Rechts
  175.       BackStyle       =   0  'Transparent
  176.       Height          =   195
  177.       Left            =   13095
  178.       TabIndex        =   6
  179.       Top             =   9345
  180.       Width           =   675
  181.    End
  182.    Begin VB.Label lbAvgAge 
  183.       Alignment       =   1  'Rechts
  184.       BackStyle       =   0  'Transparent
  185.       Height          =   195
  186.       Left            =   13095
  187.       TabIndex        =   5
  188.       Top             =   9795
  189.       Width           =   675
  190.    End
  191.    Begin VB.Label lbActive 
  192.       Alignment       =   1  'Rechts
  193.       BackStyle       =   0  'Transparent
  194.       Height          =   195
  195.       Left            =   13095
  196.       TabIndex        =   4
  197.       Top             =   9570
  198.       Width           =   675
  199.    End
  200.    Begin VB.Label lb 
  201.       AutoSize        =   -1  'True
  202.       BackStyle       =   0  'Transparent
  203.       Caption         =   "Average Age: "
  204.       Height          =   195
  205.       Index           =   1
  206.       Left            =   11445
  207.       TabIndex        =   3
  208.       Top             =   9795
  209.       Width           =   1020
  210.    End
  211.    Begin VB.Label lb 
  212.       BackStyle       =   0  'Transparent
  213.       Caption         =   "Number of living cells: "
  214.       Height          =   195
  215.       Index           =   0
  216.       Left            =   11445
  217.       TabIndex        =   2
  218.       Top             =   9570
  219.       Width           =   1590
  220.    End
  221.    Begin VB.Menu mnuFile 
  222.       Caption         =   "File"
  223.       Begin VB.Menu mnuLoad 
  224.          Caption         =   "Load Pattern from File..."
  225.       End
  226.       Begin VB.Menu mnuRandom 
  227.          Caption         =   "Load Random Pattern"
  228.       End
  229.       Begin VB.Menu sep1 
  230.          Caption         =   "-"
  231.       End
  232.       Begin VB.Menu mnuExit 
  233.          Caption         =   "Exit"
  234.       End
  235.    End
  236.    Begin VB.Menu mnuColors 
  237.       Caption         =   "Colors"
  238.       Begin VB.Menu mnuLiveColor 
  239.          Caption         =   "Live"
  240.       End
  241.       Begin VB.Menu mnuDeadColor 
  242.          Caption         =   "Dead"
  243.       End
  244.       Begin VB.Menu mnuBackColor 
  245.          Caption         =   "Background"
  246.       End
  247.       Begin VB.Menu sep2 
  248.          Caption         =   "-"
  249.       End
  250.       Begin VB.Menu mnuResetColor 
  251.          Caption         =   "Reset"
  252.       End
  253.    End
  254.    Begin VB.Menu mnuUniverse 
  255.       Caption         =   "Universe"
  256.       Begin VB.Menu mnuSize 
  257.          Caption         =   "90 x 60"
  258.          Index           =   0
  259.       End
  260.       Begin VB.Menu mnuSize 
  261.          Caption         =   "150 x 100"
  262.          Index           =   1
  263.       End
  264.       Begin VB.Menu mnuSize 
  265.          Caption         =   "180 x 120"
  266.          Index           =   2
  267.       End
  268.       Begin VB.Menu mnuSize 
  269.          Caption         =   "300 x 200"
  270.          Index           =   3
  271.       End
  272.       Begin VB.Menu mnuSize 
  273.          Caption         =   "450 x 300"
  274.          Index           =   4
  275.       End
  276.    End
  277. End
  278. Attribute VB_Name = "fGameOfLife"
  279. Attribute VB_GlobalNameSpace = False
  280. Attribute VB_Creatable = False
  281. Attribute VB_PredeclaredId = True
  282. Attribute VB_Exposed = False
  283. Option Explicit
  284.  
  285. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  286. Private Declare Function GetTickCount Lib "kernel32" () As Long
  287. Private Declare Sub InitCommonControls Lib "comctl32" ()
  288. Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
  289. Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
  290. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  291.  
  292. Private Type tCell
  293.     Age(0 To 1)         As Long     'age of cell: this and next generation (alternates)
  294.     Neighbors(0 To 1)   As Long     'number of living neighbors: this and next generation (alternates)
  295.     TopNeighbor         As Long     'index to neighbor cell above
  296.     LeftNeighbor        As Long     'index to neighbor cell left
  297.     RightNeighbor       As Long     'index to neighbor cell right
  298.     BottomNeighbor      As Long     'index to neighbor cell below
  299.     X                   As Long     'top left corner of cell on screen
  300.     Y                   As Long
  301. End Type
  302.  
  303. Private Cells()         As tCell    'array of cells
  304.  
  305. Private CellsAcross     As Long
  306. Private CellsDown       As Long
  307. Private NumCells        As Long     'number of cells
  308. Private CellSize        As Long     'size of cell on screen
  309. Private Generation      As Long     'generation counter
  310. Private LiveColor       As Long
  311. Private DeadColor       As Long
  312. Private Seed            As Long     'rnd seed
  313. Private PrevTick        As Long     'for FpS
  314. Private FPSCount        As Long
  315. Private PerfFrq         As Currency
  316. Private PerfCnt1        As Currency
  317. Private PerfCnt2        As Currency
  318. Private Paused          As Boolean
  319. Private Desc            As String   'pattern description
  320.  
  321. Private Const TooBig As String = "Pattern is too big for this universe."
  322.  
  323. Private Sub Activate()
  324.  
  325.     If Paused Then
  326.         btPause_Click
  327.     End If
  328.     DoEvents
  329.     Sleep 600
  330.  
  331. End Sub
  332.  
  333. Private Sub btInfo_Click()
  334.  
  335.     If Desc = vbNullString Then
  336.         Desc = "No Info available."
  337.     End If
  338.     MsgBox Desc, , Caption
  339.  
  340. End Sub
  341.  
  342. Private Sub btNext_Click()
  343.  
  344.     CreateNextGeneration
  345.  
  346. End Sub
  347.  
  348. Private Sub btPause_Click()
  349.  
  350.   'pause/continue execution
  351.  
  352.     Paused = Not Paused
  353.     btNext.Enabled = Paused
  354.     btPause.Caption = IIf(Paused, "Continue", "Pause")
  355.  
  356. End Sub
  357.  
  358. Private Sub btRestart_Click()
  359.  
  360.     Restart
  361.  
  362. End Sub
  363.  
  364. Private Sub CreateNextGeneration()
  365.  
  366.   'creates and displays the next generation
  367.  
  368.   Dim i             As Long
  369.   Dim CurrGen       As Long
  370.   Dim NextGen       As Long
  371.   Dim Active        As Long
  372.   Dim TotalAge      As Long
  373.   Dim Color         As Long
  374.  
  375.     QueryPerformanceCounter PerfCnt1 'for cell timing
  376.     Generation = Generation + 1
  377.     NextGen = Generation And 1
  378.     CurrGen = 1 - NextGen
  379.  
  380.     For i = 0 To NumCells - 1
  381.         Cells(i).Neighbors(NextGen) = 0 'reset neighbors for next gen
  382.     Next i
  383.  
  384.     For i = 0 To NumCells - 1
  385.  
  386.         With Cells(i)
  387.  
  388.             .Neighbors(NextGen) = .Neighbors(NextGen) + .Neighbors(CurrGen) 'neighbors for next gen
  389.  
  390.             'live and let die
  391.             If (.Age(CurrGen) And .Neighbors(CurrGen) = 2) Or .Neighbors(CurrGen) = 3 Then 'still alive or just born
  392.                 .Age(NextGen) = .Age(CurrGen) + 1
  393.                 If .Age(NextGen) = 1 Then 'just born
  394.                     UpdateVicinity i, NextGen, 1 'update neighbors
  395.                     picBuffer.Line (.X, .Y)-(.X + CellSize, .Y + CellSize), LiveColor, BF 'draw live cell
  396.                 End If
  397.                 Active = Active + 1
  398.                 TotalAge = TotalAge + .Age(NextGen) - 1
  399.               Else 'NOT (.AGE(CURRGEN)...
  400.                 .Age(NextGen) = 0
  401.                 If .Age(CurrGen) Then 'just died
  402.                     UpdateVicinity i, NextGen, -1 'update neighbors
  403.                     picBuffer.Line (.X, .Y)-(.X + CellSize, .Y + CellSize), DeadColor, BF 'draw dead cell
  404.                 End If
  405.             End If
  406.  
  407.         End With 'CELLS(I)
  408.  
  409.     Next i
  410.  
  411.     'present backbuffer
  412.     With picView
  413.         BitBlt .hDC, 0, 0, .ScaleWidth, .ScaleHeight, picBuffer.hDC, 0, 0, vbSrcCopy
  414.         .Refresh
  415.     End With 'PICVIEW
  416.     FPSCount = FPSCount + 1 'frame counter
  417.     QueryPerformanceCounter PerfCnt2 'for cell timing
  418.  
  419.     'display stats
  420.     lbGen = Generation
  421.     lbActive = Active
  422.     If Active Then
  423.         lbAvgAge = Format$(Round(TotalAge / Active, 2), "#0.00")
  424.       Else 'ACTIVE = FALSE/0
  425.         lbAvgAge = "all dead"
  426.         FPSCount = 0
  427.         Generation = Generation - 1
  428.     End If
  429.  
  430. End Sub
  431.  
  432. Private Sub CreateRandom()
  433.  
  434.   Dim i As Long
  435.  
  436.     Rnd -Seed
  437.     CreateUniverse
  438.     Caption = App.ProductName & " [Random]"
  439.     For i = 0 To NumCells - 1
  440.         Cells(i).Age(0) = Rnd
  441.         UpdateVicinity i, 0, Cells(i).Age(0)
  442.     Next i
  443.     Desc = "Random Pattern"
  444.     DisplayCurrent
  445.     Activate
  446.  
  447. End Sub
  448.  
  449. Private Sub CreateUniverse()
  450.  
  451.   'creates the cells
  452.  
  453.   Dim i     As Long
  454.  
  455.     picBuffer.Cls
  456.     picView.Cls
  457.     Generation = 0
  458.     NumCells = CellsAcross * CellsDown
  459.     ReDim Cells(0 To NumCells - 1)
  460.     CellSize = picView.ScaleWidth \ CellsAcross
  461.  
  462.     For i = 0 To NumCells - 1
  463.  
  464.         'positions and Neighbors
  465.         With Cells(i)
  466.             .X = (i Mod CellsAcross) * CellSize
  467.             .Y = (i \ CellsAcross) * CellSize
  468.             'Neighbors wrap around horizontally and vertically
  469.             .LeftNeighbor = (i \ CellsAcross) * CellsAcross + ((i - 1 + CellsAcross) Mod CellsAcross)
  470.             .RightNeighbor = (i \ CellsAcross) * CellsAcross + ((i + 1) Mod CellsAcross)
  471.             .TopNeighbor = (i + NumCells - CellsAcross) Mod NumCells
  472.             .BottomNeighbor = (i + NumCells + CellsAcross) Mod NumCells
  473.         End With 'CELLS(I)
  474.  
  475.     Next i
  476.  
  477.     CellSize = CellSize - 2 'so that a little background remains between drawn cells
  478.     Desc = vbNullString
  479.  
  480. End Sub
  481.  
  482. Private Sub DisplayCurrent()
  483.  
  484.   Dim i     As Long
  485.  
  486.     For i = 0 To NumCells - 1
  487.         With Cells(i)
  488.             If .Age(Generation And 1) Then
  489.                 picView.Line (.X, .Y)-(.X + CellSize, .Y + CellSize), LiveColor, BF
  490.                 picBuffer.Line (.X, .Y)-(.X + CellSize, .Y + CellSize), LiveColor, BF
  491.             End If
  492.         End With 'CELLS(I)
  493.     Next i
  494.     lbGen = Generation
  495.     lbActive = vbNullString
  496.     lbAvgAge = vbNullString
  497.  
  498. End Sub
  499.  
  500. Private Sub Form_Initialize()
  501.  
  502.     InitCommonControls
  503.  
  504. End Sub
  505.  
  506. Private Sub Form_Load()
  507.  
  508.   Dim CurrTick As Long
  509.  
  510.     Caption = App.ProductName
  511.     QueryPerformanceFrequency PerfFrq
  512.     Randomize Timer
  513.     mnuResetColor_Click
  514.  
  515.     Show
  516.     DoEvents
  517.     mnuSize_Click 2
  518.  
  519.     'life cycle
  520.     Do
  521.  
  522.         If Not Paused Then
  523.  
  524.             'wait a little - then breed next generation
  525.             CurrTick = scrDelay
  526.             If CurrTick Then
  527.                 Sleep CurrTick * 10
  528.             End If
  529.  
  530.             CreateNextGeneration
  531.  
  532.         End If
  533.  
  534.         'timing frames and cells per second
  535.         CurrTick = GetTickCount
  536.         If CurrTick >= PrevTick Then
  537.             PrevTick = CurrTick + 1000
  538.             lbFPS = FPSCount
  539.             If FPSCount Then
  540.                 If PerfCnt1 < PerfCnt2 Then
  541.                     lbCpS = Format$(PerfFrq * NumCells / (PerfCnt2 - PerfCnt1), "#,0")
  542.                 End If
  543.               Else 'FPSCOUNT = FALSE/0
  544.                 lbCpS = 0
  545.             End If
  546.             FPSCount = 0
  547.         End If
  548.  
  549.     Loop While DoEvents 'until form ist gone
  550.  
  551. End Sub
  552.  
  553. Private Sub GliderIni()
  554.  
  555.   'ini with a glider
  556.  
  557.   Dim i As Long
  558.   Dim j As Long
  559.  
  560.     i = (NumCells + CellsAcross) / 2
  561.     For j = 0 To 1
  562.         Cells(i).Age(j) = 1
  563.         UpdateVicinity i, j, 1
  564.  
  565.         Cells(i + CellsAcross + 1).Age(j) = 1
  566.         UpdateVicinity i + CellsAcross + 1, j, 1
  567.  
  568.         Cells(i + 2 * CellsAcross - 1).Age(j) = 1
  569.         UpdateVicinity i + 2 * CellsAcross - 1, j, 1
  570.  
  571.         Cells(i + 2 * CellsAcross).Age(j) = 1
  572.         UpdateVicinity i + 2 * CellsAcross, j, 1
  573.  
  574.         Cells(i + 2 * CellsAcross + 1).Age(j) = 1
  575.         UpdateVicinity i + 2 * CellsAcross + 1, j, 1
  576.     Next j
  577.     scrDelay = 25
  578.     Desc = "Glider"
  579.     DisplayCurrent
  580.  
  581. End Sub
  582.  
  583. Private Sub LoadArith(Filename As String)
  584.  
  585.   'loads an initial pattern
  586.  
  587.   'file format is as follows:
  588.  
  589.   'blank lines are ignored
  590.  
  591.   'lines starting with a semicolon or an apostophe are remarks and will be ignored
  592.   'lines starting with a hash mark or a quote are descriptive text
  593.   'lines starting with s= define the speed 1 - 50; illegal value are ignored
  594.   'all other lines define the x y coordinates of a pattern cell
  595.  
  596.   'lines addressing positions outside the canvas are ignored
  597.  
  598.   Dim hFile     As Long
  599.   Dim Line      As String
  600.   Dim i         As Long
  601.   Dim Pos       As Long
  602.   Dim NoFit     As Boolean
  603.  
  604.     CreateUniverse
  605.     scrDelay = 1
  606.     hFile = FreeFile
  607.     Open Filename For Input As hFile
  608.     Do Until EOF(hFile)
  609.         Line Input #hFile, Line
  610.         If Len(Line) Then
  611.             Select Case Left$(Line, 1)
  612.               Case ";", "'"
  613.                 'do nothing
  614.               Case "#", """"
  615.                 Desc = Desc & Mid$(Line, 2) & vbCrLf
  616.               Case "s"
  617.                 On Error Resume Next
  618.                     scrDelay = 51 - Val(Mid$(Line, 3)) 'speed
  619.                 On Error GoTo 0
  620.               Case Else
  621.                 i = InStr(Line, " ")
  622.                 Pos = Val(Left$(Line, i - 1)) + CellsAcross / 2 + (Val(Mid$(Line, i)) + CellsDown / 2) * CellsAcross
  623.                 If Pos >= 0 And Pos < NumCells Then
  624.                     Cells(Pos).Age(0) = 1
  625.                     UpdateVicinity Pos, 0, 1
  626.                   Else 'NOT XPOS... 'NOT POS...
  627.                     NoFit = True
  628.                 End If
  629.             End Select
  630.         End If
  631.     Loop
  632.     Close hFile
  633.     If NoFit Then
  634.         MsgBox TooBig, vbExclamation, Caption
  635.     End If
  636.     DisplayCurrent
  637.  
  638. End Sub
  639.  
  640. Private Sub LoadPattern(Filename As String)
  641.  
  642.   'loads an initial pattern
  643.  
  644.   'file format is as follows:
  645.  
  646.   'first:
  647.   'all lines are space suppressed
  648.  
  649.   'then:
  650.   'blank lines are ignored
  651.   'lines starting with a semicolon or an apostophe are remarks and will be ignored
  652.   'lines starting with a hash mark or a quote are descriptive text
  653.   'lines starting with x= define a new horizontal position, 1-based
  654.   'lines starting with y= define a new  vertical  position, 1-based
  655.   'line  starting with h= defines the width  of the pattern
  656.   'line  starting with w= defines the height of the pattern
  657.   'line  starting with s= defines the speed 1 - 50; illegal value are ignored
  658.   'all other lines define a pattern line where the characters o, *, + or 1 define a living cell
  659.  
  660.   'lines addressing positions outside the canvas are ignored
  661.  
  662.   'example:
  663.  
  664.   '       ;gliders
  665.   '       ;will place two gliders in positions (100 : 30) and (100 : 60)
  666.  
  667.   '       x = 100
  668.   '       y = 30
  669.  
  670.   '       .o
  671.   '       ..o
  672.   '       ooo
  673.  
  674.   '       y = 60
  675.  
  676.   '       .o
  677.   '       o
  678.   '       ooo
  679.  
  680.   Dim hFile     As Long
  681.   Dim LineRead  As String
  682.   Dim Line      As String
  683.   Dim i         As Long
  684.   Dim xPos      As Long
  685.   Dim yPos      As Long
  686.   Dim NoFit     As Boolean
  687.  
  688.     CreateUniverse
  689.     scrDelay = 1
  690.     hFile = FreeFile
  691.     Open Filename For Input As hFile
  692.     Do Until EOF(hFile)
  693.         Line Input #hFile, LineRead
  694.         Line = LCase$(Replace$(LineRead, " ", ""))
  695.         If Len(Line) Then
  696.             Select Case Left$(Line, 1)
  697.               Case ";", "'"
  698.                 'do nothing
  699.               Case "#", """"
  700.                 Desc = Desc & Mid$(LineRead, 2) & vbCrLf
  701.               Case "h"
  702.                 yPos = ((CellsDown - Val(Mid$(Line, 3))) \ 2) * CellsAcross
  703.               Case "w"
  704.                 xPos = (CellsAcross - Val(Mid$(Line, 3))) \ 2
  705.               Case "x"
  706.                 xPos = Val(Mid$(Line, 3)) - 1
  707.               Case "y"
  708.                 yPos = (Val(Mid$(Line, 3)) - 1) * CellsAcross
  709.               Case "s"
  710.                 On Error Resume Next
  711.                     scrDelay = 51 - Val(Mid$(Line, 3)) 'speed
  712.                 On Error GoTo 0
  713.               Case Else
  714.                 If xPos + yPos >= 0 Then
  715.                     If xPos + yPos + Len(Line) < NumCells Then
  716.                         For i = 1 To Len(Line)
  717.                             Cells(yPos + xPos + i - 1).Age(0) = Sgn(InStr("o*+1", Mid$(Line, i, 1)))
  718.                             UpdateVicinity yPos + xPos + i - 1, 0, Sgn(InStr("o*+1", Mid$(Line, i, 1)))
  719.                         Next i
  720.                         yPos = yPos + CellsAcross
  721.                       Else 'NOT XPOS...
  722.                         NoFit = True
  723.                     End If
  724.                   Else 'NOT XPOS...
  725.                     NoFit = True
  726.                 End If
  727.             End Select
  728.         End If
  729.     Loop
  730.     Close hFile
  731.     If NoFit Then
  732.         MsgBox TooBig, vbExclamation, Caption
  733.     End If
  734.     DisplayCurrent
  735.  
  736. End Sub
  737.  
  738. Private Sub mnuBackColor_Click()
  739.  
  740.     With CDl
  741.         On Error Resume Next
  742.             .ShowColor
  743.             If Err = 0 Then
  744.                 picBuffer.BackColor = .Color
  745.                 picView.BackColor = .Color
  746.                 DisplayCurrent
  747.             End If
  748.         On Error GoTo 0
  749.     End With 'CDL
  750.  
  751. End Sub
  752.  
  753. Private Sub mnuDeadColor_Click()
  754.  
  755.     With CDl
  756.         On Error Resume Next
  757.             .ShowColor
  758.             If Err = 0 Then
  759.                 DeadColor = .Color
  760.             End If
  761.         On Error GoTo 0
  762.     End With 'CDL
  763.  
  764. End Sub
  765.  
  766. Private Sub mnuExit_Click()
  767.  
  768.     Unload Me
  769.  
  770. End Sub
  771.  
  772. Private Sub mnuLiveColor_Click()
  773.  
  774.     With CDl
  775.         On Error Resume Next
  776.             .ShowColor
  777.             If Err = 0 Then
  778.                 LiveColor = .Color
  779.                 DisplayCurrent
  780.             End If
  781.         On Error GoTo 0
  782.     End With 'CDL
  783.  
  784. End Sub
  785.  
  786. Private Sub mnuLoad_Click()
  787.  
  788.     With CDl
  789.         .InitDir = App.Path & "\Patterns"
  790.         .DialogTitle = "Enter/Select file to load..."
  791.         .Filename = vbNullString
  792.         .DefaultExt = ".TXT"
  793.         .Filter = "GoL Pattern(*.TXT)|*.TXT|Arith Notation(*.LIF)|*.LIF|All Files(*.*)|*.*"
  794.         .Flags = cdlOFNPathMustExist Or cdlOFNLongNames
  795.         On Error Resume Next
  796.             .ShowOpen
  797.             If Err = 0 Then
  798.                 Seed = 0
  799.                 Caption = App.ProductName & " [" & CDl.FileTitle & "]"
  800.                 Select Case LCase$(Right$(.FileTitle, 3))
  801.                   Case "txt"
  802.                     LoadPattern .Filename
  803.                   Case "lif"
  804.                     LoadArith .Filename
  805.                   Case Else
  806.                     MsgBox "Unknown File Type:" & vbCrLf & vbCrLf & .FileTitle, vbExclamation, App.ProductName
  807.                 End Select
  808.             End If
  809.         On Error GoTo 0
  810.     End With 'CDL
  811.  
  812. End Sub
  813.  
  814. Private Sub mnuRandom_Click()
  815.  
  816.   'create random colony
  817.  
  818.     Seed = GetTickCount
  819.     CreateRandom
  820.  
  821. End Sub
  822.  
  823. Private Sub mnuResetColor_Click()
  824.  
  825.     LiveColor = vbYellow
  826.     DeadColor = &H3030&
  827.     picBuffer.BackColor = vbBlack
  828.     picView.BackColor = vbBlack
  829.     DisplayCurrent
  830.  
  831. End Sub
  832.  
  833. Private Sub mnuSize_Click(Index As Integer)
  834.  
  835.   Dim i As Long
  836.  
  837.     Select Case Index
  838.       Case 0
  839.         CellsAcross = 90 '10
  840.         CellsDown = 60
  841.       Case 1
  842.         CellsAcross = 150 '6
  843.         CellsDown = 100
  844.       Case 2
  845.         CellsAcross = 180 '5
  846.         CellsDown = 120
  847.       Case 3
  848.         CellsAcross = 300 '3
  849.         CellsDown = 200
  850.       Case 4
  851.         CellsAcross = 450 '2
  852.         CellsDown = 300
  853.     End Select
  854.     For i = 0 To 4
  855.         mnuSize(i).Checked = (i = Index)
  856.     Next i
  857.     Restart
  858.  
  859. End Sub
  860.  
  861. Private Sub picView_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
  862.  
  863.   Dim hFile As Long
  864.   Dim e As Long
  865.  
  866.     hFile = FreeFile
  867.     With Data
  868.         On Error Resume Next
  869.             Open .Files(1) For Input As hFile
  870.             e = Err
  871.         On Error GoTo 0
  872.         If e Then
  873.             MsgBox "Files only, please", vbExclamation, App.ProductName
  874.           Else 'E = FALSE/0
  875.             Close hFile
  876.             CDl.Filename = .Files(1)
  877.             Caption = App.ProductName & " [" & Mid$(.Files(1), InStrRev(.Files(1), "\") + 1) & "]"
  878.             Select Case LCase$(Right$(.Files(1), 3))
  879.               Case "txt"
  880.                 LoadPattern .Files(1)
  881.               Case "lif"
  882.                 LoadArith .Files(1)
  883.               Case Else
  884.                 Caption = App.ProductName
  885.                 MsgBox "Unknown File Type:" & vbCrLf & vbCrLf & .Files(1), vbCritical, App.ProductName
  886.             End Select
  887.         End If
  888.     End With 'DATA
  889.  
  890. End Sub
  891.  
  892. Private Sub Restart()
  893.  
  894.     CreateUniverse
  895.     With CDl
  896.         If Seed Then
  897.             CreateRandom
  898.           ElseIf Len(.Filename) Then 'SEED = FALSE/0
  899.             Select Case LCase$(Right$(.Filename, 3))
  900.               Case "txt"
  901.                 LoadPattern .Filename
  902.               Case "lif"
  903.                 LoadArith .Filename
  904.             End Select
  905.           Else 'LEN(.FILENAME) = FALSE/0
  906.             GliderIni
  907.         End If
  908.     End With 'CDL
  909.     Activate
  910.  
  911. End Sub
  912.  
  913. Private Sub UpdateVicinity(ByVal Idx As Long, ByVal NextGen As Long, ByVal IncDec As Long)
  914.  
  915.   'updates the vicinity of a cell
  916.  
  917.     With Cells(Idx) 'current cell
  918.  
  919.         Cells(.TopNeighbor).Neighbors(NextGen) = Cells(.TopNeighbor).Neighbors(NextGen) + IncDec 'current cell's top neighbor(north)
  920.  
  921.         Cells(.BottomNeighbor).Neighbors(NextGen) = Cells(.BottomNeighbor).Neighbors(NextGen) + IncDec 'current cell's bottom neighbor(south)
  922.  
  923.         With Cells(.LeftNeighbor) 'current cell's left neighbor(west)
  924.             .Neighbors(NextGen) = .Neighbors(NextGen) + IncDec
  925.             Cells(.TopNeighbor).Neighbors(NextGen) = Cells(.TopNeighbor).Neighbors(NextGen) + IncDec 'left neighbor's top neighbor(north west)
  926.             Cells(.BottomNeighbor).Neighbors(NextGen) = Cells(.BottomNeighbor).Neighbors(NextGen) + IncDec 'left neighbor's bottom neighbor(south west)
  927.         End With 'CELLS(.LEFTNeighbor)
  928.  
  929.         With Cells(.RightNeighbor) 'current cell's right neighbor(east)
  930.             .Neighbors(NextGen) = .Neighbors(NextGen) + IncDec
  931.             Cells(.TopNeighbor).Neighbors(NextGen) = Cells(.TopNeighbor).Neighbors(NextGen) + IncDec 'right neighbor's top neighbor(north east)
  932.             Cells(.BottomNeighbor).Neighbors(NextGen) = Cells(.BottomNeighbor).Neighbors(NextGen) + IncDec 'right neighbor's bottom neighbor(south east)
  933.         End With 'CELLS(.RIGHTNeighbor)
  934.  
  935.     End With 'CELLS(IDX)
  936.  
  937. End Sub
  938.  
  939. ':) Ulli's VB Code Formatter V2.23.17 (2008-Apr-01 19:36)  Decl: 39  Code: 619  Total: 658 Lines
  940. ':) CommentOnly: 53 (8,1%)  Commented: 66 (10%)  Filled: 515 (78,3%)  Empty: 143 (21,7%)  Max Logic Depth: 7
  941.