[Tutor] Making Doubly Linked List with Less Lines of Code.

WolfRage wolfrage8765 at gmail.com
Fri Jan 2 15:49:30 CET 2015


import sys



class GameTile():

def __init__(self, id, **kwargs):

# id is (X,Y)

self.id = id



class GameGrid():

def __init__(self, **kwargs):

self.cols = 8

self.rows = 8

# grid is 2d array as y, x ie [y][x].

self.grid = [[None] * self.rows for i in range(self.cols)]


def make_grid(self):

for row in range(0, self.rows):

for col in range(0, self.cols):

self.grid[col][row] = GameTile(id=str(row) + ',' + str(col))


def print_by_row(self):

for col in range(0, self.cols):

for row in range(0, self.rows):

print(self.grid[col][row].id)


def print_by_col(self):

for row in range(0, self.rows):

for col in range(0, self.cols):

print(self.grid[col][row].id)


def check_bounds(self, x, y):

if (0 <= x < self.rows) and (0 <= y < self.cols):

return True

return False


def lookup_node(self, x, y):

if not self.check_bounds(x, y):

return False

return self.grid[y][x]


def draw_grid(self):

for col in range(0, self.cols):

print(end='| ')

for row in range(0, self.rows):

print(self.grid[col][row].id, end=' | ')

print()



temp = GameGrid()

temp.make_grid()

temp.draw_grid()

print(sys.getsizeof(temp.grid))


#The above code shows fine to me with indentation preserved. Plain-Text 
was selected in Thunderbird.



More information about the Tutor mailing list