Visual Basic 2010 (Windows) Guide
Conway's Game Of Life:Extra Bits

This page contains the remaining code needed to add some additional features to the application developed using instructions from the previous page.

Exit Menu Item

Double click on the Exit item in the menu. You only need one line of code to exit the program and that is,

Me.Close()

Saving An Image

Add the following code to the very top of the code window,

Imports System.Drawing.Drawing2D
Imports System.IO

The following function makes a Bitmap version of the picture box using the data stored,

Function BitmapFromGrid() As Bitmap
   Dim bmp As Bitmap = New Bitmap(640, 640)
   Dim g As Graphics = Graphics.FromImage(bmp)
   Dim rect As Rectangle
   For i As Integer = 0 To 63
      For j As Integer = 0 To 63
         If grid(i, j) Then
            rect = New Rectangle(i * 10, j * 10, 10, 10)
            g.FillRectangle(Brushes.Black, rect)
         End If
      Next
   Next
   Return bmp
End Function

Now find the menu item for saving an image. Double click on it and add the following code to the event handler,

If Not tmrLife.Enabled Then
   Dim sd As SaveFileDialog = New SaveFileDialog()
   sd.Title = "Save The Image"
   sd.Filter = "Windows Bitmap|*.bmp"
   Dim b As Bitmap
   If sd.ShowDialog = Windows.Forms.DialogResult.OK Then
      b = BitmapFromGrid()
      b.Save(sd.FileName)
   End If
End If

Opening & Saving Files

The following function produces a string of 1s and 0 s that match the live and dead cells of the grid. This can be used to create a text file that we can open at a later date.

Function ProduceGridString() As String
   Dim s As String = ""
   For i As Integer = 0 To 63
      For j As Integer = 0 To 63
         If grid(i, j) = False Then
            s = s + Trim(Str(0))
         Else
            s = s + Trim(Str(1))
         End If
      Next
   Next
   Return s
End Function

This procedure sets the values of the cells using a string representation like that produced from the previous function.

Sub GridFromString(ByVal s As String)
   Dim count As Integer = 0
   For i As Integer = 0 To 63
      For j As Integer = 0 To 63
         If s.Substring(count, 1) = "0" Then
            grid(i, j) = False
         Else
            grid(i, j) = True
         End If
         count += 1
      Next
   Next
End Sub

The code we need for the Save menu item is,

If Not tmrLife.Enabled Then
   Dim sd As SaveFileDialog = New SaveFileDialog()
   sd.Title = "Save The Input"
   sd.Filter = "Life Input File|*.lif"
   If sd.ShowDialog = Windows.Forms.DialogResult.OK Then
         File.WriteAllText(sd.FileName, ProduceGridString())
   End If
End If

For the Open File menu item, we need,

If Not tmrLife.Enabled Then
   Dim od As OpenFileDialog = New OpenFileDialog()
   od.Title = "Open The Input"
   od.Filter = "Life Input File|*.lif"
   Dim inpt As String = ""
   If od.ShowDialog = Windows.Forms.DialogResult.OK Then
      inpt = File.ReadAllText(od.FileName)
      GridFromString(inpt)
      generations = 0
      lblGenerations.Text = "Generations: " + Str(generations)
      picLife.Refresh()
   End If
End If