home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AMOS PD CD
/
amospdcd.iso
/
451-475
/
apd460
/
steve_bennett
/
maze.doc
< prev
next >
Wrap
Text File
|
1992-07-26
|
10KB
|
277 lines
*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
DIMENSION A VARIABLE - WHAT DO YOU MEAN?
This was written for people new to AMOS.
*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
This is not designed to be a tutorial on variables. I wrote this to
try and explain in a (I hope) simple way to help newcomers understand
how I used a dimensioned variable to store the data needed for the
'Walk_Around_A_Maze' routine that is stored on the disk.
I think that you will be able to understand all of the code in the
program but many of you may not have needed to use a dimensioned
variable in your own programmes, so this is for you.
Please read through this carefully.
First of all it may be wise to go through what a variable is!
If you ask ten people to explain variables in their own words then
you would get ten different views, but this is in my opinion is the
best way to explain.
Think of a variable as a BOX somewhere in the Amiga's memory. We can
use this box to store numbers or words. Letters/Words are stored in
special type of variables, but because the Maze Routine uses numbers
we will only look at this type of variable.
As I said, a variable is a box that we can store numbers in, but how
do we do it? Well, we first need to give a box a name. This name
can be whatever you wish, except that you can't use AMOS commands for
the box name.
EXAMPLES OF ALLOWED VARIABLE NAMES:
A
TEST
SCORE
LIVES
DEATH
All of these names are ok because they do not contain AMOS commands
in them.
EXAMPLES OF VARIABLE NAMES THAT ARE NOT ALLOWED:
PLAYER - AMOS would read this as (Play)er
IFIT - AMOS would read this as (If)it
THEN_DIE - AMOS would read this as (Then)_die
SHOOTER - AMOS would read this as (Shoot)er
BELLS - AMOS would read this as (Bell)s
So you can see that these names are not allowed beacuse AMOS would
read the first part of each name as a command.
OK! So now we know what names we can and can't use for our variable
box name, but how do we put a number inside a box.
This is simple. Just give a box a name and put the number straight
inside it.
EXAMPLES:
A=10 - Puts the value 10 inside a variable box called 'A'
LIVES=5 - Puts " " 5 " " " " " 'LIVES'
SCORE=2000 - Puts " " 200 " " " " " 'SCORE'
But how do we find out whats inside a variable box?
Well the best way is to use the 'Print' command to print out a
variables value direct to the screen. So if we had a program like
this -
A=10
B=20
Print A+B
All we are saying is -
1. Put a value of 10 inside a variable box called 'A'
2. Put a value of 20 inside a variable box called 'B'
3. Add the value inside box 'A' to the value inside box 'B' and
1print out the answer - 30 - 10+20.
You can print out the contents of any variable you want by just
typing - Print ('Variable Box Name')
Variables make life easy for programmers because it allows us to
store a value inside a box with a certain name. So if we have a
variable called 'SCORE', if we wanted to increase the contents of
'SCORE' by 1000 because we have got a bonus or something in a game,
we simple type -
SCORE=SCORE+1000
If you want to use the 'Add' command to do the same thing you can -
Add SCORE,1000
Both of these methods simply add 1000 to the present contents of the
variable 'SCORE'. So -
If SCORE had a value of 0 when we updated it, we would get -
0 + 1000 =1000
If SCORE had a value of 100 when we updated it, we would get -
100 + 1000 =1100
And so on.
I hope that you understood all that!.
Simply remember that a variable is simply a box that we can store
values in.
We can print out the values inside these variable boxes using
'Print'. We can add the contents of variable boxes together -
A+B+SCORE And so on.
Now then, we go on to Dimensioned Variables. At first these may
sound difficult, but they are not as hard to understand as you may
think.
Now we know that a variable can be thought of as a box. Well we can
think of a dimensioned variable as a box full of boxes. What do I
mean? Well -
Lets say for example we have a variable called 'STEVE'. If we then
dimension 'STEVE' by 10 by using the line -
Dim STEVE(10)
We would have a variable box called STEVE, but inside that box we
would have 10 smaller boxes. Now we call each of these boxes by the
same variable name, but we use a number to tell AMOS which little box
we are looking at.
EXAMPLE:
If we dimension 'STEVE' by 10, we would have a variable box called
STEVE with these other little boxes inside -
STEVE(0) Would be the big box
STEVE(1) Would be the first little box inside the big one.
STEVE(2) Would be the second little box inside the big one.
STEVE(3) Would be the third little box inside the big one.
STEVE(10) Would be the tenth little box inside the big box.
And so on.
So instead of having just 1 variable called STEVE, we now have 11
variables called STEVE From STEVE(0) To STEVE(10).
Each one of these smaller boxes can store values just the same, all
we do is use a number after the variable name to ensure the correct
value goes in the correct box.
EXAMPLES:
STEVE(2)=50-Puts the value of 50 inside the variable called STEVE(2).
STEVE(9)=90-Puts the value of 90 inside the variavle called STEVE(9).
Because we ony dimensioned STEVE by 10, we can only use boxes from -
STEVE(0) To STEVE(10) If you try to store a number inside STEVE(11)
you will get an error, because we only created 10 little boxes to
start with. If you wanted to use higher numbers for the little boxes
you need to dimension them to a higher number to start with -
EXAMPLES:
Dim STEVE(20)-Gives us 21 variable boxes called STEVE(0) To STEVE(20)
Dim STEVE(50)-Gives us 51 variable boxes called STEVE(0) To STEVE(50)
If you are still with me then the next bit will be the hardest to
understand.
Because the MAZE Routine on the disk uses a 18 X 14 grid for the Maze
layout, we need to dimension a variable that can store all the
possible positions our PacMan could be on the grid. This means that
we need a variable that can store every grid position from -
0 Squares Down - 0 Squares Right
To
14 Squares Down - 18 Squares Right
But how do we do it?
First of all we need to find a name for our variable, lets call it
CHECK_SQUARE. Now we need to hold 14 possible grid positions Down
the screen so we would dimension the variable CHECK_SQUARE by 14 -
Dim CHECK_SQUARE(14)
But hold it! That would mean we could only store 15 grid positions
in CHECK_SQUARE - CHECK_SQUARE(0) To CHECK_SQUARE(14) - Thats no
good. What we need is a variable that can store all the grid
positions we need - 14 X 18 = 252 possible positions on the grid. So
how do we do that?
Quite simply by adding another number to our Dimension command.
We know that we have 14 grid positions Down the maze and we used -
Dim CHECK_SQUARE(14)
to hold these values, so now we add the number 18 to the array to
hold the possible positions accross the screen.
What we have now is a Dimensioned variable command like this -
Dim CHECK_SQUARE(14,18)
Again, dont look at it in and think 'Heeeeeellllppppp', its just a
way of creating more little boxes inside the variable CHECK_SQUARE.
We know that if we dimension CHECK_SQUARE by 10, we get 11 variable
boxes -
CHECK_SQUARE(0) CHECK_SQUARE(1)
CHECK_SQUARE(2) CHECK_SQUARE(3)
CHECK_SQUARE(4) CHECK_SQUARE(5)
CHECK_SQUARE(6) CHECK_SQUARE(7)
CHECK_SQUARE(7) CHECK_SQUARE(9)
CHECK_SQUARE(10)
Now if we add another number to the value - say 3, we get -
Dim CHECK_SQUARE(3,10)
This would give us 44 variable boxes. These would range from -
CHECK_SQUARE(0,0) To CHECK_SQUARE(3,10)
EXAMPLES:
CHECK_SQUARE(0,0)=10
CHECK_SQUARE(0,1)=20
CHECK_SQUARE(1,2)=30
CHECK_SQUARE(2,0)=40
CHECK_SQUARE(3,1)=50
CHECK_SQUARE(3,10)=60
ETC.
In my Maze routine I didnt use (0,0) - (0,9). Instead I started from
(1,1) which holds the value of the Top Left Hand Corner of my Grid.
It would have made more sense if I had started from (0,0) because you
could then say grid position (0,0) is the Top corner, and (1,1) is 1
right 1 down, but no one said I was perfect.
At first what I have written here will look difficult, but the thing
to do is to load in AMOS and practice. I would print out this .Doc
so that you can read through it as you work. You will find that
within 10 minutes you will understand dimensioned variables and how
they work.
Start off by dimensioning a variable by 10 - Say Dim A(10). Then in
your program give each variable in that array a number and print it
out on screen.
EXAMPLE:
Dim A(10) : Rem Gives us 11 little variable boxes to use
For T=0 To 10 : Rem Set up a loop to count from 0 To 10
R=Rnd(50) : Rem Give 'R' a RaNDom number from 0 To 50
A(T)=R : Rem Put the value of 'R' inside Variable A(T)
Print A(T) : Rem Print out the contents of Variable A(T)
Next T : Rem Continue the loop until T=10
The program is just a simple one to show you where to begin. All it
does is to create 11 variable boxes named A(0) To A(10). It then
puts a random number inside each of these 11 boxes in turn -
A(0),A(1),A(2),A(3) - etc and prints out the contents of that
variable box.
Dont rely on me to show you too much, have a go yourself, its easier
that it first looks.
Steve Bennett.
*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*