# Зачем просто, если можно сложно: Sarven Sentry help

**URL:** <https://discourse.codecombat.com/t/sarven-sentry-help/20134>\
**Category:** Level Help\
**Created:** [January 30, 2020, 3:48pm UTC](https://discourse.codecombat.com/t/sarven-sentry-help/20134 "2020-01-30T15:48:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [January 30, 2020, 3:48pm UTC](https://discourse.codecombat.com/t/sarven-sentry-help/20134/1 "2020-01-30T15:48:08Z")

</div>

Why should you make it easy when you can make it difficult?  
See [video](https://player.vimeo.com/video/388253380?autoplay=1)  
and code ( not fully working, do not delete):

```python
points = [{"id": "Fidsdale", "x": 20, "y": 31, "fl": 0},
           # put into points array all archer ids and 
           # x, y points where the hero must build things
             ]
             
speakers = []
things = []
def onHear(event):
    message = event.message
    speakers.append(event.speaker)
    replacements = [',', '!', '!','.', '–'] # '–' very obscure char
    for r in replacements:
        message = message.replace(r, '')
    words = message.split(" ")
    word = [e for e in words if e in ["Yak", "Ogre"]][0]
    if word == "Yak":
        things.append("fence")
    elif word == "Ogre":
        things.append("fire-trap")
        
        
pet.on("hear", onHear);
i_builts = 0;

while True:
    length = len(speakers)
    if length:
        speaker = speakers[length-1]
    while speaker:
        for point in points:
            if point.id == speaker.id:
                while not point.fl:
                    if hero.built.length == i_builts:
                        hero.buildXY(things[length-1], point.x, point.y)
                    else:
                        point.fl = hero.built.length
                        i_builts +=1
                        break;
            
        break;  
    hero.move(Vector(39, 37));

```

The code is too complicated and hard to explain. Can you help me to simplify it ( keeping the original idea - No Flags ) and make it comprehensible by a 10 years old?

---

<div class="post-metadata">

**Author:** ![Alexbrand](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Alexbrand](https://discourse.codecombat.com/u/Alexbrand)\
**Post date:** [January 30, 2020, 8:55pm UTC](https://discourse.codecombat.com/t/sarven-sentry-help/20134/2 "2020-01-30T20:55:17Z")

</div>

Oh, man, this is genious… I understand the idea and, mainly, what code does. But I couldn’t be able to wright such solution) The elegance of conception amuses me. Очень круто.

About simplifying - at first look all I can see is that: maybe it’s easier to define all build points and then choose one which is closer to “speaker”? Something like:

```python
BuildPoints = [BuildPoint1, BuildPoint2, BuildPoint3]
BuildPoint = speaker.findNearest(BuildPoints)
hero.buildXY(things[length-1], BuildPoint.x,BuildPoint.y)

```

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [February 1, 2020, 8:53pm UTC](https://discourse.codecombat.com/t/sarven-sentry-help/20134/3 "2020-02-01T20:53:18Z")

</div>

@Alexbrand : The position of every named archer and and the nearest “thing” position are static, not dynamic, so no need to add more complexity. (Personally I will prefer the names of the friends and enemies to be absolutely random. There will be no default and user code that works only in limited cases, far less confused users from hero.attack(“String”))  
There were errors in the first code, check them in the edit history. Initially wrote the code in javascript -check if a word exists in a sentence was easy  
if ( message.search(“fence”) != -1)  
but it in python I had unexpected errors even trying different approaches ( I quit CoCo python and python very quickly sensing it was not “my” language)

onHear with wordInText(text, word) function stolen from another level:

```python
def wordInText(text, word):
    for i in range(len(text) - len(word) + 1):
        for j in range(len(word)):
            shiftedIndex = i + j
            if text[shiftedIndex] != word[j]:
                break
            if j == len(word) - 1:
                return True
    return False

def onHear(event):  
    message = event.message
    speakers.append(event.speaker)
    if wordInText(message,"Yak"):
        things.append("fence")
    elif wordInText(message,"Ogre"):
        things.append("fire-trap")

```
