This is my most recent rendition of the code. It still needs to be improved. Below I am answering some questions that were posed. Also I think my code is currently wrong because I think the print_by functions are both printing the list the same way. Tomorrow I will fix this and improve the code.

Thank you guys for your help so far.



class GameTile():

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

self.id = id

self.value = None

self.next_node_in_row = None

self.next_node_in_col = None

self.prev_node_in_row = None

self.prev_node_in_col = None


class GameGrid():

def __init__(self, **kwargs):

self.num_of_cols = 8

self.num_of_rows = 10

# Each variable below is a list of links to the head

# node in the respective row or column.

self.rows = [None] * self.num_of_rows

self.cols = [None] * self.num_of_cols

self.skip_to_row = None

self.skip_to_col = None

self.tile_list = list()


def make_grid_nodes(self):

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

for column in range(0, self.num_of_cols):

tile = GameTile(id=str(column) + ',' + str(row))

self.tile_list.append(tile)

if column == 0: # New Head of Row

self.rows[row] = tile

else:

prev_row.next_node_in_row = tile

tile.prev_node_in_row = prev_row

prev_row = tile

if row == 0: # New Head of Column

self.cols[column] = tile

else:

prev_col.next_node_in_col = tile

tile.prev_node_in_col = prev_col

prev_col = tile


def print_by_rows(self):

for col, the_column in enumerate(self.cols):

print(the_column.id)

if the_column.next_node_in_row is not None:

node = the_column.next_node_in_row

while node.next_node_in_row is not None:

print(node.id)

node = node.next_node_in_row


def print_by_row(self):

for row in self.rows:

print(row.id)

if row.next_node_in_row is not None:

node = row.next_node_in_row

while node.next_node_in_row is not None:

print(node.id)

node = node.next_node_in_row


def print_by_col(self):

for col in self.cols:

print(col.id)

if col.next_node_in_col is not None:

node = col.next_node_in_col

while node.next_node_in_col is not None:

print(node.id)

node = node.next_node_in_col


def draw_grid(self):

import time

sep = '─────────────────'

f = '░│'

l = '◈│'

row = sep + '\n│'

last_col = None

current_col = None

val = None

for node in self.tile_list:

if node.value == None:

val = l

else:

val = f

current_col = node.id.split(',')[1]

if last_col is None:

row += val

elif current_col == last_col:

row += val

else:

row += '\n' + sep + '\n│' + val

last_col = node.id.split(',')[1]

row += '\n' + sep

print(row)

#time.sleep(1)


temp = GameGrid()

temp.make_grid_nodes()

#temp.draw_grid()

temp.print_by_row()

print('BREAK Now COLUMNS')

temp.print_by_col()



On 12/25/2014 12:31 AM, Danny Yoo wrote:
What are the _operations_ you want to support?  Can you say more about this?
I need to support lookups, no insertions or deletions are required. This code will be used to quickly lookup nodes, and check for specific patterns by checking the nodes values in either a row or column. The code to perform the pattern matching was already written and is fast, but I will add it to the code once the creation

On 12/24/2014 04:56 PM, Steven D'Aprano wrote:
Wow. It certainly is bloated.
Agreed.
Look for the opportunity to write code like this instead of using range:

     for col, the_column in enumerate(self.columns):
         self.columns[col] = process(the_column)
This caught my eye, and I did try to implement it. But why use this instead of range? I am using Python3. I did find that enumerate is potentially faster but sometimes slower, as it depends on what is being done. Perhaps because it is considered more Pythonic? So what is your reason for this suggestion?
Any time you write more than a trivial amount of code twice, you should
move it into a function. Then, instead of:
I agree, should have done, and need to look for these opportunities sooner.
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to