We spoke last time about using string variables in place of often-used character sequences. For example, a settings file name. The advantage is that if you decide to change them in the future you can do so in one fell swoop without having to trace mentions throughout a large program. The same applies to often used numbers as well. For example, you might want to adapt the MiLo program to handle different number limits for other lotteries, or to handle a different number of balls.
The principle is the same as for strings; we define a variable of the appropriate type to hold the number and assign it somewhere up at the top of the code where we can be sure to find it again easily in future. Then it's just a matter of going through the program replacing the hard-coded numbers with the variable name.
As a first example of this, let's give MiLo the ability to simulate a lottery where the drawn numbers are only from 1 to 40. At the top of the program, define a new global variable:
GLOBAL maxnum% rem Max number that can be on a ball
and then a few lines down set it to a value:
maxnum%=40
You don't need to be psychic to work out that the next place
we're headed is the picknum: procedure. Locate it and change the 50 to maxnum%
as shown in the screenshot:
That should be the only place we mentioned this number, so go
ahead and translate MiLo and verify that the new set of numbers is now always
between 1 and 40. If you were to go back and change the variable to 70, you'd
see numbers up to that limit and so on...
By the way, if any experienced programmers are reading this as saying to themselves: "There's a better way still to handle these numbers, using Constants", then you're dead right. But I'll come onto those when this series goes OPL/32-only in the future. For the moment, I'm trying very hard to make everything work on OPL/16 and OPL/32, with no code changes whatsoever.
Let's complete the job by making a similar variable for the number of balls to be drawn in the lottery. Make a new global integer variable and set it up with the two lines:
GLOBAL nballs% rem Number of balls to be drawn
nballs%=6
This time our first destination will be the generate:
procedure. Change the comment at the top to not mention the number 6
specifically:
REM Generate a set of unique, random lottery numbers and display!
Now replace both limits on the WHILE loop to be nballs% rather than 6, as shown in the screenshot:
You'll also need to find the sort: procedure and make a
similar change:
i%=1 :WHILE i%<=(nballs%-1) rem (nballs%-1) possible comparisons etc etc.
Note that it's (nballs%-1) because we're replacing the number 5.
In other words, the number here needed to be one less than the number of balls
drawn.
If you're really on the ball, you'll note that there are three possible problems to what we've just done. Firstly, our numbers%() array was only defined to be 6 long, meaning that something truly awful is bound to happen if we make nballs% greater than 6! The second problem is that the code which saves the numbers away for posterity is also hard-coded to use a maximum of 6 numbers at present. Finally, having more than six numbers means that the screen area cleared to display them will no longer be large enough.
It's also worth noting that you'll have problems if you set nballs% too
low, to 1 or even zero.
For the moment, though, just keep nballs% to between 2 and 6. Set it to
a few values, play around with maxnum% as well and have some fun checking that
everything still works OK! Next week, we'll look at ways to handle all the
possible problems mentioned above and talk in general about making bomb-proof
programs.
See you then!
Go to next lesson | Programming index