Cubic Minefield - Math Help

Hey guys,
I am just writing to see if anyone can give me some insight as the what exactly is happening here. I get that we are crafting a polynomial equation to give us a graph essentially that the hero follows…but looking at the code I am kind of at a loss.

First off:

# This function returns the number multiplied by the times
def mult(number, times):
    total = 0
    while times > 0:
        total += number
        times -= 1
    return total
  1. What is this? I don’t see the function “mult” used anywhere else in the code. What number? Times of what?

while True: y = a * power(x, 3) + b * power(x, 2) + c * power(x, 1) + d * power(x, 0) hero.moveXY(x, y) x = x + 5

  1. ^^^^ having trouble with formatting here, sorry…So I get that this is the cubic equation where a, b, c, d are coefficients stored in the tower. But why is x = x + 5? Is this the slope?

Sorry guys, math is rusty. TBH I hated math in high school and didn’t pay attention, but as an adult it is fun, but I have to relearn everything now and APPLY it to coding. Having fun, but could use some noob love.

Thanks in advance,


1 Like
  1. mult() is just a glorified multiplication function. It’s explained in the help guide, but essentially multiplication is basically adding a number n times together. The goal is to translate this concept into a power function, which is multiplying a number n times together. (You could actually use it in the power() function, but would have probably been easier to use the * operator.)

  2. It might make more sense to read the code like this:

    f(x) = ax3 + bx2 + cx1 + dx0
    Δx = 5
    x0 = hero.pos.x

    Then to calculate the appropriate y value you call f(x), where x initially is x0. The hero then moves to the coordinates (x, f(x)). x is then incremented by Δx, which is the change in x. It’s basically how much the hero should step in the x direction. The process loops, and you get a nice cubic equation graph as the hero moves to a different set of coordinates. So with the full steps all written out, it looks like

    moveXY(x0, f(x0))
    x1 = x0 + Δx
    moveXY(x1, f(x1))
    x2 = x1 + Δx
    moveXY(x2, f(x2))

    xn = xn-1 + Δx
    moveXY(xn, f(xn))

    You can play around with the Δx value to get a smaller or larger step size, and a smoother or more jagged curve.


Thank you for the response…this helped a lot. One more question though…my comments in the code ask for me:

def mult(number, times):
    total = 0 # setting the total at 0...simple enough
    while times > 0: # so as long as the number we are multiplying by is greater than 0, run this loop...okay. i will use 5 * 10 for an example.
        total += number # ok so here, we are simply assigning our "number" right? both by adding 5 to the total AND setting our 'number'
        times -= 1 # here is where I am confused...why subtract and assign? is this because we have already assigned 5 to total and we are saying okay now add our number (5) to itself 9 (10 - 1) more times?
    return total

You basically got it right; the times -= 1 is really more of a counter, to count down to 0 how many times to add the number to the total. The total += number is really a shorthand way of saying total = total + number, so it’s really just adding whatever number is to the current total value.


Got it! Great explanation, times as a counter finally made it click in my head. Thanks so much for your help! I’ll probably bug you for some help with modulo in the near future, but I am gonna try and figure it out myself first haha.

Well I understand the idea that you use a cubic function to move through the level, but I have absolutely no idea how to implement that into the code, nor do I understand what any of the guide code means. Any help here?