*Solved* Confusion with built-in CodeCombat array output:


#1

Prerequisites:

  1. I am currently on Cloudrip Mountain lv 7
  2. I was competent at python before discovering CodeCombat, so I use functions etc.
  3. I am playing on a free account.

Problem:

while True:
    items = self.findByType(self.findItems())

####What Exactly does the result of items. Exaclty what would it look like if I manualy wrote it out?

I want to directly edit the resultant dictionary with item positions. Or rather, make a copy dictionary and edit that.

To test what I thought was the structure I tried to use type() But it seems to be unsupported.
So. What is the exact result? What is the data structure of 'items?
The best I can figure out:

    items == {'name':{'x':x, 'y':y, 'z':z}, ...etc}

Therefore this next code fails, as items[i] is a dictionary. but why does it not output:{'x':x, 'y':y, 'z':z}

    for i in items:
        hero.say(items[i])

Suspecting that it was a set a nestled dictionaries I tried to print a single element:

    for i in items:
        for n in i:
            self.say(n)

But that returns hasEffect, bountyGold, distanceTo, iD very confuing…


My over all goal it to take the items duplicate with .copy(1) and then do some math on the individual x, y, z coordinates. I want it in a single element that I can access .pos out of if possible, but I’m not sure I have access to that in the elements I create.
Thank you for your help

Cheers! -CryogenicMiner


#2

Oh could it be using an array()? I really can not figure out what is the output of hero.findItems()


#3

hero.findItems() should return an array of ‘Thangs’ (the game’s term for 'entities), specifically things which are collectable (coins, potions).

Further, hero.findByType("munchkin") returns an array of Thangs which match the string argument provided. You can see what something’s type is by accessing the element’s .type attribute. Using hero.findByType(hero.findItems()) doesn’t make sense. I’m not sure, but if hero.findByType() forces an Array.toString, the result would be something like this: hero.findByType("Gold Coin, Potion, Gold Coin, Silver Coin") which doesn’t match any known Thang’s types.

Here are some examples:

items = hero.findItems() # items is an array of all things which can be picked up
firstItem = items[0] # firstItem is now the first element of items, (the actual order of items is determined by which items were added to the scene first -> last, 0 -> length -1)
hero.say(firstItem.type) # The hero will now say the type of the firstItem (something like Potion or Gold Coin)
closestItem = hero.findNearest(items) # closestItem is single thang from the array (or None if the array in empty)
potions = hero.findByType("potion") # potions is now an array of thangs which have the .type property "potion"
closestPotion = hero.findNearest(potions) # closestPotion is now the nearest single thang (or None)

#4

Ok, that clears up a lot of things. Thank you very very much.
So the follow up questions if you are willing to answer is:

Can I duplicate an array, and edit the individual properties?

I really appreciate the help I have both received and seen here on the forums. It is nice to the Dev team helping out.


#5

If anyone who reads this is curious, I’m working on implementing a Vector Field Histogram (VFH) path finding AI. I already have successfully implemented a Gravitational Potential Field path finding algorithm. Which for those not familiar assigns a modified ‘gravitational’ force to each object. Then through vector force addition the ‘pull’ is calculated and our beloved hero moves t’words the most coins per distance/ value/ quantity. It worked but I need more flexibility than it offers.


#6

There is API protection when it comes to editing Thang object properties (and for a good reason too), which means that you can’t exactly edit the original properties. Though if you were only interested in just a coin value and it’s position, you could use .pos (which is a Vector) and .value (a number), and create a separate array to store the computation, and pair the data up with matching indexes or tuples.

You could try to create a forces array with a list comprehension (haven’t tested this myself though):

forces = [compute_force(hero.pos, coin.pos, coin.value) for coin in coins]
coin_force = [(coin, compute_force(hero.pos, coin.pos, coin.value)) for coin in coins] # tuple

And with that data you could do a number of things, such as summing up a net force with a reduce function, or simply selecting the coin with the greatest force.


#7

Ok, that all makes good sense. I have been making lists and using them, and they work just fine. I was hoping that I could just duplicate it for ease/ clean code.
Thanks for your help!
-Cheers


#8

@CryogenicMiner - Do you think it is possible to make a level on Code Combat to teach ‘Gravitational Potential Field path finding algorithm’. I think it would be a very good learning - teaching coding, applied polynomial maths (the gravitation pull) and a sense of vectors on 2D space (the game map for our hero) …

Cheers


#9

Oh yeah, totally! I think it should be quite possible. Although without any direct reading or teachable material a basic understanding of unit vectors, vector mathematics and conceptual idea of the gravitational formula and gravitational fields would be necessary. So in that sense, we may need a whole bunch of levels that builds up the idea. But after Gravitational Potential Feild algorithms, the possibilities quickly open up to Force Vector Histogram algorithms and the variations. Which are more adaptive, although probably not quite as ‘cool’ of an application of science as making the character fall into orbit around coins :wink:


#10

@CryogenicMiner - nice. once I complete all the levels, I will try making new levels… with obviously this in mid along with other ideas that I have. Building up the concept level by level will be ideal. Directly jumping into it full force will not be good for teaching. Thanks for the basic idea.


#11

I’m not finished with the levels yet either. But when you do, and start level designing make a new post and I will see how I can help either conceptually, or acutely designing.


#12

Ok, long way to go for the same. Hope to start soon though…