# STUPID CODE WONT WORK(sowing fire help)

**URL:** <https://discourse.codecombat.com/t/stupid-code-wont-work-sowing-fire-help/35867>\
**Category:** Level Help\
**Created:** [July 29, 2023, 5:18pm UTC](https://discourse.codecombat.com/t/stupid-code-wont-work-sowing-fire-help/35867 "2023-07-29T17:18:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![herokolbert](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/herokolbert/32/34618_2.png) [@herokolbert](https://discourse.codecombat.com/u/herokolbert)\
**Post date:** [July 29, 2023, 5:18pm UTC](https://discourse.codecombat.com/t/stupid-code-wont-work-sowing-fire-help/35867/1 "2023-07-29T17:18:58Z")

</div>

My hero keeps running straight into the traps but i did everything right 😭 😭 😭 😭 😭 😭 😭

```python
// Goal: build three rows of nine fire-traps.

// Returns "retreat", "attack", "start-next-trap-column", or "build-next-trap-in-column"
function chooseStrategy() {
    var enemies = hero.findEnemies();

    // If there are overwhelming ogre forces, return the "retreat" strategy.
    if(enemies.length > 20) {
        return "retreat";
    } 
    // If there are some ogres, return the "attack" strategy.
    if (enemies.length > 0) {
        return "attack"
    }
    
    // Use x % 9 === 0 to see if x is divisible by 9.
    // Use this.built.length to see how many traps you have built.
    // If you have finished a column of 9 traps, return "start-next-trap-column"
    if (hero.built.length % 9 === 0) {
        return "start-next-trap-column"
    }
    // Otherwise, return "build-next-trap-in-column"
    else {
        return "build-next-trap-in-column";
    }
}

var trapsInColumn = 9;
var startX = 40;
var columnX = startX;

// Build the next trap in a column in the correct place.
function buildNextTrapInColumn(columnX,numTraps) {
    // Change newY to use % to wrap around and only build trapsInColumn (9) traps per column
    var newY = 7 * (numTraps & trapsInColumn) + 10; // ∆ Change this to use % 9!
    if (hero.pos.y < newY) {
        hero.move({"x": columnX - 5, "y": newY});
    } else {
        buildTrap(columnX,newY);
    }
}

// Start a new column of traps.
function startNextTrapColumn(columnX, numTraps) {
    var newX = startX - (Math.floor(numTraps / trapsInColumn) * 6);
    if (hero.pos.y > 10) {
        hero.move({"x": newX - 5, "y": 10});
        return columnX;
    } else {
        buildTrap(newX,10);
        return newX;
    }
}

function buildTrap(x, y) {
    hero.buildXY("fire-trap", x, y);
}

function commandAttack() {
    // Have your griffin riders fend off the attackers.
    var friends = hero.findFriends();
    
    for(var i = 0; i < friends.length;i++) {
        var friend = friends[i]
        hero.command(friend, "attack", friend.findNearestEnemy());
    }
}

function commandRetreat() {
    hero.say("Retreat!");
    // You and your griffin riders retreat to safety behind the traps.
    hero.moveXY(4, 42);
    for(var i = 0; i < friends.length;i++) {
        var friend = friends[i]
        hero.command(friend, "move", hero.pos);
    }
}

while(true) {
    var strategy = chooseStrategy();
    if(strategy == "attack") {
        commandAttack();
    } else if(strategy == "build-next-trap-in-column") {
        buildNextTrapInColumn(columnX, hero.built.length);
    } else if(strategy == "start-next-trap-column") {
        columnX = startNextTrapColumn(columnX, hero.built.length);
    } else if(strategy == "retreat") {
        commandRetreat();
    }
}

```

---

<div class="post-metadata">

**Author:** ![herokolbert](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/herokolbert/32/34618_2.png) [@herokolbert](https://discourse.codecombat.com/u/herokolbert)\
**Post date:** [July 29, 2023, 5:21pm UTC](https://discourse.codecombat.com/t/stupid-code-wont-work-sowing-fire-help/35867/2 "2023-07-29T17:21:16Z")

</div>

im the stupidest being in the universe i solved it 🤣 🤣 🤣 🤣 🤣 🤣 🤣 🤣 🤣 🤣

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [July 30, 2023, 5:21am UTC](https://discourse.codecombat.com/t/stupid-code-wont-work-sowing-fire-help/35867/3 "2023-07-30T05:21:42Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
