Gold rush - beginner. Code problems


#1
// This code runs once per frame. Choose where to move to grab gold!
// First player to 150 gold wins.
// This is an example of grabbing the 0th coin from the items array.
var items = this.getItems();
var i = 0;
if (items[i]) {
    if (items[i].bountyGold >= 3) {
        this.move(items[i].pos);
    } else {
            i++;
    }
} else {
    i = 0;
}
// Click on a coin to see its API.

Hey there can some of you fine ladies and fellas please read my code and tell me what the dump I’m doing wrong? As you can see it runs once per frame. I’m a real n00b and I apologize for that but can someone help me out here? Thanks!


#2

well, if the code were run from the top every frame, “i” would always be zero…

var i = 0

you need to loop over the coins and pick a different coin (closer and/or move valuable) to collect otherwise you will always run after the first coin in the array.


#3

Gah! Of course!
Thanks man.


#4

alright so I adjusted the code so that it was inside a for loop

var items = this.getItems();
for (var i = 0; i < items.length; i++) {
    if (items[i]) {
        if (items[i].bountyGold > 3 && this.distance(items[i]) <= 40) {
            this.moveXY(items[i].pos.x, items[i].pos.y);
        } else {
            i++;
        }
     } else {
        i = 0;
     }
  }

but now the error message “Only the last action set in ChooseAction(); will be applied” What does this mean??


#5

What happens if there are two coins with bountyGold > 3 && distance <= 40?

The moveXY command will be called multiple times. Thus you set the desired action more than once per frame. The game warns you that something unexpected may happen.
In your case that simply is that you will collect the last coin to which the above conditions apply first.


#6

sounds like it really is running “once” per frame.

the error/warning says that you can only make one action . . . since your “action” (moveXY) is in the loop you issue several before the loop is done. pick the one you want then when the loop is done move to it


#7

but I had thought that since I was using “i” I would be addressing each item one at a time?


#8

but you only get one action per frame


#9

SO would I solve the problem by putting this into a function, and calling the function to return the best Item?


#10

You solve it by moving the moveXY out of the loop.

keeping track of the best one (no need for a function just do it right there)


#11

Alright so I had no success moving the “moveXY” outside of the loop. All my character did then was continuously travel off of the right side of the screen. So I tried scrapping all of that and taking an entirely different approach. Still without any luck. Why doesn’t this work. I feel like it should. The code is looped automatically and I am not sure how to circumvent that.

this.itemsDistances = function() {
    for (var i = 0; i < this.getItems().length; i++) {
        return this.distance(this.getItems[i]);
        }
     };
    Array.min = function( array ){
        return Math.min.apply( Math, array );
    };
for (var j = 0; j < this.getItems().length; j++) {
    if (Array.min(this.itemsDistances) == this.getItems[j]) {
        this.move(this.getItems[j].pos);
    }
}

#12

Seriously, indentations please! Use them right, then they are your friends, otherwise your worst foes.
Okay, maybe not in this case, but still!


I’m not entirely sure what happens in your code. But from what I can see, this.itemDistances() will always return the distance to the first item (at the time where the return statement is executed i == 0).

You then (over-?)write a function to Array.min. I’m not sure, what Math.min.apply does, but I’m pretty sure it can’t apply the whole Math-library to the array (or vice versa).

Given that your intention with Array.min is to get the element with the smallest distance, then and only then would your last for-loop work.


I call it very unlikely that your last for-loops works. Do you get any error? Red circles below your characters feet or something?


#13

The “usual” approach is:

#written in pseudo-python  (indent is like javascript: { code; };  )
index = 0
bestDistance = 9999
closestItem = -1
while index < length of itemArray
    currentItem = itemArray[index]
    if currentItem distance < bestDistance:
        closestItem = index
        bestDistance = currentItem distance
    index += 1

# closestItem is either -1 or it was set to the index of the closest item
if closestIem >= 0
    moveXY(itemArray[closestItem].pos)
else
    say "Ack Phbt, I can't see any items"

#14

On a related note:
http://discourse.codecombat.com/t/mad-maxer-help-with-arrays/2700/2?u=j_f_b_m