@Xector64 to answer a few of your questions:
.1. Let us define a variable called enemies
and set it equal to the available enemies in the game
enemies = hero.findEnemies()
Now that enemies
is defined. We check a property of the array called length
. If an array has anything in it then the length
will be > 0.
if enemies.length > 0:
hero.say( "There are " + enemies.length + " enemies here with me in the game!")
To speak directly about the word “index”, I would believe it is correct to say that there really is only one “index”, however there are many “positions” in that index.
define index: an alphabetical list of names, subjects, etc., with references to the places where they occur, typically found at the end of a book.
So in our programming “index” we have a list of numbers. 0,1,2,3 … depending on the number of elements in the array. Again I believe it is correct to ask, “Where in the arrays index is the "thrower"
enemy?”
So when we use variables we are actually storing the location in the index. We use the name index as part of the variable name to help us define the purpose behind the variable. For example:
enemies = hero.findEnemies()
enemyIndex = 0
while (enemyIndex < enemies.length):
if (enemies[ enemyIndex ].type == "thrower" ):
hero.say("The thrower enemy is at index position " + enemyIndex )
enemyIndex += 1
The reason why we initialize the variable to 0 is that the arrays first position in the index is zero.
.2. Lets start by saying that we have a classroom full of students and they are all very disorderly. Now what if you could magically make them all line up in a row? This would be similar to the code segment we have in the game called hero.findFriends()
it essentially goes out and creates a list of all the ally game characters.
So now that we have them all lined up they are just like the array. Lets say we like counting from left to right, then we can say that the student in the first spot, lets count from 0, is the students[0]
student. And so fourth. Well referring to Mark as students[0] will become very odd, especially if we ask him to read out loud from the current chapter on Computer Science.
Imagine it, “students[0] please stand up and read Chapter 2 Arrays starting from how we define them”. Mark will surely feel like less of a person being called students[0]. But Mark is still the first student in our line, he is the one furthest on the left. So even though this isn’t how we would refer to him in real life, in the world of programming it is as valid.
Now lets be a good teacher and choose to create a variable named Mark and set that equal to students[0] because after all they are one in the same person and Mark seems to like the idea of having a variable refer to him that was named after him!
mark = students[0]
Now in our code anywhere we wish to talk about Mark we can use our variable mark
. Think about it though, couldn’t we just as easily named the variable student
? In this case it refers to a single student and not the entire class list. This is the same as in our game. We are just asking to select 1 enemy from the list of many enemies. An array is a “list” of things.
If you need to, think of the array as a bunch of box cars on a locomotive train. In our case each boxcar only holds one student (or enemy). enemies[0]
or enemies[ enemyIndex ]
where enemyIndex = 0
both refer to the contents stored in the array at that location. Like box car number 0. Tell me what is in boxcar number 0.
.3. Let me ask you this question, “If you fail to increment the position in the array, then what happens to your logic statement that is looked at that terminates the array?”
enemyIndex < len(enemies)
if enemyIndex = 0
and len(enemies)
is equal to 10 because there were 10 students in our class. What happens if I never increment enemyIndex
?
0 < 10
is always true and the loop will never end.
.4. If you still have questions please ask I would be glad to further give examples.