Click here to Skip to main content
15,881,793 members
Articles / Programming Languages / Visual Basic

Puzzle Square

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
11 Jan 2013CPOL5 min read 26.3K   704   10  
The goal of the program was to make things simple yet easy to understand and quickly changeable based on future ideas.

Public Class FrmMain

   Dim SquareData As String = ""

   Dim ColorArray(4) As Color

   Dim MaxColors As Integer

   Dim InitializingFlag As Boolean = False

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

   InitializingFlag = True

   ' Default to Demo Mode OFF
   DemoTimer.Enabled = False

   ' default to 2 colors to be used!
   ' ComboBox contains the values 2, 3, and 4
   cmbColorsToUse.SelectedIndex = 0

   StartNewGame()

End Sub

Private Sub StartNewGame()

   InitializingFlag = True

   ' Setup our Colors
   ColorArray(1) = Color.Red
   ColorArray(2) = Color.Yellow
   ColorArray(3) = Color.Green
   ColorArray(4) = Color.Blue

   ' Use between 2 and 4 colors based on ComboBox Selection...
   MaxColors = Val(cmbColorsToUse.Text) ' .SelectedValue

   'MaxColors = 2 ' Use between 2 and 4 colors

   ' Array lines up like the following for the numbers in the SquareData:
   '
   '   1 2 3
   '   4 5 6
   '   7 8 9
   '
   '             123456789
   SquareData = "121212121" ' Assume 2 colors only at this moment...

   ' If 3 Colors to be used, then initialize the Cube Array with the 3 colors as a starting point!
   If MaxColors = 3 Then
      SquareData = "123123123"
   End If

   ' If 4 Colors to be used, then initialize the Cube Array with the 4 colors as a starting point!
   If MaxColors = 4 Then
      SquareData = "123412341"
   End If

   'MsgBox("Add randomize logic here...")

   ' Get Current Values for these to use for the Pseudo
   '  Randomize Logic Below:
   ' - Hour
   ' - Minute
   ' - Second
   ' - Month
   ' - Day
   ' - Year
   Dim CurrentHour As Integer = TimeOfDay.Hour
   Dim CurrentMinute As Integer = TimeOfDay.Minute
   Dim CurrentSecond As Integer = TimeOfDay.Second

   Dim CurrentMonth As Integer = Today.Date.Month
   Dim CurrentDay As Integer = Today.Date.Day
   Dim CurrentYear As Integer = Today.Date.Year

   ' AI1, AI2, and AJ2 are used to pseudo randomize
   '  the random number seed!
   Dim AI1 As Integer = 0
   Dim AI2 As Integer = _
                        ((CurrentYear Mod 20) * 4) + _
                        ((CurrentSecond Mod 9) * 3) + _
                        ((CurrentMonth Mod 3) * 5) + 37
   '
   'Dim AJ1 As Integer = 0
   Dim AJ2 As Single = _
                       ((CurrentHour Mod 6) * 5) + _
                       ((CurrentDay Mod 13) * 3) + _
                       ((CurrentMinute Mod 7) * 2) + _
                       ((CurrentSecond Mod 12) * 3) + 13
   ' 
   Dim AB1 As Single = 0
   '
   ' Pseudo Randomize the seeding of random number
   ' - This insures a Random Universe each time the game
   '   is run.
   For AI1 = 0 To AI2
      AB1 = Rnd(AJ2) 'AB1 = AJ2 * Rnd(1)
   Next AI1

   ' Randomly Pick an index from 1 to 9
