Click here to Skip to main content
15,896,154 members
Articles / Programming Languages / Visual Basic

Mastermind: An Evolutionary Computing Framework Demo

Rate me:
Please Sign up or sign in to vote.
4.29/5 (8 votes)
16 Sep 2004CPOL2 min read 51.4K   161   24  
Demonstration on how to use the evolutionary computing framework
Public Class MastermindMainForm
    Inherits System.Windows.Forms.Form

#Region "Public properties"
    Dim Environment As MastermindEnvironment
#End Region

#Region "Private properties"
    Private _InGame As Boolean

    Private _LastGuessBottom As Integer
#End Region

    Private WriteOnly Property InGame() As Boolean
        Set(ByVal Value As Boolean)
            _InGame = Value
            Me.MenuItem_File_NextGeneration.Enabled = Value
        End Set
    End Property
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents StatusBar_Mastermind As System.Windows.Forms.StatusBar
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem_File As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem_Help As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItemHelp_About As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem_File_Settings As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem_File_NextGeneration As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem_File_Exit As System.Windows.Forms.MenuItem
    Friend WithEvents MastermindRow_Test As Mastermind.MastermindRow
    Friend WithEvents Panel_Guesses As System.Windows.Forms.Panel
    Friend WithEvents ToolTip_Guesses As System.Windows.Forms.ToolTip
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.StatusBar_Mastermind = New System.Windows.Forms.StatusBar()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu()
        Me.MenuItem_File = New System.Windows.Forms.MenuItem()
        Me.MenuItem_File_Settings = New System.Windows.Forms.MenuItem()
        Me.MenuItem_File_NextGeneration = New System.Windows.Forms.MenuItem()
        Me.MenuItem_File_Exit = New System.Windows.Forms.MenuItem()
        Me.MenuItem_Help = New System.Windows.Forms.MenuItem()
        Me.MenuItemHelp_About = New System.Windows.Forms.MenuItem()
        Me.MastermindRow_Test = New Mastermind.MastermindRow()
        Me.Panel_Guesses = New System.Windows.Forms.Panel()
        Me.ToolTip_Guesses = New System.Windows.Forms.ToolTip(Me.components)
        Me.SuspendLayout()
        '
        'StatusBar_Mastermind
        '
        Me.StatusBar_Mastermind.Location = New System.Drawing.Point(0, 356)
        Me.StatusBar_Mastermind.Name = "StatusBar_Mastermind"
        Me.StatusBar_Mastermind.Size = New System.Drawing.Size(346, 22)
        Me.StatusBar_Mastermind.TabIndex = 0
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem_File, Me.MenuItem_Help})
        '
        'MenuItem_File
        '
        Me.MenuItem_File.Index = 0
        Me.MenuItem_File.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem_File_Settings, Me.MenuItem_File_NextGeneration, Me.MenuItem_File_Exit})
        Me.MenuItem_File.Text = "&File"
        '
        'MenuItem_File_Settings
        '
        Me.MenuItem_File_Settings.Index = 0
        Me.MenuItem_File_Settings.Text = "&Settings"
        '
        'MenuItem_File_NextGeneration
        '
        Me.MenuItem_File_NextGeneration.Enabled = False
        Me.MenuItem_File_NextGeneration.Index = 1
        Me.MenuItem_File_NextGeneration.Text = "&Next generation"
        '
        'MenuItem_File_Exit
        '
        Me.MenuItem_File_Exit.Index = 2
        Me.MenuItem_File_Exit.Text = "&Exit"
        '
        'MenuItem_Help
        '
        Me.MenuItem_Help.Index = 1
        Me.MenuItem_Help.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItemHelp_About})
        Me.MenuItem_Help.Text = "&Help"
        '
        'MenuItemHelp_About
        '
        Me.MenuItemHelp_About.Index = 0
        Me.MenuItemHelp_About.Text = "&About"
        '
        'MastermindRow_Test
        '
        Me.MastermindRow_Test.BackColor = System.Drawing.Color.FromArgb(CType(224, Byte), CType(224, Byte), CType(224, Byte))
        Me.MastermindRow_Test.Dock = System.Windows.Forms.DockStyle.Top
        Me.MastermindRow_Test.Name = "MastermindRow_Test"
        Me.MastermindRow_Test.Size = New System.Drawing.Size(346, 40)
        Me.MastermindRow_Test.TabIndex = 1
        Me.ToolTip_Guesses.SetToolTip(Me.MastermindRow_Test, "Target peg setup")
        '
        'Panel_Guesses
        '
        Me.Panel_Guesses.AutoScroll = True
        Me.Panel_Guesses.Dock = System.Windows.Forms.DockStyle.Fill
        Me.Panel_Guesses.Location = New System.Drawing.Point(0, 40)
        Me.Panel_Guesses.Name = "Panel_Guesses"
        Me.Panel_Guesses.Size = New System.Drawing.Size(346, 316)
        Me.Panel_Guesses.TabIndex = 2
        '
        'MastermindMainForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(346, 378)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel_Guesses, Me.MastermindRow_Test, Me.StatusBar_Mastermind})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.Menu = Me.MainMenu1
        Me.Name = "MastermindMainForm"
        Me.Text = "Mastermind (Evolutionary Computing Demo)"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub MenuItem_File_Exit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItem_File_Exit.Click
        Me.Close()
    End Sub

    Private Sub MenuItem_File_Settings_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItem_File_Settings.Click

        Dim pThis As New Properties()
        pThis.ShowDialog()
        Environment = New MastermindEnvironment(pThis.PopulationSize, pThis.CorrectGuess)
        MastermindRow_Test.Guess = pThis.CorrectGuess

        Me.StatusBar_Mastermind.Text = "Population: " & pThis.PopulationSize.ToString

        InGame = True

    End Sub

    Private Sub MenuItem_File_NextGeneration_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItem_File_NextGeneration.Click
        Try
            Environment.NextGeneration()
        Catch exNG As Exception
            MessageBox.Show(exNG.Message, "Error generating the next generation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End Try
        Dim GuessCtrl As New MastermindRow()
        With GuessCtrl
            .Guess = Environment.BestGuess
        End With
        Panel_Guesses.Controls.Add(GuessCtrl)
        GuessCtrl.Top = _LastGuessBottom
        _LastGuessBottom = _LastGuessBottom + GuessCtrl.Height
        If Environment.MaximumScore = Environment.GetHealth(Environment.BestGuess) Then
            MessageBox.Show("Mastermind solved", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            Me.StatusBar_Mastermind.Text = "Score : " & Environment.GetHealth(Environment.BestGuess) & " out of " & Environment.MaximumScore
        End If

    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions