home *** CD-ROM | disk | FTP | other *** search
/ AMOS PD CD / amospdcd.iso / 451-475 / apd460 / steve_bennett / maze.doc < prev    next >
Text File  |  1992-07-26  |  10KB  |  277 lines

  1. *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
  2.  
  3.               DIMENSION A VARIABLE - WHAT DO YOU MEAN?
  4.  
  5.               This was written for people new to AMOS.
  6.  
  7. *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
  8.  
  9.  This  is not designed to be a tutorial on variables.  I wrote this to
  10. try and explain in a (I hope) simple way to help newcomers understand
  11. how  I  used  a dimensioned variable to store the data needed for the
  12. 'Walk_Around_A_Maze' routine that is stored on the disk.
  13.  
  14. I  think  that  you will be able to understand all of the code in the
  15. program  but  many  of  you  may not have needed to use a dimensioned
  16. variable in your own programmes, so this is for you.
  17.  
  18. Please read through this carefully.
  19.  
  20. First of all it may be wise to go through what a variable is!
  21.  
  22. If  you  ask  ten people to explain variables in their own words then
  23. you  would  get ten different views, but this is in my opinion is the
  24. best way to explain.
  25.  
  26. Think of a variable as a BOX somewhere in the Amiga's memory.  We can
  27. use  this box to store numbers or words.  Letters/Words are stored in
  28. special  type of variables, but because the Maze Routine uses numbers
  29. we will only look at this type of variable.
  30.  
  31. As  I said, a variable is a box that we can store numbers in, but how
  32. do  we  do  it?  Well, we first need to give a box a name.  This name
  33. can be whatever you wish, except that you can't use AMOS commands for
  34. the box name.
  35.  
  36. EXAMPLES OF ALLOWED VARIABLE NAMES:
  37.  
  38. A
  39. TEST
  40. SCORE
  41. LIVES
  42. DEATH
  43.  
  44. All  of  these names are ok because they do not contain AMOS commands
  45. in them.
  46.  
  47. EXAMPLES OF VARIABLE NAMES THAT ARE NOT ALLOWED:
  48.  
  49. PLAYER      - AMOS would read this as (Play)er
  50. IFIT        - AMOS would read this as (If)it
  51. THEN_DIE    - AMOS would read this as (Then)_die
  52. SHOOTER     - AMOS would read this as (Shoot)er
  53. BELLS       - AMOS would read this as (Bell)s
  54.  
  55. So  you  can  see that these names are not allowed beacuse AMOS would
  56. read the first part of each name as a command.
  57.  
  58. OK!   So now we know what names we can and can't use for our variable
  59. box name, but how do we put a number inside a box.
  60.  
  61. This  is  simple.  Just give a box a name and put the number straight
  62. inside it.
  63.  
  64. EXAMPLES:
  65.  
  66. A=10         - Puts the value 10 inside a variable box called 'A'
  67. LIVES=5      - Puts  "    "   5    "    "    "      "    "    'LIVES'
  68. SCORE=2000   - Puts  "    "   200  "    "    "      "    "    'SCORE'
  69.  
  70. But how do we find out whats inside a variable box?
  71.  
  72. Well  the  best  way  is  to  use  the 'Print' command to print out a
  73. variables  value  direct  to the screen.  So if we had a program like
  74. this -
  75.  
  76. A=10
  77. B=20
  78. Print A+B
  79.  
  80. All we are saying is -
  81. 1. Put a value of 10 inside a variable box called 'A'
  82. 2. Put a value of 20 inside a variable box called 'B'
  83. 3. Add  the  value  inside  box 'A' to the value inside box 'B' and
  84. 1print out the answer - 30 - 10+20.
  85. You  can  print  out  the  contents  of any variable you want by just
  86. typing - Print ('Variable Box Name')
  87.  
  88. Variables  make  life  easy  for  programmers because it allows us to
  89. store  a  value  inside  a  box with a certain name.  So if we have a
  90. variable  called  'SCORE',  if  we wanted to increase the contents of
  91. 'SCORE'  by  1000 because we have got a bonus or something in a game,
  92. we simple type -
  93.  
  94. SCORE=SCORE+1000
  95.  
  96. If you want to use the 'Add' command to do the same thing you can -
  97.  
  98. Add SCORE,1000
  99.  
  100. Both  of these methods simply add 1000 to the present contents of the
  101. variable 'SCORE'.  So -
  102.  
  103. If SCORE had a value of 0 when we updated it, we would get -
  104. 0 + 1000 =1000
  105.  
  106. If SCORE had a value of 100 when we updated it, we would get -
  107. 100 + 1000 =1100
  108.  
  109. And so on.
  110.  
  111. I hope that you understood all that!. 
  112. Simply  remember  that  a  variable is simply a box that we can store
  113. values in.
  114.  
  115. We  can  print  out  the  values  inside  these  variable boxes using
  116. 'Print'.   We  can  add  the  contents  of  variable boxes together -
  117. A+B+SCORE And so on.
  118.  
  119. Now  then,  we  go  on  to Dimensioned Variables.  At first these may
  120. sound  difficult,  but  they are not as hard to understand as you may
  121. think.
  122.  
  123. Now  we know that a variable can be thought of as a box.  Well we can
  124. think  of  a  dimensioned variable as a box full of boxes.  What do I
  125. mean?  Well -
  126.  
  127. Lets  say  for example we have a variable called 'STEVE'.  If we then
  128. dimension 'STEVE' by 10 by using the line -
  129.  
  130. Dim STEVE(10)
  131.  
  132. We  would  have  a  variable box called STEVE, but inside that box we
  133. would  have 10 smaller boxes.  Now we call each of these boxes by the
  134. same variable name, but we use a number to tell AMOS which little box
  135. we are looking at.
  136.  
  137. EXAMPLE:
  138.  
  139. If  we  dimension  'STEVE' by 10, we would have a variable box called
  140. STEVE with these other little boxes inside -
  141.  
  142. STEVE(0)  Would be the big box
  143. STEVE(1)  Would be the first  little box inside the big one.
  144. STEVE(2)  Would be the second little box inside the big one.
  145. STEVE(3)  Would be the third  little box inside the big one.
  146. STEVE(10) Would be the tenth  little box inside the big box.
  147. And so on.
  148.  
  149. So  instead  of  having  just 1 variable called STEVE, we now have 11
  150. variables called STEVE From STEVE(0) To STEVE(10).
  151.  
  152. Each  one  of these smaller boxes can store values just the same, all
  153. we  do  is use a number after the variable name to ensure the correct
  154. value goes in the correct box.
  155.  
  156. EXAMPLES:
  157.  
  158. STEVE(2)=50-Puts the value of 50 inside the variable called STEVE(2).
  159. STEVE(9)=90-Puts the value of 90 inside the variavle called STEVE(9).
  160.  
  161. Because  we ony dimensioned STEVE by 10, we can only use boxes from -
  162. STEVE(0)  To  STEVE(10) If you try to store a number inside STEVE(11)
  163. you  will  get  an  error, because we only created 10 little boxes to
  164. start with.  If you wanted to use higher numbers for the little boxes
  165. you need to dimension them to a higher number to start with -
  166.  
  167. EXAMPLES:
  168.  
  169. Dim STEVE(20)-Gives us 21 variable boxes called STEVE(0) To STEVE(20)
  170. Dim STEVE(50)-Gives us 51 variable boxes called STEVE(0) To STEVE(50)
  171.  
  172. If  you  are  still  with me then the next bit will be the hardest to
  173. understand.
  174.  
  175. Because the MAZE Routine on the disk uses a 18 X 14 grid for the Maze
  176. layout,  we  need  to  dimension  a  variable  that can store all the
  177. possible  positions our PacMan could be on the grid.  This means that
  178. we need a variable that can store every grid position from -
  179.  
  180. 0 Squares  Down - 0 Squares Right
  181. To
  182. 14 Squares Down - 18 Squares Right
  183.  
  184. But how do we do it?
  185.  
  186. First  of  all  we need to find a name for our variable, lets call it
  187. CHECK_SQUARE.   Now  we  need to hold 14 possible grid positions Down
  188. the screen so we would dimension the variable CHECK_SQUARE by 14 -
  189.  
  190. Dim CHECK_SQUARE(14)
  191.  
  192. But  hold  it!  That would mean we could only store 15 grid positions
  193. in  CHECK_SQUARE  -  CHECK_SQUARE(0)  To  CHECK_SQUARE(14) - Thats no
  194. good.   What  we  need  is  a  variable  that  can store all the grid
  195. positions we need - 14 X 18 = 252 possible positions on the grid.  So
  196. how do we do that?
  197.  
  198. Quite simply by adding another number to our Dimension command.
  199. We know that we have 14 grid positions Down the maze and we used -
  200. Dim CHECK_SQUARE(14)
  201. to  hold  these  values,  so now we add the number 18 to the array to
  202. hold the possible positions accross the screen.
  203.  
  204. What we have now is a Dimensioned variable command like this -
  205.  
  206. Dim CHECK_SQUARE(14,18)
  207.  
  208. Again,  dont  look  at it in and think 'Heeeeeellllppppp', its just a
  209. way of creating more little boxes inside the variable CHECK_SQUARE.
  210.  
  211. We  know  that if we dimension CHECK_SQUARE by 10, we get 11 variable
  212. boxes -
  213.  
  214. CHECK_SQUARE(0)   CHECK_SQUARE(1)
  215. CHECK_SQUARE(2)   CHECK_SQUARE(3)
  216. CHECK_SQUARE(4)   CHECK_SQUARE(5)
  217. CHECK_SQUARE(6)   CHECK_SQUARE(7)
  218. CHECK_SQUARE(7)   CHECK_SQUARE(9)
  219. CHECK_SQUARE(10)
  220.  
  221. Now if we add another number to the value - say 3, we get -
  222.  
  223. Dim CHECK_SQUARE(3,10)
  224.  
  225. This would give us 44 variable boxes. These would range from -
  226.  
  227. CHECK_SQUARE(0,0) To CHECK_SQUARE(3,10)
  228.  
  229. EXAMPLES:
  230.  
  231. CHECK_SQUARE(0,0)=10
  232. CHECK_SQUARE(0,1)=20
  233. CHECK_SQUARE(1,2)=30
  234. CHECK_SQUARE(2,0)=40
  235. CHECK_SQUARE(3,1)=50
  236. CHECK_SQUARE(3,10)=60
  237.  
  238. ETC.
  239.  
  240. In my Maze routine I didnt use (0,0) - (0,9).  Instead I started from
  241. (1,1)  which  holds the value of the Top Left Hand Corner of my Grid.
  242. It would have made more sense if I had started from (0,0) because you
  243. could  then say grid position (0,0) is the Top corner, and (1,1) is 1
  244. right 1 down, but no one said I was perfect.
  245.  
  246. At  first what I have written here will look difficult, but the thing
  247. to  do  is to load in AMOS and practice.  I would print out this .Doc
  248. so  that  you  can  read  through it as you work.  You will find that
  249. within  10  minutes you will understand dimensioned variables and how
  250. they work.
  251.  
  252. Start  off by dimensioning a variable by 10 - Say Dim A(10).  Then in
  253. your  program  give each variable in that array a number and print it
  254. out on screen.
  255.  
  256. EXAMPLE:
  257.  
  258. Dim A(10)      : Rem Gives us 11 little variable boxes to use
  259. For T=0 To 10  : Rem Set up a loop to count from 0 To 10
  260.    R=Rnd(50)   : Rem Give 'R' a RaNDom number from 0 To 50
  261.    A(T)=R      : Rem Put the value of 'R' inside Variable A(T)
  262.    Print A(T)  : Rem Print out the contents of   Variable A(T) 
  263. Next T         : Rem Continue the loop until T=10
  264.  
  265. The  program is just a simple one to show you where to begin.  All it
  266. does  is  to  create  11 variable boxes named A(0) To A(10).  It then
  267. puts  a  random  number  inside  each  of  these  11  boxes in turn -
  268. A(0),A(1),A(2),A(3)  -  etc  and  prints  out  the  contents  of that
  269. variable box.
  270.  
  271. Dont  rely on me to show you too much, have a go yourself, its easier
  272. that it first looks.
  273.  
  274. Steve Bennett.
  275.  
  276. *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
  277.