'Python: Gridmap can't calculate print out the route in the Grid Map
I am trying to implement GridMap in python and trying implement str(self) that prints, The Starting Location, Ending Location and the Route between them. I've managed to do the starting and ending locations but not the Route between them. when i try to calculate the route it doesn't appear on my gridmap
class GridMap:
def __init__(self, row, column):
self.row = row
self.column = column
self.start_col = None
self.start_row = None
self.end_col = None
self.end_row = None
self.change_column = None
self.change_row = None
def add_start(self, row, column):
self.start_row = row
self.start_col = column
def add_end(self, row, column):
self.end_col = column
self.end_row = row
self.DX = self.start_row - self.end_row
self.DY = self.start_col - self.end_col
def __str__(self):
new_str = ""
#new_str += ("0" * self.row + "\n") * self.column
for i in range(self.column):
new_str += "\n"
for j in range(self.row):
if j == self.start_col and i == self.start_row:
new_str += "A"
elif j == self.end_col and i == self.end_row:
new_str += "Z"
elif i == self.change_row and j == self.end_col:
new_str += "X"
if self.change_row != 0:
if self.change_row > 0:
self.change_row - 1
else:
self.DX + 1
elif i == self.end_row and j ==self.DY :
new_str += "X"
else:
new_str += "0"
return new_str
m = GridMap(5, 5)
print(m)
m.add_start(4, 4)
print(m)
m.add_end(0, 3)
print(m)
expected result
00000
00000
00000
00000
00000
00000
00000
00000
00000
0000A
000Z0
000X0
000X0
000X0
000XA
got:
00000
00000
00000
00000
00000
00000
00000
00000
00000
0000A
0X0Z0
00000
00000
00000
0000A
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
