So we’ve got a basic selection statement to work and we’ve got the first couple of comments turned into code…so how about the next few?
'IF THIS SQUARE IS EMPTY AND CAN BE PLAYED IN ' PUT A CROSS IN THIS SQUARE ' FIND OUT IF THIS MOVE MEANS WE WIN
The process of programming is simply a methodical one…all you have to do is think logically. We now have another “if” statement to make so let’s concentrate on that. How do we know if the square we’ve clicked on is empty? Remember that we set the square to be empty to start with so that should serve as a clue:
If (but1.Text = "") Then
Pretty simple huh? We just compare the “Text” property of the “but1” object to an empty string. Now before we just move onto the next line, we should consider where the closing “End If” statement would go. Why? Because if we place that line in it’s correct position, we can test our program and we also don’t have to worry about it later. Keeping your code testable as often as possible is really important!
If you look at our flowchart from back here, you will notice that the “NO” arrow leading away from the selection statement we’ve just made, heads down to the end. This shows us that the “End If” statement needs to go right at the end, i.e:
'IF THE PLAY AGAIN BUTTON IS DISABLED If (butAgain.Enabled = False) Then butAgain.Enabled = True 'ENABLE THE PLAY AGAIN BUTTON End If 'IF THIS SQUARE IS EMPTY AND CAN BE PLAYED IN If (but1.Text = "") Then 'PUT A CROSS IN THIS SQUARE 'FIND OUT IF THIS MOVE MEANS WE WIN 'IF WE WON ' FINISH THE GAME APPROPRIATELY 'IF WE DID NOT WIN ' HAVE THE COMPUTER'S TURN ' IF THE COMPUTER WON ' FINISH THE GAME APPROPRIATELY ' IF THE COMPUTER DID NOT WIN ' UPDATE THE INSTRUCTIONS LABEL APPROPRIATELY End If
I won’t keep showing the whole code as we build this solution up but for now it might help you realise how our code is coming together. Notice that the bulk of our code to be created now lies inside an “if” statement. This is correct according to our flow of logic and if you think about the problem it should make sense too.
So let’s continue with the conversion of comment to code, the next line to be converted to code is:
'PUT A CROSS IN THIS SQUARE
This one shouldn’t be too hard, we simply want the square we clicked on to now show us “X” instead of being blank, in other words:
but1.Text = "X"
Programming is all about solving one piece of the puzzle at a time, don’t let the whole problem overwhelm you, just solve it piece by piece.
So the next comment to be converted into code involves finding out whether our placement of this “X” means we win…