473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

2d class member list of objects not updating

93 64KB
I have a problem updating self.tiles[][].type in line 29. Once leaving the for loops in Map's __init__, the tiles list "forgets" the update somehow.

- I've tried adding a setter function for "type" in Tile but that didn't work also tried making the "type" a class level variable but that didn't and wouldn't help anyway.
- I don't suspect its something to do with reference since I'm only working on the instances and never go to the class for any variables.
- It might be that the tiles array in map doesn't contain instances in the way I think they do, but idk.

Would appreciate if anyone tell me why it doesn't update the tiles list after the for loops in __init__.

(debugging print's included)
Expand|Select|Wrap|Line Numbers
  1. import tkinter as tk
  2. from tkinter import *
  3.  
  4. class Tile:
  5.     def __init__(self, type, pos = (0, 0), isPath = False): #, cost):
  6.         self.type = type
  7.         self.pos = pos
  8.         self.isPath = isPath
  9.         #self.cost = cost
  10.  
  11.     def getColor(self):
  12.         if (self.isPath):           # Tile is a part of path
  13.             return "red"
  14.         elif (self.type == "."):    # Traversable tile
  15.             return "white"
  16.         elif (self.type == "#"):    # Intraversable tile
  17.             return "brown"
  18.  
  19. class Map:
  20.     def __init__(self, filename):
  21.         self.width = len(open(filename, "r").readline()) - 1            # Find size of first line
  22.         self.height = sum(1 for line in open(filename))                 # Find nr of lines
  23.         self.tiles = [[Tile(".")] * self.width] * self.height           # Ready tile
  24.  
  25.         print("Map.tiles 2d list content:")
  26.  
  27.         with open(filename, "r") as f:                                  # Open given file
  28.             print("Updating in __init__:")
  29.             for y, line in enumerate(f):                                # For every line
  30.                 for x, letter in enumerate(line):                       # For every letter
  31.                     if (letter != "\n"):                                # Ignore newline
  32.                         self.tiles[y][x].type = letter                  # Update tile type
  33.                         print(self.tiles[y][x].type, end = "")
  34.                         self.tiles[y][x].pos = (x, y)                   # Tell tile where it is on map
  35.                         if (letter == "A"):                             # Save start index
  36.                             self.start = (x, y)
  37.                         elif (letter == "B"):                           # Save goal index
  38.                             self.goal = (x, y)
  39.                 print("")
  40.             print("")
  41.  
  42.             print("Just after updating in __init__:")
  43.             for row in self.tiles:
  44.                 for tile in row:
  45.                     print(tile.type, end = "")
  46.                 print("")
  47.         print("")
  48.  
  49.     def AStar(self, start, goal):
  50.         print("")
  51.  
  52.     def drawMap(self, canvas):
  53.         print("When displaying on screen in drawMap:")
  54.         tileDim = (canvas.winfo_width() / self.width, canvas.winfo_height() / self.height)  # Find tile size
  55.         for y, row in enumerate(self.tiles):                                                # For every row
  56.             for x, tile in enumerate(row):                                                   # For every column in said row
  57.                 canvas.create_rectangle(tileDim[0] * x,                                     # Draw tile
  58.                                         tileDim[1] * y, 
  59.                                         (tileDim[0] * x) + tileDim[0], 
  60.                                         (tileDim[1] * y) + tileDim[1], 
  61.                                         fill = tile.getColor())
  62.                 print(tile.type, end = "")
  63.             print("")
  64.  
  65. root = tk.Tk()
  66. root.title("A* Pathfinding")
  67. root.geometry("720x480")
  68.  
  69. canvas = tk.Canvas(root, width = root.winfo_width(), height = root.winfo_height())
  70. canvas.pack(side="top", fill = "both", expand = True)
  71. canvas.update()
  72.  
  73. obj = Map("./boards/board-1-1.txt")
  74. obj.drawMap(canvas)
  75.  
  76. root.mainloop()
  77.  
Output (terminal):
Expand|Select|Wrap|Line Numbers
  1. Map.tiles 2d list content:
  2. Updating in __init__:
  3. ....................
  4. ....................
  5. .........######.....
  6. ...........A..#..B..
  7. .........######.....
  8. ....................
  9. ....................
  10.  
  11. Just after updating in __init__:
  12. ....................
  13. ....................
  14. ....................
  15. ....................
  16. ....................
  17. ....................
  18. ....................
  19.  
  20. When displaying on screen in drawMap:
  21. ....................
  22. ....................
  23. ....................
  24. ....................
  25. ....................
  26. ....................
  27. ....................
  28.  
Sep 19 '18 #1

✓ answered by Xillez

Solved it by making the "self." variables in Map global using the "global" key word (and removing the "self." of course). It might not be a good thing, but it works

3 1419
dwblas
626 Expert 512MB
Some simple print statements should show the problem. For example
Expand|Select|Wrap|Line Numbers
  1. self.tiles[y][x].type = letter
  2. print(self.tiles[y][x])
  3.  
  4. ## this should likely be
  5. self.tiles[y][0][0].type
  6. """ as the class instance is in the first position,
  7.  offset=0, of the sub-list, and the first/only position
  8.  in that sub-list.  print() self.tiles[y][0][0] and/or 
  9.  self.tiles[y][0][0].type to test for yourself
  10.  self.tiles = [[Tile(".")] * self.width] * self.height
  11. """
  12.  
  13. ## A dictionary would work better here IMHO
  14. self.tiles={}
  15. self.tiles[Tile(".")]=self.width] * self.height           
  16.  
