[SOLVED] Bookkeeper help!


#1

Hi,

(btw I am new to this site not the game)
I have been playing this for a while now but I got stuck at the bookkeeper level, I keep trying different codes but every time the game says that I have reached the hard limit of 3000000 or that there is a infant loop. Please help!

This is what I have so far for this attempt:

loop:
enemy = self.findNearest(self.findEnemies())
if enemy:
self.attack(enemy)

if self.now() > 15:
    break

Tell Naria how many enemies you defeated.

self.say(“I got 4”)
while self.now() < 30:
if item:
item = self.findNearestitems()
x = item.pos.x
y = item.pos.y
self.moveXY(x, y)

(I hope that I am not a idiot because I cant find any info about this on how to complete it)


#2

Please post your code according to the FAQ

This post talks about infinite loops within this level. Hopefully it will help you understand how to fix your code.


#3

Your code is not formatted by i see a

while self.now<30 :

What happens when your item is null? And it will be null because you have not given it any value so far


#4

As @Hinkle and @AdrianCgw mentioned. Please format your code according to the FAQ. It helps us to be able to read your code more easily. And also if there is any more info you can give, please provide.

Cheers! @Luke10


#5

Hello! i need some help in my code something wrong

loop {
    var enemy = this.findNearest(this.findEnemies());
    var defeated = 0;

    if (enemy) {
        this.attack(enemy);
        if(enemy.health <= 0) {
       defeated += 1;}          // Keep count whenever an enemy is defeated.
        if(this.now() > 15) {        // Fight enemies for 15 seconds.
        break;
    }
this.moveXY(59, 33);
this.say("" + defeated + "");   // Tell Naria how many enemies you defeated.
}
}

after each attack before killing enemy it goes to Naria and says 1 or 0 even if killed mere than 1
If anyone have an idea please help me


#6

@User01 You cannot just say that something is wrong tell us exactly what is wrong and are there any errors in your code.


#7

i’m sorry may be my explanation in the bottom if mesage is not evident (my english is not native).
firstly :
I mean after each attack means after each hitting with the sword even if enemy wasn’t killed but only its health only decreased hero goes to Naria. If i want to make hero go to tell Nira only if enemy killed what would be the correct code?

second and more important because it is condition if level to pass it :
The result of :

this.say(""+defeated+"");

not correct, because i may kill 2 or 3 enemies but hero says 0 or 1 .


#8

Your code basically does:

loop {
    if (enemy) {
        attack_enemy
        go_to_Naria_and_tell_her
    }
}

The go_to_Naria_and_tell_her code is inside the loop so it will be executed every time.

What you need is:

loop {
    if(enemy){
        attack_enemy
        if(this.now()>15)
             break;
    }
}
got_to_Naria_and_tell_her

You have to break out of the loop to reach the tell_Naria code. After that you will have to create second and third loops for the rest of the level


#9

Thank you very much for your attention. Yes I investigated it too and now hero is not going after each attack to tell_Naria
But the second problem is counting. After hero killed all enemies it still says 0 killed .

       this.attack(enemy)
           if(enemy.health <= 0) {
    defeated += 1;
            }

didn’t work with me. I tried in mozilla ang Chrome. Have you got ane idea AdrianCgw?


#10
loop {
  enemy_killed=0;
  if (killed_enemy)
      enemy_killed++;
}

What values do you think enemy_killed will take?


#11

If i understand Increment correctly enemy_killed value will take “1”


#12

And on the second iteration when every instruction in the loop is executed again?


#13

also 1 , because loop starts from beginning


#14

So if you want to actually count the number of enemies killed you have to move the initialization outside the loop:

enemy_killed=0;
loop {
  if (killed_enemy)
      enemy_killed++;
}

In your code you have:

var defeated = 0;

inside your loop. It resets the 0 all the time, that is why you don’t say the correct number of enemies killed


#15

help me, please … … my character just run to the archer without attacking. Here is my code
time = self.now()
enemy = self.findNearestEnemy()
enemykilled = 0
coin = self.findNearestItem()
coincollected = 0
if time < 15:
if enemy:
self.attack(enemy)
enemykilled += 1
else:
self.shield()
if time > 15:
self.moveXY(59, 33)
self.say(enemykilled)
if time < 30 and time > 15:
if coin:
coinX = coin.pos.x
coinY = coin.pos.y
self.moveXY(coinX, coinY)
coincollected += 1
self.moveXY(59, 33)
self.say(coincollected)
if time < 45 and time > 30:
enemykilled = 0
self.attack(enemy)
enemykilled += 1
self.moveXY(59, 33)
self.say(enemykilled)
What’s wrong?
thanks


#16

Check the other code posted in this thread. In pseudocode you must try to do this:

initialize the number of enemies killed to 0
loop:
    find enemy 
    if enemy:
        while enemy_not_dead_yet:
            attack enemy
        #end of while - enemy just died here
       increment the number of killed enemies
    #end of enemy code
    get current time
    if time > 15 break
#end of loop
go to Naria
tell her number of enemies killed

Errors in your code:

  • you do not have a loop
  • you are mixing the initializations (enemykiled=0) which must be executed only once,
    with the updates (time=self.now(), enemy=self.find …) which need to executed for each new loop
  • you are not making sure the enemy is dead

#17

Thank you so much! now i understood and i fixed my code so 2/3 of level is done.


#18

having trouble can any body help me

// Fight enemies for 15 seconds.
// Keep count whenever an enemy is defeated.
loop {
var enemy = this.findNearestEnemy();

if(enemy){
    this.attack(enemy);
    if(this.now()>15)
         break;
}

}
// Tell Naria how many enemies you defeated.
enemy_killed=0;
loop {
if (this.killedenemy)
this.enemykilled++;
}
// Collect coins until the clock reaches 30 seconds.
var item = this.findNearestItem();

// Tell Naria how much gold you collected.

// Fight enemies until the clock reaches 45 seconds.
// Remember to reset the count of defeated enemies!

// Tell Naria how many enemies you defeated.


#19

Please remember to format your ENTIRE code for the sake of the people reading it!!!


#20

You didn’t even put any code for collecting coins!!! You never tried finishing the level!!! Please do that before going to people for help ok? :slight_smile: