Let's walk through what you need to do in order to bring your Slider game to life. As with Tic Tac Toe, we'll start with some "set up" - defining variables and writing routines that will help us to get the game ready to play.
You should start by looking at your code, which should be blank at this point. Put in your header and the Option Explicit line to start. Also, add an integer variable called gintSpacePos, which will hold the current position of the "space" on the form.
Once we've got these preliminaries out of the way, let's think about what should happen when the "Start New Game" button is pressed. Basically, we want to randomly rearrange all the tiles, right? Check out the code below...an explanation follows:
We start out by defining a number of variables with a local scope. The first two of these ("i" and "j") are integers that we'll use to navigate the command button array and an associated integer array that we call "intIndexArray." "intRandNum will hold a random number between 1 and 8, which will be used to randomly assign numbers to game spaces. "blnNotFound" and "blnIsInArray" are flags that we'll use to determine processing as we walk through the following code.
The first thing we do is to order the numbers 1 - 8 left to right, up to down (in other words, we put the 8 numbers back into the positions that they were at when we originally set up the form). We also make the first 8 command buttons be visible. For the 9th command button, we make its caption be "" and make it be invisible. This is where the "space" will be located (which we indicate by setting gintSpacePos = 9).
Now for the fun stuff. In the next section of code, we have a nested IF inside a FOR loop, a nested WHILE loop within the nested IF, and a nested FOR loop within the nested WHILE. Whew! The only way I can ever figure this stuff out is to really work through it. So, I set up a table that will track the values of various variables. Here's the one I'm going to use for this example:
Variable |
Value |
i |
1 |
j | 1 |
blnNotFound | True |
blnIsInArray | False |
intRandNum | 0 |
OK, let's see what happens here...
Variable |
Value |
i |
2 |
j | 8 |
blnNotFound | False |
blnIsInArray | True |
intRandNum | 5 |
We now run through the logic again.
Let's go through one more time, OK? Hopefully, by then end of that, you'll really understand...
Get it? We'd continue on in this process eight times (i = 1 to 8). At the end of this processing, we have our eight numbers randomly assigned to the first 8 positions in our game and have a blank space in the ninth position.
If you run the program now, the form will come up with the numbers in displayed in order from 1 - 8 and a blank (although not empty) space in the 9th position. Does that seem right? Notice that if you select Options-->New Game, nothing happens. In fact nothing happens if you hit Options-->Exit or Help-->About either! Why is that? It's simple...we haven't written any sub routines to run when we click on those options...Try this for Options-->New Game and Options-->Exit. You should be able to do Help-->About on your own.
Now run the program again and see what happens when you click on Options-->New Game. You should be able to select it over and over again and get a different random arrangement of the pieces each time.
One last thing before we learn how to move the pieces around...when the game starts up the first time, shouldn't it have a blank space and have the numbers be randomly positioned already? Yes it should. Doing this isn't difficult, since we already have the NewGame routine....the only hard thing is knowing where to put the code. We really want to run our NewGame routine when the program starts up, right?
The good news is that VB provides this opportunity. There is a special sub routine reserved for just this purpose. It is called "Form_Load()." This routine is run whenever the form is loaded...makes sense, right? Now, to get the Form_Load() routine to show up in your code, select "Form" on the drop down menu at the top left of the code window and then select "Load" from the drop down menu at the top left (it should be the default).
Now that the routine is there, take a look at the code below...I've added a couple of extra things in there that are important.
At the bottom, you see that we run the NewGame routine, which is what we started out to do in the first place. Just before that, we run the command called "Randomize." That basically initializes the rnd function so that it doesn't start it's random number generation at the same place all the time...don't sweat this too much, just make sure to always have a "Randomize" statement in your code if you're using the rnd function. Putting it in the Form_Load() routine is also a good idea.
Now, on to the code that centers the form. This code is pretty cool and very useful. I'm not going to take too much time to explain it, but basically, it centers the form in the window on every computer, regardless of the size of the monitor or the resolution. Neat, huh?
OK, I think we're set to move on. Assuming everything is working well in your program so far, you can move on by clicking here or selecting Step 3: Moving Pieces on the navigation bar.