Click here to Skip to main content
15,867,835 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   1
The goal of the program was to make things simple yet easy to understand and quickly changeable based on future ideas.

Puzzle Square - Original Concept

The goal of the program was to make things simple yet easy to understand and quickly changeable based on future ideas.

The seed of the idea came from seeing a light puzzle many years ago but I don't remember exactly what the game was called or how it worked. What I do remember was that You would press a button on its' screen and it would change the pattern shown on its' display. The object was to get all of the lights to turn on so you would "Win".

So I thought it would be fun to take the idea and make it into a simple "workable game" using Visual Basic under Microsoft Visual Studio 2012 Ultimate Software. I have written a lot of code using Microsoft Visual Studio 2003 as well as its' 2005 and 2008 versions. So part of this was to get familiar with the 2012 version of Microsoft Visual Studio.

Visual Basic (VB) was chosen because I write VB code quickly versus using C / C++ / C#. I do write some code in C++ / C# but that wasn't on my radar for this project.

Puzzle Square - From Concept to Working Code

First step on writing the program was deciding on a name using Visual Basic then doing the initial layout of only the 9 buttons on the screen.

So far so good.

Then put some minor code behind the 9 buttons so that when a button was pressed that it would pass its' value from 1 to 9 to a common routine called "ChangeValuesBasedOnButtonPress" which would later be used to update the buttons based on the predefined pattern for that button as described in the detailed help form.

The first 2 colors were predetermined to be "Red" and "Yellow" respectively.

Started with "2 color" mode with "SquareData" being set to "121212121" which equates to Red on every odd numbered square and Yellow on every even numbered square.

Next thing was to work on the code for ChangeValuesBasedOnButtonPress so that the puzzle would change the colors to the next color for the 4 or 5 buttons affected by the button just pressed. "UpdateSquareValue" function rotates the selected values to the next color number for each affected square. If the next color number is greater than the maximum number of colors selected in the "Color Modes" combobox, then that value gets reset to "1". (Red) Then once all of the squares are updated for button pressed, then the UpdateButtonColors Function is called which then updates the colors on the 9 buttons on the screen AND determines if ALL the colors of all the buttons are the same which then shows a MessageBox saying "You WON!!!".

VB
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

Next added in the Randomization Code to randomly pick a number from 1 to 9 at the "Rerandomize:" label in the "StartNewGame" function which then effective acts as if someone were to pick one of the 9 buttons to press. This occurs 101 times to give a pseudo random pattern.  

Later decided on the 3rd and 4th colors of "Green" and "Blue" when "3 Color" and "4 Color" modes are chosen.

Game defaults to "2 Color" Mode when it first starts up ... then stays in that mode until you change the number of colors to any of these 3 values: 2 Color Mode, 3 Color Mode, or 4 Color Mode. When a Color Mode is changed, then the Puzzle Square is re-randomized.

Image 1 

Image 2

Image 3

Found that If I did not count the colors when randomizing the colors that you often didn't get all the colors used for the Color modes ... So I quickly modified the "StartNewGame" Function to count the number of colors then if the minimum desired colors for each color used in the puzzle was not met to then "re-randomize" the puzzle until the minimum number of colors was met.

The randomizer seeding logic was copied as is from my original CodeProject code called "Star Trek for Windows". This randomizer logic uses the Current Date and Current Time to insure better number randomization. Otherwise you will get the SAME puzzle when the program launches due to fact that the random number generator was not randomized properly.

Added the "InitializingFlag" flag close to the last thing so as to not update the puzzle while the randomization is being generated.

Last function added was the "Demo Mode" to Demonstrate how well the random number generation works.

Final notes:

  1. All generated puzzles ARE solvable.

    Occasionally you may have to press the center square to solve the problem. Otherwise you may get "stuck" with a seemingly unsolvable puzzle.

    2 color mode is very easy.

    3 color mode is mediumly hard.

    4 color mode is very challenging at times.

    I played this game daily for 10 to 15 minutes over a period of 2 weeks or so to prove that each generated puzzle IS solvable.

  2. After you play the game quite a few times, you will "see" the patterns and it becomes much easier to solve in any of the 3 Selectable "Color Modes".
  3. There are many things that could be done to make this program more polished ... that I will leave up to you to decide.

The code is provided for you to play with as you decide.

The Main Form could be locked on its' size ... I missed that when I uploaded the code to this project.

Now go experiment and have fun!

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

 
GeneralThis game is available in 2 forms on the Windows Phone Store... Pin
Destiny77719-Jun-17 12:33
Destiny77719-Jun-17 12:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.