Visual Basic 2010 (Windows) Guide


A Javascript version of the Rotatamundo game appears on this site at,

Play the game a few times to see how it works.

The instructions on this page will get you started. You will have to do some work to complete the program.

Set Up The Form

Start a new project and resize the form.

Add Labels to the form and set their text to the numbers you see below.

Visual Basic Screenshot

The names of the labels should be Label1 for the label that is shown with a 1, Label2 for the label with a 2 and so on. You will need to set AutoSize to false for each of the labels to be able to size them as you would like.

Coding The Solution

Bring up the code window by pressing the F7 key. You will need a global variable. It will be an array of integers and will store the order of the numbers in the puzzle. It will begin with the numbers set to the solved position,

Dim permutation() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Notice that 0 is included. That is because VB starts its arrays from 0. We will be ignoring this value in our program.

We will need a procedure to write the numbers of the current state of the puzzle into the labels on the form. The code for that procedure is as follows,

Sub UpdateDisplay()
   Dim boxes() As Label = {Label1, Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8, Label9}
   For i As Integer = 1 To 9
      boxes(i).Text = permutation(i)
End Sub

Label 1 appears twice in the list. The first mention is a dummy for the 0 position we are not using.

Return to the designer window and double click on the empty space of the form. The code window should pop up showing the Form_Load event code. Make sure it reads as follows,

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

Coding The Moves

There are 4 moves - A, B, C & D. We can write one procedure that performs them all.

The following code contains the logic for the A move. We will make sure that this move works before sorting out the others.

Sub DoMove(move As Integer)
   Dim temp As Integer
   Select Case move
      Case 0
         temp = permutation(4)
         permutation(4) = permutation(5)
         permutation(5) = permutation(2)
         permutation(2) = permutation(1)
         permutation(1) = temp
      Case 1
      Case 2
      Case 3
   End Select
End Sub

Create a button for Move A. Name the button btnA and double click on it to bring up the code window.

Private Sub btnA_Click(sender As System.Object, e As System.EventArgs) Handles btnA.Click
End Sub

Save and test that pressing button A makes the correct set of numbers 'rotate'.

Code The Remaining Moves

Create the buttons you need for the remaining moves. Double click on the B button. The code you will need is DoMove(1). For button C you will be doing Move 2 and for button D, Move 3.

The DoMove procedure now needs lines of code for the remaining moves. If you look at how the 0 move was coded, you should have an idea of what the other moves will look like. The numbers in brackets will be different because different parts of the puzzle rotate.

This is quite challenging to do. To help yourself, look at the image of the completed grid. Look at which position is assigned to the temp variable. Look then at exactly which positions are swapped around. The order matters here. Do one button at a time and check each one carefully before proceeding.

A Shuffle Procedure

For this to work as a game, you need to mix up the positions. We already have a procedure that will do a move. If we do say, 50 random moves, we will have a shuffled puzzle.

Here is some pseudocode describing the Shuffle procedure,

Sub Shuffle()
   Declare random number generator
   Declare integer to store each move temporarily
   For i ← 1 To 50
      Generate random integer from 0 To 3
      DoMove(random integer)
End Sub

Adjust the Form Load event in the code so that it calls this procedure before updating the display.



After each move, you should get the program to check whether the puzzle is solved. The best place to do this would be after the UpdateDisplay() line in the DoMove() procedure.

The following pseudocode describes this process,

won = true
For i ← 1 To 9
   If permutation(i) <> i Then won = false
If won = true Then tell the user they have won
The simplest way to inform the user of a win is to pop up a Message Box. You can do that with the following code, MessageBox.Show("You have won")

Once a win has been determined, it is worth running the Shuffle() procedure to make a new game.

Counting Moves

Keeping track of the number of moves the user took to solve the puzzle adds a nice feature to the game. You would need another global variable. When you shuffle the puzzle, you set the value of this variable to 0. When a move is made, you should add 1 to the number. After each move, you should write this number into a label you have placed on the form.

Extreme Rotation

You can make harder versions of the game - a few have been made in Javascript on this site. 4x4 or larger grids present quite a difficulty for the puzzle solver. Obviously there is more to program but it is all based on the ideas you have read on this page.