How many non diagonal moves from origin point?

Medullan Source
87 76 67 58 49 40 31 22 13  4
77 68 59 50 41 32 23 14  5 -4
69 60 51 42 33 24 15  6 -3-12
61 52 43 34 25 16  7 -2-11-20
53 44 35 26 17  8 -1-10-19-28
45 36 27 18  9  0 -9-18-27-36
37 28 19 10  1 -8-17-26-35-44
29 20 11  2 -7-16-25-34-43-52
21 12  3 -6-15-24-33-42-51-60

I am using this grid of numbers to define levels in a pygame tile based dictionary of maps. Each number is a dictionary key defining a maps location in reference to each other map with the value being a 2d list. I need a function that returns the number of non diagonal moves away from 0 each number in the grid is. I am going to use this number to calculate difficulty level. So the farther the player gets from the origin point of 0 the more difficult the game gets. To better understand how these numbers are generated... A move North increments by 8, a move south increments by -8, a move east increments by -9, and a move west increments by 9. So for example maps 17,-1,1,and -17 would equal 2 while maps 8,9,-9,-8 would equal 1 and so on. I am making some headway learning to write decent code, but sometimes the math required is beyond my capabilities.

python-3.xmathpygameabsolute-value

Answers

answered 3 years ago Medullan #1

Okay so I figured out a much less complicated way to solve my problem. I used a list of 2 values. I incremented or decremented position 0 by 1 for north,south moves and incremented or decremented position 1 by 1 for west,east moves respectively. I then added the absolute value of position 0 and the absolute value of position 1 and returned the answer. This seems to be giving me an accurate distance from 0. YAY!

dif_list =[0,0]
if move == 'north':
  dif_list[0] += 1
if move == 'south':
  dif_list[0] -= 1
if move == 'west':
  dif_list[1] += 1
if move == 'east':
  dif_list[1] -= 1
  
def difficulty():
  num = abs(dif_list[0])+abs(dif_list[1])
  return num

comments powered by Disqus