# Harrowland - conceptual issues, multiple loops

**URL:** <https://discourse.codecombat.com/t/harrowland-conceptual-issues-multiple-loops/6694>\
**Category:** Uncategorized\
**Created:** [February 6, 2016, 1:00am UTC](https://discourse.codecombat.com/t/harrowland-conceptual-issues-multiple-loops/6694 "2016-02-06T01:00:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![George\_Christovich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/george_christovich/32/2839_2.png) [@George\_Christovich](https://discourse.codecombat.com/u/George_Christovich)\
**Post date:** [February 6, 2016, 1:00am UTC](https://discourse.codecombat.com/t/harrowland-conceptual-issues-multiple-loops/6694/1 "2016-02-06T01:00:09Z")

</div>

First, I haven’t posted any code in this topic because I am trying to understand the conceptual issues.

Second, I am working on Harrowland. My assumptions are 1) the Simple CPU is set to be approximately equal to whatever character I use. 2) the Simple CPU is set to run a simple, not terribly efficient set of loops 3) the problem is designed to test for the ability to write a more efficient loop than the Simple CPU is using.

Third) I have come close to winning, but have not won.

Fourth) I think the solution is to sort the enemies to first ignore the yaks, second attack the throwers, third reduce the remaining sub-enemies and finally to attack the Hero.

I only know the type of “sand-yak” and “archer”. I don’t know the type of the other sub-enemies or the hero.

I am only using “loop” and “while”. I don’t know if I missed a lesson on “for” loops or if they are being reserved for later lessons. I think that the “for” loop would be helpful here, but I could be wrong.

The best solution that I can imagine, but I don’t know how to execute, is to sort the Hero and the sand-yaks out of “enemies = self.findEnemies()” and create a second array of the remaining sub-enemies. Once, I had cycled through the second array, I could attack the Hero.

I have tried to simulate that tactic with successive loops. I am not having very much luck. Assuming that the remarks above indicate that I understand the problem, I have some specific questions.

1. What are the types of the enemies in the problem and how can I identify them myself?
2. Should I be using “for” statements to solve the problem, and where do I find the information on them?
3. Is it possible to implement nested “loop” statements efficiently?
4. Is somebody willing to discuss how “while” loops end?

Thank you very much,  
George34

---

<div class="post-metadata">

**Author:** ![AdrianCgw](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@AdrianCgw](https://discourse.codecombat.com/u/AdrianCgw)\
**Post date:** [February 6, 2016, 3:18am UTC](https://discourse.codecombat.com/t/harrowland-conceptual-issues-multiple-loops/6694/2 "2016-02-06T03:18:14Z")

</div>

1. 

```python
enemyDef.type=["catapult","munchkin","scout","thrower","chieftain","witch","ogre",
	"warlock","fangrider","headhunter","brawler","shaman","skeleton","tower",
     "decaying-skeleton","decaying-shaman","burl","sand-yak","ice-yak","yeti","door"];

```

Test the enemy type like this:

```python
if enemy.type is not "yak":

```

Enemy hero or your hero is different. There can be of many types “captain”, “knight” "librarian"  
Instead we use their id:  
The Human hero is always Hero Placeholder  
The Ogre hero is always Hero Placeholder 1

```python
if enemy.id is "Hero Placeholder 1":

```

1. [http://www.tutorialspoint.com/python/python\_for\_loop.htm](http://www.tutorialspoint.com/python/python_for_loop.htm)

Basically there are two types of loops.

1. if you want just to get an element from an array:

```python
for soldier in soldiers:
    self.command(soldier,...

```

1. If you want to do something for each value of i from 2 to N-1 (!!! stops before N)

```python
for i in range(2,N):

```

A specific case is when you want to iterate over a list but you also want to know the exact position you are in the list.  
len(my\_list) says how long is your list

```python
for i in range(len(soldiers)):
  self.command(soldiers[i],"defend",my_points[i]

```

Here we have two lists and we want the 3rd soldier to go to the 3rd point in my list of points.

1. 

If you use a loop you need to break out of it:

```python
loop:
   #do stuff
   if something_happend:
        break;

```

Generally you can implement it with a while. **Loop** is codeCombat construction, in reallity is equivalent to while(true)

```python
while (something_no_yet_happened)
   do stuff 

```

After your code is executed a test is performed to see if the something\_no\_yet\_happened is true, If it is true you get out of the loop

1. 

```python
enemies =self.findEnemies()
i=0
while (i < len(enemies)):
    enemy=enemies[i] #start looking at the enemy on the position i  
    while(enemy.health>0): #not dead yet
        self.attack(enemy)
   i++ #add 1 to i so in the next loop you'll look at the next enemy in list

```

---

<div class="post-metadata">

**Author:** ![AdrianCgw](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@AdrianCgw](https://discourse.codecombat.com/u/AdrianCgw)\
**Post date:** [February 6, 2016, 3:22am UTC](https://discourse.codecombat.com/t/harrowland-conceptual-issues-multiple-loops/6694/3 "2016-02-06T03:22:39Z")

</div>

In harrowland the enemy hero has the same equipment as your hero and it always attacks his nearest enemy.

You have to do better than this:

1. attack a better target
2. or use equipment with special properties: cleave, shield bash, ranger’s throws, wizard’s spells etc

---

<div class="post-metadata">

**Author:** ![George\_Christovich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/george_christovich/32/2839_2.png) [@George\_Christovich](https://discourse.codecombat.com/u/George_Christovich)\
**Post date:** [February 6, 2016, 3:35am UTC](https://discourse.codecombat.com/t/harrowland-conceptual-issues-multiple-loops/6694/4 "2016-02-06T03:35:32Z")

</div>

Hoo ah!

Your answers are always very useful.

Thank you very much.

George34

---

<div class="post-metadata">

**Author:** ![Hinkle](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hinkle/32/3967_2.png) [@Hinkle](https://discourse.codecombat.com/u/Hinkle)\
**Post date:** [February 6, 2016, 3:56am UTC](https://discourse.codecombat.com/t/harrowland-conceptual-issues-multiple-loops/6694/5 "2016-02-06T03:56:56Z")

</div>

It may be worth checking out my [posters on this forum](https://discourse.codecombat.com/t/code-combat-posters-and-curriculum-standards-assessments-and-lesson-plans/6011) (post 2)

It has lost of example code and this (although I am missing some types):

 ![](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/9/99f0186e6be1541647e3ba21537e211b15a77573.png)

---

<div class="post-metadata">

**Author:** ![George\_Christovich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/george_christovich/32/2839_2.png) [@George\_Christovich](https://discourse.codecombat.com/u/George_Christovich)\
**Post date:** [February 6, 2016, 4:16am UTC](https://discourse.codecombat.com/t/harrowland-conceptual-issues-multiple-loops/6694/6 "2016-02-06T04:16:50Z")

</div>

Thank you Hinkle.

The documents and the posters are very helpful. I am doing this with my son (11), and I thought that I was going to have to generate some kind of guide for him. These are far, far better than anything I could hope to do with the time I have available. Thanks for making them available.

Thanks,  
George34
