Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

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

Why should you make it easy when you can make it difficult?
See video
and code ( not fully working, do not delete):

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?

2 Likes

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:

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

@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:

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")