Sep 19 '18 #2
Xillez
93 64KB
Appreciate the answer but...

I tried printing with self.tiles[y][0][0] with and without .type and it says "Tile object does not support indexing". The [][][] tries to fetch the a position within a 3d list, but since [[] * width] * height makes a 2d list this is understandable, so the object lies in the inner list.

As far as I understand the code should be correct (coming from a c++/java programming background, which of course doesn't mean that python behaves the same)

But the main problem is that the the 2d list "tiles" looses it's updated state once exiting the for loops updating them (in __init__). I have a print out within the same function and yet it prints only dots... so I suspect it might be something with the self and it not being a Tile object but something else, OR, it might be something with the list (or the initialization of it) that doesn't allow me to update them.
Sep 20 '18 #3
Xillez
93 64KB
Solved it by making the "self." variables in Map global using the "global" key word (and removing the "self." of course). It might not be a good thing, but it works
Sep 20 '18 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: dasod | last post by:
I would like to know if my method to remove list objects is correct in this small test program. It seems to me that there might be a simplier way, but I'm afraid I don't know enough about list...
2
by: claire.bell1 | last post by:
Hi, Im having problems initialising class member objects in the class's constructor e.g. My program isnt about monkeys, but there is too much code to post here. class monkey { private: int...
16
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
13
by: jt | last post by:
Being a newbie in C++ and comming from C, I can't find information on how to access a class member function outside its class. Below is a snippet of the class and its member function: look at...
1
by: Just D | last post by:
Hi, Does anybody know is it possible to get the list of the public members of the class during run-time using some trick if the class is, say, unknown or I just want to write some universal...
2
by: Crirus | last post by:
I have some question related to the other post about iteration on a class members. I had found a way to figure out when a member of my class is array so I can pull out it's elements to iterate...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
2
by: .rhavin grobert | last post by:
i have (do try to have?) the following... & = breakpoints in debugger // ---------------------------------------------------------------- // cx.h class CX { public: CX(CX* pcx = NULL);...
13
by: stephen b | last post by:
(apologies for cross posting from the moderated group..i'm sure you understand) Hello, I'm passing an array into a Constructor and hoping to use it as a pointer and store it as a class member...
2
by: qambary | last post by:
In this Assignment, you will design a class member Type. A. Each object of member Type can hold the name of a person, member ID, number of books bought, and amount spent. B. Include the member...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.