On 01/02/2015 12:08 PM, Dave Angel wrote:
class GameGrid():
     def __init__(self, cols=8, rows=7, **kwargs):

You probably want to reverse the order of the parameters;  you've got
almost everything else row-major

You are right, a lot of inconsistency that will lead to errors latter on. I need to make everything column-major. So that the grid (x,y) are the same to grid (col,row). I have too much confusion throughout my code, so I will correct the code.

Since both stages are being done in the same method, you don't need the
part which initializes to None.

     def make_grid(self):
         # grid is 2d array as x, y ie [x][y].
         self.grid = []
         for row_num in range(self.rows):
             self.grid.append( [GameTile(row=row_num, col=col_num)  for
col_num in range(self.cols)] )

Implemented the second version.

     def print_by_row(self):
         for col in self.grid:
             for row in col:
                 print(row)

This cannot work.  The items in self.grid are rows.  Calling one of them
col doesn't make it so.  In other words, the method as written will do
exactly what print_by_col() does.

Good point, I did not read the output thoroughly enough to catch this.

Try the following:

     def print_by_row(self):
         for col_number in range(self.cols):
             for row in self.grid:
                 print(row[col_number])

Implemented.

This one should work fine.  But one of the names could be improved:


     def print_by_col(self):
         for row in self.grid:
             for tile in row:
                 print(tile)

Thanks, improved.

All code untested.  i hope it helps.

Definitely. Thanks.

I have updated the code and I realized that all of my earlier confusion was even greater, so I have also added comments to make things make sense. Now the print_by_ methods work as I envision and in accordance with the comments.

import sys


class GameTile():
    def __init__(self, col, row, **kwargs):
        # id is grid (X,Y) which is equal to grid (col,row)
        self.id = str(col) + ',' + str(row)
        self.col = col
        self.row = row

    def __str__(self):
        return '%d, %d' % (self.col, self.row)


class GameGrid():
    def __init__(self, cols=8, rows=7, **kwargs):
        self.cols = cols
        self.rows = rows
        self.make_grid()

    def make_grid(self):
        # grid is 2d array as x, y ie [x][y].
        self.grid = []
        for row_num in range(self.rows):
            self.grid.append([GameTile(col=col_num, row=row_num) for
                col_num in range(self.cols)])

    def print_by_col(self):
        # As in going down the column assuming we started at the top.
        for col_num in range(self.cols):
            for row in self.grid:
                print(row[col_num])

    def print_by_row(self):
        # As in going right across the row assuming we started at the left.
        for row in self.grid:
            for node in row:
                print(node)

    def check_bounds(self, x, y):
        return (0 <= x < self.rows) and (0 <= y < self.cols)

    def lookup_node(self, x, y):
        if not self.check_bounds(x, y):
            return False
        return self.grid[x][y]

    def draw(self):
        for col in self.grid:
            print(end='| ')
            for row in col:
                print(row, end=' | ')
            print()


grid = GameGrid(3, 3)
grid.draw()
print(sys.getsizeof(grid.grid))
print()
grid.print_by_row()
print()
grid.print_by_col()

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to