Rerandomize:

   ' Do 101 loops simulating button press of any of the 9 buttons
   For AI1 = 0 To 100
      AI2 = Int(Rnd(1) * 9 + 1)
      ChangeValuesBasedOnButtonPress(AI2)
   Next AI1

   Dim I1 As Integer = 0 ' Red Color Count
   Dim I2 As Integer = 0 ' Yellow Color Count
   Dim I3 As Integer = 0 ' Green Color Count
   Dim I4 As Integer = 0 ' Blue Color Count

   Dim Temp As String = ""

   ' Find out how many of each color are currently used!
   For AI1 = 1 To 9

      Temp = Mid(SquareData, AI1, 1)

      Select Case Temp
         Case 1
            I1 = I1 + 1 ' Red

         Case 2
            I2 = I2 + 1 ' Yellow

         Case 3
            I3 = I3 + 1 ' Green

         Case 4
            I4 = I4 + 1 ' Blue

      End Select

   Next AI1

   ' If 2 color mode, then make sure that we have at least 2 of each color!
   If MaxColors = 2 Then

      ' If Less than 2 Reds, then rerandomize!
      If (I1 < 2) Then
         GoTo Rerandomize
      End If

      ' If Less than 2 Yellows, then rerandomize!
      If (I2 < 2) Then
         GoTo Rerandomize
      End If

   End If

   ' If 3 color modes, then make sure that we have at least 2 of each color!
   If MaxColors = 3 Then

      ' If Less than 2 Reds, then rerandomize!
      If (I1 < 2) Then
         GoTo Rerandomize
      End If

      ' If Less than 2 Yellows, then rerandomize!
      If (I2 < 2) Then
         GoTo Rerandomize
      End If

      ' If Less than 2 Greens, then rerandomize!
      If (I3 < 2) Then
         GoTo Rerandomize
      End If

   End If

   ' If 4 colors, then make sure that we have at least 1 of each color!
   If MaxColors = 4 Then

      ' If Less than 1 Red, then rerandomize!
      If (I1 < 1) Then
         GoTo Rerandomize
      End If

      ' If Less than 1 Yellow, then rerandomize!
      If (I2 < 1) Then
         GoTo Rerandomize
      End If

      ' If Less than 1 Green, then rerandomize!
      If (I3 < 1) Then
         GoTo Rerandomize
      End If

      ' If Less than 1 Blue, then rerandomize!
      If (I4 < 1) Then
         GoTo Rerandomize
      End If

   End If

   ' Initialization Finished
   InitializingFlag = False

   ' So Update the Colors on the Buttons to match our Array Values for the Colors
   UpdateButtonColors()

End Sub

Private Sub btnSelect1_Click(sender As Object, e As EventArgs) Handles btnSelect1.Click

   ChangeValuesBasedOnButtonPress(1)

End Sub

Private Sub btnSelect2_Click(sender As Object, e As EventArgs) Handles btnSelect2.Click

   ChangeValuesBasedOnButtonPress(2)

End Sub

Private Sub btnSelect3_Click(sender As Object, e As EventArgs) Handles btnSelect3.Click

   ChangeValuesBasedOnButtonPress(3)

End Sub

Private Sub btnSelect4_Click(sender As Object, e As EventArgs) Handles btnSelect4.Click

   ChangeValuesBasedOnButtonPress(4)

End Sub

Private Sub btnSelect5_Click(sender As Object, e As EventArgs) Handles btnSelect5.Click

   ChangeValuesBasedOnButtonPress(5)

End Sub

Private Sub btnSelect6_Click(sender As Object, e As EventArgs) Handles btnSelect6.Click

   ChangeValuesBasedOnButtonPress(6)

End Sub

Private Sub btnSelect7_Click(sender As Object, e As EventArgs) Handles btnSelect7.Click

   ChangeValuesBasedOnButtonPress(7)

End Sub

Private Sub btnSelect8_Click(sender As Object, e As EventArgs) Handles btnSelect8.Click

   ChangeValuesBasedOnButtonPress(8)

End Sub

Private Sub btnSelect9_Click(sender As Object, e As EventArgs) Handles btnSelect9.Click

   ChangeValuesBasedOnButtonPress(9)

End Sub

