Advertisement
abbarnes

poc_grid [codeskulptor]

Dec 12th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None | 0 0
  1. """
  2. Grid class
  3. """
  4.  
  5. EMPTY = 0
  6. FULL = 1
  7.  
  8. class Grid:
  9.     """
  10.    Implementation of 2D grid of cells
  11.    Includes boundary handling
  12.    """
  13.    
  14.     def __init__(self, grid_height, grid_width):
  15.         """
  16.        Initializes grid to be empty, take height and width of grid as parameters
  17.        Indexed by rows (left to right), then by columns (top to bottom)
  18.        """
  19.         self._grid_height = grid_height
  20.         self._grid_width = grid_width
  21.         self._cells = [[EMPTY for dummy_col in range(self._grid_width)]
  22.                        for dummy_row in range(self._grid_height)]
  23.                
  24.     def __str__(self):
  25.         """
  26.        Return multi-line string represenation for grid
  27.        """
  28.         ans = ""
  29.         for row in range(self._grid_height):
  30.             ans += str(self._cells[row])
  31.             ans += "\n"
  32.         return ans
  33.    
  34.     def get_grid_height(self):
  35.         """
  36.        Return the height of the grid for use in the GUI
  37.        """
  38.         return self._grid_height
  39.  
  40.     def get_grid_width(self):
  41.         """
  42.        Return the width of the grid for use in the GUI
  43.        """
  44.         return self._grid_width
  45.  
  46.  
  47.     def clear(self):
  48.         """
  49.        Clears grid to be empty
  50.        """
  51.         self._cells = [[EMPTY for dummy_col in range(self._grid_width)]
  52.                        for dummy_row in range(self._grid_height)]
  53.                
  54.     def set_empty(self, row, col):
  55.         """
  56.        Set cell with index (row, col) to be empty
  57.        """
  58.         self._cells[row][col] = EMPTY
  59.    
  60.     def set_full(self, row, col):
  61.         """
  62.        Set cell with index (row, col) to be full
  63.        """
  64.         self._cells[row][col] = FULL
  65.    
  66.     def is_empty(self, row, col):
  67.         """
  68.        Checks whether cell with index (row, col) is empty
  69.        """
  70.         return self._cells[row][col] == EMPTY
  71.  
  72.     def four_neighbors(self, row, col):
  73.         """
  74.        Returns horiz/vert neighbors of cell (row, col)
  75.        """
  76.         ans = []
  77.         if row > 0:
  78.             ans.append((row - 1, col))
  79.         if row < self._grid_height - 1:
  80.             ans.append((row + 1, col))
  81.         if col > 0:
  82.             ans.append((row, col - 1))
  83.         if col < self._grid_width - 1:
  84.             ans.append((row, col + 1))
  85.         return ans
  86.  
  87.     def eight_neighbors(self, row, col):
  88.         """
  89.        Returns horiz/vert neighbors of cell (row, col) as well as
  90.        diagonal neighbors
  91.        """
  92.         ans = []
  93.         if row > 0:
  94.             ans.append((row - 1, col))
  95.         if row < self._grid_height - 1:
  96.             ans.append((row + 1, col))
  97.         if col > 0:
  98.             ans.append((row, col - 1))
  99.         if col < self._grid_width - 1:
  100.             ans.append((row, col + 1))
  101.         if (row > 0) and (col > 0):
  102.             ans.append((row - 1, col - 1))
  103.         if (row > 0) and (col < self._grid_width - 1):
  104.             ans.append((row - 1, col + 1))
  105.         if (row < self._grid_height - 1) and (col > 0):
  106.             ans.append((row + 1, col - 1))
  107.         if (row < self._grid_height - 1) and (col < self._grid_width - 1):
  108.             ans.append((row + 1, col + 1))
  109.         return ans
  110.    
  111.     def get_index(self, point, cell_size):
  112.         """
  113.        Takes point in screen coordinates and returns index of
  114.        containing cell
  115.        """
  116.         return (point[1] / cell_size, point[0] / cell_size)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement