Public Class Form1
Private pic As PictureBox
Private btnStartStop As Button
Private Running As Boolean = False
Private Const BOARD_WIDTH As Integer = 50
Private Const BOARD_HEIGHT As Integer = 50
Private CellSize As New SizeF
Private cells(BOARD_WIDTH - 1, BOARD_HEIGHT - 1) As State
Private Rects(BOARD_WIDTH - 1, BOARD_HEIGHT - 1) As RectangleF
Private linePen As New Pen(Color.Green) With {.DashStyle = Drawing2D.DashStyle.Dot}
Private CellBrush As New SolidBrush(Color.White)
Private Enum State
Empty = 0
Birth = 1
Alive = 2
Dying = 3
End Enum
Public Sub New()
InitializeComponent()
Me.SuspendLayout()
pic = New PictureBox With _
{.BackColor = Color.Black, _
.Left = 120, .Top = 0, .Height = Me.ClientRectangle.Height, .Width = Me.ClientRectangle.Width - 120, _
.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right Or AnchorStyles.Left Or AnchorStyles.Top}
btnStartStop = New Button With _
{.Text = "Start", .Width = 100, .Height = Me.Font.Height * 2, .Left = 10, .Top = 10}
Me.Controls.AddRange(New Control() {pic, btnStartStop})
Me.AcceptButton = btnStartStop
Me.ResumeLayout()
End Sub
Private Sub Drawing(ByVal sender As Object, ByVal e As PaintEventArgs)
Dim x, y As Single
Dim ix, iy As Integer
For x = 0 To BOARD_WIDTH - 1
For y = 0 To BOARD_HEIGHT - 1
ix = CInt(x)
iy = CInt(y)
Select Case cells(ix, iy)
Case State.Dying
cells(ix, iy) = State.Empty
Case State.Birth
cells(ix, iy) = State.Alive
End Select
If cells(CInt(x), CInt(y)) = State.Alive Then e.Graphics.FillRectangle(CellBrush, Rects(CInt(x), CInt(y)))
Next
Next
For x = 0 To pic.ClientRectangle.Width - 1 Step CellSize.Width
e.Graphics.DrawLine(linePen, x, 0, x, pic.ClientRectangle.Height - 1)
Next
For y = 0 To pic.ClientRectangle.Height - 1 Step CellSize.Height
e.Graphics.DrawLine(linePen, 0, y, pic.ClientRectangle.Width - 1, y)
Next
End Sub
Private Sub StartStop(ByVal sender As Object, ByVal e As EventArgs)
Running = Not Running
If Running Then
btnStartStop.Text = "Stop"
RemoveHandler pic.MouseClick, AddressOf UserClick
Else
btnStartStop.Text = "Start"
AddHandler pic.MouseClick, AddressOf UserClick
End If
If Running Then GoForthAndMultiply()
End Sub
Private Sub GoForthAndMultiply()
Do
Application.DoEvents()
Generation()
pic.Invalidate()
If Running = False Then Exit Sub
Loop
End Sub
Private Sub Generation()
' cell dies if it has less than 2 or more than 3 neighbors
' cell is born if it has exactly 3 neighbors
Dim x, y As Integer
Dim Neighbors As Integer = 0
For x = 0 To BOARD_WIDTH - 1
For y = 0 To BOARD_HEIGHT - 1
If y > 0 AndAlso cells(x, y - 1) >= State.Alive Then Neighbors += 1
If y > 0 AndAlso x < (BOARD_WIDTH - 1) AndAlso cells(x + 1, y - 1) >= State.Alive Then Neighbors += 1
If x < (BOARD_WIDTH - 1) AndAlso cells(x + 1, y) >= State.Alive Then Neighbors += 1
If x < (BOARD_WIDTH - 1) AndAlso y < (BOARD_HEIGHT - 1) AndAlso cells(x + 1, y + 1) >= State.Alive Then Neighbors += 1
If y < (BOARD_HEIGHT - 1) AndAlso cells(x, y + 1) >= State.Alive Then Neighbors += 1
If x > 0 AndAlso y < (BOARD_HEIGHT - 1) AndAlso cells(x - 1, y + 1) >= State.Alive Then Neighbors += 1
If x > 0 AndAlso cells(x - 1, y) >= State.Alive Then Neighbors += 1
If x > 0 AndAlso y > 0 AndAlso cells(x - 1, y - 1) >= State.Alive Then Neighbors += 1
Select Case Neighbors
Case 0, 1, Is > 3
If cells(x, y) <> State.Empty Then cells(x, y) = State.Dying
Case 3
If cells(x, y) = State.Empty Then cells(x, y) = State.Birth
End Select
Neighbors = 0
Next
Next
End Sub
Private Sub UserClick(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim cell As New Point
Dim x, y As Integer
x = CInt(Fix((e.X / CellSize.Width)))
y = CInt(Fix((e.Y / CellSize.Height)))
Select Case cells(x, y)
Case State.Alive, State.Dying
cells(x, y) = State.Empty
Case State.Empty, State.Birth
cells(x, y) = State.Alive
End Select
pic.Invalidate()
End Sub
Private Sub SetCellSize()
Dim w, h As Single
w = CSng(pic.ClientRectangle.Width / BOARD_WIDTH)
h = CSng(pic.ClientRectangle.Height / BOARD_HEIGHT)
Dim x, y As Integer
For y = 0 To BOARD_HEIGHT - 1
For x = 0 To BOARD_WIDTH - 1
Rects(x, y) = New RectangleF(x * w, y * h, w, h)
Next
Next
CellSize.Width = w
CellSize.Height = h
End Sub
Private Sub Pic_Resize(ByVal sender As Object, ByVal e As System.EventArgs)
SetCellSize()
pic.Invalidate()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AddHandler btnStartStop.Click, AddressOf StartStop
AddHandler pic.Paint, AddressOf Drawing
AddHandler pic.MouseClick, AddressOf UserClick
AddHandler pic.Resize, AddressOf Pic_Resize
SetCellSize()
End Sub
End Class