Private Sub UpdateButtonColors()

  ' If initializing the SquareData values, then exit without updating the button colors!
   If InitializingFlag = True Then
      Exit Sub
   End If

   Dim x As Integer = 0

   Dim NewBackColor As Color = Color.Black

   For i = 1 To 9

      x = Val(Mid(SquareData, i, 1))

      NewBackColor = ColorArray(x)

      Select Case i

          Case 1
             btnSelect1.BackColor = NewBackColor

          Case 2
             btnSelect2.BackColor = NewBackColor

          Case 3
             btnSelect3.BackColor = NewBackColor

          Case 4
             btnSelect4.BackColor = NewBackColor

          Case 5
             btnSelect5.BackColor = NewBackColor

          Case 6
             btnSelect6.BackColor = NewBackColor

          Case 7
             btnSelect7.BackColor = NewBackColor

          Case 8
             btnSelect8.BackColor = NewBackColor

          Case 9
             btnSelect9.BackColor = NewBackColor

      End Select

   Next i

   ' IF ALL the Values are the same, then the Puzzle has been solved!
   If SquareData = "111111111" Or _
      SquareData = "222222222" Or _
      SquareData = "333333333" Or _
      SquareData = "444444444" Then
      MsgBox("You WON!!!")
   End If

End Sub

Private Sub ChangeValuesBasedOnButtonPress(ByVal BtnNumber As Integer)

   Dim ChangeArray As String = ""

   ' Based on the Button pressed, change the proper colors on the buttons surrounding it using our
   '  predetermined pattern as described on the Help Form.
   Select Case BtnNumber

      Case 1
         ChangeArray = "1245"

      Case 2
         ChangeArray = "1235"

      Case 3
         ChangeArray = "2356"

      Case 4
         ChangeArray = "1457"

      Case 5
         ChangeArray = "24568"

      Case 6
         ChangeArray = "3569"

      Case 7
         ChangeArray = "4578"

      Case 8
         ChangeArray = "5789"

      Case 9
         ChangeArray = "5689"

   End Select

   Dim x As Integer = 0

   For i = 1 To Len(ChangeArray)

      x = Val(Mid(ChangeArray, i, 1))

      UpdateSquareValue(x)

   Next

   UpdateButtonColors()

End Sub

' Increment the Square Value to the Next Color
Private Sub UpdateSquareValue(ByVal UpdateSquare As Integer)

   Dim NewValue As Integer

   NewValue = Val(Mid(SquareData, UpdateSquare, 1)) + 1

   ' If the New Color Value is beyond the number of Colors that we are currently using...
   ' ...then we will reset the color to Red!
   If NewValue > MaxColors Then
      NewValue = 1
   End If

   ' Update the value in our Square Data String
   Mid(SquareData, UpdateSquare, 1) = Microsoft.VisualBasic.Right(Trim(Str(NewValue)), 1)

End Sub

Private Sub cmbColorsToUse_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbColorsToUse.SelectedIndexChanged

   If InitializingFlag = True Then
      Exit Sub
   End If

   StartNewGame()

End Sub

' Toggle demo Mode ON or OFF!
Private Sub btnDemoMode_Click(sender As Object, e As EventArgs) Handles btnDemoMode.Click

   If DemoTimer.Enabled = True Then
      DemoTimer.Enabled = False
      btnDemoMode.Text = "Demo Mode OFF"
   Else
      DemoTimer.Enabled = True
      btnDemoMode.Text = "Demo Mode ON"
   End If

End Sub

Private Sub DemoTimer_Tick(sender As Object, e As EventArgs) Handles DemoTimer.Tick

   ' While Demo Mode is ON, we will regenerate the Square Colors based on the
   '  StartNewGame Subroutine Logic
   StartNewGame()

End Sub

Private Sub btnHelp_Click(sender As Object, e As EventArgs) Handles btnHelp.Click

   ' Show the Help Dialog and wait until the OK button on it is pressed
   Dim HelpForm As FrmHelp = New FrmHelp
   HelpForm.ShowDialog()

End Sub

Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click

   ' Close our program
   Me.Close()

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 (Senior)
United States United States
BillNew - Software developer since November 1977.

Comments and Discussions